/[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 03:10:52	24
+++ trunk/EmuXPortal/Api/EmuConfig.cs	2012/04/06 12:14:21	46
@@ -9,7 +9,7 @@
 
 namespace EmuXPortal.Api
 {
-    public interface IEmuConfig
+    public interface IEmuConfig : IComparable
     {
         string PlatformNameShort { get; }
         string PlatformNameLong { get; }
@@ -18,6 +18,9 @@
         string EmuPath { get; }
         string EmuOptions { get; }
         string EmuRomPath { get; set; }
+        string ToString();
+
+        void RefreshConfig();
     }
 
    
@@ -26,73 +29,8 @@
     {
         private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
         #region load
-        public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); }
-        public static IEmuConfig Load(string config_path, string rom_path)
-        {
-            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
-                {
-                    using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
-                    {
-                        using (XmlReader reader = XmlReader.Create(fs))
-                        {
-                            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);
-                        }
-                    }
-                    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 config;
-        }
+        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)
@@ -112,6 +50,7 @@
             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); } }
@@ -127,8 +66,76 @@
         #endregion
 
         #region private class EmuConfig : IEmuConfig
-        private class EmuConfig : IEmuConfig
+        private class EmuConfig : IEmuConfig, IComparable
         {
+            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)
+                {
+                    logger.WriteLine("Found EMU Config File: {0}", config_path);
+                    logger.WriteLine("\tLoading Config: {0}", config_path);
+                    //bool loaded = false;
+                    try
+                    {
+                        using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
+                        {
+                            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);
+                            }
+                        }
+                        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;
+            }
+
             private const string Unknown_Platform = "Unknown Platform";
             public EmuConfig() : this("") { }
             public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
@@ -188,6 +195,23 @@
             public string EmuOptions { get; set; }
             public string EmuRomPath { get; set; }
             #endregion      
+        
+            public override string ToString()
+            {
+                return string.Format("{2} {0}{3}{1}","{","}", PlatformNameLong,PlatformNameShort);
+            }
+            #region IComparable Members
+
+            public int CompareTo(object obj)
+            {
+                return this.PlatformNameLong.CompareTo((obj as EmuConfig).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
         }
         #endregion
 

 

  ViewVC Help
Powered by ViewVC 1.1.22