using System.Collections.Generic; using System.Linq; using System.Globalization; using BizHawk.Common; namespace BizHawk.Emulation.Common { public class GameInfo { 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; } private Dictionary Options { get; set; } = new Dictionary(); public GameInfo() { } public GameInfo Clone() { var ret = (GameInfo)MemberwiseClone(); ret.Options = new Dictionary(Options); return ret; } public static GameInfo NullInstance => new GameInfo { Name = "Null", System = "NULL", Hash = "", Region = "", Status = RomStatus.GoodDump, ForcedCore = "", NotInDatabase = false }; internal GameInfo(CompactGameInfo cgi) { Name = cgi.Name; System = cgi.System; Hash = cgi.Hash; Region = cgi.Region; Status = cgi.Status; ForcedCore = cgi.ForcedCore; NotInDatabase = false; ParseOptionsDictionary(cgi.MetaData); } public void AddOption(string option) { Options[option] = ""; } public void AddOption(string option, string param) { Options[option] = param; } public void RemoveOption(string option) { Options.Remove(option); } 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); } /// /// /// Gets a boolean value from the database /// /// The option to look up /// The value to return if the option is invalid or doesn't exist /// The boolean value from the database if present, otherwise the given default value public bool GetBool(string parameter, bool defaultVal) { if (OptionPresent(parameter) && OptionValue(parameter) == "true") { return true; } if (OptionPresent(parameter) && OptionValue(parameter) == "false") { return false; } return defaultVal; } /// /// /// Gets an integer value from the database /// /// The option to look up /// The value to return if the option is invalid or doesn't exist /// The integer value from the database if present, otherwise the given default value public int GetInt(string parameter, int defaultVal) { if (OptionPresent(parameter)) { try { return int.Parse(OptionValue(parameter)); } catch { return defaultVal; } } return defaultVal; } public ICollection GetOptions() { return Options.Keys; } public IDictionary GetOptionsDict() { return new ReadOnlyDictionary(Options); } private void ParseOptionsDictionary(string metaData) { if (string.IsNullOrEmpty(metaData)) { return; } var options = metaData.Split(';').Where(opt => string.IsNullOrEmpty(opt) == false).ToArray(); foreach (var opt in options) { var parts = opt.Split('='); var key = parts[0]; 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"; } public static bool IsRomStatusBad(this GameInfo game) { return game.Status == RomStatus.BadDump || game.Status == RomStatus.Overdump; } } }