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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (hide annotations) (download)
Thu Aug 23 02:44:26 2012 UTC (8 years, 3 months ago) by william
File size: 20269 byte(s)
PERFORMANCE OPTIMIZATION:
** only get defaultimage resource once (don't duplicate the resource)

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 william 105 public interface IEmuConfig : IComparable<IEmuConfig>, IDisposable
13 william 17 {
14 william 85 string ConfigPath { get; }
15 william 17 string PlatformNameShort { get; }
16     string PlatformNameLong { get; }
17     Image PlatformImage { get; }
18     string Extenstions { get; }
19     string EmuPath { get; }
20     string EmuOptions { get; }
21     string EmuRomPath { get; set; }
22 william 32 string ToString();
23 william 46
24 william 85 bool HasExternalConfigs { get; }
25     List<IEmuConfig> ExternalConfigs { get; }
26    
27    
28     string GameTitle { get; }
29     string GameImage { get; }
30     string GameExe { get; }
31     string GameExeArgs { get; }
32    
33    
34 william 46 void RefreshConfig();
35 william 110 void ReleasePlatformImageResource();
36 william 17 }
37    
38    
39 william 85
40 william 17 public static class EmuConfigLoader
41     {
42 william 85 public static readonly IEmuConfig Empty = new EmuConfig();
43 william 17 private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
44 william 23 #region load
45 william 46 public static IEmuConfig Load(string rom_path) { return new EmuConfig().Create(rom_path); }
46     public static IEmuConfig Load(string config_path, string rom_path) { return new EmuConfig().Create(config_path, rom_path); }
47 william 23 #endregion
48     #region parse emu options
49     public static string GetEMUOptions(IRomConfig config)
50     {
51     EMUOptions EMUOptions = new EMUOptions(config);
52     return EMUOptions.Options;
53     }
54     #endregion
55     #region private class EMUOptions
56     private class EMUOptions
57     {
58 william 24 #region Replaceable Constant Options
59     private const string ROM_FILE = "%ROM_FILE%";
60     private const string ROM_PATH = "%ROM_PATH%";
61     #endregion
62 william 23 private Dictionary<string, string> options_dict = new Dictionary<string, string>();
63     public EMUOptions(IRomConfig config)
64     {
65     init_dict(config);
66 william 46 config.Config.RefreshConfig();
67 william 23 string options = config.Config.EmuOptions;
68     string real_options = options;
69     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); } }
70     Options = real_options;
71     }
72     private void init_dict(IRomConfig config)
73     {
74 william 24 options_dict.Add(ROM_FILE, config.RomFile);
75     options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
76 william 23 }
77     public string Options { get; set; }
78     }
79     #endregion
80    
81 william 17 #region private class EmuConfig : IEmuConfig
82 william 105 private class EmuConfig : IEmuConfig, IComparable<IEmuConfig>, IDisposable
83 william 17 {
84 william 46 public IEmuConfig Create(string rom_path) { return this.Create(string.Empty, rom_path); }
85     public IEmuConfig Create(string config_path, string rom_path)
86     {
87     EmuRomPath = rom_path;
88     if (config_path == "") { config_path = string.Format(@"{0}\{1}", rom_path, EMU_CONFIG); }
89    
90     // read the actual config emu.config
91     FileInfo fi = new FileInfo(config_path);
92     if (fi.Exists)
93     {
94     logger.WriteLine("Found EMU Config File: {0}", config_path);
95     logger.WriteLine("\tLoading Config: {0}", config_path);
96 william 85 this.ConfigPath = config_path;
97 william 46 //bool loaded = false;
98     try
99     {
100     using (FileStream fs = new FileStream(config_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
101     {
102     using (XmlReader reader = XmlReader.Create(fs))
103     {
104 william 85 reader.Read();
105     bool InConfigSection = false;
106     while (!reader.EOF)
107     {
108     string value = "";
109     switch (reader.Name.ToUpper())
110     {
111     case "CONFIG": if (reader.IsStartElement()) { InConfigSection = true; } break;
112     case "PLATFORMNAMESHORT":
113     if (reader.IsStartElement())
114     {
115     value = reader.ReadElementContentAsString();
116     PlatformNameShort = (value == "") ? PlatformNameShort : value;
117     logger.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort);
118     }
119     break;
120     case "PLATFORMNAMELONG":
121     if (reader.IsStartElement())
122     {
123     value = reader.ReadElementContentAsString();
124     PlatformNameLong = (value == "") ? PlatformNameLong : value;
125     logger.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong);
126     }
127     break;
128     case "PLATFORMIMAGE":
129     if (reader.IsStartElement())
130     {
131     string platform_image = reader.ReadElementContentAsString();
132 william 110 PlatformImage = (platform_image == "") ? null : Image.FromFile(string.Format(@"{0}\{1}", rom_path, platform_image));
133 william 85 string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
134     logger.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
135     }
136     break;
137     case "EXTENSIONS":
138     if (reader.IsStartElement())
139     {
140     value = reader.ReadElementContentAsString();
141     Extenstions = (value == "") ? Extenstions : value;
142     logger.WriteLine("\t\tEXTENSIONS={0}", Extenstions);
143     }
144     break;
145     case "EMULATORPATH":
146     if (reader.IsStartElement())
147     {
148     value = reader.ReadElementContentAsString();
149     EmuPath = (value == "") ? EmuPath : value;
150     logger.WriteLine("\t\tEMULATORPATH={0}", EmuPath);
151     }
152     break;
153     case "EMULATOROPTIONS":
154     if (reader.IsStartElement())
155     {
156     value = reader.ReadElementContentAsString();
157     EmuOptions = (value == "") ? EmuOptions : value;
158     logger.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions);
159     }
160     break;
161     #region External Non-Emulator Rom Support
162     case "GAMETITLE":
163     if (reader.IsStartElement())
164     {
165     value = reader.ReadElementContentAsString();
166     string gametitle = (value == "") ? GameTitle : value;
167     GameTitle = gametitle;
168     logger.WriteLine("\tGAMETITLE={0}", GameTitle);
169     }
170     break;
171     case "GAMEIMAGE":
172     if (reader.IsStartElement())
173     {
174     value = reader.ReadElementContentAsString();
175     string gameimage = (value == "") ? GameImage : value;
176     GameImage = gameimage;
177     logger.WriteLine("\tGAMEIMAGE={0}", GameImage);
178     }
179     break;
180     case "GAMEEXE":
181     if (reader.IsStartElement())
182     {
183     value = reader.ReadElementContentAsString();
184     string gameexe = (value == "") ? GameExe : value;
185     GameExe = gameexe;
186     logger.WriteLine("\tGAMEEXE={0}", GameExe);
187     }
188     break;
189     case "GAMEEXEARGS":
190     if (reader.IsStartElement())
191     {
192     value = reader.ReadElementContentAsString();
193     string gameexeargs = (value == "") ? GameExeArgs : value;
194     GameExeArgs = gameexeargs;
195     logger.WriteLine("\tGAMEEXEARGS={0}", GameExeArgs);
196     }
197     break;
198     case "EXTERNALCONFIGSPATH":
199     if (reader.IsStartElement())
200     {
201     string searchPattern = "*.config";
202     string external_configs_path = reader.ReadElementContentAsString();
203     //EmuOptions = (external_configs_path == "") ? EmuOptions : value;
204     logger.WriteLine("\tEXTERNALCONFIGSPATH={0}", external_configs_path);
205     DirectoryInfo ext_path = new DirectoryInfo(external_configs_path);
206     string ext_rom_path = string.Format(@"{0}\{1}", rom_path, external_configs_path);
207     // try the path in romroot
208     if (!ext_path.Exists) { ext_path = new DirectoryInfo(ext_rom_path); }
209     if (ext_path.Exists)
210     {
211     List<IEmuConfig> externalconfigs = new List<IEmuConfig>();
212     List<string> external_config_files = (searchPattern == "*.*")
213     ? new List<string>(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly))
214     : new List<string>(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly).Where(s => searchPattern.Contains(Path.GetExtension(s).ToLower())));
215     if (external_config_files.Count > 0)
216     external_config_files.Sort(); // sort the files (they should already be sorted alphabetically by GetFiles())
217     foreach (string file in external_config_files)
218     {
219     IEmuConfig config = EmuConfigLoader.Load(file, rom_path);
220     if (config != null)
221     externalconfigs.Add(config);
222     }
223     if (externalconfigs.Count > 0)
224     ExternalConfigs = externalconfigs;
225     }
226     }
227     break;
228     #endregion
229     default:
230     if (InConfigSection && (reader.Name != string.Empty) && reader.IsStartElement())
231     logger.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path);
232     break;
233     }
234     reader.Read();
235     }
236 william 46 }
237     }
238     logger.WriteLine("\tLoaded Config: {0}", config_path);
239     //loaded = true;
240     }
241     catch (Exception ex)
242     {
243     logger.WriteLine("\tFailed to Load Config: {0}", config_path);
244     Console.WriteLine(ex.ToString());
245     logger.WriteLine("Error: {0}", ex.ToString());
246     //loaded = false;
247     }
248     }
249     else { logger.WriteLine("Could not find EMU Config File: {0}", config_path); }
250    
251     return this;
252     }
253    
254 william 17 private const string Unknown_Platform = "Unknown Platform";
255     public EmuConfig() : this("") { }
256     public EmuConfig(string PlatformNameShort) : this(PlatformNameShort, PlatformNameShort) { }
257     public EmuConfig(string PlatformNameShort, string PlatformNameLong) : this(PlatformNameShort, PlatformNameLong, "") { }
258     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
259     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
260     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
261     public EmuConfig(string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
262     {
263     this.PlatformNameShort = PlatformNameShort;
264     this.PlatformNameLong = PlatformNameLong;
265     this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
266     this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
267     this.EmuPath = EmuPath;
268     this.EmuOptions = EmuOptions;
269 william 85 this.ExternalConfigs = new List<IEmuConfig>();
270     this.GameTitle = "";
271     this.GameImage = "";
272     this.GameExe = "";
273     this.GameExeArgs = "";
274 william 17 }
275     #region IEmuConfig members
276 william 85 public string ConfigPath { get; set; }
277 william 17 private string _PlatformNameShort;
278     public string PlatformNameShort
279     {
280     get
281     {
282     try
283     {
284     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
285     return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
286     }
287 william 85 catch (Exception ex)
288 william 17 {
289 william 85 logger.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString());
290 william 17 return Unknown_Platform;
291     }
292     }
293 william 20 set { _PlatformNameShort = value; }
294 william 17 }
295     private string _PlatformNameLong;
296     public string PlatformNameLong
297     {
298     get
299     {
300     try
301     {
302     DirectoryInfo t = new DirectoryInfo(EmuRomPath);
303     return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
304     }
305     catch (Exception ex)
306     {
307     logger.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
308     return Unknown_Platform;
309     }
310     }
311 william 20 set { _PlatformNameLong = value; }
312 william 17 }
313 william 20 public Image PlatformImage { get; set; }
314     public string Extenstions { get; set; }
315     public string EmuPath { get; set; }
316     public string EmuOptions { get; set; }
317 william 17 public string EmuRomPath { get; set; }
318 william 85 public bool HasExternalConfigs { get { return ExternalConfigs.Count > 0; } }
319     public List<IEmuConfig> ExternalConfigs { get; private set; }
320    
321     public string GameTitle { get; set; }
322     public string GameImage { get; set; }
323     public string GameExe { get; set; }
324     public string GameExeArgs { get; set; }
325 william 110
326     public void ReleasePlatformImageResource()
327     {
328     if (this.PlatformImage != null)
329     this.PlatformImage.Dispose();
330     }
331 william 85 #endregion
332    
333 william 32 public override string ToString()
334     {
335 william 85 return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
336 william 32 }
337 william 31 #region IComparable Members
338    
339 william 104 public int CompareTo(IEmuConfig obj)
340 william 31 {
341 william 104 return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
342 william 31 }
343 william 46 public void RefreshConfig()
344     {
345     logger.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
346     this.Create(EmuRomPath);
347     }
348 william 31 #endregion
349 william 105
350     public void Dispose()
351     {
352     if (this.PlatformImage != null)
353     this.PlatformImage.Dispose();
354     }
355 william 17 }
356     #endregion
357    
358     }
359     }

  ViewVC Help
Powered by ViewVC 1.1.22