/[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/04 05:42:03	32
@@ -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,12 +18,15 @@
         string EmuPath { get; }
         string EmuOptions { get; }
         string EmuRomPath { get; set; }
+        string ToString();
     }
 
+   
 
     public static class EmuConfigLoader
     {
         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)
         {
@@ -35,15 +39,97 @@
             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
+            else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
+            
+            return config;
+        }
+        #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
+        {
+            #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("Could not find EMU Config File: {0}", config_path);
+                init_dict(config);
+                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;
             }
-            return config;
+            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
+        private class EmuConfig : IEmuConfig, IComparable
         {
             private const string Unknown_Platform = "Unknown Platform";
             public EmuConfig() : this("") { }
@@ -78,7 +164,7 @@
                         return Unknown_Platform;
                     }
                 }
-                private set { _PlatformNameShort = value; }
+                set { _PlatformNameShort = value; }
             }
             private string _PlatformNameLong;
             public string PlatformNameLong
@@ -96,14 +182,27 @@
                         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);
+            }
+
+            #endregion
         }
         #endregion
 

 

  ViewVC Help
Powered by ViewVC 1.1.22