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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations) (download)
Wed Apr 4 03:09:19 2012 UTC (8 years, 6 months ago) by william
File size: 9551 byte(s)
+ add support to replace emu placeholder options with the real options

1 william 17 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Drawing;
6     using System.IO;
7     using EmuXPortal.Logging;
8 william 20 using System.Xml;
9 william 17
10     namespace EmuXPortal.Api
11     {
12     public interface IEmuConfig
13     {
14     string PlatformNameShort { get; }
15     string PlatformNameLong { get; }
16     Image PlatformImage { get; }
17     string Extenstions { get; }
18     string EmuPath { get; }
19     string EmuOptions { get; }
20     string EmuRomPath { get; set; }
21     }
22    
23 william 23
24 william 17
25     public static class EmuConfigLoader
26     {
27     private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
28 william 23 #region load
29 william 17 public static IEmuConfig Load(string rom_path) { return Load(string.Empty, rom_path); }
30     public static IEmuConfig Load(string config_path, string rom_path)
31     {
32     EmuConfig config = new EmuConfig();
33     config.EmuRomPath = rom_path;
34     if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
35    
36     // read the actual config emu.config
37     FileInfo fi = new FileInfo(config_path);
38     if (fi.Exists)
39     {
40     logger.WriteLine("Found EMU Config File: {0}", config_path);
41 william 20 logger.WriteLine("\tLoading Config: {0}", config_path);
42     //bool loaded = false;
43     try
44     {
45     using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
46     {
47     using (XmlReader reader = XmlReader.Create(fs))
48     {
49 william 22 string value = "";
50 william 20 reader.ReadToFollowing("PLATFORMNAMESHORT");
51 william 22 value = reader.ReadElementContentAsString();
52     config.PlatformNameShort = (value == "") ? config.PlatformNameShort: value;
53 william 21 logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", config.PlatformNameShort);
54 william 20
55     reader.ReadToFollowing("PLATFORMNAMELONG");
56 william 22 value = reader.ReadElementContentAsString();
57     config.PlatformNameLong = (value == "") ? config.PlatformNameLong : value;
58 william 21 logger.WriteLine("\t\tPLATFORMNAMELONG={0}", config.PlatformNameLong);
59 william 20
60     reader.ReadToFollowing("PLATFORMIMAGE");
61 william 21 string platform_image = reader.ReadElementContentAsString();
62 william 20 config.PlatformImage = (platform_image == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(string.Format(@"{0}\{1}",rom_path,platform_image));
63     string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
64 william 21 logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
65 william 20
66     reader.ReadToFollowing("EXTENSIONS");
67 william 22 value = reader.ReadElementContentAsString();
68     config.Extenstions = (value == "") ? config.Extenstions : value;
69 william 21 logger.WriteLine("\t\tEXTENSIONS={0}", config.Extenstions);
70 william 20
71     reader.ReadToFollowing("EMULATORPATH");
72 william 22 value = reader.ReadElementContentAsString();
73     config.EmuPath = (value == "") ? config.EmuPath : value;
74 william 21 logger.WriteLine("\t\tEMULATORPATH={0}", config.EmuPath);
75 william 20
76     reader.ReadToFollowing("EMULATOROPTIONS");
77 william 22 value = reader.ReadElementContentAsString();
78     config.EmuOptions = (value == "") ? config.EmuOptions : value;
79     logger.WriteLine("\tEMULATOROPTIONS={0}", config.EmuOptions);
80 william 20 }
81     }
82     logger.WriteLine("\tLoaded Config: {0}", config_path);
83     //loaded = true;
84     }
85     catch(Exception ex)
86     {
87     logger.WriteLine("\tFailed to Load Config: {0}", config_path);
88     Console.WriteLine(ex.ToString());
89     logger.WriteLine("Error: {0}", ex.ToString());
90     //loaded = false;
91     }
92 william 17 }
93 william 20 else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
94 william 17 return config;
95     }
96 william 23 #endregion
97     #region parse emu options
98     public static string GetEMUOptions(IRomConfig config)
99     {
100     EMUOptions EMUOptions = new EMUOptions(config);
101     return EMUOptions.Options;
102     }
103     #endregion
104     #region private class EMUOptions
105     private class EMUOptions
106     {
107     private Dictionary<string, string> options_dict = new Dictionary<string, string>();
108     public EMUOptions(IRomConfig config)
109     {
110     init_dict(config);
111     string options = config.Config.EmuOptions;
112     string real_options = options;
113     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); } }
114     Options = real_options;
115     }
116     private void init_dict(IRomConfig config)
117     {
118     options_dict.Add("%ROM_FILE%", config.RomFile);
119     options_dict.Add("%ROM_PATH%", config.Config.EmuRomPath);
120     }
121     public const string ROM_FILE = "%ROM_FILE%";
122     public string Options { get; set; }
123     }
124     #endregion
125    
126 william 17 #region private class EmuConfig : IEmuConfig
127     private class EmuConfig : IEmuConfig
128     {
129     private const string Unknown_Platform = "Unknown Platform";
130     public EmuConfig() : this("") { }
131     public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
132     public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
133     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
134     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
135     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
136     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
137     {
138     this.PlatformNameShort = PlatformNameShort;
139     this.PlatformNameLong = PlatformNameLong;
140     this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
141     this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
142     this.EmuPath = EmuPath;
143     this.EmuOptions = EmuOptions;
144     }
145     #region IEmuConfig members
146     private string _PlatformNameShort;
147     public string PlatformNameShort
148     {
149     get
150     {
151     try
152     {
153     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
154     return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
155     }
156     catch(Exception ex)
157     {
158     logger.WriteLine("PlatformNameShort.get() Error: {0}",ex.ToString());
159     return Unknown_Platform;
160     }
161     }
162 william 20 set { _PlatformNameShort = value; }
163 william 17 }
164     private string _PlatformNameLong;
165     public string PlatformNameLong
166     {
167     get
168     {
169     try
170     {
171     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
172     return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
173     }
174     catch (Exception ex)
175     {
176     logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
177     return Unknown_Platform;
178     }
179     }
180 william 20 set { _PlatformNameLong = value; }
181 william 17 }
182 william 20 public Image PlatformImage { get; set; }
183     public string Extenstions { get; set; }
184     public string EmuPath { get; set; }
185     public string EmuOptions { get; set; }
186 william 17 public string EmuRomPath { get; set; }
187     #endregion
188     }
189     #endregion
190    
191     }
192     }

  ViewVC Help
Powered by ViewVC 1.1.22