Migrate ApiHawk from `CoreSystem` enum to const strings, and clean up

This commit is contained in:
YoshiRulz 2022-05-24 02:48:11 +10:00
parent 1026503d92
commit 9bdf043f36
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
6 changed files with 68 additions and 39 deletions
ExternalToolProjects/HelloWorld
src

View File

@ -10,7 +10,7 @@ namespace HelloWorld
{
/// <remarks>All of this is example code, but it's at least a little more substantiative than a simple "hello world".</remarks>
[ExternalTool("HelloWorld", Description = "An example of how to interact with EmuHawk")]
// [ExternalToolApplicability.SingleRom(CoreSystem.NES, "EA343F4E445A9050D4B4FBAC2C77D0693B1D0922")] // example of limiting tool usage (this is SMB1)
// [ExternalToolApplicability.SingleRom(VSystemID.Raw.NES, "EA343F4E445A9050D4B4FBAC2C77D0693B1D0922")] // example of limiting tool usage (this is SMB1)
[ExternalToolEmbeddedIcon("HelloWorld.icon_Hello.ico")]
public partial class CustomMainForm : ToolFormBase, IExternalToolForm
{

View File

@ -98,24 +98,35 @@ namespace BizHawk.Client.Common
CoreSystem.AppleII => VSystemID.Raw.AppleII,
CoreSystem.Atari2600 => VSystemID.Raw.A26,
CoreSystem.Atari7800 => VSystemID.Raw.A78,
CoreSystem.ChannelF => VSystemID.Raw.ChannelF,
CoreSystem.ColecoVision => VSystemID.Raw.Coleco,
CoreSystem.Commodore64 => VSystemID.Raw.C64,
CoreSystem.GameBoyLink => VSystemID.Raw.GBL,
CoreSystem.GameBoy => VSystemID.Raw.GB,
CoreSystem.GameBoyAdvance => VSystemID.Raw.GBA,
CoreSystem.Genesis => VSystemID.Raw.GEN,
CoreSystem.GGL => VSystemID.Raw.GGL,
CoreSystem.Intellivision => VSystemID.Raw.INTV,
CoreSystem.Libretro => VSystemID.Raw.Libretro,
CoreSystem.Lynx => VSystemID.Raw.Lynx,
CoreSystem.MAME => VSystemID.Raw.MAME,
CoreSystem.MasterSystem => VSystemID.Raw.SMS,
CoreSystem.MSX => VSystemID.Raw.MSX,
CoreSystem.NeoGeoPocket => VSystemID.Raw.NGP,
CoreSystem.NES => VSystemID.Raw.NES,
CoreSystem.Nintendo64 => VSystemID.Raw.N64,
CoreSystem.NintendoDS => VSystemID.Raw.NDS,
CoreSystem.Null => VSystemID.Raw.NULL,
CoreSystem.PCEngine => VSystemID.Raw.PCE,
CoreSystem.PcFx => VSystemID.Raw.PCFX,
CoreSystem.Playstation => VSystemID.Raw.PSX,
CoreSystem.Saturn => VSystemID.Raw.SAT,
CoreSystem.SNES => VSystemID.Raw.SNES,
CoreSystem.SuperGameBoy => VSystemID.Raw.SGB,
CoreSystem.TI83 => VSystemID.Raw.TI83,
CoreSystem.UzeBox => VSystemID.Raw.UZE,
CoreSystem.Vectrex => VSystemID.Raw.VEC,
CoreSystem.VirtualBoy => VSystemID.Raw.VB,
CoreSystem.WonderSwan => VSystemID.Raw.WSWAN,
CoreSystem.ZXSpectrum => VSystemID.Raw.ZXSpectrum,
CoreSystem.AmstradCPC => VSystemID.Raw.AmstradCPC,

View File

@ -21,8 +21,6 @@ namespace BizHawk.Client.Common
private readonly IGameInfo Game;
public static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new BizHawkSystemIdToEnumConverter();
private readonly IVideoProvider VideoProvider;
public event BeforeQuickLoadEventHandler BeforeQuickLoad;

View File

@ -1,8 +1,11 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.StringExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
@ -13,17 +16,21 @@ namespace BizHawk.Client.Common
[Obsolete("this is the default behaviour, you can safely omit this attribute")]
public sealed class Always : ExternalToolApplicabilityAttributeBase
{
public override bool NotApplicableTo(CoreSystem system) => false;
public override bool NotApplicableTo(string sysID)
=> false;
public override bool NotApplicableTo(string romHash, CoreSystem? system) => false;
public override bool NotApplicableTo(string romHash, string? sysID)
=> false;
}
[AttributeUsage(AttributeTargets.Class)]
public sealed class AnyRomLoaded : ExternalToolApplicabilityAttributeBase
{
public override bool NotApplicableTo(CoreSystem system) => system == CoreSystem.Null;
public override bool NotApplicableTo(string sysID)
=> sysID is VSystemID.Raw.NULL;
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system == CoreSystem.Null;
public override bool NotApplicableTo(string romHash, string? sysID)
=> sysID is VSystemID.Raw.NULL;
}
[AttributeUsage(AttributeTargets.Class)]
@ -31,19 +38,25 @@ namespace BizHawk.Client.Common
{
private readonly IList<string> _romHashes;
private readonly CoreSystem _system;
private readonly string _sysID;
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
public RomWhitelist(CoreSystem system, params string[] romHashes)
: this(SystemIdConverter.ConvertBack(system), romHashes) {}
public RomWhitelist(string sysID, params string[] romHashes)
{
if (system == CoreSystem.Null) throw new ArgumentException("there are no roms for the NULL system", nameof(system));
if (sysID is VSystemID.Raw.NULL) throw new ArgumentException("there are no roms for the NULL system", nameof(sysID));
if (!romHashes.All(NumericStringExtensions.IsHex)) throw new ArgumentException("misformatted hash", nameof(romHashes));
_system = system;
_romHashes = romHashes.ToList();
_sysID = sysID;
}
public override bool NotApplicableTo(CoreSystem system) => system != _system;
public override bool NotApplicableTo(string sysID)
=> sysID != _sysID;
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system || !_romHashes.Contains(romHash);
public override bool NotApplicableTo(string romHash, string? sysID)
=> sysID != _sysID || !_romHashes.Contains(romHash);
}
[AttributeUsage(AttributeTargets.Class)]
@ -51,44 +64,54 @@ namespace BizHawk.Client.Common
{
private readonly string _romHash;
private readonly CoreSystem _system;
private readonly string _sysID;
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
public SingleRom(CoreSystem system, string romHash)
: this(SystemIdConverter.ConvertBack(system), romHash) {}
public SingleRom(string sysID, string romHash)
{
if (system == CoreSystem.Null) throw new ArgumentException("there are no roms for the NULL system", nameof(system));
if (sysID is VSystemID.Raw.NULL) throw new ArgumentException("there are no roms for the NULL system", nameof(sysID));
if (!romHash.IsHex()) throw new ArgumentException("misformatted hash", nameof(romHash));
_system = system;
_romHash = romHash;
_sysID = sysID;
}
public override bool NotApplicableTo(CoreSystem system) => system != _system;
public override bool NotApplicableTo(string sysID)
=> sysID != _sysID;
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system || romHash != _romHash;
public override bool NotApplicableTo(string romHash, string? sysID)
=> sysID != _sysID || romHash != _romHash;
}
[AttributeUsage(AttributeTargets.Class)]
public sealed class SingleSystem : ExternalToolApplicabilityAttributeBase
{
private readonly CoreSystem _system;
private readonly string _sysID;
[Obsolete("replace CoreSystem with string from VSystemID.Raw")]
public SingleSystem(CoreSystem system)
{
_system = system;
}
: this(SystemIdConverter.ConvertBack(system)) {}
public override bool NotApplicableTo(CoreSystem system) => system != _system;
public SingleSystem(string sysID)
=> _sysID = sysID;
public override bool NotApplicableTo(string romHash, CoreSystem? system) => system != _system;
public override bool NotApplicableTo(string sysID)
=> sysID != _sysID;
public override bool NotApplicableTo(string romHash, string? sysID)
=> sysID != _sysID;
}
}
public abstract class ExternalToolApplicabilityAttributeBase : Attribute
{
public abstract bool NotApplicableTo(CoreSystem system);
protected static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new();
public abstract bool NotApplicableTo(string romHash, CoreSystem? system);
public abstract bool NotApplicableTo(string sysID);
public bool NotApplicableTo(string romHash) => NotApplicableTo(romHash, null);
public abstract bool NotApplicableTo(string romHash, string? sysID);
public class DuplicateException : Exception {}
}
@ -96,16 +119,14 @@ namespace BizHawk.Client.Common
[AttributeUsage(AttributeTargets.Class)]
public sealed class ExternalToolAttribute : Attribute
{
public string Description { get; set; }
public string? Description { get; set; }
public string[] LoadAssemblyFiles { get; set; }
public string[]? LoadAssemblyFiles { get; set; }
public readonly string Name;
public ExternalToolAttribute(string name)
{
Name = string.IsNullOrWhiteSpace(name) ? Guid.NewGuid().ToString() : name;
}
public ExternalToolAttribute(string? name)
=> Name = string.IsNullOrWhiteSpace(name) ? Guid.NewGuid().ToString() : name!;
public class MissingException : Exception {}
}
@ -118,8 +139,6 @@ namespace BizHawk.Client.Common
/// <param name="resourcePath">The full path, including the assembly name.</param>
public ExternalToolEmbeddedIconAttribute(string resourcePath)
{
ResourcePath = resourcePath;
}
=> ResourcePath = resourcePath;
}
}

View File

@ -379,7 +379,7 @@ namespace BizHawk.Client.EmuHawk
Controls.Add(_presentationPanel);
Controls.SetChildIndex(_presentationPanel, 0);
ExtToolManager = new ExternalToolManager(Config.PathEntries, () => (EmuClientApi.SystemIdConverter.Convert(Emulator.SystemId), Game.Hash));
ExtToolManager = new ExternalToolManager(Config.PathEntries, () => (Emulator.SystemId, Game.Hash));
Tools = new ToolManager(this, Config, DisplayManager, ExtToolManager, InputManager, Emulator, MovieSession, Game);
// TODO GL - move these event handlers somewhere less obnoxious line in the On* overrides

View File

@ -8,12 +8,13 @@ using System.Reflection;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
{
public sealed class ExternalToolManager
{
private readonly Func<(CoreSystem System, string Hash)> _getLoadedRomInfoCallback;
private readonly Func<(string SysID, string Hash)> _getLoadedRomInfoCallback;
private PathEntryCollection _paths;
@ -23,7 +24,7 @@ namespace BizHawk.Client.EmuHawk
internal readonly IList<string> PossibleExtToolTypeNames = new List<string>();
public ExternalToolManager(PathEntryCollection paths, Func<(CoreSystem System, string Hash)> getLoadedRomInfoCallback)
public ExternalToolManager(PathEntryCollection paths, Func<(string SysID, string Hash)> getLoadedRomInfoCallback)
{
_getLoadedRomInfoCallback = getLoadedRomInfoCallback;
Restart(paths);
@ -102,7 +103,7 @@ namespace BizHawk.Client.EmuHawk
var (system, loadedRomHash) = _getLoadedRomInfoCallback();
if (applicabilityAttrs[0].NotApplicableTo(system))
{
item.ToolTipText = system == CoreSystem.Null
item.ToolTipText = system is VSystemID.Raw.NULL
? "This tool doesn't work when no rom is loaded"
: "This tool doesn't work with this system";
return item;