BizHawk/BizHawk.Emulation.Common/Database/GameInfo.cs

184 lines
4.3 KiB
C#
Raw Normal View History

2011-08-04 04:45:53 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
2011-08-04 04:45:53 +00:00
using BizHawk.Common;
namespace BizHawk.Emulation.Common
2011-08-04 04:45:53 +00:00
{
public class GameInfo
{
2017-04-24 12:41:55 +00:00
public string Name { get; set; }
public string System { get; set; }
public string Hash { get; set; }
public string Region { get; set; }
public RomStatus Status { get; set; } = RomStatus.NotInDatabase;
public bool NotInDatabase { get; set; } = true;
public string FirmwareHash { get; set; }
public string ForcedCore { get; private set; }
2017-04-24 12:41:55 +00:00
private Dictionary<string, string> Options { get; set; } = new Dictionary<string, string>();
2017-04-24 12:41:55 +00:00
public GameInfo()
{
}
2014-05-23 15:47:48 +00:00
public GameInfo Clone()
{
var ret = (GameInfo)MemberwiseClone();
ret.Options = new Dictionary<string, string>(Options);
return ret;
}
2017-04-24 12:41:55 +00:00
public static GameInfo NullInstance => new GameInfo
{
2017-04-24 12:41:55 +00:00
Name = "Null",
System = "NULL",
2017-05-10 11:45:23 +00:00
Hash = "",
Region = "",
2017-04-24 12:41:55 +00:00
Status = RomStatus.GoodDump,
2017-05-10 11:45:23 +00:00
ForcedCore = "",
2017-04-24 12:41:55 +00:00
NotInDatabase = false
};
internal GameInfo(CompactGameInfo cgi)
{
Name = cgi.Name;
System = cgi.System;
Hash = cgi.Hash;
2014-03-18 03:28:25 +00:00
Region = cgi.Region;
Status = cgi.Status;
ForcedCore = cgi.ForcedCore;
NotInDatabase = false;
ParseOptionsDictionary(cgi.MetaData);
}
public void AddOption(string option)
{
2017-05-10 11:45:23 +00:00
Options[option] = "";
}
public void AddOption(string option, string param)
{
Options[option] = param;
}
public void RemoveOption(string option)
{
Options.Remove(option);
}
2017-04-24 12:41:55 +00:00
public bool this[string option] => Options.ContainsKey(option);
public bool OptionPresent(string option)
{
return Options.ContainsKey(option);
}
public string OptionValue(string option)
{
return Options.ContainsKey(option) ? Options[option] : null;
}
public int GetIntValue(string option)
{
return int.Parse(Options[option]);
}
public string GetStringValue(string option)
{
return Options[option];
}
public int GetHexValue(string option)
{
return int.Parse(Options[option], NumberStyles.HexNumber);
}
2017-04-24 12:41:55 +00:00
/// <summary>
/// /// Gets a boolean value from the database
/// </summary>
/// <param name="parameter">The option to look up</param>
/// <param name="defaultVal">The value to return if the option is invalid or doesn't exist</param>
2017-04-24 12:41:55 +00:00
/// <returns> The boolean value from the database if present, otherwise the given default value</returns>
public bool GetBool(string parameter, bool defaultVal)
{
if (OptionPresent(parameter) && OptionValue(parameter) == "true")
2017-04-24 12:41:55 +00:00
{
return true;
2017-04-24 12:41:55 +00:00
}
if (OptionPresent(parameter) && OptionValue(parameter) == "false")
{
2013-05-27 01:59:45 +00:00
return false;
2017-04-24 12:41:55 +00:00
}
return defaultVal;
}
2017-04-24 12:41:55 +00:00
/// <summary>
/// /// Gets an integer value from the database
/// </summary>
/// <param name="parameter">The option to look up</param>
/// <param name="defaultVal">The value to return if the option is invalid or doesn't exist</param>
2017-04-24 12:41:55 +00:00
/// <returns> The integer value from the database if present, otherwise the given default value</returns>
public int GetInt(string parameter, int defaultVal)
{
if (OptionPresent(parameter))
2017-04-24 12:41:55 +00:00
{
try
{
return int.Parse(OptionValue(parameter));
}
catch
{
return defaultVal;
}
2017-04-24 12:41:55 +00:00
}
return defaultVal;
}
public ICollection<string> GetOptions()
{
return Options.Keys;
}
2017-04-24 12:41:55 +00:00
public IDictionary<string, string> GetOptionsDict()
{
return new ReadOnlyDictionary<string, string>(Options);
}
2017-04-24 12:41:55 +00:00
private void ParseOptionsDictionary(string metaData)
{
if (string.IsNullOrEmpty(metaData))
2017-04-24 12:41:55 +00:00
{
return;
2017-04-24 12:41:55 +00:00
}
var options = metaData.Split(';').Where(opt => string.IsNullOrEmpty(opt) == false).ToArray();
foreach (var opt in options)
{
var parts = opt.Split('=');
var key = parts[0];
2017-05-10 11:45:23 +00:00
var value = parts.Length > 1 ? parts[1] : "";
Options[key] = value;
}
}
}
public static class GameInfoExtensions
{
public static bool IsNullInstance(this GameInfo game)
{
return game == null || game.System == "NULL";
}
2020-03-15 23:37:49 +00:00
public static bool IsRomStatusBad(this GameInfo game)
{
return game.Status == RomStatus.BadDump || game.Status == RomStatus.Overdump;
}
}
2011-08-04 04:45:53 +00:00
}