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
This commit is contained in:
kalimag 2024-09-11 14:35:54 +02:00 committed by James Groom
parent 76e73e1897
commit f90126f19b
4 changed files with 42 additions and 12 deletions

View File

@ -73,7 +73,11 @@ namespace BizHawk.Client.Common
public int? Compare => _compare.HasValue && !IsSeparator ? _compare : null; public int? Compare => _compare.HasValue && !IsSeparator ? _compare : null;
public MemoryDomain Domain => _watch.Domain; public MemoryDomain Domain
{
get => _watch.Domain;
set => _watch.Domain = value;
}
public WatchSize Size => _watch.Size; public WatchSize Size => _watch.Size;

View File

@ -404,6 +404,24 @@ namespace BizHawk.Client.Common
return true; return true;
} }
public void UpdateDomains(IMemoryDomains domains)
{
for (int i = _cheatList.Count - 1; i >= 0; i--)
{
var cheat = _cheatList[i];
var newDomain = domains[cheat.Domain.Name];
if (newDomain is not null)
{
cheat.Domain = newDomain;
}
else
{
_cheatList.RemoveAt(i);
Changes = true;
}
}
}
private static readonly RigidMultiPredicateSort<Cheat> ColumnSorts private static readonly RigidMultiPredicateSort<Cheat> ColumnSorts
= new RigidMultiPredicateSort<Cheat>(new Dictionary<string, Func<Cheat, IComparable>> = new RigidMultiPredicateSort<Cheat>(new Dictionary<string, Func<Cheat, IComparable>>
{ {

View File

@ -3783,21 +3783,32 @@ namespace BizHawk.Client.EmuHawk
} }
} }
var previousRom = CurrentlyOpenRom;
CurrentlyOpenRom = oaOpenrom?.Path ?? openAdvancedArgs; CurrentlyOpenRom = oaOpenrom?.Path ?? openAdvancedArgs;
CurrentlyOpenRomArgs = args; CurrentlyOpenRomArgs = args;
Tools.Restart(Config, Emulator, Game); Tools.Restart(Config, Emulator, Game);
if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains()) if (previousRom != CurrentlyOpenRom)
{ {
CheatList.SetDefaultFileName(Tools.GenerateDefaultCheatFilename()); CheatList.NewList(Tools.GenerateDefaultCheatFilename(), autosave: true);
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains())) if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains())
{ {
AddOnScreenMessage("Cheats file loaded"); if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains()))
{
AddOnScreenMessage("Cheats file loaded");
}
} }
else if (CheatList.Any()) }
else
{
if (Emulator.HasMemoryDomains())
{ {
CheatList.Clear(); CheatList.UpdateDomains(Emulator.AsMemoryDomains());
}
else
{
CheatList.NewList(Tools.GenerateDefaultCheatFilename(), autosave: true);
} }
} }
@ -3838,6 +3849,7 @@ namespace BizHawk.Client.EmuHawk
DisplayManager.UpdateGlobals(Config, Emulator); DisplayManager.UpdateGlobals(Config, Emulator);
DisplayManager.Blank(); DisplayManager.Blank();
ExtToolManager.BuildToolStrip(); ExtToolManager.BuildToolStrip();
CheatList.NewList("", autosave: true);
OnRomChanged(); OnRomChanged();
return false; return false;
} }
@ -3973,6 +3985,7 @@ namespace BizHawk.Client.EmuHawk
PauseOnFrame = null; PauseOnFrame = null;
CurrentlyOpenRom = null; CurrentlyOpenRom = null;
CurrentlyOpenRomArgs = null; CurrentlyOpenRomArgs = null;
CheatList.NewList("", autosave: true);
OnRomChanged(); OnRomChanged();
} }
} }

View File

@ -586,11 +586,6 @@ namespace BizHawk.Client.EmuHawk
_emulator = emulator; _emulator = emulator;
_game = game; _game = game;
_apiProvider = null; _apiProvider = null;
// If Cheat tool is loaded, restarting will restart the list too anyway
if (!Has<Cheats>())
{
_owner.CheatList.NewList(GenerateDefaultCheatFilename(), autosave: true);
}
var unavailable = new List<IToolForm>(); var unavailable = new List<IToolForm>();