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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (show annotations) (download)
Thu Aug 23 04:47:35 2012 UTC (8 years, 2 months ago) by william
File size: 20558 byte(s)
+ BUGFIX (1.0.0.4): properly send roms to emulator

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(logger log);
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(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 private 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 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 log.WriteLine("Found EMU Config File: {0}", config_path);
95 log.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 log.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 log.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 log.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 log.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 log.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 log.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 log.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 log.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 log.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 log.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 log.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(log, 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 log.WriteLine("Warning: Unknown or Unrecognized config option: {0} in {1}", reader.Name, config_path);
232 break;
233 }
234 reader.Read();
235 }
236 }
237 }
238 log.WriteLine("\tLoaded Config: {0}", config_path);
239 //loaded = true;
240 }
241 catch (Exception ex)
242 {
243 log.WriteLine("\tFailed to Load Config: {0}", config_path);
244 Console.WriteLine(ex.ToString());
245 log.WriteLine("Error: {0}", ex.ToString());
246 //loaded = false;
247 }
248 }
249 else { log.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 private logger log;
256 public EmuConfig() : this(null, "") { }
257 public EmuConfig(logger log) : this(log,"") { }
258 public EmuConfig(logger log, string PlatformNameShort) : this(log, PlatformNameShort, PlatformNameShort) { }
259 public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong) : this(log, PlatformNameShort, PlatformNameLong, "") { }
260 public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
261 public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
262 public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this(log, PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
263 public EmuConfig(logger log, string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
264 {
265 this.PlatformNameShort = PlatformNameShort;
266 this.PlatformNameLong = PlatformNameLong;
267 this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(PlatformImage);
268 this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
269 this.EmuPath = EmuPath;
270 this.EmuOptions = EmuOptions;
271 this.ExternalConfigs = new List<IEmuConfig>();
272 this.GameTitle = "";
273 this.GameImage = "";
274 this.GameExe = "";
275 this.GameExeArgs = "";
276 this.log = log;
277 }
278 #region IEmuConfig members
279 public string ConfigPath { get; set; }
280 private string _PlatformNameShort;
281 public string PlatformNameShort
282 {
283 get
284 {
285 try
286 {
287 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
288 return (_PlatformNameShort == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
289 }
290 catch (Exception ex)
291 {
292 log.WriteLine("PlatformNameShort.get() Error: {0}", ex.ToString());
293 return Unknown_Platform;
294 }
295 }
296 set { _PlatformNameShort = value; }
297 }
298 private string _PlatformNameLong;
299 public string PlatformNameLong
300 {
301 get
302 {
303 try
304 {
305 DirectoryInfo t = new DirectoryInfo(EmuRomPath);
306 return (_PlatformNameLong == "") ? string.Format("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
307 }
308 catch (Exception ex)
309 {
310 log.WriteLine("PlatformNameLong.get() Error: {0}", ex.ToString());
311 return Unknown_Platform;
312 }
313 }
314 set { _PlatformNameLong = value; }
315 }
316 public Image PlatformImage { get; set; }
317 public string Extenstions { get; set; }
318 public string EmuPath { get; set; }
319 public string EmuOptions { get; set; }
320 public string EmuRomPath { get; set; }
321 public bool HasExternalConfigs { get { return ExternalConfigs.Count > 0; } }
322 public List<IEmuConfig> ExternalConfigs { get; private set; }
323
324 public string GameTitle { get; set; }
325 public string GameImage { get; set; }
326 public string GameExe { get; set; }
327 public string GameExeArgs { get; set; }
328
329 public void ReleasePlatformImageResource()
330 {
331 if (this.PlatformImage != null)
332 this.PlatformImage.Dispose();
333 }
334 #endregion
335
336 public override string ToString()
337 {
338 return string.Format("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
339 }
340 #region IComparable Members
341
342 public int CompareTo(IEmuConfig obj)
343 {
344 return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
345 }
346 public void RefreshConfig(logger log)
347 {
348 log.WriteLine("Refreshing config for: {0} from {1}", this.ToString(), string.Format(@"{0}\{1}", EmuRomPath, EMU_CONFIG));
349 this.Create(log,EmuRomPath);
350 }
351 #endregion
352
353 public void Dispose()
354 {
355 if (this.PlatformImage != null)
356 this.PlatformImage.Dispose();
357 }
358 }
359 #endregion
360
361 }
362 }

  ViewVC Help
Powered by ViewVC 1.1.22