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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations) (download)
Thu Aug 23 02:44:26 2012 UTC (8 years, 3 months ago) by william
File size: 20269 byte(s)
PERFORMANCE OPTIMIZATION:
** only get defaultimage resource once (don't duplicate the resource)

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

  ViewVC Help
Powered by ViewVC 1.1.22