Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex editor shows wrong "File on Disk" when switching games #3527

Open
kalimag opened this issue Jan 19, 2023 · 2 comments
Open

Hex editor shows wrong "File on Disk" when switching games #3527

kalimag opened this issue Jan 19, 2023 · 2 comments
Labels
App: EmuHawk Relating to EmuHawk frontend Repro: Affects 2.3/2.3.1/2.3.2 (2.3.3 has its own label) Repro: Affects 2.8 Repro: Patch pending Potentially fixed in dev build, see readme for download

Comments

@kalimag
Copy link
Contributor

kalimag commented Jan 19, 2023

Summary

When loading two different roms while the hex editor is open, the "File on Disk" in the hex edtior will show the contents of the previous rom, rather than the current rom. Additionally, if the previous rom was a MAME archive, an error will occur.

Repro

  1. Open the hex editor and keep it open
  2. Load any rom A (e.g a N64 rom with an easily identifiable header)
  3. Load a different rom B
  4. Switch to the "File on Disk" domain in the hex editor
  5. "File on Disk" will display contents of rom A

Alternatively, load a MAME archive at step 2 to reproduce the exception

Output

If the first rom was a MAME archive:

HawkFile: Can't call GetStream() before you've successfully bound something!
   at BizHawk.Common.HawkFile.GetStream() in F:\Emulation\BizHawk\src\BizHawk.Common\HawkFile\HawkFile.cs:line 267
   at BizHawk.Client.EmuHawk.HexEditor.GetRomBytes() in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\tools\HexEditor\HexEditor.cs:line 446
   at BizHawk.Client.EmuHawk.HexEditor.Restart() in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\tools\HexEditor\HexEditor.cs:line 210
   at BizHawk.Client.EmuHawk.ToolManager.Restart(Config config, IEmulator emulator, IGameInfo game) in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\tools\ToolManager.cs:line 521
   at BizHawk.Client.EmuHawk.MainForm.LoadRomInternal(String path, LoadRomArgs args, Boolean& failureIsFromAskSave) in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\MainForm.cs:line 3934
   at BizHawk.Client.EmuHawk.MainForm.LoadRom(String path, LoadRomArgs args, Boolean& failureIsFromAskSave) in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\MainForm.cs:line 3714
   at BizHawk.Client.EmuHawk.MainForm.LoadRom(String path, LoadRomArgs args) in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\MainForm.cs:line 3710
   at BizHawk.Client.EmuHawk.MainForm.OpenRom() in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\MainForm.cs:line 2427
   at BizHawk.Client.EmuHawk.MainForm.OpenRomMenuItem_Click(Object sender, EventArgs e) in F:\Emulation\BizHawk\src\BizHawk.Client.EmuHawk\MainForm.Events.cs:line 296

Host env.

  • BizHawk 2.3 (earliest tested) up to 2.9 dev

Cause

The issue is that HexEditor.GetRomBytes() (called from HexEditor.Restart()) loads the rom from MainForm.CurrentlyOpenRomArgs, but MainForm.LoadRomInternal calls Tools.Restart() before updating CurrentlyOpenRomArgs:

private byte[] GetRomBytes()
{
var path = MainForm.CurrentlyOpenRomArgs.OpenAdvanced.SimplePath;
if (string.IsNullOrEmpty(path))
{
return new byte[] { 0xFF };
}
using var file = new HawkFile(path);

Tools.Restart(Config, Emulator, Game);
if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains())
{
CheatList.SetDefaultFileName(Tools.GenerateDefaultCheatFilename());
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains()))
{
AddOnScreenMessage("Cheats file loaded");
}
else if (CheatList.Any())
{
CheatList.Clear();
}
}
CurrentlyOpenRom = oaOpenrom?.Path ?? openAdvancedArgs;
CurrentlyOpenRomArgs = args;

@YoshiRulz YoshiRulz added App: EmuHawk Relating to EmuHawk frontend Repro: Affects 2.3/2.3.1/2.3.2 (2.3.3 has its own label) Repro: Affects 2.8 labels Jan 19, 2023
@kalimag
Copy link
Contributor Author

kalimag commented Jan 19, 2023

In general, I think calling Tools.Restart before updating CurrentlyOpenRom/CurrentlyOpenRomArgs is unexpected and undesirable.

Other places where those properties are read inside Restart:

// Don't reset scroll bar if restarting the same rom
if (_lastRom != MainForm.CurrentlyOpenRom)
{
_lastRom = MainForm.CurrentlyOpenRom;
ResetScrollBar();
}

if (_lastRom != MainForm.CurrentlyOpenRom)
{
_lastRom = MainForm.CurrentlyOpenRom;
SetupControlsAndProperties();
}

public override void Restart()
{
FileSelectorPanel.Controls.Clear();
AddButton_Click(null, null);
AddButton_Click(null, null);
if (!Game.IsNullInstance() && !MainForm.CurrentlyOpenRom.EndsWith(".xml"))
{
if (MainForm.CurrentlyOpenRom.Contains("|"))
{
var pieces = MainForm.CurrentlyOpenRom.Split('|');
var directory = Path.GetDirectoryName(pieces[0]) ?? "";
var filename = Path.ChangeExtension(pieces[1], ".xml");
NameBox.Text = Path.Combine(directory, filename);
}
else
{
NameBox.Text = Path.ChangeExtension(MainForm.CurrentlyOpenRom, ".xml");
}
if (SystemDropDown.Items.Contains(Emulator.SystemId))
{
SystemDropDown.SelectedItem = Emulator.SystemId;
}
else if (Emulator is SMS sms && sms.IsGameGear)
{
SystemDropDown.SelectedItem = VSystemID.Raw.GGL;
}
FileSelectors.First().Path = MainForm.CurrentlyOpenRom;
Recalculate();
}
}

All instances of tools reading those properties inside Restart assume that the properties contain the new rom that is being loaded, and are therefore currently bugged. They don't update their UIs when switching roms (the update will happen after a second restart), and MultiDiskBundler throws an NRE if no rom was loaded before.

Could also negatively affect external tools.

@YoshiRulz
Copy link
Member

Context: these are ((this as ToolFormBase).MainForm as IMainFormForTools).* and so have found their way into ApiHawk. But whether they were ever meant to be isn't really relevant when the current behaviour also breaks first-party tools.

@YoshiRulz YoshiRulz added the Repro: Patch pending Potentially fixed in dev build, see readme for download label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
App: EmuHawk Relating to EmuHawk frontend Repro: Affects 2.3/2.3.1/2.3.2 (2.3.3 has its own label) Repro: Affects 2.8 Repro: Patch pending Potentially fixed in dev build, see readme for download
Projects
None yet
Development

No branches or pull requests

2 participants