Skip to content

Commit 0d65cfc

Browse files
authored
Tool: Makes header editor work with archived/soft-patched ROMs. (SourMesen#31)
The NES header editor is currently unable to save a ROM if it was loaded from an archive (such as .zip) or if the ROM was soft-patched. This is because the editor reloads the ROM from disk to then apply the new header and save a copy, but the C# compnent is not able to open archives or apply patches. If the ROM file is in an archive, it throws an Unexpected Error and fails. This change instead makes the editor use the current PRG and CHR ROM. This means it doesn't need to reload the ROM, so it can save it regardless of how it was loaded. It also means that it saves any changes that have been made to the ROM in the session, either by self-flashing or by editing them via tools, which could be seen as a pro or a con. Also, because Mesen does not currently use misc ROM, it is unable to include that in the saved ROM, but this is something we could feasibly add later. This change was written by Sour.
1 parent fa92ae2 commit 0d65cfc

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

UI/Debugger/ViewModels/NesHeaderEditViewModel.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ public class NesHeaderEditViewModel : DisposableViewModel
2929
[Reactive] public string ErrorMessage { get; private set; } = "";
3030

3131
private RomInfo _romInfo;
32+
private byte[] _prgRom;
33+
private byte[] _chrRom;
3234

3335
public NesHeaderEditViewModel()
3436
{
3537
bool releaseDebugger = !DebugWindowManager.HasOpenedDebugWindows();
3638
bool paused = EmuApi.IsPaused();
3739
byte[] headerBytes = DebugApi.GetRomHeader();
40+
_prgRom = DebugApi.GetMemoryState(MemoryType.NesPrgRom);
41+
_chrRom = DebugApi.GetMemoryState(MemoryType.NesChrRom);
3842
if(releaseDebugger) {
3943
//GetRomHeader will initialize the debugger - stop the debugger if no other debug window is opened
4044
DebugApi.ReleaseDebugger();
@@ -150,16 +154,14 @@ private string GetErrorMessage()
150154

151155
public async Task<bool> Save(Window wnd)
152156
{
153-
string? filepath = await FileDialogHelper.SaveFile(Path.GetDirectoryName(_romInfo.RomPath), Path.GetFileName(_romInfo.RomPath), wnd, FileDialogHelper.NesExt);
157+
string? filepath = await FileDialogHelper.SaveFile(Path.GetDirectoryName(_romInfo.RomPath), _romInfo.GetRomName(), wnd, FileDialogHelper.NesExt);
154158
if(filepath != null) {
155-
byte[]? data = FileHelper.ReadAllBytes(_romInfo.RomPath);
156-
if(data != null) {
157-
byte[] header = Header.ToBytes();
158-
for(int i = 0; i < 16; i++) {
159-
data[i] = header[i];
160-
}
161-
return FileHelper.WriteAllBytes(filepath, data);
162-
}
159+
byte[] data = new byte[_prgRom.Length + _chrRom.Length + 16];
160+
byte[] header = Header.ToBytes();
161+
Array.Copy(header, data, 16);
162+
Array.Copy(_prgRom, 0, data, 16, _prgRom.Length);
163+
Array.Copy(_chrRom, 0, data, 16 + _prgRom.Length, _chrRom.Length);
164+
return FileHelper.WriteAllBytes(filepath, data);
163165
}
164166
return false;
165167
}

0 commit comments

Comments
 (0)