Skip to content

Commit f90126f

Browse files
kalimagYoshiRulz
authored andcommitted
Overhaul cheat handling during rom load/close
- Stop overwriting/deleting other games' cheat files after rom change - Always clear/update references to disposed memory domains - Keep current cheats after core restart
1 parent 76e73e1 commit f90126f

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/BizHawk.Client.Common/tools/Cheat.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ public Cheat(Cheat cheat)
7373

7474
public int? Compare => _compare.HasValue && !IsSeparator ? _compare : null;
7575

76-
public MemoryDomain Domain => _watch.Domain;
76+
public MemoryDomain Domain
77+
{
78+
get => _watch.Domain;
79+
set => _watch.Domain = value;
80+
}
7781

7882
public WatchSize Size => _watch.Size;
7983

src/BizHawk.Client.Common/tools/CheatList.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,24 @@ public bool Load(IMemoryDomains domains, string path, bool append)
404404
return true;
405405
}
406406

407+
public void UpdateDomains(IMemoryDomains domains)
408+
{
409+
for (int i = _cheatList.Count - 1; i >= 0; i--)
410+
{
411+
var cheat = _cheatList[i];
412+
var newDomain = domains[cheat.Domain.Name];
413+
if (newDomain is not null)
414+
{
415+
cheat.Domain = newDomain;
416+
}
417+
else
418+
{
419+
_cheatList.RemoveAt(i);
420+
Changes = true;
421+
}
422+
}
423+
}
424+
407425
private static readonly RigidMultiPredicateSort<Cheat> ColumnSorts
408426
= new RigidMultiPredicateSort<Cheat>(new Dictionary<string, Func<Cheat, IComparable>>
409427
{

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,21 +3783,32 @@ private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFr
37833783
}
37843784
}
37853785

3786+
var previousRom = CurrentlyOpenRom;
37863787
CurrentlyOpenRom = oaOpenrom?.Path ?? openAdvancedArgs;
37873788
CurrentlyOpenRomArgs = args;
37883789

37893790
Tools.Restart(Config, Emulator, Game);
37903791

3791-
if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains())
3792+
if (previousRom != CurrentlyOpenRom)
37923793
{
3793-
CheatList.SetDefaultFileName(Tools.GenerateDefaultCheatFilename());
3794-
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains()))
3794+
CheatList.NewList(Tools.GenerateDefaultCheatFilename(), autosave: true);
3795+
if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains())
37953796
{
3796-
AddOnScreenMessage("Cheats file loaded");
3797+
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains()))
3798+
{
3799+
AddOnScreenMessage("Cheats file loaded");
3800+
}
3801+
}
3802+
}
3803+
else
3804+
{
3805+
if (Emulator.HasMemoryDomains())
3806+
{
3807+
CheatList.UpdateDomains(Emulator.AsMemoryDomains());
37973808
}
3798-
else if (CheatList.Any())
3809+
else
37993810
{
3800-
CheatList.Clear();
3811+
CheatList.NewList(Tools.GenerateDefaultCheatFilename(), autosave: true);
38013812
}
38023813
}
38033814

@@ -3838,6 +3849,7 @@ private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFr
38383849
DisplayManager.UpdateGlobals(Config, Emulator);
38393850
DisplayManager.Blank();
38403851
ExtToolManager.BuildToolStrip();
3852+
CheatList.NewList("", autosave: true);
38413853
OnRomChanged();
38423854
return false;
38433855
}
@@ -3973,6 +3985,7 @@ public void CloseRom(bool clearSram = false)
39733985
PauseOnFrame = null;
39743986
CurrentlyOpenRom = null;
39753987
CurrentlyOpenRomArgs = null;
3988+
CheatList.NewList("", autosave: true);
39763989
OnRomChanged();
39773990
}
39783991
}

src/BizHawk.Client.EmuHawk/tools/ToolManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,6 @@ public void Restart(Config config, IEmulator emulator, IGameInfo game)
586586
_emulator = emulator;
587587
_game = game;
588588
_apiProvider = null;
589-
// If Cheat tool is loaded, restarting will restart the list too anyway
590-
if (!Has<Cheats>())
591-
{
592-
_owner.CheatList.NewList(GenerateDefaultCheatFilename(), autosave: true);
593-
}
594589

595590
var unavailable = new List<IToolForm>();
596591

0 commit comments

Comments
 (0)