Skip to content

Commit 9bdf043

Browse files
committed
Migrate ApiHawk from CoreSystem enum to const strings, and clean up
1 parent 1026503 commit 9bdf043

File tree

6 files changed

+68
-39
lines changed

6 files changed

+68
-39
lines changed

ExternalToolProjects/HelloWorld/CustomMainForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace HelloWorld
1010
{
1111
/// <remarks>All of this is example code, but it's at least a little more substantiative than a simple "hello world".</remarks>
1212
[ExternalTool("HelloWorld", Description = "An example of how to interact with EmuHawk")]
13-
// [ExternalToolApplicability.SingleRom(CoreSystem.NES, "EA343F4E445A9050D4B4FBAC2C77D0693B1D0922")] // example of limiting tool usage (this is SMB1)
13+
// [ExternalToolApplicability.SingleRom(VSystemID.Raw.NES, "EA343F4E445A9050D4B4FBAC2C77D0693B1D0922")] // example of limiting tool usage (this is SMB1)
1414
[ExternalToolEmbeddedIcon("HelloWorld.icon_Hello.ico")]
1515
public partial class CustomMainForm : ToolFormBase, IExternalToolForm
1616
{

src/BizHawk.Client.Common/Api/BizHawkSystemIdToCoreSystemEnumConverter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,35 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
9898
CoreSystem.AppleII => VSystemID.Raw.AppleII,
9999
CoreSystem.Atari2600 => VSystemID.Raw.A26,
100100
CoreSystem.Atari7800 => VSystemID.Raw.A78,
101+
CoreSystem.ChannelF => VSystemID.Raw.ChannelF,
101102
CoreSystem.ColecoVision => VSystemID.Raw.Coleco,
102103
CoreSystem.Commodore64 => VSystemID.Raw.C64,
103104
CoreSystem.GameBoyLink => VSystemID.Raw.GBL,
104105
CoreSystem.GameBoy => VSystemID.Raw.GB,
105106
CoreSystem.GameBoyAdvance => VSystemID.Raw.GBA,
106107
CoreSystem.Genesis => VSystemID.Raw.GEN,
108+
CoreSystem.GGL => VSystemID.Raw.GGL,
107109
CoreSystem.Intellivision => VSystemID.Raw.INTV,
108110
CoreSystem.Libretro => VSystemID.Raw.Libretro,
109111
CoreSystem.Lynx => VSystemID.Raw.Lynx,
112+
CoreSystem.MAME => VSystemID.Raw.MAME,
110113
CoreSystem.MasterSystem => VSystemID.Raw.SMS,
114+
CoreSystem.MSX => VSystemID.Raw.MSX,
115+
CoreSystem.NeoGeoPocket => VSystemID.Raw.NGP,
111116
CoreSystem.NES => VSystemID.Raw.NES,
112117
CoreSystem.Nintendo64 => VSystemID.Raw.N64,
118+
CoreSystem.NintendoDS => VSystemID.Raw.NDS,
113119
CoreSystem.Null => VSystemID.Raw.NULL,
114120
CoreSystem.PCEngine => VSystemID.Raw.PCE,
121+
CoreSystem.PcFx => VSystemID.Raw.PCFX,
115122
CoreSystem.Playstation => VSystemID.Raw.PSX,
116123
CoreSystem.Saturn => VSystemID.Raw.SAT,
117124
CoreSystem.SNES => VSystemID.Raw.SNES,
125+
CoreSystem.SuperGameBoy => VSystemID.Raw.SGB,
118126
CoreSystem.TI83 => VSystemID.Raw.TI83,
127+
CoreSystem.UzeBox => VSystemID.Raw.UZE,
128+
CoreSystem.Vectrex => VSystemID.Raw.VEC,
129+
CoreSystem.VirtualBoy => VSystemID.Raw.VB,
119130
CoreSystem.WonderSwan => VSystemID.Raw.WSWAN,
120131
CoreSystem.ZXSpectrum => VSystemID.Raw.ZXSpectrum,
121132
CoreSystem.AmstradCPC => VSystemID.Raw.AmstradCPC,

src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public sealed class EmuClientApi : IEmuClientApi
2121

2222
private readonly IGameInfo Game;
2323

24-
public static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new BizHawkSystemIdToEnumConverter();
25-
2624
private readonly IVideoProvider VideoProvider;
2725

2826
public event BeforeQuickLoadEventHandler BeforeQuickLoad;
Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections.Generic;
35
using System.Linq;
46

57
using BizHawk.Common.StringExtensions;
8+
using BizHawk.Emulation.Common;
69

710
namespace BizHawk.Client.Common
811
{
@@ -13,99 +16,117 @@ public static class ExternalToolApplicability
1316
[Obsolete("this is the default behaviour, you can safely omit this attribute")]
1417
public sealed class Always : ExternalToolApplicabilityAttributeBase
1518
{
16-
public override bool NotApplicableTo(CoreSystem system) => false;
19+
public override bool NotApplicableTo(string sysID)
20+
=> false;
1721

18-
public override bool NotApplicableTo(string romHash, CoreSystem? system) => false;
22+
public override bool NotApplicableTo(string romHash, string? sysID)
23+
=> false;
1924
}
2025

2126
[AttributeUsage(AttributeTargets.Class)]
2227
public sealed class AnyRomLoaded : ExternalToolApplicabilityAttributeBase
2328
{
24-
public override bool NotApplicableTo(CoreSystem system) => system == CoreSystem.Null;
29+
public override bool NotApplicableTo(string sysID)
30+
=> sysID is VSystemID.Raw.NULL;
2531

26-
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system == CoreSystem.Null;
32+
public override bool NotApplicableTo(string romHash, string? sysID)
33+
=> sysID is VSystemID.Raw.NULL;
2734
}
2835

2936
[AttributeUsage(AttributeTargets.Class)]
3037
public sealed class RomWhitelist : ExternalToolApplicabilityAttributeBase
3138
{
3239
private readonly IList<string> _romHashes;
3340

34-
private readonly CoreSystem _system;
41+
private readonly string _sysID;
3542

43+
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
3644
public RomWhitelist(CoreSystem system, params string[] romHashes)
45+
: this(SystemIdConverter.ConvertBack(system), romHashes) {}
46+
47+
public RomWhitelist(string sysID, params string[] romHashes)
3748
{
38-
if (system == CoreSystem.Null) throw new ArgumentException("there are no roms for the NULL system", nameof(system));
49+
if (sysID is VSystemID.Raw.NULL) throw new ArgumentException("there are no roms for the NULL system", nameof(sysID));
3950
if (!romHashes.All(NumericStringExtensions.IsHex)) throw new ArgumentException("misformatted hash", nameof(romHashes));
40-
_system = system;
4151
_romHashes = romHashes.ToList();
52+
_sysID = sysID;
4253
}
4354

44-
public override bool NotApplicableTo(CoreSystem system) => system != _system;
55+
public override bool NotApplicableTo(string sysID)
56+
=> sysID != _sysID;
4557

46-
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system || !_romHashes.Contains(romHash);
58+
public override bool NotApplicableTo(string romHash, string? sysID)
59+
=> sysID != _sysID || !_romHashes.Contains(romHash);
4760
}
4861

4962
[AttributeUsage(AttributeTargets.Class)]
5063
public sealed class SingleRom : ExternalToolApplicabilityAttributeBase
5164
{
5265
private readonly string _romHash;
5366

54-
private readonly CoreSystem _system;
67+
private readonly string _sysID;
5568

69+
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
5670
public SingleRom(CoreSystem system, string romHash)
71+
: this(SystemIdConverter.ConvertBack(system), romHash) {}
72+
73+
public SingleRom(string sysID, string romHash)
5774
{
58-
if (system == CoreSystem.Null) throw new ArgumentException("there are no roms for the NULL system", nameof(system));
75+
if (sysID is VSystemID.Raw.NULL) throw new ArgumentException("there are no roms for the NULL system", nameof(sysID));
5976
if (!romHash.IsHex()) throw new ArgumentException("misformatted hash", nameof(romHash));
60-
_system = system;
6177
_romHash = romHash;
78+
_sysID = sysID;
6279
}
6380

64-
public override bool NotApplicableTo(CoreSystem system) => system != _system;
81+
public override bool NotApplicableTo(string sysID)
82+
=> sysID != _sysID;
6583

66-
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system || romHash != _romHash;
84+
public override bool NotApplicableTo(string romHash, string? sysID)
85+
=> sysID != _sysID || romHash != _romHash;
6786
}
6887

6988
[AttributeUsage(AttributeTargets.Class)]
7089
public sealed class SingleSystem : ExternalToolApplicabilityAttributeBase
7190
{
72-
private readonly CoreSystem _system;
91+
private readonly string _sysID;
7392

93+
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
7494
public SingleSystem(CoreSystem system)
75-
{
76-
_system = system;
77-
}
95+
: this(SystemIdConverter.ConvertBack(system)) {}
7896

79-
public override bool NotApplicableTo(CoreSystem system) => system != _system;
97+
public SingleSystem(string sysID)
98+
=> _sysID = sysID;
8099

81-
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system;
100+
public override bool NotApplicableTo(string sysID)
101+
=> sysID != _sysID;
102+
103+
public override bool NotApplicableTo(string romHash, string? sysID)
104+
=> sysID != _sysID;
82105
}
83106
}
84107

85108
public abstract class ExternalToolApplicabilityAttributeBase : Attribute
86109
{
87-
public abstract bool NotApplicableTo(CoreSystem system);
110+
protected static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new();
88111

89-
public abstract bool NotApplicableTo(string romHash, CoreSystem? system);
112+
public abstract bool NotApplicableTo(string sysID);
90113

91-
public bool NotApplicableTo(string romHash) => NotApplicableTo(romHash, null);
114+
public abstract bool NotApplicableTo(string romHash, string? sysID);
92115

93116
public class DuplicateException : Exception {}
94117
}
95118

96119
[AttributeUsage(AttributeTargets.Class)]
97120
public sealed class ExternalToolAttribute : Attribute
98121
{
99-
public string Description { get; set; }
122+
public string? Description { get; set; }
100123

101-
public string[] LoadAssemblyFiles { get; set; }
124+
public string[]? LoadAssemblyFiles { get; set; }
102125

103126
public readonly string Name;
104127

105-
public ExternalToolAttribute(string name)
106-
{
107-
Name = string.IsNullOrWhiteSpace(name) ? Guid.NewGuid().ToString() : name;
108-
}
128+
public ExternalToolAttribute(string? name)
129+
=> Name = string.IsNullOrWhiteSpace(name) ? Guid.NewGuid().ToString() : name!;
109130

110131
public class MissingException : Exception {}
111132
}
@@ -118,8 +139,6 @@ public sealed class ExternalToolEmbeddedIconAttribute : Attribute
118139

119140
/// <param name="resourcePath">The full path, including the assembly name.</param>
120141
public ExternalToolEmbeddedIconAttribute(string resourcePath)
121-
{
122-
ResourcePath = resourcePath;
123-
}
142+
=> ResourcePath = resourcePath;
124143
}
125144
}

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ void MainForm_MouseClick(object sender, MouseEventArgs e)
379379
Controls.Add(_presentationPanel);
380380
Controls.SetChildIndex(_presentationPanel, 0);
381381

382-
ExtToolManager = new ExternalToolManager(Config.PathEntries, () => (EmuClientApi.SystemIdConverter.Convert(Emulator.SystemId), Game.Hash));
382+
ExtToolManager = new ExternalToolManager(Config.PathEntries, () => (Emulator.SystemId, Game.Hash));
383383
Tools = new ToolManager(this, Config, DisplayManager, ExtToolManager, InputManager, Emulator, MovieSession, Game);
384384

385385
// TODO GL - move these event handlers somewhere less obnoxious line in the On* overrides

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
using System.Windows.Forms;
99
using BizHawk.Client.Common;
1010
using BizHawk.Common;
11+
using BizHawk.Emulation.Common;
1112

1213
namespace BizHawk.Client.EmuHawk
1314
{
1415
public sealed class ExternalToolManager
1516
{
16-
private readonly Func<(CoreSystem System, string Hash)> _getLoadedRomInfoCallback;
17+
private readonly Func<(string SysID, string Hash)> _getLoadedRomInfoCallback;
1718

1819
private PathEntryCollection _paths;
1920

@@ -23,7 +24,7 @@ public sealed class ExternalToolManager
2324

2425
internal readonly IList<string> PossibleExtToolTypeNames = new List<string>();
2526

26-
public ExternalToolManager(PathEntryCollection paths, Func<(CoreSystem System, string Hash)> getLoadedRomInfoCallback)
27+
public ExternalToolManager(PathEntryCollection paths, Func<(string SysID, string Hash)> getLoadedRomInfoCallback)
2728
{
2829
_getLoadedRomInfoCallback = getLoadedRomInfoCallback;
2930
Restart(paths);
@@ -102,7 +103,7 @@ private ToolStripMenuItem GenerateToolTipFromFileName(string fileName)
102103
var (system, loadedRomHash) = _getLoadedRomInfoCallback();
103104
if (applicabilityAttrs[0].NotApplicableTo(system))
104105
{
105-
item.ToolTipText = system == CoreSystem.Null
106+
item.ToolTipText = system is VSystemID.Raw.NULL
106107
? "This tool doesn't work when no rom is loaded"
107108
: "This tool doesn't work with this system";
108109
return item;

0 commit comments

Comments
 (0)