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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22