BizHawk/BizHawk.Client.Common/CoreFileProvider.cs

145 lines
3.4 KiB
C#
Raw Normal View History

2013-10-25 00:59:34 +00:00
using System;
using System.IO;
using BizHawk.Emulation.Common;
2013-10-25 00:59:34 +00:00
namespace BizHawk.Client.Common
{
public class CoreFileProvider : ICoreFileProvider
{
2014-01-08 03:53:53 +00:00
public string SubfileDirectory { get; set; }
public FirmwareManager FirmwareManager { get; set; }
2013-10-25 00:59:34 +00:00
2014-01-08 03:53:53 +00:00
private readonly Action<string> _showWarning;
public CoreFileProvider(Action<string> showWarning)
{
2014-01-08 03:53:53 +00:00
_showWarning = showWarning;
}
2013-10-25 00:59:34 +00:00
public string PathSubfile(string fname)
{
2017-05-10 11:45:23 +00:00
return Path.Combine(SubfileDirectory ?? "", fname);
2013-10-25 00:59:34 +00:00
}
public string DllPath()
{
return Path.Combine(PathManager.GetExeDirectoryAbsolute(), "dll");
}
2015-11-06 14:31:50 +00:00
public string GetSaveRAMPath()
{
return PathManager.SaveRamPath(Global.Game);
}
public string GetRetroSaveRAMDirectory()
{
return PathManager.RetroSaveRAMDirectory(Global.Game);
}
public string GetRetroSystemPath()
{
return PathManager.RetroSystemPath(Global.Game);
}
public string GetGameBasePath()
{
return PathManager.GetGameBasePath(Global.Game);
}
#region EmuLoadHelper api
2014-01-08 03:53:53 +00:00
private void FirmwareWarn(string sysID, string firmwareID, bool required, string msg = null)
{
if (required)
{
2017-05-09 12:21:38 +00:00
var fullmsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? ": " + msg : ".")}";
2014-07-31 21:40:25 +00:00
throw new MissingFirmwareException(fullmsg);
}
2014-07-03 18:17:09 +00:00
if (msg != null)
{
2017-05-09 12:21:38 +00:00
var fullmsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}";
2014-07-03 18:17:09 +00:00
_showWarning(fullmsg);
}
}
2017-05-09 12:21:38 +00:00
public string GetFirmwarePath(string sysId, string firmwareId, bool required, string msg = null)
{
2017-05-09 12:21:38 +00:00
var path = FirmwareManager.Request(sysId, firmwareId);
if (path != null && !File.Exists(path))
2014-01-08 03:53:53 +00:00
{
path = null;
2014-01-08 03:53:53 +00:00
}
if (path == null)
2014-01-08 03:53:53 +00:00
{
2017-05-09 12:21:38 +00:00
FirmwareWarn(sysId, firmwareId, required, msg);
2014-01-08 03:53:53 +00:00
}
return path;
}
2017-05-09 12:21:38 +00:00
private byte[] GetFirmwareWithPath(string sysId, string firmwareId, bool required, string msg, out string path)
{
byte[] ret = null;
2017-05-09 12:21:38 +00:00
var path_ = GetFirmwarePath(sysId, firmwareId, required, msg);
if (path_ != null && File.Exists(path_))
{
try
{
ret = File.ReadAllBytes(path_);
}
2017-05-09 12:21:38 +00:00
catch (IOException)
{
}
}
if (ret == null && path_ != null)
2014-01-08 03:53:53 +00:00
{
2017-05-09 12:21:38 +00:00
FirmwareWarn(sysId, firmwareId, required, msg);
2014-01-08 03:53:53 +00:00
}
path = path_;
return ret;
}
2017-05-09 12:21:38 +00:00
public byte[] GetFirmware(string sysId, string firmwareId, bool required, string msg = null)
{
string unused;
2017-05-09 12:21:38 +00:00
return GetFirmwareWithPath(sysId, firmwareId, required, msg, out unused);
}
2017-05-09 12:21:38 +00:00
public byte[] GetFirmwareWithGameInfo(string sysId, string firmwareId, bool required, out GameInfo gi, string msg = null)
{
string path;
2017-05-09 12:21:38 +00:00
byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out path);
if (ret != null && path != null)
{
gi = Database.GetGameInfo(ret, path);
}
else
{
gi = null;
}
2017-05-09 12:21:38 +00:00
return ret;
}
#endregion
// this should go away now
public static void SyncCoreCommInputSignals(CoreComm target)
{
2015-04-21 23:50:15 +00:00
string superhack = null;
2017-05-18 16:36:38 +00:00
if (target.CoreFileProvider is CoreFileProvider)
2017-05-09 12:21:38 +00:00
{
2017-05-09 18:19:55 +00:00
superhack = ((CoreFileProvider)target.CoreFileProvider).SubfileDirectory;
2017-05-09 12:21:38 +00:00
}
var cfp = new CoreFileProvider(target.ShowMessage) { SubfileDirectory = superhack };
target.CoreFileProvider = cfp;
cfp.FirmwareManager = Global.FirmwareManager;
}
2013-10-25 00:59:34 +00:00
}
}