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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 170 - (hide annotations) (download)
Mon Aug 4 05:01:23 2014 UTC (6 years, 3 months ago) by william
File size: 21038 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 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 126 void RefreshConfig(logger log);
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 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 105 private 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     this.PlatformNameShort = PlatformNameShort;
280     this.PlatformNameLong = PlatformNameLong;
281     this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
282     this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
283     this.EmuPath = EmuPath;
284     this.EmuOptions = EmuOptions;
285 william 85 this.ExternalConfigs = new List<IEmuConfig>();
286     this.GameTitle = "";
287     this.GameImage = "";
288     this.GameExe = "";
289     this.GameExeArgs = "";
290 william 112 this.log = log;
291 william 17 }
292     #region IEmuConfig members
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