some cleanup of RomLoader.cs

This commit is contained in:
adelikat 2020-02-08 10:46:46 -06:00
parent 01640e8c0b
commit 4fed717858
1 changed files with 60 additions and 100 deletions

View File

@ -13,14 +13,12 @@ using BizHawk.Emulation.Cores.Computers.AppleII;
using BizHawk.Emulation.Cores.Computers.Commodore64; using BizHawk.Emulation.Cores.Computers.Commodore64;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx; using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
using BizHawk.Emulation.Cores.Nintendo.Gameboy; using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Nintendo.GBHawk;
using BizHawk.Emulation.Cores.Nintendo.GBHawkLink; using BizHawk.Emulation.Cores.Nintendo.GBHawkLink;
using BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x; using BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x;
using BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x; using BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x;
using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Cores.Sega.GGHawkLink; using BizHawk.Emulation.Cores.Sega.GGHawkLink;
using BizHawk.Emulation.Cores.Sega.Saturn;
using BizHawk.Emulation.Cores.Sony.PSP; using BizHawk.Emulation.Cores.Sony.PSP;
using BizHawk.Emulation.Cores.Sony.PSX; using BizHawk.Emulation.Cores.Sony.PSX;
using BizHawk.Emulation.Cores.Computers.SinclairSpectrum; using BizHawk.Emulation.Cores.Computers.SinclairSpectrum;
@ -30,7 +28,6 @@ using BizHawk.Emulation.DiscSystem;
using BizHawk.Emulation.Cores.Consoles.Sega.Saturn; using BizHawk.Emulation.Cores.Consoles.Sega.Saturn;
using BizHawk.Emulation.Cores.Consoles.NEC.PCFX; using BizHawk.Emulation.Cores.Consoles.NEC.PCFX;
using BizHawk.Emulation.Cores.Computers.AmstradCPC; using BizHawk.Emulation.Cores.Computers.AmstradCPC;
using BizHawk.Emulation.Cores.Consoles.Vectrex;
using BizHawk.Emulation.Cores.Consoles.ChannelF; using BizHawk.Emulation.Cores.Consoles.ChannelF;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
@ -39,7 +36,7 @@ namespace BizHawk.Client.Common
{ {
public enum LoadErrorType public enum LoadErrorType
{ {
Unknown, MissingFirmware, XML, DiscError Unknown, MissingFirmware, Xml, DiscError
} }
// helper methods for the settings events // helper methods for the settings events
@ -69,7 +66,7 @@ namespace BizHawk.Client.Common
return e.Settings; return e.Settings;
} }
// For not throwing errors but simply outputing information to the screen // For not throwing errors but simply outputting information to the screen
public Action<string> MessageCallback { get; set; } public Action<string> MessageCallback { get; set; }
private void DoMessageCallback(string message) private void DoMessageCallback(string message)
@ -102,18 +99,18 @@ namespace BizHawk.Client.Common
RomPath = path; RomPath = path;
} }
public string Message { get; private set; } public string Message { get; }
public string AttemptedCoreLoad { get; private set; } public string AttemptedCoreLoad { get; }
public string RomPath { get; private set; } public string RomPath { get; }
public bool? Deterministic { get; set; } public bool? Deterministic { get; set; }
public bool Retry { get; set; } public bool Retry { get; set; }
public LoadErrorType Type { get; private set; } public LoadErrorType Type { get; }
} }
public class SettingsLoadArgs : EventArgs public class SettingsLoadArgs : EventArgs
{ {
public object Settings { get; set; } public object Settings { get; set; }
public Type Core { get; private set; } public Type Core { get; }
public SettingsLoadArgs(Type t) public SettingsLoadArgs(Type t)
{ {
Core = t; Core = t;
@ -133,24 +130,24 @@ namespace BizHawk.Client.Common
public Func<RomGame, string> ChoosePlatform { get; set; } public Func<RomGame, string> ChoosePlatform { get; set; }
// in case we get sent back through the picker more than once, use the same choice the second time // in case we get sent back through the picker more than once, use the same choice the second time
private int? previouschoice; private int? _previousChoice;
private int? HandleArchive(HawkFile file) private int? HandleArchive(HawkFile file)
{ {
if (previouschoice.HasValue) if (_previousChoice.HasValue)
{ {
return previouschoice; return _previousChoice;
} }
if (ChooseArchive != null) if (ChooseArchive != null)
{ {
previouschoice = ChooseArchive(file); _previousChoice = ChooseArchive(file);
return previouschoice; return _previousChoice;
} }
return null; return null;
} }
// May want to phase out this method in favor of the overload with more paramaters // May want to phase out this method in favor of the overload with more parameters
private void DoLoadErrorCallback(string message, string systemId, LoadErrorType type = LoadErrorType.Unknown) private void DoLoadErrorCallback(string message, string systemId, LoadErrorType type = LoadErrorType.Unknown)
{ {
OnLoadError?.Invoke(this, new RomErrorArgs(message, systemId, type)); OnLoadError?.Invoke(this, new RomErrorArgs(message, systemId, type));
@ -208,17 +205,16 @@ namespace BizHawk.Client.Common
var sw = new StringWriter(); var sw = new StringWriter();
foreach (var e in xmlGame.AssetFullPaths) foreach (var e in xmlGame.AssetFullPaths)
{ {
Disc disc = null;
string discPath = e; string discPath = e;
//--- load the disc in a context which will let us abort if it's going to take too long //--- load the disc in a context which will let us abort if it's going to take too long
var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 }; var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 };
discMountJob.Run(); discMountJob.Run();
disc = discMountJob.OUT_Disc; var disc = discMountJob.OUT_Disc;
if (discMountJob.OUT_SlowLoadAborted) if (discMountJob.OUT_SlowLoadAborted)
{ {
DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", systemId, LoadErrorType.DiscError); DoLoadErrorCallback("This disc would take too long to load. Run it through DiscoHawk first, or find a new rip because this one is probably junk", systemId, LoadErrorType.DiscError);
} }
else if (discMountJob.OUT_ErrorLevel) else if (discMountJob.OUT_ErrorLevel)
@ -277,7 +273,7 @@ namespace BizHawk.Client.Common
file.Open(path); file.Open(path);
// if the provided file doesnt even exist, give up! // if the provided file doesn't even exist, give up!
if (!file.Exists) if (!file.Exists)
{ {
return false; return false;
@ -301,7 +297,7 @@ namespace BizHawk.Client.Common
var retro = new LibretroCore(nextComm, nextComm.LaunchLibretroCore); var retro = new LibretroCore(nextComm, nextComm.LaunchLibretroCore);
nextEmulator = retro; nextEmulator = retro;
// kind of dirty.. we need to stash this, and then we can unstash it in a moment, in case the core doesnt fail // kind of dirty.. we need to stash this, and then we can unstash it in a moment, in case the core doesn't fail
var oldGame = Global.Game; var oldGame = Global.Game;
if (retro.Description.SupportsNoGame && string.IsNullOrEmpty(path)) if (retro.Description.SupportsNoGame && string.IsNullOrEmpty(path))
@ -311,7 +307,7 @@ namespace BizHawk.Client.Common
var gameName = codePathPart; var gameName = codePathPart;
Global.Game = game = new GameInfo { Name = gameName, System = "Libretro" }; Global.Game = game = new GameInfo { Name = gameName, System = "Libretro" };
// if we are allowed to run NoGame and we dont have a game, boot up the core that way // if we are allowed to run NoGame and we don't have a game, boot up the core that way
bool ret = retro.LoadNoGame(); bool ret = retro.LoadNoGame();
Global.Game = oldGame; Global.Game = oldGame;
@ -378,7 +374,7 @@ namespace BizHawk.Client.Common
else else
{ {
// at this point, file is either assigned to the ROM path, if it exists, // at this point, file is either assigned to the ROM path, if it exists,
// or is empty and corecomm is not a libretro core // or is empty and CoreComm is not a libretro core
// so, we still need to check path here before continuing // so, we still need to check path here before continuing
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
{ {
@ -387,7 +383,7 @@ namespace BizHawk.Client.Common
} }
// if not libretro: // if not libretro:
// do extension checknig // do extension checking
ext = file.Extension.ToLowerInvariant(); ext = file.Extension.ToLowerInvariant();
// do the archive binding we had to skip // do the archive binding we had to skip
@ -416,22 +412,21 @@ namespace BizHawk.Client.Common
// load discs for all the m3u // load discs for all the m3u
m3u.Rebase(Path.GetDirectoryName(path)); m3u.Rebase(Path.GetDirectoryName(path));
List<Disc> discs = new List<Disc>(); var discs = new List<Disc>();
List<string> discNames = new List<string>(); var discNames = new List<string>();
StringWriter sw = new StringWriter(); var sw = new StringWriter();
foreach (var e in m3u.Entries) foreach (var e in m3u.Entries)
{ {
Disc disc = null;
string discPath = e.Path; string discPath = e.Path;
//--- load the disc in a context which will let us abort if it's going to take too long //--- load the disc in a context which will let us abort if it's going to take too long
var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 }; var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 };
discMountJob.Run(); discMountJob.Run();
disc = discMountJob.OUT_Disc; var disc = discMountJob.OUT_Disc;
if (discMountJob.OUT_SlowLoadAborted) if (discMountJob.OUT_SlowLoadAborted)
{ {
DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError); DoLoadErrorCallback("This disc would take too long to load. Run it through DiscoHawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError);
return false; return false;
} }
@ -491,15 +486,13 @@ namespace BizHawk.Client.Common
throw new InvalidOperationException("Can't load CD files from archives!"); throw new InvalidOperationException("Can't load CD files from archives!");
} }
string discHash = null;
//--- load the disc in a context which will let us abort if it's going to take too long //--- load the disc in a context which will let us abort if it's going to take too long
var discMountJob = new DiscMountJob { IN_FromPath = path, IN_SlowLoadAbortThreshold = 8 }; var discMountJob = new DiscMountJob { IN_FromPath = path, IN_SlowLoadAbortThreshold = 8 };
discMountJob.Run(); discMountJob.Run();
if (discMountJob.OUT_SlowLoadAborted) if (discMountJob.OUT_SlowLoadAborted)
{ {
DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError); DoLoadErrorCallback("This disc would take too long to load. Run it through DiscoHawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError);
return false; return false;
} }
@ -508,19 +501,14 @@ namespace BizHawk.Client.Common
throw new InvalidOperationException($"\r\n{discMountJob.OUT_Log}"); throw new InvalidOperationException($"\r\n{discMountJob.OUT_Log}");
} }
var disc = discMountJob.OUT_Disc; var disc = discMountJob.OUT_Disc;
// ----------- // -----------
// TODO - use more sophisticated IDer // TODO - use more sophisticated IDer
var discType = new DiscIdentifier(disc).DetectDiscType(); var discType = new DiscIdentifier(disc).DetectDiscType();
if (discType == DiscType.SonyPSX) var discHash = discType == DiscType.SonyPSX
{ ? new DiscHasher(disc).Calculate_PSX_BizIDHash().ToString("X8")
discHash = new DiscHasher(disc).Calculate_PSX_BizIDHash().ToString("X8"); : new DiscHasher(disc).OldHash();
}
else
{
discHash = new DiscHasher(disc).OldHash();
}
game = Database.CheckDatabase(discHash); game = Database.CheckDatabase(discHash);
if (game == null) if (game == null)
@ -567,15 +555,9 @@ namespace BizHawk.Client.Common
case DiscType.AudioDisc: case DiscType.AudioDisc:
case DiscType.UnknownCDFS: case DiscType.UnknownCDFS:
case DiscType.UnknownFormat: case DiscType.UnknownFormat:
if (PreferredPlatformIsDefined(ext)) game.System = PreferredPlatformIsDefined(ext)
{ ? Global.Config.PreferredPlatformsForExtensions[ext]
game.System = Global.Config.PreferredPlatformsForExtensions[ext]; : "NULL";
}
else
{
game.System = "NULL"; // "PCECD";
}
break; break;
} }
} }
@ -604,7 +586,7 @@ namespace BizHawk.Client.Common
} }
else else
{ {
StringWriter sw = new StringWriter(); var sw = new StringWriter();
sw.WriteLine("Disc was identified (99.99% confidently) as known good with disc id hash CRC32:{0:X8}", discHash); sw.WriteLine("Disc was identified (99.99% confidently) as known good with disc id hash CRC32:{0:X8}", discHash);
sw.WriteLine("Nonetheless it could be an unrecognized romhack or patched version."); sw.WriteLine("Nonetheless it could be an unrecognized romhack or patched version.");
sw.WriteLine("According to redump.org, the ideal hash for entire disc is: CRC32:{0:X8}", game.GetStringValue("dh")); sw.WriteLine("According to redump.org, the ideal hash for entire disc is: CRC32:{0:X8}", game.GetStringValue("dh"));
@ -732,7 +714,7 @@ namespace BizHawk.Client.Common
break; break;
case "ZXSpectrum": case "ZXSpectrum":
List<GameInfo> zxGI = new List<GameInfo>(); var zxGI = new List<GameInfo>();
foreach (var a in xmlGame.Assets) foreach (var a in xmlGame.Assets)
{ {
zxGI.Add(new GameInfo { Name = Path.GetFileNameWithoutExtension(a.Key) }); zxGI.Add(new GameInfo { Name = Path.GetFileNameWithoutExtension(a.Key) });
@ -740,15 +722,15 @@ namespace BizHawk.Client.Common
nextEmulator = new ZXSpectrum( nextEmulator = new ZXSpectrum(
nextComm, nextComm,
xmlGame.Assets.Select(a => a.Value), //.First(), xmlGame.Assets.Select(a => a.Value),
zxGI, // GameInfo.NullInstance, zxGI,
(ZXSpectrum.ZXSpectrumSettings)GetCoreSettings<ZXSpectrum>(), (ZXSpectrum.ZXSpectrumSettings)GetCoreSettings<ZXSpectrum>(),
(ZXSpectrum.ZXSpectrumSyncSettings)GetCoreSyncSettings<ZXSpectrum>(), (ZXSpectrum.ZXSpectrumSyncSettings)GetCoreSyncSettings<ZXSpectrum>(),
Deterministic); Deterministic);
break; break;
case "AmstradCPC": case "AmstradCPC":
List<GameInfo> cpcGI = new List<GameInfo>(); var cpcGI = new List<GameInfo>();
foreach (var a in xmlGame.Assets) foreach (var a in xmlGame.Assets)
{ {
cpcGI.Add(new GameInfo { Name = Path.GetFileNameWithoutExtension(a.Key) }); cpcGI.Add(new GameInfo { Name = Path.GetFileNameWithoutExtension(a.Key) });
@ -768,17 +750,16 @@ namespace BizHawk.Client.Common
var sw = new StringWriter(); var sw = new StringWriter();
foreach (var e in entries) foreach (var e in entries)
{ {
Disc disc = null;
string discPath = e; string discPath = e;
//--- load the disc in a context which will let us abort if it's going to take too long //--- load the disc in a context which will let us abort if it's going to take too long
var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 }; var discMountJob = new DiscMountJob { IN_FromPath = discPath, IN_SlowLoadAbortThreshold = 8 };
discMountJob.Run(); discMountJob.Run();
disc = discMountJob.OUT_Disc; var disc = discMountJob.OUT_Disc;
if (discMountJob.OUT_SlowLoadAborted) if (discMountJob.OUT_SlowLoadAborted)
{ {
DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "PSX", LoadErrorType.DiscError); DoLoadErrorCallback("This disc would take too long to load. Run it through DiscoHawk first, or find a new rip because this one is probably junk", "PSX", LoadErrorType.DiscError);
return false; return false;
} }
@ -899,7 +880,7 @@ namespace BizHawk.Client.Common
} }
catch catch
{ {
DoLoadErrorCallback(ex.ToString(), "DGB", LoadErrorType.XML); DoLoadErrorCallback(ex.ToString(), "DGB", LoadErrorType.Xml);
return false; return false;
} }
} }
@ -1047,14 +1028,9 @@ namespace BizHawk.Client.Common
if (preference == "neshawk") if (preference == "neshawk")
{ {
if (Global.Config.UseSubNESHawk) core = Global.Config.UseSubNESHawk
{ ? CoreInventory.Instance["NES", "SubNESHawk"]
core = CoreInventory.Instance["NES", "SubNESHawk"]; : CoreInventory.Instance["NES", "NesHawk"];
}
else
{
core = CoreInventory.Instance["NES", "NesHawk"];
}
} }
else else
{ {
@ -1067,14 +1043,9 @@ namespace BizHawk.Client.Common
case "GB": case "GB":
if (!Global.Config.GbAsSgb) if (!Global.Config.GbAsSgb)
{ {
if (Global.Config.GbUseGbHawk) core = Global.Config.GbUseGbHawk
{ ? CoreInventory.Instance["GB", "GBHawk"]
core = CoreInventory.Instance["GB", "GBHawk"]; : CoreInventory.Instance["GB", "Gambatte"];
}
else
{
core = CoreInventory.Instance["GB", "Gambatte"];
}
} }
else else
{ {
@ -1094,14 +1065,9 @@ namespace BizHawk.Client.Common
case "GBC": case "GBC":
if (!Global.Config.GbAsSgb) if (!Global.Config.GbAsSgb)
{ {
if (Global.Config.GbUseGbHawk) core = Global.Config.GbUseGbHawk
{ ? CoreInventory.Instance["GBC", "GBHawk"]
core = CoreInventory.Instance["GBC", "GBHawk"]; : CoreInventory.Instance["GBC", "Gambatte"];
}
else
{
core = CoreInventory.Instance["GBC", "Gambatte"];
}
} }
else else
{ {
@ -1119,8 +1085,8 @@ namespace BizHawk.Client.Common
} }
break; break;
case "A78": case "A78":
var gamedbpath = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb_a7800.csv"); var gameDbPath = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "gamedb_a7800.csv");
nextEmulator = new A7800Hawk(nextComm, game, rom.RomData, gamedbpath, GetCoreSettings<A7800Hawk>(), GetCoreSyncSettings<A7800Hawk>()); nextEmulator = new A7800Hawk(nextComm, game, rom.RomData, gameDbPath, GetCoreSettings<A7800Hawk>(), GetCoreSyncSettings<A7800Hawk>());
break; break;
case "C64": case "C64":
var c64 = new C64(nextComm, Enumerable.Repeat(rom.FileData, 1), rom.GameInfo, GetCoreSettings<C64>(), GetCoreSyncSettings<C64>()); var c64 = new C64(nextComm, Enumerable.Repeat(rom.FileData, 1), rom.GameInfo, GetCoreSettings<C64>(), GetCoreSyncSettings<C64>());
@ -1143,23 +1109,16 @@ namespace BizHawk.Client.Common
nextEmulator = cpc; nextEmulator = cpc;
break; break;
case "GBA": case "GBA":
if (Global.Config.GbaUsemGba) core = Global.Config.GbaUsemGba
{ ? CoreInventory.Instance["GBA", "mGBA"]
core = CoreInventory.Instance["GBA", "mGBA"]; : CoreInventory.Instance["GBA", "VBA-Next"];
}
else
{
core = CoreInventory.Instance["GBA", "VBA-Next"];
}
break; break;
case "PSX": case "PSX":
nextEmulator = new Octoshock(nextComm, null, null, rom.FileData, GetCoreSettings<Octoshock>(), GetCoreSyncSettings<Octoshock>()); nextEmulator = new Octoshock(nextComm, null, null, rom.FileData, GetCoreSettings<Octoshock>(), GetCoreSyncSettings<Octoshock>());
nextEmulator.CoreComm.RomStatusDetails = "PSX etc."; nextEmulator.CoreComm.RomStatusDetails = "PSX etc.";
break; break;
case "Arcade": case "Arcade":
string gameName = ""; nextEmulator = new MAME(nextComm, file.Directory, file.CanonicalName, GetCoreSyncSettings<MAME>(), out var gameName);
nextEmulator = new MAME(nextComm, file.Directory, file.CanonicalName, GetCoreSyncSettings<MAME>(), out gameName);
rom.GameInfo.Name = gameName; rom.GameInfo.Name = gameName;
break; break;
case "GEN": case "GEN":
@ -1186,7 +1145,7 @@ namespace BizHawk.Client.Common
if (core != null) if (core != null)
{ {
// use coreinventory // use CoreInventory
nextEmulator = core.Create(nextComm, game, rom.RomData, rom.FileData, Deterministic, GetCoreSettings(core.Type), GetCoreSyncSettings(core.Type)); nextEmulator = core.Create(nextComm, game, rom.RomData, rom.FileData, Deterministic, GetCoreSettings(core.Type), GetCoreSyncSettings(core.Type));
} }
} }
@ -1226,7 +1185,8 @@ namespace BizHawk.Client.Common
return LoadRom(path, nextComm, true, recursiveCount + 1); return LoadRom(path, nextComm, true, recursiveCount + 1);
} }
else if (ex is MissingFirmwareException)
if (ex is MissingFirmwareException)
{ {
DoLoadErrorCallback(ex.Message, system, path, Deterministic, LoadErrorType.MissingFirmware); DoLoadErrorCallback(ex.Message, system, path, Deterministic, LoadErrorType.MissingFirmware);
} }
@ -1239,7 +1199,7 @@ namespace BizHawk.Client.Common
return LoadRom(path, nextComm, false, recursiveCount + 1); return LoadRom(path, nextComm, false, recursiveCount + 1);
} }
// handle exceptions thrown by the new detected systems that bizhawk does not have cores for // handle exceptions thrown by the new detected systems that BizHawk does not have cores for
else if (ex is NoAvailableCoreException) else if (ex is NoAvailableCoreException)
{ {
DoLoadErrorCallback($"{ex.Message}\n\n{ex}", system); DoLoadErrorCallback($"{ex.Message}\n\n{ex}", system);