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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 199 - (hide annotations) (download)
Mon Aug 4 11:35:06 2014 UTC (6 years, 2 months ago) by william
File size: 21584 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22