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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22