/[EmuXPortal]/trunk/EmuXPortal/Api/EmuConfig.cs
ViewVC logotype

Diff of /trunk/EmuXPortal/Api/EmuConfig.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/EmuXPortal/Api/EmuConfig.cs	2012/04/04 02:39:20	22
+++ trunk/EmuXPortal/Api/EmuConfig.cs	2012/08/22 02:36:47	105
@@ -9,8 +9,9 @@
 
 namespace EmuXPortal.Api
 {
-    public interface IEmuConfig
+    public interface IEmuConfig : IComparable<IEmuConfig>, IDisposable
     {
+        string ConfigPath { get; }
         string PlatformNameShort { get; }
         string PlatformNameLong { get; }
         Image PlatformImage { get; }
@@ -18,82 +19,237 @@
         string EmuPath { get; }
         string EmuOptions { get; }
         string EmuRomPath { get; set; }
+        string ToString();
+
+        bool HasExternalConfigs { get; }
+        List<IEmuConfig> ExternalConfigs { get; }
+
+
+        string GameTitle { get; }
+        string GameImage { get; }
+        string GameExe { get; }
+        string GameExeArgs { get; }
+
+
+        void RefreshConfig();
     }
 
 
+
     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
-        public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); }
-        public static IEmuConfig Load(string config_path, string rom_path)
+        #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); }
+        #endregion
+        #region parse emu options
+        public static string GetEMUOptions(IRomConfig config)
+        {
+            EMUOptions EMUOptions = new EMUOptions(config);
+            return EMUOptions.Options;
+        }
+        #endregion
+        #region private class EMUOptions
+        private class EMUOptions
         {
-            EmuConfig config = new EmuConfig();
-            config.EmuRomPath = rom_path;
-            if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
-
-            // 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);
-                //bool loaded = false;
-                try
+            #region Replaceable Constant Options
+            private const string ROM_FILE = "%ROM_FILE%";
+            private const string ROM_PATH = "%ROM_PATH%";
+            #endregion
+            private Dictionary<string, string> options_dict = new Dictionary<string, string>();
+            public EMUOptions(IRomConfig config)
+            {
+                init_dict(config);
+                config.Config.RefreshConfig();
+                string options = config.Config.EmuOptions;
+                string real_options = options;
+                foreach (KeyValuePair<string, string> pair in options_dict) { if (options.ToLower().Contains(pair.Key.ToLower())) { real_options = real_options.ToLower().Replace(pair.Key.ToLower(), pair.Value); } }
+                Options = real_options;
+            }
+            private void init_dict(IRomConfig config)
+            {
+                options_dict.Add(ROM_FILE, config.RomFile);
+                options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
+            }
+            public string Options { get; set; }
+        }
+        #endregion
+
+        #region private class EmuConfig : IEmuConfig
+        private class EmuConfig : IEmuConfig, IComparable<IEmuConfig>, IDisposable
+        {
+            public IEmuConfig Create(string rom_path) { return this.Create(string.Empty, rom_path); }
+            public IEmuConfig Create(string config_path, string rom_path)
+            {
+                EmuRomPath = rom_path;
+                if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
+
+                // read the actual config emu.config
+                FileInfo fi = new FileInfo(config_path);
+                if (fi.Exists)
                 {
-                    using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
+                    logger.WriteLine("Found EMU Config File: {0}", config_path);
+                    logger.WriteLine("\tLoading Config: {0}", config_path);
+                    this.ConfigPath = config_path;
+                    //bool loaded = false;
+                    try
                     {
-                        using (XmlReader reader = XmlReader.Create(fs))
+                        using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                         {
-                            string value = "";
-                            reader.ReadToFollowing("PLATFORMNAMESHORT");
-                            value = reader.ReadElementContentAsString();
-                            config.PlatformNameShort = (value == "") ? config.PlatformNameShort: value;
-                            logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", config.PlatformNameShort);
-
-                            reader.ReadToFollowing("PLATFORMNAMELONG");
-                            value = reader.ReadElementContentAsString();
-                            config.PlatformNameLong = (value == "") ? config.PlatformNameLong : value;
-                            logger.WriteLine("\t\tPLATFORMNAMELONG={0}", config.PlatformNameLong);
-
-                            reader.ReadToFollowing("PLATFORMIMAGE");
-                            string platform_image = reader.ReadElementContentAsString();                            
-                            config.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();
-                            config.Extenstions = (value == "") ? config.Extenstions : value;
-                            logger.WriteLine("\t\tEXTENSIONS={0}", config.Extenstions);
-
-                            reader.ReadToFollowing("EMULATORPATH");
-                            value = reader.ReadElementContentAsString();
-                            config.EmuPath = (value == "") ? config.EmuPath : value;
-                            logger.WriteLine("\t\tEMULATORPATH={0}", config.EmuPath);
-
-                            reader.ReadToFollowing("EMULATOROPTIONS");
-                            value = reader.ReadElementContentAsString();
-                            config.EmuOptions = (value == "") ? config.EmuOptions : value;
-                            logger.WriteLine("\tEMULATOROPTIONS={0}", config.EmuOptions);
+                            using (XmlReader reader = XmlReader.Create(fs))
+                            {
+                                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;
+                                                logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort);
+                                            }
+                                            break;
+                                        case "PLATFORMNAMELONG":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                PlatformNameLong = (value == "") ? PlatformNameLong : value;
+                                                logger.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong);
+                                            }
+                                            break;
+                                        case "PLATFORMIMAGE":
+                                            if (reader.IsStartElement())
+                                            {
+                                                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);
+                                            }
+                                            break;
+                                        case "EXTENSIONS":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                Extenstions = (value == "") ? Extenstions : value;
+                                                logger.WriteLine("\t\tEXTENSIONS={0}", Extenstions);
+                                            }
+                                            break;
+                                        case "EMULATORPATH":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                EmuPath = (value == "") ? EmuPath : value;
+                                                logger.WriteLine("\t\tEMULATORPATH={0}", EmuPath);
+                                            }
+                                            break;
+                                        case "EMULATOROPTIONS":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                EmuOptions = (value == "") ? EmuOptions : value;
+                                                logger.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;
+                                                logger.WriteLine("\tGAMETITLE={0}", GameTitle);
+                                            }
+                                            break;
+                                        case "GAMEIMAGE":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                string gameimage = (value == "") ? GameImage : value;
+                                                GameImage = gameimage;
+                                                logger.WriteLine("\tGAMEIMAGE={0}", GameImage);
+                                            }
+                                            break;
+                                        case "GAMEEXE":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                string gameexe = (value == "") ? GameExe : value;
+                                                GameExe = gameexe;
+                                                logger.WriteLine("\tGAMEEXE={0}", GameExe);
+                                            }
+                                            break;
+                                        case "GAMEEXEARGS":
+                                            if (reader.IsStartElement())
+                                            {
+                                                value = reader.ReadElementContentAsString();
+                                                string gameexeargs = (value == "") ? GameExeArgs : value;
+                                                GameExeArgs = gameexeargs;
+                                                logger.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;
+                                                logger.WriteLine("\tEXTERNALCONFIGSPATH={0}", external_configs_path);
+                                                DirectoryInfo ext_path = new DirectoryInfo(external_configs_path);
+                                                string ext_rom_path = string.Format(@"{0}\{1}", rom_path, external_configs_path);
+                                                // try the path in romroot
+                                                if (!ext_path.Exists) { ext_path = new DirectoryInfo(ext_rom_path); }
+                                                if (ext_path.Exists)
+                                                {
+                                                    List<IEmuConfig> externalconfigs = new List<IEmuConfig>();
+                                                    List<string> external_config_files = (searchPattern == "*.*")
+                                                         ? new List<string>(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly))
+                                                         : new List<string>(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(file, rom_path);
+                                                        if (config != null)
+                                                            externalconfigs.Add(config);
+                                                    }
+                                                    if (externalconfigs.Count > 0)
+                                                        ExternalConfigs = externalconfigs;
+                                                }
+                                            }
+                                            break;
+                                        #endregion
+                                        default:
+                                            if (InConfigSection && (reader.Name != string.Empty) && reader.IsStartElement())
+                                                logger.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path);
+                                            break;
+                                    }
+                                    reader.Read();
+                                }
+                            }
                         }
+                        logger.WriteLine("\tLoaded Config: {0}", config_path);
+                        //loaded = true;
+                    }
+                    catch (Exception ex)
+                    {
+                        logger.WriteLine("\tFailed to Load Config: {0}", config_path);
+                        Console.WriteLine(ex.ToString());
+                        logger.WriteLine("Error: {0}", ex.ToString());
+                        //loaded = false;
                     }
-                    logger.WriteLine("\tLoaded Config: {0}", config_path);
-                    //loaded = true;
-                }
-                catch(Exception ex)
-                {
-                    logger.WriteLine("\tFailed to Load Config: {0}", config_path);
-                    Console.WriteLine(ex.ToString());
-                    logger.WriteLine("Error: {0}", ex.ToString());
-                    //loaded = false;
                 }
+                else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
+
+                return this;
             }
-            else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
-            return config;
-        }
-        #region private class EmuConfig : IEmuConfig
-        private class EmuConfig : IEmuConfig
-        {
+
             private const string Unknown_Platform = "Unknown Platform";
             public EmuConfig() : this("") { }
             public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
@@ -109,8 +265,14 @@
                 this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
                 this.EmuPath = EmuPath;
                 this.EmuOptions = EmuOptions;
+                this.ExternalConfigs = new List<IEmuConfig>();
+                this.GameTitle = "";
+                this.GameImage = "";
+                this.GameExe = "";
+                this.GameExeArgs = "";
             }
             #region IEmuConfig members
+            public string ConfigPath { get; set; }
             private string _PlatformNameShort;
             public string PlatformNameShort
             {
@@ -121,9 +283,9 @@
                         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());
+                        logger.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString());
                         return Unknown_Platform;
                     }
                 }
@@ -152,7 +314,37 @@
             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<IEmuConfig> ExternalConfigs { get; private set; }
+
+            public string GameTitle { get; set; }
+            public string GameImage { get; set; }
+            public string GameExe { get; set; }
+            public string GameExeArgs { get; set; }
+            #endregion
+
+            public override string ToString()
+            {
+                return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
+            }
+            #region IComparable Members
+
+            public int CompareTo(IEmuConfig obj)
+            {
+                return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
+            }
+            public void RefreshConfig()
+            {
+                logger.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
+                this.Create(EmuRomPath);
+            }
+            #endregion
+
+            public void Dispose()
+            {
+                if (this.PlatformImage != null)
+                    this.PlatformImage.Dispose();
+            }
         }
         #endregion
 

 

  ViewVC Help
Powered by ViewVC 1.1.22