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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 200 - (hide annotations) (download)
Mon Aug 4 12:39:13 2014 UTC (6 years, 3 months ago) by william
File size: 23318 byte(s)
** fix issues with games, that have been favorited
** a favorited game, with an image, cannot be displayed more than once
** also, the platform name was not generated properly for external configs

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

  ViewVC Help
Powered by ViewVC 1.1.22