--- trunk/EmuXPortal/Api/EmuConfig.cs 2012/04/06 12:14:21 46 +++ trunk/EmuXPortal/Api/EmuConfig.cs 2014/08/04 12:39:13 200 @@ -6,11 +6,14 @@ using System.IO; using EmuXPortal.Logging; using System.Xml; +using System.Diagnostics; namespace EmuXPortal.Api { - public interface IEmuConfig : IComparable + public interface IEmuConfig : IComparable, IDisposable { + bool IsFavorites { get; } + string ConfigPath { get; } string PlatformNameShort { get; } string PlatformNameLong { get; } Image PlatformImage { get; } @@ -20,22 +23,32 @@ string EmuRomPath { get; set; } string ToString(); - void RefreshConfig(); - } + bool HasExternalConfigs { get; } + List ExternalConfigs { get; } - + + string GameTitle { get; } + string GameImage { get; } + string GameExe { get; } + string GameExeArgs { get; } + + + void RefreshConfig(logger log); + void ReleasePlatformImageResource(); + } public static class EmuConfigLoader { + public static readonly IEmuConfig Empty = new EmuConfig(); private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration #region load - public static IEmuConfig Load(string rom_path) { return new EmuConfig().Create(rom_path); } - public static IEmuConfig Load(string config_path, string rom_path) { return new EmuConfig().Create(config_path, rom_path); } + public static IEmuConfig Load(logger log,string rom_path) { return new EmuConfig().Create(log, rom_path); } + public static IEmuConfig Load(logger log,string config_path, string rom_path) { return new EmuConfig().Create(log, config_path, rom_path); } #endregion #region parse emu options - public static string GetEMUOptions(IRomConfig config) + public static string GetEMUOptions(logger log, IRomConfig config) { - EMUOptions EMUOptions = new EMUOptions(config); + EMUOptions EMUOptions = new EMUOptions(log, config); return EMUOptions.Options; } #endregion @@ -47,10 +60,10 @@ private const string ROM_PATH = "%ROM_PATH%"; #endregion private Dictionary options_dict = new Dictionary(); - public EMUOptions(IRomConfig config) + public EMUOptions(logger log, IRomConfig config) { init_dict(config); - config.Config.RefreshConfig(); + config.Config.RefreshConfig(log); string options = config.Config.EmuOptions; string real_options = options; foreach (KeyValuePair pair in options_dict) { if (options.ToLower().Contains(pair.Key.ToLower())) { real_options = real_options.ToLower().Replace(pair.Key.ToLower(), pair.Value); } } @@ -66,20 +79,65 @@ #endregion #region private class EmuConfig : IEmuConfig - private class EmuConfig : IEmuConfig, IComparable + internal class EmuConfig : IEmuConfig, IComparable, IDisposable { - public IEmuConfig Create(string rom_path) { return this.Create(string.Empty, rom_path); } - public IEmuConfig Create(string config_path, string rom_path) + public IEmuConfig Create(logger log, string rom_path) { return this.Create(log, string.Empty, rom_path); } + public IEmuConfig Create(logger log, string config_path, string rom_path) { - EmuRomPath = rom_path; - if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); } + IEmuConfig parent_rom = null; + if (rom_path == "") + { + if (config_path != "") + { + EmuRomPath = config_path.Replace(EMU_CONFIG, "").TrimEnd(new char[] { '\\' }); + } + else + { + EmuRomPath = Config.RomPath; + } + } + else { EmuRomPath = rom_path; } + if (config_path == "") + { + config_path = string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG); + } + + + if (rom_path == "") + { + FileInfo emu_config = new FileInfo(string.Format(@"{0}\{1}", new FileInfo(config_path).Directory, EMU_CONFIG)); + //if (emu_config.Exists) + //{ + // EmuRomPath = emu_config.Directory.FullName; + //} + while (!emu_config.Exists) + { + try + { + emu_config = new FileInfo(string.Format(@"{0}\{1}", emu_config.Directory.Parent.FullName, EMU_CONFIG)); + } + catch (Exception) + { + break; + } + } + if (emu_config.Exists) + { + EmuRomPath = emu_config.Directory.FullName; + // load the rom config + parent_rom = EmuConfigLoader.Load(log, emu_config.FullName, EmuRomPath); + } + + + } // read the actual config emu.config FileInfo fi = new FileInfo(config_path); if (fi.Exists) { - logger.WriteLine("Found EMU Config File: {0}", config_path); - logger.WriteLine("\tLoading Config: {0}", config_path); + log.WriteLine("Found EMU Config File: {0}", config_path); + log.WriteLine("\tLoading Config: {0}", config_path); + this.ConfigPath = config_path; //bool loaded = false; try { @@ -87,72 +145,203 @@ { using (XmlReader reader = XmlReader.Create(fs)) { - string value = ""; - reader.ReadToFollowing("PLATFORMNAMESHORT"); - value = reader.ReadElementContentAsString(); - PlatformNameShort = (value == "") ? PlatformNameShort : value; - logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort); - - reader.ReadToFollowing("PLATFORMNAMELONG"); - value = reader.ReadElementContentAsString(); - PlatformNameLong = (value == "") ? PlatformNameLong : value; - logger.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong); - - reader.ReadToFollowing("PLATFORMIMAGE"); - string platform_image = reader.ReadElementContentAsString(); - PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}", rom_path, platform_image)); - string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image; - logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image); - - reader.ReadToFollowing("EXTENSIONS"); - value = reader.ReadElementContentAsString(); - Extenstions = (value == "") ? Extenstions : value; - logger.WriteLine("\t\tEXTENSIONS={0}", Extenstions); - - reader.ReadToFollowing("EMULATORPATH"); - value = reader.ReadElementContentAsString(); - EmuPath = (value == "") ? EmuPath : value; - logger.WriteLine("\t\tEMULATORPATH={0}", EmuPath); - - reader.ReadToFollowing("EMULATOROPTIONS"); - value = reader.ReadElementContentAsString(); - EmuOptions = (value == "") ? EmuOptions : value; - logger.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions); + reader.Read(); + bool InConfigSection = false; + while (!reader.EOF) + { + string value = ""; + switch (reader.Name.ToUpper()) + { + case "CONFIG": if (reader.IsStartElement()) { InConfigSection = true; } break; + case "PLATFORMNAMESHORT": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + PlatformNameShort = (value == "") ? PlatformNameShort : value; + log.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort); + } + break; + case "PLATFORMNAMELONG": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + PlatformNameLong = (value == "") ? PlatformNameLong : value; + log.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong); + } + break; + case "PLATFORMIMAGE": + if (reader.IsStartElement()) + { + string platform_image = reader.ReadElementContentAsString(); + PlatformImage = (platform_image == "") ? null : Image.FromFile(string.Format(@"{0}\{1}", EmuRomPath, platform_image)); + string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image; + log.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image); + } + break; + case "EXTENSIONS": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + Extenstions = (value == "") ? Extenstions : value; + log.WriteLine("\t\tEXTENSIONS={0}", Extenstions); + } + break; + case "EMULATORPATH": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + EmuPath = (value == "") ? EmuPath : value; + log.WriteLine("\t\tEMULATORPATH={0}", EmuPath); + } + break; + case "EMULATOROPTIONS": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + EmuOptions = (value == "") ? EmuOptions : value; + log.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions); + } + break; + #region External Non-Emulator Rom Support + case "GAMETITLE": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + string gametitle = (value == "") ? GameTitle : value; + GameTitle = gametitle; + log.WriteLine("\tGAMETITLE={0}", GameTitle); + } + break; + case "GAMEIMAGE": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + string gameimage = (value == "") ? GameImage : value; + GameImage = gameimage; + log.WriteLine("\tGAMEIMAGE={0}", GameImage); + } + break; + case "GAMEEXE": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + string gameexe = (value == "") ? GameExe : value; + GameExe = gameexe; + log.WriteLine("\tGAMEEXE={0}", GameExe); + } + break; + case "GAMEEXEARGS": + if (reader.IsStartElement()) + { + value = reader.ReadElementContentAsString(); + string gameexeargs = (value == "") ? GameExeArgs : value; + GameExeArgs = gameexeargs; + log.WriteLine("\tGAMEEXEARGS={0}", GameExeArgs); + } + break; + case "EXTERNALCONFIGSPATH": + if (reader.IsStartElement()) + { + string searchPattern = "*.config"; + string external_configs_path = reader.ReadElementContentAsString(); + //EmuOptions = (external_configs_path == "") ? EmuOptions : value; + log.WriteLine("\tEXTERNALCONFIGSPATH={0}", external_configs_path); + DirectoryInfo ext_path = new DirectoryInfo(external_configs_path); + string ext_rom_path = string.Format(@"{0}\{1}", EmuRomPath, external_configs_path); + // try the path in romroot + if (!ext_path.Exists) { ext_path = new DirectoryInfo(ext_rom_path); } + if (ext_path.Exists) + { + List externalconfigs = new List(); + List external_config_files = (searchPattern == "*.*") + ? new List(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly)) + : new List(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly).Where(s => searchPattern.Contains(Path.GetExtension(s).ToLower()))); + if (external_config_files.Count > 0) + external_config_files.Sort(); // sort the files (they should already be sorted alphabetically by GetFiles()) + foreach (string file in external_config_files) + { + IEmuConfig config = EmuConfigLoader.Load(log, file, EmuRomPath); + if (config != null) + externalconfigs.Add(config); + } + if (externalconfigs.Count > 0) + ExternalConfigs = externalconfigs; + } + } + break; + #endregion + default: + if (InConfigSection && (reader.Name != string.Empty) && reader.IsStartElement()) + log.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path); + break; + } + reader.Read(); + } } } - logger.WriteLine("\tLoaded Config: {0}", config_path); + log.WriteLine("\tLoaded Config: {0}", config_path); //loaded = true; } catch (Exception ex) { - logger.WriteLine("\tFailed to Load Config: {0}", config_path); + log.WriteLine("\tFailed to Load Config: {0}", config_path); Console.WriteLine(ex.ToString()); - logger.WriteLine("Error: {0}", ex.ToString()); + log.WriteLine("Error: {0}", ex.ToString()); //loaded = false; } } - else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); } + else { log.WriteLine("Could not find EMU Config File: {0}", config_path); } + + if (parent_rom != null) + { + if (parent_rom.HasExternalConfigs) + { + foreach (var rom in parent_rom.ExternalConfigs) + { + if (rom.ConfigPath == config_path) + { + this.PlatformNameShort = parent_rom.PlatformNameShort; + this.PlatformNameLong = parent_rom.PlatformNameLong; + break; + } + } + + } + } return this; } private const string Unknown_Platform = "Unknown Platform"; - public EmuConfig() : this("") { } - public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { } - public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { } - public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { } - public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { } - public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { } - public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions) + private logger log; + public EmuConfig() : this(null, "") { } + public EmuConfig(logger log) : this(log,"") { } + public EmuConfig(logger log, string PlatformNameShort) : this(log, PlatformNameShort, PlatformNameShort) { } + public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong) : this(log, PlatformNameShort, PlatformNameLong, "") { } + public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, "") { } + public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { } + public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { } + public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions) { + + + this.PlatformNameShort = PlatformNameShort; this.PlatformNameLong = PlatformNameLong; this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage); this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions; this.EmuPath = EmuPath; this.EmuOptions = EmuOptions; + this.ExternalConfigs = new List(); + this.GameTitle = ""; + this.GameImage = ""; + this.GameExe = ""; + this.GameExeArgs = ""; + this.log = log; } #region IEmuConfig members + public bool IsFavorites { get { return false; } } + public string ConfigPath { get; set; } private string _PlatformNameShort; public string PlatformNameShort { @@ -160,12 +349,17 @@ { try { - DirectoryInfo t = new DirectoryInfo(EmuRomPath); + //if (string.IsNullOrEmpty(EmuRomPath)) + //{ + // return "EmuRomPath null"; + //} + DirectoryInfo t = new DirectoryInfo(EmuRomPath); return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort; + } - catch(Exception ex) + catch (Exception ex) { - logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString()); + log.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString()); return Unknown_Platform; } } @@ -183,7 +377,7 @@ } catch (Exception ex) { - logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString()); + log.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString()); return Unknown_Platform; } } @@ -194,24 +388,43 @@ public string EmuPath { get; set; } public string EmuOptions { get; set; } public string EmuRomPath { get; set; } - #endregion - + public bool HasExternalConfigs { get { return ExternalConfigs.Count > 0; } } + public List ExternalConfigs { get; private set; } + + public string GameTitle { get; set; } + public string GameImage { get; set; } + public string GameExe { get; set; } + public string GameExeArgs { get; set; } + + public void ReleasePlatformImageResource() + { + if (this.PlatformImage != null) + this.PlatformImage.Dispose(); + } + #endregion + public override string ToString() { - return string.Format("{2} {0}{3}{1}","{","}", PlatformNameLong,PlatformNameShort); + return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort); } #region IComparable Members - public int CompareTo(object obj) + public int CompareTo(IEmuConfig obj) { - return this.PlatformNameLong.CompareTo((obj as EmuConfig).PlatformNameLong); + return this.PlatformNameLong.CompareTo(obj.PlatformNameLong); } - public void RefreshConfig() + public void RefreshConfig(logger log) { - logger.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG)); - this.Create(EmuRomPath); + log.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG)); + this.Create(log,EmuRomPath); } #endregion + + public void Dispose() + { + if (this.PlatformImage != null) + this.PlatformImage.Dispose(); + } } #endregion