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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 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 using System.Xml;
9 using System.Diagnostics;
10
11 namespace EmuXPortal.Api
12 {
13 public interface IEmuConfig : IComparable<IEmuConfig>, IDisposable
14 {
15 bool IsFavorites { get; }
16 string ConfigPath { get; }
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 string ToString();
25
26 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 void RefreshConfig(logger log);
37 void ReleasePlatformImageResource();
38 }
39
40 public static class EmuConfigLoader
41 {
42 public static readonly IEmuConfig Empty = new EmuConfig();
43 private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
44 #region load
45 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 #endregion
48 #region parse emu options
49 public static string GetEMUOptions(logger log, IRomConfig config)
50 {
51 EMUOptions EMUOptions = new EMUOptions(log, config);
52 return EMUOptions.Options;
53 }
54 #endregion
55 #region private class EMUOptions
56 private class EMUOptions
57 {
58 #region Replaceable Constant Options
59 private const string ROM_FILE = "%ROM_FILE%";
60 private const string ROM_PATH = "%ROM_PATH%";
61 #endregion
62 private Dictionary<string, string> options_dict = new Dictionary<string, string>();
63 public EMUOptions(logger log, IRomConfig config)
64 {
65 init_dict(config);
66 config.Config.RefreshConfig(log);
67 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 options_dict.Add(ROM_FILE, config.RomFile);
75 options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
76 }
77 public string Options { get; set; }
78 }
79 #endregion
80
81 #region private class EmuConfig : IEmuConfig
82 internal class EmuConfig : IEmuConfig, IComparable<IEmuConfig>, IDisposable
83 {
84 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 {
87 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 // read the actual config emu.config
105 FileInfo fi = new FileInfo(config_path);
106 if (fi.Exists)
107 {
108 log.WriteLine("Found EMU Config File: {0}", config_path);
109 log.WriteLine("\tLoading Config: {0}", config_path);
110 this.ConfigPath = config_path;
111 //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 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 log.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort);
132 }
133 break;
134 case "PLATFORMNAMELONG":
135 if (reader.IsStartElement())
136 {
137 value = reader.ReadElementContentAsString();
138 PlatformNameLong = (value == "") ? PlatformNameLong : value;
139 log.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong);
140 }
141 break;
142 case "PLATFORMIMAGE":
143 if (reader.IsStartElement())
144 {
145 string platform_image = reader.ReadElementContentAsString();
146 PlatformImage = (platform_image == "") ? null : Image.FromFile(string.Format(@"{0}\{1}", EmuRomPath, platform_image));
147 string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
148 log.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
149 }
150 break;
151 case "EXTENSIONS":
152 if (reader.IsStartElement())
153 {
154 value = reader.ReadElementContentAsString();
155 Extenstions = (value == "") ? Extenstions : value;
156 log.WriteLine("\t\tEXTENSIONS={0}", Extenstions);
157 }
158 break;
159 case "EMULATORPATH":
160 if (reader.IsStartElement())
161 {
162 value = reader.ReadElementContentAsString();
163 EmuPath = (value == "") ? EmuPath : value;
164 log.WriteLine("\t\tEMULATORPATH={0}", EmuPath);
165 }
166 break;
167 case "EMULATOROPTIONS":
168 if (reader.IsStartElement())
169 {
170 value = reader.ReadElementContentAsString();
171 EmuOptions = (value == "") ? EmuOptions : value;
172 log.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions);
173 }
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 log.WriteLine("\tGAMETITLE={0}", GameTitle);
183 }
184 break;
185 case "GAMEIMAGE":
186 if (reader.IsStartElement())
187 {
188 value = reader.ReadElementContentAsString();
189 string gameimage = (value == "") ? GameImage : value;
190 GameImage = gameimage;
191 log.WriteLine("\tGAMEIMAGE={0}", GameImage);
192 }
193 break;
194 case "GAMEEXE":
195 if (reader.IsStartElement())
196 {
197 value = reader.ReadElementContentAsString();
198 string gameexe = (value == "") ? GameExe : value;
199 GameExe = gameexe;
200 log.WriteLine("\tGAMEEXE={0}", GameExe);
201 }
202 break;
203 case "GAMEEXEARGS":
204 if (reader.IsStartElement())
205 {
206 value = reader.ReadElementContentAsString();
207 string gameexeargs = (value == "") ? GameExeArgs : value;
208 GameExeArgs = gameexeargs;
209 log.WriteLine("\tGAMEEXEARGS={0}", GameExeArgs);
210 }
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 log.WriteLine("\tEXTERNALCONFIGSPATH={0}", external_configs_path);
219 DirectoryInfo ext_path = new DirectoryInfo(external_configs_path);
220 string ext_rom_path = string.Format(@"{0}\{1}", EmuRomPath, external_configs_path);
221 // 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 IEmuConfig config = EmuConfigLoader.Load(log, file, EmuRomPath);
234 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 log.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path);
246 break;
247 }
248 reader.Read();
249 }
250 }
251 }
252 log.WriteLine("\tLoaded Config: {0}", config_path);
253 //loaded = true;
254 }
255 catch (Exception ex)
256 {
257 log.WriteLine("\tFailed to Load Config: {0}", config_path);
258 Console.WriteLine(ex.ToString());
259 log.WriteLine("Error: {0}", ex.ToString());
260 //loaded = false;
261 }
262 }
263 else { log.WriteLine("Could not find EMU Config File: {0}", config_path); }
264
265 return this;
266 }
267
268 private const string Unknown_Platform = "Unknown Platform";
269 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 {
279 //if (string.IsNullOrEmpty(EmuRomPath))
280 //{
281 // if (Debugger.IsAttached)
282 // Debugger.Break();
283 //}
284 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 this.ExternalConfigs = new List<IEmuConfig>();
291 this.GameTitle = "";
292 this.GameImage = "";
293 this.GameExe = "";
294 this.GameExeArgs = "";
295 this.log = log;
296 }
297 #region IEmuConfig members
298 public bool IsFavorites { get { return false; } }
299 public string ConfigPath { get; set; }
300 private string _PlatformNameShort;
301 public string PlatformNameShort
302 {
303 get
304 {
305 try
306 {
307 //if (string.IsNullOrEmpty(EmuRomPath))
308 //{
309 // return "EmuRomPath null";
310 //}
311 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
312 return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
313
314 }
315 catch (Exception ex)
316 {
317 log.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString());
318 return Unknown_Platform;
319 }
320 }
321 set { _PlatformNameShort = value; }
322 }
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 log.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
336 return Unknown_Platform;
337 }
338 }
339 set { _PlatformNameLong = value; }
340 }
341 public Image PlatformImage { get; set; }
342 public string Extenstions { get; set; }
343 public string EmuPath { get; set; }
344 public string EmuOptions { get; set; }
345 public string EmuRomPath { get; set; }
346 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
354 public void ReleasePlatformImageResource()
355 {
356 if (this.PlatformImage != null)
357 this.PlatformImage.Dispose();
358 }
359 #endregion
360
361 public override string ToString()
362 {
363 return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
364 }
365 #region IComparable Members
366
367 public int CompareTo(IEmuConfig obj)
368 {
369 return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
370 }
371 public void RefreshConfig(logger log)
372 {
373 log.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
374 this.Create(log,EmuRomPath);
375 }
376 #endregion
377
378 public void Dispose()
379 {
380 if (this.PlatformImage != null)
381 this.PlatformImage.Dispose();
382 }
383 }
384 #endregion
385
386 }
387 }

  ViewVC Help
Powered by ViewVC 1.1.22