This will get simpler I promise

This commit is contained in:
nattthebear 2020-07-12 11:37:06 -04:00
parent 6f218ff20b
commit 4ad89667d6
2 changed files with 77 additions and 62 deletions

View File

@ -51,6 +51,29 @@ namespace BizHawk.Client.Common
public string Extension { get; set; } public string Extension { get; set; }
public GameInfo Game { get; set; } public GameInfo Game { get; set; }
} }
private class CoreInventoryParameters : ICoreInventoryParameters
{
private readonly RomLoader _parent;
public CoreInventoryParameters(RomLoader parent)
{
_parent = parent;
}
public CoreComm Comm { get; set; }
public GameInfo Game { get; set; }
public List<IRomAsset> Roms { get; set; } = new List<IRomAsset>();
public List<IDiscAsset> Discs { get; set; } = new List<IDiscAsset>();
public bool DeterministicEmulationRequested { get; set; }
public object FetchSettings(Type emulatorType, Type settingsType)
=> _parent.GetCoreSettings(emulatorType, settingsType);
public object FetchSyncSettings(Type emulatorType, Type syncSettingsType)
=> _parent.GetCoreSyncSettings(emulatorType, syncSettingsType);
}
private readonly Config _config; private readonly Config _config;
public RomLoader(Config config) public RomLoader(Config config)
@ -554,16 +577,23 @@ namespace BizHawk.Client.Common
break; break;
} }
nextEmulator = core.Create( nextEmulator = core.Create(new CoreInventoryParameters(this)
nextComm, {
game, Comm = nextComm,
rom.RomData, Game = game,
rom.FileData, Roms =
Deterministic, {
GetCoreSettings(core.Type, core.SettingsType), new RomAsset
GetCoreSyncSettings(core.Type, core.SyncSettingsType), {
rom.Extension RomData = rom.RomData,
); FileData = rom.FileData,
Extension = rom.Extension,
Game = game
}
},
DeterministicEmulationRequested = Deterministic
});
} }
private void LoadPSF(string path, CoreComm nextComm, HawkFile file, out IEmulator nextEmulator, out RomGame rom, out GameInfo game) private void LoadPSF(string path, CoreComm nextComm, HawkFile file, out IEmulator nextEmulator, out RomGame rom, out GameInfo game)

View File

@ -22,26 +22,12 @@ namespace BizHawk.Emulation.Cores
public string Extension { get; set; } public string Extension { get; set; }
public GameInfo Game { get; set; } public GameInfo Game { get; set; }
} }
// expected names and types of the parameters
private static readonly Dictionary<string, Type> ParamTypes = new Dictionary<string, Type>();
// map parameter names to locations in the constructor // map parameter names to locations in the constructor
private readonly Dictionary<string, int> _paramMap = new Dictionary<string, int>(); private readonly Dictionary<string, int> _paramMap = new Dictionary<string, int>();
// If true, this is a new style constructor that takes a CoreLoadParameters object
private readonly bool _useCoreLoadParameters; private readonly bool _useCoreLoadParameters;
static Core()
{
var pp = typeof(Core).GetMethod("Create")?.GetParameters();
if (pp != null)
{
foreach (var p in pp)
{
ParamTypes.Add(p.Name.ToLowerInvariant(), p.ParameterType);
}
}
}
public Core(string name, Type type, ConstructorInfo ctor) public Core(string name, Type type, ConstructorInfo ctor)
{ {
Name = name; Name = name;
@ -63,10 +49,6 @@ namespace BizHawk.Emulation.Cores
{ {
var p = pp[i]; var p = pp[i];
string pName = p.Name.ToLowerInvariant(); string pName = p.Name.ToLowerInvariant();
if (!ParamTypes.TryGetValue(pName, out _))
{
throw new InvalidOperationException($"Unexpected parameter name {p.Name} in constructor for {Type}");
}
if (pName == "settings") if (pName == "settings")
{ {
if (p.ParameterType == typeof(object)) if (p.ParameterType == typeof(object))
@ -100,48 +82,37 @@ namespace BizHawk.Emulation.Cores
/// <summary> /// <summary>
/// Instantiate an emulator core /// Instantiate an emulator core
/// </summary> /// </summary>
public IEmulator Create public IEmulator Create(ICoreInventoryParameters cip)
(
CoreComm comm,
GameInfo game,
byte[] rom,
byte[] file,
bool deterministic,
object settings,
object syncSettings,
string extension
)
{ {
if (_useCoreLoadParameters) if (_useCoreLoadParameters)
{ {
var paramType = typeof(CoreLoadParameters<,>).MakeGenericType(new[] { SettingsType, SyncSettingsType }); var paramType = typeof(CoreLoadParameters<,>).MakeGenericType(new[] { SettingsType, SyncSettingsType });
// TODO: clean this up // TODO: clean this up
dynamic param = Activator.CreateInstance(paramType); dynamic param = Activator.CreateInstance(paramType);
param.Comm = comm; param.Comm = cip.Comm;
param.Game = game; param.Game = cip.Game;
param.Settings = (dynamic)settings; param.Settings = (dynamic)cip.FetchSettings(Type, SettingsType);
param.SyncSettings = (dynamic)syncSettings; param.SyncSettings = (dynamic)cip.FetchSyncSettings(Type, SyncSettingsType);
param.Roms.Add(new RomGameFake param.Roms = cip.Roms;
{ param.Discs = cip.Discs;
RomData = rom, param.DeterministicEmulationRequested = cip.DeterministicEmulationRequested;
FileData = file,
Extension = extension,
Game = game,
});
param.DeterministicEmulationRequested = deterministic;
return (IEmulator)CTor.Invoke(new object[] { param }); return (IEmulator)CTor.Invoke(new object[] { param });
} }
object[] o = new object[_paramMap.Count]; else
Bp(o, "comm", comm); {
Bp(o, "game", game); // cores using the old constructor parameters can only take a single rom, so assume that here
Bp(o, "rom", rom); object[] o = new object[_paramMap.Count];
Bp(o, "file", file); Bp(o, "comm", cip.Comm);
Bp(o, "deterministic", deterministic); Bp(o, "game", cip.Game);
Bp(o, "settings", settings); Bp(o, "rom", cip.Roms[0].RomData);
Bp(o, "syncsettings", syncSettings); Bp(o, "file", cip.Roms[0].FileData);
Bp(o, "extension", extension); Bp(o, "deterministic", cip.DeterministicEmulationRequested);
Bp(o, "settings", cip.FetchSettings(Type, SettingsType));
Bp(o, "syncsettings", cip.FetchSyncSettings(Type, SyncSettingsType));
Bp(o, "extension", cip.Roms[0].Extension);
return (IEmulator)CTor.Invoke(o); return (IEmulator)CTor.Invoke(o);
}
} }
} }
@ -243,4 +214,18 @@ namespace BizHawk.Emulation.Cores
public IEnumerable<string> Systems => _systems; public IEnumerable<string> Systems => _systems;
} }
/// <summary>
/// What CoreInventory needs to synthesize CoreLoadParameters for a core
/// </summary>
public interface ICoreInventoryParameters
{
CoreComm Comm { get; }
GameInfo Game { get; }
List<IRomAsset> Roms { get; }
List<IDiscAsset> Discs { get; }
bool DeterministicEmulationRequested { get; }
object FetchSettings(Type emulatorType, Type settingsType);
object FetchSyncSettings(Type emulatorType, Type syncSettingsType);
}
} }