a round of code cleanup on RomLoader

This commit is contained in:
adelikat 2013-12-29 23:54:40 +00:00
parent dae86e770c
commit 134783ff36
1 changed files with 118 additions and 127 deletions

View File

@ -1,50 +1,53 @@
using System; using System;
using System.Collections.Generic; using System.IO;
using System.Linq;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
using BizHawk.Emulation.DiscSystem; using BizHawk.Emulation.Cores.Atari.Atari2600;
using System.IO; using BizHawk.Emulation.Cores.Atari.Atari7800;
using BizHawk.Emulation.Cores.Calculators;
using BizHawk.Emulation.Cores.ColecoVision;
using BizHawk.Emulation.Cores.Computers.Commodore64;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx; using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
using BizHawk.Emulation.Cores.Intellivision;
using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Nintendo.GBA;
using BizHawk.Emulation.Cores.Nintendo.N64;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Emulation.Cores.Sega.Saturn; 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.PCEngine; using BizHawk.Emulation.DiscSystem;
using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Cores.Intellivision;
using BizHawk.Emulation.Cores.ColecoVision;
using BizHawk.Emulation.Cores.Nintendo.GBA;
using BizHawk.Emulation.Cores.Atari.Atari7800;
using BizHawk.Emulation.Cores.Computers.Commodore64;
using BizHawk.Emulation.Cores.Calculators;
using BizHawk.Emulation.Cores.Atari.Atari2600;
using BizHawk.Emulation.Cores.Nintendo.N64;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public class RomLoader public class RomLoader
{ {
// helper methods for the settings events // helper methods for the settings events
private object GetCoreSettings<T>()
object GetCoreSettings<T>()
where T : IEmulator where T : IEmulator
{ {
var e = new SettingsLoadArgs(typeof(T)); var e = new SettingsLoadArgs(typeof(T));
if (OnLoadSettings != null) if (OnLoadSettings != null)
{
OnLoadSettings(this, e); OnLoadSettings(this, e);
}
return e.Settings; return e.Settings;
} }
object GetCoreSyncSettings<T>() private object GetCoreSyncSettings<T>()
where T : IEmulator where T : IEmulator
{ {
var e = new SettingsLoadArgs(typeof(T)); var e = new SettingsLoadArgs(typeof(T));
if (OnLoadSyncSettings != null) if (OnLoadSyncSettings != null)
{
OnLoadSyncSettings(this, e); OnLoadSyncSettings(this, e);
}
return e.Settings; return e.Settings;
} }
@ -85,17 +88,17 @@ namespace BizHawk.Client.Common
Settings = null; Settings = null;
} }
} }
public delegate void SettingsLoadEventHandler(object sender, SettingsLoadArgs e); public delegate void SettingsLoadEventHandler(object sender, SettingsLoadArgs e);
public event SettingsLoadEventHandler OnLoadSettings; public event SettingsLoadEventHandler OnLoadSettings;
public event SettingsLoadEventHandler OnLoadSyncSettings; public event SettingsLoadEventHandler OnLoadSyncSettings;
public delegate void LoadErrorEventHandler(object sener, RomErrorArgs e); public delegate void LoadErrorEventHandler(object sener, RomErrorArgs e);
public event LoadErrorEventHandler OnLoadError; public event LoadErrorEventHandler OnLoadError;
public Func<HawkFile, int?> ChooseArchive; public Func<HawkFile, int?> ChooseArchive { get; set; }
public Action<string> CoreCommMessageCallback; // TODO: eww, do we have to do this? public Action<string> CoreCommMessageCallback { get; set; } // TODO: eww, do we have to do this?
private int? HandleArchive(HawkFile file) private int? HandleArchive(HawkFile file)
{ {
@ -116,10 +119,10 @@ namespace BizHawk.Client.Common
} }
// TODO I'm in mainform.cs and here, move me to a common place that both can call, static method on the config object? // TODO I'm in mainform.cs and here, move me to a common place that both can call, static method on the config object?
private void CommitCoreSettingsToConfig() private static void CommitCoreSettingsToConfig()
{ {
// save settings object // save settings object
Type t = Global.Emulator.GetType(); var t = Global.Emulator.GetType();
Global.Config.PutCoreSettings(Global.Emulator.GetSettings(), t); Global.Config.PutCoreSettings(Global.Emulator.GetSettings(), t);
// don't trample config with loaded-from-movie settings // don't trample config with loaded-from-movie settings
@ -160,7 +163,7 @@ namespace BizHawk.Client.Common
// if we have an archive and need to bind something, then pop the dialog // if we have an archive and need to bind something, then pop the dialog
if (file.IsArchive && !file.IsBound) if (file.IsArchive && !file.IsBound)
{ {
var result = ChooseArchive(file); var result = HandleArchive(file);
if (result.HasValue) if (result.HasValue)
{ {
file.BindArchiveMember(result.Value); file.BindArchiveMember(result.Value);
@ -223,40 +226,27 @@ namespace BizHawk.Client.Common
switch (game.System) switch (game.System)
{ {
case "GEN": case "GEN":
{
var genesis = new GPGX( var genesis = new GPGX(
nextComm, null, disc, "GEN", GetCoreSettings<GPGX>()); nextComm, null, disc, "GEN", GetCoreSettings<GPGX>());
nextEmulator = genesis; nextEmulator = genesis;
}
break; break;
case "SAT": case "SAT":
{ nextEmulator = new Yabause(nextComm, disc, GetCoreSyncSettings<Yabause>());
var saturn = new Yabause(nextComm, disc, GetCoreSyncSettings<Yabause>());
nextEmulator = saturn;
}
break; break;
case "PSP": case "PSP":
{ nextEmulator = new PSP(nextComm, file.Name);
var psp = new PSP(nextComm, file.Name);
nextEmulator = psp;
}
break; break;
case "PSX": case "PSX":
{ nextEmulator = new Octoshock(nextComm);
var psx = new Octoshock(nextComm); (nextEmulator as Octoshock).LoadCuePath(file.CanonicalFullPath);
nextEmulator = psx;
psx.LoadCuePath(file.CanonicalFullPath);
nextEmulator.CoreComm.RomStatusDetails = "PSX etc."; nextEmulator.CoreComm.RomStatusDetails = "PSX etc.";
}
break; break;
case "PCE": case "PCE":
case "PCECD": case "PCECD":
{
var biosPath = Global.FirmwareManager.Request("PCECD", "Bios"); var biosPath = Global.FirmwareManager.Request("PCECD", "Bios");
if (File.Exists(biosPath) == false) if (!File.Exists(biosPath))
{ {
ThrowLoadError("PCE-CD System Card not found. Please check the BIOS path in Config->Paths->PC Engine." ThrowLoadError("PCE-CD System Card not found. Please check the BIOS path in Config->Paths->PC Engine.", game.System);
, game.System);
return false; return false;
} }
@ -266,20 +256,20 @@ namespace BizHawk.Client.Common
{ {
ThrowLoadError( ThrowLoadError(
"The PCE-CD System Card you have selected is known to be a bad dump. This may cause problems playing PCE-CD games.\n\n" "The PCE-CD System Card you have selected is known to be a bad dump. This may cause problems playing PCE-CD games.\n\n"
+ "It is recommended that you find a good dump of the system card. Sorry to be the bearer of bad news!" + "It is recommended that you find a good dump of the system card. Sorry to be the bearer of bad news!",
, game.System); game.System);
} }
else if (rom.GameInfo.NotInDatabase) else if (rom.GameInfo.NotInDatabase)
{ {
ThrowLoadError( ThrowLoadError(
"The PCE-CD System Card you have selected is not recognized in our database. That might mean it's a bad dump, or isn't the correct rom." "The PCE-CD System Card you have selected is not recognized in our database. That might mean it's a bad dump, or isn't the correct rom.",
, game.System); game.System);
} }
else if (rom.GameInfo["BIOS"] == false) else if (rom.GameInfo["BIOS"] == false)
{ {
ThrowLoadError( ThrowLoadError(
"The PCE-CD System Card you have selected is not a BIOS image. You may have selected the wrong rom." "The PCE-CD System Card you have selected is not a BIOS image. You may have selected the wrong rom.",
, game.System); game.System);
} }
if (rom.GameInfo["SuperSysCard"]) if (rom.GameInfo["SuperSysCard"])
@ -290,8 +280,8 @@ namespace BizHawk.Client.Common
if (game["NeedSuperSysCard"] && game["SuperSysCard"] == false) if (game["NeedSuperSysCard"] && game["SuperSysCard"] == false)
{ {
ThrowLoadError( ThrowLoadError(
"This game requires a version 3.0 System card and won't run with the system card you've selected. Try selecting a 3.0 System Card in Config->Paths->PC Engine." "This game requires a version 3.0 System card and won't run with the system card you've selected. Try selecting a 3.0 System Card in Config->Paths->PC Engine.",
, game.System); game.System);
} }
game.FirmwareHash = Util.Hash_SHA1(rom.RomData); game.FirmwareHash = Util.Hash_SHA1(rom.RomData);
@ -299,29 +289,29 @@ namespace BizHawk.Client.Common
break; break;
} }
} }
}
else if (file.Extension.ToLower() == ".xml") else if (file.Extension.ToLower() == ".xml")
{ {
try try
{ {
var XMLG = XmlGame.Create(file); // if load fails, are we supposed to retry as a bsnes XML???????? var xmlGame = XmlGame.Create(file); // if load fails, are we supposed to retry as a bsnes XML????????
game = XMLG.GI; game = xmlGame.GI;
switch (game.System) switch (game.System)
{ {
case "DGB": case "DGB":
var left = Database.GetGameInfo(xmlGame.Assets["LeftRom"], "left.gb");
var L = Database.GetGameInfo(XMLG.Assets["LeftRom"], "left.gb"); var right = Database.GetGameInfo(xmlGame.Assets["RightRom"], "right.gb");
var R = Database.GetGameInfo(XMLG.Assets["RightRom"], "right.gb"); nextEmulator = new GambatteLink(
nextComm,
var gbl = new GambatteLink(nextComm, L, XMLG.Assets["LeftRom"], R, XMLG.Assets["RightRom"], left,
xmlGame.Assets["LeftRom"],
right,
xmlGame.Assets["RightRom"],
GetCoreSettings<GambatteLink>(), GetCoreSettings<GambatteLink>(),
GetCoreSyncSettings<GambatteLink>()); GetCoreSyncSettings<GambatteLink>());
nextEmulator = gbl;
// other stuff todo // other stuff todo
break; break;
default: default:
return false; return false;
} }
@ -336,7 +326,7 @@ namespace BizHawk.Client.Common
rom = new RomGame(file); rom = new RomGame(file);
game = rom.GameInfo; game = rom.GameInfo;
bool isXml = false; var isXml = false;
// other xml has already been handled // other xml has already been handled
if (file.Extension.ToLower() == ".xml") if (file.Extension.ToLower() == ".xml")
@ -350,14 +340,14 @@ namespace BizHawk.Client.Common
case "SNES": case "SNES":
{ {
// need to get rid of this hack at some point // need to get rid of this hack at some point
((CoreFileProvider)nextComm.CoreFileProvider).SubfileDirectory = Path.GetDirectoryName(path.Replace("|", string.Empty)); //Dirty hack to get around archive filenames (since we are just getting the directory path, it is safe to mangle the filename ((CoreFileProvider)nextComm.CoreFileProvider).SubfileDirectory = Path.GetDirectoryName(path.Replace("|", String.Empty)); // Dirty hack to get around archive filenames (since we are just getting the directory path, it is safe to mangle the filename
var snes = new LibsnesCore(nextComm, GetCoreSettings<LibsnesCore>(), GetCoreSyncSettings<LibsnesCore>()); var snes = new LibsnesCore(nextComm, GetCoreSettings<LibsnesCore>(), GetCoreSyncSettings<LibsnesCore>());
nextEmulator = snes; nextEmulator = snes;
byte[] romData = isXml ? null : rom.FileData; var romData = isXml ? null : rom.FileData;
byte[] xmlData = isXml ? rom.FileData : null; var xmlData = isXml ? rom.FileData : null;
snes.Load(game, romData, Deterministic, xmlData); snes.Load(game, romData, Deterministic, xmlData);
} }
break; break;
case "SMS": case "SMS":
case "SG": case "SG":
@ -365,7 +355,10 @@ namespace BizHawk.Client.Common
nextEmulator = new SMS(nextComm, game, rom.RomData, GetCoreSettings<SMS>(), GetCoreSyncSettings<SMS>()); nextEmulator = new SMS(nextComm, game, rom.RomData, GetCoreSettings<SMS>(), GetCoreSyncSettings<SMS>());
break; break;
case "A26": case "A26":
nextEmulator = new Atari2600(nextComm, game, rom.FileData, nextEmulator = new Atari2600(
nextComm,
game,
rom.FileData,
GetCoreSettings<Atari2600>(), GetCoreSettings<Atari2600>(),
GetCoreSyncSettings<Atari2600>()); GetCoreSyncSettings<Atari2600>());
break; break;
@ -375,16 +368,17 @@ namespace BizHawk.Client.Common
nextEmulator = new PCEngine(nextComm, game, rom.RomData, GetCoreSettings<PCEngine>()); nextEmulator = new PCEngine(nextComm, game, rom.RomData, GetCoreSettings<PCEngine>());
break; break;
case "GEN": case "GEN":
{
// nextEmulator = new Genesis(nextComm, game, rom.RomData); // nextEmulator = new Genesis(nextComm, game, rom.RomData);
nextEmulator = new GPGX(nextComm, rom.RomData, null, "GEN", GetCoreSyncSettings<GPGX>()); nextEmulator = new GPGX(nextComm, rom.RomData, null, "GEN", GetCoreSyncSettings<GPGX>());
break; break;
}
case "TI83": case "TI83":
nextEmulator = new TI83(nextComm, game, rom.RomData); nextEmulator = new TI83(nextComm, game, rom.RomData);
break; break;
case "NES": case "NES":
nextEmulator = new NES(nextComm, game, rom.FileData, nextEmulator = new NES(
nextComm,
game,
rom.FileData,
GetCoreSettings<NES>(), GetCoreSettings<NES>(),
Global.MovieSession.Movie.Header.BoardProperties); Global.MovieSession.Movie.Header.BoardProperties);
break; break;
@ -392,10 +386,12 @@ namespace BizHawk.Client.Common
case "GBC": case "GBC":
if (!Global.Config.GB_AsSGB) if (!Global.Config.GB_AsSGB)
{ {
var gb = new Gameboy(nextComm, game, rom.FileData, nextEmulator = new Gameboy(
nextComm,
game,
rom.FileData,
GetCoreSettings<Gameboy>(), GetCoreSettings<Gameboy>(),
GetCoreSyncSettings<Gameboy>()); GetCoreSyncSettings<Gameboy>());
nextEmulator = gb;
} }
else else
{ {
@ -410,33 +406,26 @@ namespace BizHawk.Client.Common
catch catch
{ {
// failed to load SGB bios. to avoid catch-22, disable SGB mode // failed to load SGB bios. to avoid catch-22, disable SGB mode
ThrowLoadError("Failed to load a GB rom in SGB mode. Disabling SGB Mode." ThrowLoadError("Failed to load a GB rom in SGB mode. Disabling SGB Mode.", game.System);
, game.System);
Global.Config.GB_AsSGB = false; Global.Config.GB_AsSGB = false;
throw; throw;
} }
} }
break; break;
case "Coleco": case "Coleco":
{ nextEmulator = new ColecoVision(nextComm, game, rom.RomData, GetCoreSyncSettings<ColecoVision>());
var c = new ColecoVision(nextComm, game, rom.RomData, GetCoreSyncSettings<ColecoVision>());
nextEmulator = c;
}
break; break;
case "INTV": case "INTV":
{ nextEmulator = new Intellivision(nextComm, game, rom.RomData);
var intv = new Intellivision(nextComm, game, rom.RomData);
nextEmulator = intv;
}
break; break;
case "A78": case "A78":
var gamedbpath = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "EMU7800.csv"); var gamedbpath = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "EMU7800.csv");
var a78 = new Atari7800(nextComm, game, rom.RomData, gamedbpath); nextEmulator = new Atari7800(nextComm, game, rom.RomData, gamedbpath);
nextEmulator = a78;
break; break;
case "C64": case "C64":
C64 c64 = new C64(nextComm, game, rom.RomData, rom.Extension); var c64 = new C64(nextComm, game, rom.RomData, rom.Extension);
c64.HardReset(); c64.HardReset(); // TODO: The core should be responsible for this!
nextEmulator = c64; nextEmulator = c64;
break; break;
case "GBA": case "GBA":
@ -446,17 +435,18 @@ namespace BizHawk.Client.Common
gba.Load(rom.RomData); gba.Load(rom.RomData);
nextEmulator = gba; nextEmulator = gba;
} }
break; break;
case "N64": case "N64":
Global.Game = game; Global.Game = game;
nextEmulator = new N64(nextComm, game, rom.RomData, GetCoreSyncSettings<N64>()); nextEmulator = new N64(nextComm, game, rom.RomData, GetCoreSyncSettings<N64>());
break; break;
case "DEBUG": case "DEBUG":
if (VersionInfo.INTERIM) if (VersionInfo.INTERIM)
{ {
nextEmulator = LibRetroEmulator.CreateDebug(nextComm, rom.RomData); nextEmulator = LibRetroEmulator.CreateDebug(nextComm, rom.RomData);
} }
break; break;
} }
} }
@ -472,6 +462,7 @@ namespace BizHawk.Client.Common
ThrowLoadError("Exception during loadgame:\n\n" + ex, "NULL"); ThrowLoadError("Exception during loadgame:\n\n" + ex, "NULL");
return false; return false;
} }
Rom = rom; Rom = rom;
LoadedEmulator = nextEmulator; LoadedEmulator = nextEmulator;
NextComm = nextComm; NextComm = nextComm;