/[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 01:32:37	17
+++ trunk/EmuXPortal/Api/EmuConfig.cs	2012/04/06 12:14:21	46
@@ -5,10 +5,11 @@
 using System.Drawing;
 using System.IO;
 using EmuXPortal.Logging;
+using System.Xml;
 
 namespace EmuXPortal.Api
 {
-    public interface IEmuConfig
+    public interface IEmuConfig : IComparable
     {
         string PlatformNameShort { get; }
         string PlatformNameLong { get; }
@@ -17,34 +18,124 @@
         string EmuPath { get; }
         string EmuOptions { get; }
         string EmuRomPath { get; set; }
+        string ToString();
+
+        void RefreshConfig();
     }
 
+   
 
     public static class EmuConfigLoader
     {
         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)
         {
-            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)
+            EMUOptions EMUOptions = new EMUOptions(config);
+            return EMUOptions.Options;
+        }
+        #endregion
+        #region private class EMUOptions
+        private class EMUOptions
+        {
+            #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)
             {
-                logger.WriteLine("Found EMU Config File: {0}", config_path);
+                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;
             }
-            else
+            private void init_dict(IRomConfig config)
             {
-                logger.WriteLine("Could not find EMU Config File: {0}", config_path);
+                options_dict.Add(ROM_FILE, config.RomFile);
+                options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
             }
-            return config;
+            public string Options { get; set; }
         }
+        #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) { }
@@ -78,7 +169,7 @@
                         return Unknown_Platform;
                     }
                 }
-                private set { _PlatformNameShort = value; }
+                set { _PlatformNameShort = value; }
             }
             private string _PlatformNameLong;
             public string PlatformNameLong
@@ -96,14 +187,31 @@
                         return Unknown_Platform;
                     }
                 }
-                private set { _PlatformNameLong = value; }
+                set { _PlatformNameLong = value; }
             }
-            public Image PlatformImage { get; private set; }
-            public string Extenstions { get; private set; }
-            public string EmuPath { get; private set; }
-            public string EmuOptions { get; private set; }
+            public Image PlatformImage { get; set; }
+            public string Extenstions { get; set; }
+            public string EmuPath { get; set; }
+            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