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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (show annotations) (download)
Mon Aug 4 06:31:48 2014 UTC (6 years, 3 months ago) by william
File size: 21136 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
10 namespace EmuXPortal.Api
11 {
12 public interface IEmuConfig : IComparable<IEmuConfig>, IDisposable
13 {
14 bool IsFavorites { get; }
15 string ConfigPath { get; }
16 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 string ToString();
24
25 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 void RefreshConfig(logger log);
36 void ReleasePlatformImageResource();
37 }
38
39 public static class EmuConfigLoader
40 {
41 public static readonly IEmuConfig Empty = new EmuConfig();
42 private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
43 #region load
44 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 #endregion
47 #region parse emu options
48 public static string GetEMUOptions(logger log, IRomConfig config)
49 {
50 EMUOptions EMUOptions = new EMUOptions(log, config);
51 return EMUOptions.Options;
52 }
53 #endregion
54 #region private class EMUOptions
55 private class EMUOptions
56 {
57 #region Replaceable Constant Options
58 private const string ROM_FILE = "%ROM_FILE%";
59 private const string ROM_PATH = "%ROM_PATH%";
60 #endregion
61 private Dictionary<string, string> options_dict = new Dictionary<string, string>();
62 public EMUOptions(logger log, IRomConfig config)
63 {
64 init_dict(config);
65 config.Config.RefreshConfig(log);
66 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 options_dict.Add(ROM_FILE, config.RomFile);
74 options_dict.Add(ROM_PATH, config.Config.EmuRomPath);
75 }
76 public string Options { get; set; }
77 }
78 #endregion
79
80 #region private class EmuConfig : IEmuConfig
81 private class EmuConfig : IEmuConfig, IComparable<IEmuConfig>, IDisposable
82 {
83 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 {
86 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 // read the actual config emu.config
104 FileInfo fi = new FileInfo(config_path);
105 if (fi.Exists)
106 {
107 log.WriteLine("Found EMU Config File: {0}", config_path);
108 log.WriteLine("\tLoading Config: {0}", config_path);
109 this.ConfigPath = config_path;
110 //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 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 log.WriteLine("\t\tPLATFORMNAMESHORT={0}", PlatformNameShort);
131 }
132 break;
133 case "PLATFORMNAMELONG":
134 if (reader.IsStartElement())
135 {
136 value = reader.ReadElementContentAsString();
137 PlatformNameLong = (value == "") ? PlatformNameLong : value;
138 log.WriteLine("\t\tPLATFORMNAMELONG={0}", PlatformNameLong);
139 }
140 break;
141 case "PLATFORMIMAGE":
142 if (reader.IsStartElement())
143 {
144 string platform_image = reader.ReadElementContentAsString();
145 PlatformImage = (platform_image == "") ? null : Image.FromFile(string.Format(@"{0}\{1}", EmuRomPath, platform_image));
146 string str_platform_image = (platform_image == "") ? "DefaultPlatformImage" : platform_image;
147 log.WriteLine("\t\tPLATFORMIMAGE={0}", str_platform_image);
148 }
149 break;
150 case "EXTENSIONS":
151 if (reader.IsStartElement())
152 {
153 value = reader.ReadElementContentAsString();
154 Extenstions = (value == "") ? Extenstions : value;
155 log.WriteLine("\t\tEXTENSIONS={0}", Extenstions);
156 }
157 break;
158 case "EMULATORPATH":
159 if (reader.IsStartElement())
160 {
161 value = reader.ReadElementContentAsString();
162 EmuPath = (value == "") ? EmuPath : value;
163 log.WriteLine("\t\tEMULATORPATH={0}", EmuPath);
164 }
165 break;
166 case "EMULATOROPTIONS":
167 if (reader.IsStartElement())
168 {
169 value = reader.ReadElementContentAsString();
170 EmuOptions = (value == "") ? EmuOptions : value;
171 log.WriteLine("\tEMULATOROPTIONS={0}", EmuOptions);
172 }
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 log.WriteLine("\tGAMETITLE={0}", GameTitle);
182 }
183 break;
184 case "GAMEIMAGE":
185 if (reader.IsStartElement())
186 {
187 value = reader.ReadElementContentAsString();
188 string gameimage = (value == "") ? GameImage : value;
189 GameImage = gameimage;
190 log.WriteLine("\tGAMEIMAGE={0}", GameImage);
191 }
192 break;
193 case "GAMEEXE":
194 if (reader.IsStartElement())
195 {
196 value = reader.ReadElementContentAsString();
197 string gameexe = (value == "") ? GameExe : value;
198 GameExe = gameexe;
199 log.WriteLine("\tGAMEEXE={0}", GameExe);
200 }
201 break;
202 case "GAMEEXEARGS":
203 if (reader.IsStartElement())
204 {
205 value = reader.ReadElementContentAsString();
206 string gameexeargs = (value == "") ? GameExeArgs : value;
207 GameExeArgs = gameexeargs;
208 log.WriteLine("\tGAMEEXEARGS={0}", GameExeArgs);
209 }
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 log.WriteLine("\tEXTERNALCONFIGSPATH={0}", external_configs_path);
218 DirectoryInfo ext_path = new DirectoryInfo(external_configs_path);
219 string ext_rom_path = string.Format(@"{0}\{1}", EmuRomPath, external_configs_path);
220 // 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 IEmuConfig config = EmuConfigLoader.Load(log, file, EmuRomPath);
233 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 log.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path);
245 break;
246 }
247 reader.Read();
248 }
249 }
250 }
251 log.WriteLine("\tLoaded Config: {0}", config_path);
252 //loaded = true;
253 }
254 catch (Exception ex)
255 {
256 log.WriteLine("\tFailed to Load Config: {0}", config_path);
257 Console.WriteLine(ex.ToString());
258 log.WriteLine("Error: {0}", ex.ToString());
259 //loaded = false;
260 }
261 }
262 else { log.WriteLine("Could not find EMU Config File: {0}", config_path); }
263
264 return this;
265 }
266
267 private const string Unknown_Platform = "Unknown Platform";
268 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 {
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 this.ExternalConfigs = new List<IEmuConfig>();
285 this.GameTitle = "";
286 this.GameImage = "";
287 this.GameExe = "";
288 this.GameExeArgs = "";
289 this.log = log;
290 }
291 #region IEmuConfig members
292 public bool IsFavorites { get { return false; } }
293 public string ConfigPath { get; set; }
294 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 catch (Exception ex)
305 {
306 log.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString());
307 return Unknown_Platform;
308 }
309 }
310 set { _PlatformNameShort = value; }
311 }
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 log.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
325 return Unknown_Platform;
326 }
327 }
328 set { _PlatformNameLong = value; }
329 }
330 public Image PlatformImage { get; set; }
331 public string Extenstions { get; set; }
332 public string EmuPath { get; set; }
333 public string EmuOptions { get; set; }
334 public string EmuRomPath { get; set; }
335 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
343 public void ReleasePlatformImageResource()
344 {
345 if (this.PlatformImage != null)
346 this.PlatformImage.Dispose();
347 }
348 #endregion
349
350 public override string ToString()
351 {
352 return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
353 }
354 #region IComparable Members
355
356 public int CompareTo(IEmuConfig obj)
357 {
358 return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
359 }
360 public void RefreshConfig(logger log)
361 {
362 log.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
363 this.Create(log,EmuRomPath);
364 }
365 #endregion
366
367 public void Dispose()
368 {
369 if (this.PlatformImage != null)
370 this.PlatformImage.Dispose();
371 }
372 }
373 #endregion
374
375 }
376 }

  ViewVC Help
Powered by ViewVC 1.1.22