/[EmuXPortal]/branches/mono/EmuXPortal.Api/Api/EmuConfig.cs
ViewVC logotype

Contents of /branches/mono/EmuXPortal.Api/Api/EmuConfig.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 396 - (show annotations) (download)
Sun Jun 23 23:32:26 2019 UTC (7 months ago) by william
File size: 14774 byte(s)
use HashSet instead of List
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 System.Xml;
8 using System.Diagnostics;
9 using Enterprise.Logging;
10 using Enterprise.CrossPlatform;
11 using System.Xml.Linq;
12
13 namespace EmuXPortal.Api {
14 public interface IEmuConfig : IComparable<IEmuConfig>, IDisposable {
15 bool IsFavorites { get; }
16 string ConfigPath { get; }
17 string ConfigFile { get; }
18 string PlatformPath { get; }
19 string PlatformNameShort { get; }
20 string PlatformNameLong { get; }
21 String PlatformImage { get; }
22 string GetFullPlatformImage ();
23 string Extenstions { get; }
24 string EmuBinaryPath { get; }
25 string EmuOptions { get; }
26 string EmuRomPath { get; set; }
27 string ToString ();
28
29
30 bool HasExternalConfigs { get; }
31 HashSet<IEmuConfig> ExternalConfigs { get; }
32
33
34 string GameTitle { get; }
35 string GameImage { get; }
36 string GameExe { get; }
37 string GameExeArgs { get; }
38
39
40 void RefreshConfig ();
41 }
42
43 public static class EmuConfigLoader {
44 public static readonly IEmuConfig Empty = new EmuConfig ();
45 private const string EMU_CONFIG = "emu.config"; // if this file signifies the emulator configuration
46 public static string getEMUConfigFile () { return EMU_CONFIG; }
47 #region load
48 public static IEmuConfig Load (string rom_path) { return new EmuConfig ().Create (rom_path); }
49 public static IEmuConfig Load (string config_path, string rom_path) { return new EmuConfig ().Create (config_path, rom_path); }
50 #endregion
51 #region parse emu options
52 public static string GetEMUOptions (IRomConfig config)
53 {
54 EMUOptions EMUOptions = new EMUOptions (config);
55 return EMUOptions.Options;
56 }
57 #endregion
58 #region private class EMUOptions
59 private class EMUOptions {
60 #region Replaceable Constant Options
61 private const string ROM_FILE = "%ROM_FILE%";
62 private const string ROM_PATH = "%ROM_PATH%";
63 #endregion
64 private Dictionary<string, string> options_dict = new Dictionary<string, string> ();
65 public EMUOptions (IRomConfig config)
66 {
67 init_dict (config);
68 if (!config.isCachedLocally && File.Exists (config.EmuConfig.ConfigPath)) {
69 config.EmuConfig.RefreshConfig ();
70 }
71 string options = config.EmuConfig.EmuOptions;
72 string real_options = options;
73 foreach (KeyValuePair<string, string> pair in options_dict) {
74 if (options.ToLower ().Contains (pair.Key.ToLower ())) {
75 real_options = real_options.ToLower ().Replace (pair.Key.ToLower (), string.Format ("{0}", pair.Value));
76 }
77 }
78 Options = real_options;
79 }
80 private void init_dict (IRomConfig config)
81 {
82 options_dict.Add (ROM_FILE, OSInfo.FormatPath (string.Format ("\"{0}\"", config.GetLocalRomFileOverride ())));
83 if (config.isCachedLocally) {
84 options_dict.Add (ROM_PATH, OSInfo.FormatPath (string.Format ("\"{0}\"", Config.LocalRomPath)));
85 } else {
86 options_dict.Add (ROM_PATH, OSInfo.FormatPath (string.Format ("\"{0}\"", config.EmuConfig.EmuRomPath)));
87 }
88 }
89 public string Options { get; set; }
90 }
91 #endregion
92
93 #region private class EmuConfig : IEmuConfig
94 internal class EmuConfig : IEmuConfig, IComparable<IEmuConfig>, IDisposable {
95 public IEmuConfig Create (string rom_path) { return this.Create (string.Empty, rom_path); }
96 public IEmuConfig Create (string config_path, string rom_path)
97 {
98 IEmuConfig parent_rom = null;
99 if (rom_path == "") {
100 if (config_path != "") {
101 EmuRomPath = config_path.Replace (EMU_CONFIG, "").TrimEnd (new char [] { '\\' });
102 } else {
103 EmuRomPath = Config.RomPath;
104 }
105 } else {
106 EmuRomPath = rom_path;
107 }
108 if (config_path == "") {
109 /*config_path = !EmuRomPath.ToLower ().EndsWith (EMU_CONFIG.ToLower ()) ?
110 OSInfo.FormatPath (string.Format (@"{0}\{1}", EmuRomPath, EMU_CONFIG)) :
111 EmuRomPath;*/
112 if (Directory.Exists (EmuRomPath)) {
113 DirectoryInfo di = null;
114 try {
115 di = new DirectoryInfo (EmuRomPath);
116 config_path = OSInfo.FormatPath (string.Format (@"{0}\{1}", di.Parent.FullName, EMU_CONFIG));
117 } finally {
118 di = null;
119 }
120 } else {
121 throw new DirectoryNotFoundException ("Could not find rom path!");
122 }
123 }
124
125
126 if (rom_path == "") {
127 FileInfo emu_config = new FileInfo (OSInfo.FormatPath (string.Format (@"{0}\{1}", new FileInfo (config_path).Directory, EMU_CONFIG)));
128 //if (emu_config.Exists)
129 //{
130 // EmuRomPath = emu_config.Directory.FullName;
131 //}
132 while (!emu_config.Exists) {
133 try {
134 emu_config = new FileInfo (OSInfo.FormatPath (string.Format (@"{0}\{1}", emu_config.Directory.Parent.FullName, EMU_CONFIG)));
135 } catch (Exception) {
136 break;
137 }
138 }
139 if (emu_config.Exists) {
140 EmuRomPath = emu_config.Directory.FullName;
141 // load the rom config
142 parent_rom = EmuConfigLoader.Load (emu_config.FullName, EmuRomPath);
143 }
144
145
146 }
147
148 // read the actual config emu.config
149 FileInfo fi = new FileInfo (config_path);
150 if (fi.Exists) {
151 this.ConfigPath = OSInfo.FormatPath (config_path);
152 //gLog.Debug.WriteLine("Found EMU Config File: {0}", this.ConfigPath);
153 //gLog.Debug.WriteLine("\tLoading Config: {0}", this.ConfigPath);
154
155 XDocument xdoc = XDocument.Load (fi.FullName);
156
157 this.PlatformNameShort = xdoc.Root.Element (InternalConsts.PLATFORMNAMESHORT) == null ? "" : xdoc.Root.Element (InternalConsts.PLATFORMNAMESHORT).Value;
158 this.PlatformNameLong = xdoc.Root.Element (InternalConsts.PLATFORMNAMELONG) == null ? "" : xdoc.Root.Element (InternalConsts.PLATFORMNAMELONG).Value;
159 this.PlatformImage = xdoc.Root.Element (InternalConsts.PLATFORMIMAGE) == null ? "" : xdoc.Root.Element (InternalConsts.PLATFORMIMAGE).Value;
160 this.Extenstions = xdoc.Root.Element (InternalConsts.EXTENSIONS) == null ? "" : xdoc.Root.Element (InternalConsts.EXTENSIONS).Value;
161 this.EmuBinaryPath = xdoc.Root.Element (InternalConsts.EMULATORPATH) == null ? "" : xdoc.Root.Element (InternalConsts.EMULATORPATH).Value;
162 this.EmuOptions = xdoc.Root.Element (InternalConsts.EMULATOROPTIONS) == null ? "" : xdoc.Root.Element (InternalConsts.EMULATOROPTIONS).Value;
163
164 this.GameTitle = xdoc.Root.Element (InternalConsts.GAMETITLE) == null ? "" : xdoc.Root.Element (InternalConsts.GAMETITLE).Value;
165 this.GameImage = xdoc.Root.Element (InternalConsts.GAMEIMAGE) == null ? "" : xdoc.Root.Element (InternalConsts.GAMEIMAGE).Value;
166 this.GameExe = xdoc.Root.Element (InternalConsts.GAMEEXE) == null ? "" : xdoc.Root.Element (InternalConsts.GAMEEXE).Value;
167 this.GameExeArgs = xdoc.Root.Element (InternalConsts.GAMEEXEARGS) == null ? "" : xdoc.Root.Element (InternalConsts.GAMEEXEARGS).Value;
168
169 string external_configs_path = xdoc.Root.Element (InternalConsts.EXTERNALCONFIGSPATH) == null ? "" : xdoc.Root.Element (InternalConsts.EXTERNALCONFIGSPATH).Value;
170 if (!string.IsNullOrEmpty (external_configs_path)) {
171 //string searchPattern = "*.config";
172 DirectoryInfo ext_path = new DirectoryInfo (external_configs_path);
173 string ext_rom_path = OSInfo.FormatPath (string.Format (@"{0}\{1}", EmuRomPath, external_configs_path));
174 if (!ext_path.Exists) {
175 ext_path = new DirectoryInfo (ext_rom_path);
176 }
177 if (ext_path.Exists) {
178 HashSet<IEmuConfig> externalconfigs = new HashSet<IEmuConfig> ();
179 // HashSet<string> external_config_files = (searchPattern == "*.*")
180 // ? new HashSet<string>(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly))
181 // : new HashSet<string>(Directory.GetFiles(ext_path.FullName, "*.*", SearchOption.TopDirectoryOnly).Where(s => searchPattern.Contains(Path.GetExtension(s).ToLower())));
182
183 HashSet<string> external_config_files = new HashSet<string> (Directory.GetFiles (ext_path.FullName, "*.config", SearchOption.TopDirectoryOnly));
184 if (external_config_files.Count > 0) {
185 external_config_files = new HashSet<string> (external_config_files.OrderBy (s => s));
186 }
187 foreach (string file in external_config_files) {
188 IEmuConfig config = EmuConfigLoader.Load (file, EmuRomPath);
189 if (config != null)
190 externalconfigs.Add (config);
191 }
192 if (externalconfigs.Count > 0)
193 ExternalConfigs = externalconfigs;
194 }
195 }
196 fi = null;
197 } else { gLog.Error.WriteLine ("Could not find EMU Config File: {0}", config_path); }
198
199 if (parent_rom != null) {
200 if (parent_rom.HasExternalConfigs) {
201 foreach (var rom in parent_rom.ExternalConfigs) {
202 if (rom.ConfigPath == config_path) {
203 this.PlatformNameShort = parent_rom.PlatformNameShort;
204 this.PlatformNameLong = parent_rom.PlatformNameLong;
205 break;
206 }
207 }
208
209 }
210 }
211
212 return this;
213 }
214
215 private const string Unknown_Platform = "Unknown Platform";
216 public EmuConfig (IEmuPlatform iep)
217 {
218 this.PlatformPath = iep.PlatformPath;
219 this.ConfigPath = iep.ConfigFile;
220 this.EmuRomPath = iep.EmulatorRomPath;
221 this.PlatformNameShort = iep.PlatformNameShort;
222 this.PlatformNameLong = iep.PlatformNameLong;
223 this.PlatformImage = iep.PlatformImage;
224 this.Extenstions = iep.Extenstions;
225
226
227 this.EmuOptions = iep.EmulatorOptions;
228 this.EmuBinaryPath = iep.EmulatorBinaryPath;
229 this.GameTitle = "";
230 this.GameImage = "";
231 this.GameExe = "";
232 this.GameExeArgs = "";
233
234 this.ExternalConfigs = new HashSet<IEmuConfig> ();
235 if (iep.HasExternalConfigs) {
236 foreach (var ieec in iep.ExternalConfigs) {
237 IEmuConfig iec = new EmuConfig (ieec);
238 iec.EmuRomPath = iep.EmulatorRomPath;
239 this.ExternalConfigs.Add (iec);
240 }
241 }
242 }
243 public EmuConfig (IExternalEmuConfig ieec)
244 {
245 this.ConfigPath = ieec.ConfigFile;
246 this.GameTitle = ieec.GameTitle;
247 this.GameImage = ieec.GameImage;
248 this.GameExe = ieec.GameBinary;
249 this.GameExeArgs = ieec.GameBinaryArgs;
250 }
251 public EmuConfig () : this (null, "") { }
252 public EmuConfig (string PlatformNameShort) : this (PlatformNameShort, PlatformNameShort) { }
253 public EmuConfig (string PlatformNameShort, string PlatformNameLong) : this (PlatformNameShort, PlatformNameLong, "") { }
254 public EmuConfig (string PlatformNameShort, string PlatformNameLong, string PlatformImage) : this (PlatformNameShort, PlatformNameLong, PlatformImage, "") { }
255 public EmuConfig (string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions) : this (PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, "") { }
256 public EmuConfig (string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath) : this (PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuPath, "") { }
257 public EmuConfig (string PlatformNameShort, string PlatformNameLong, string PlatformImage, string Extenstions, string EmuPath, string EmuOptions)
258 {
259 this.PlatformNameShort = PlatformNameShort;
260 this.PlatformNameLong = PlatformNameLong;
261 //this.PlatformImage = (PlatformImage == "") ? Properties.Resources.DefaultPlatformImage : Image.FromFile(OSInfo.FormatPath(PlatformImage));
262 this.PlatformImage = PlatformImage;
263 this.Extenstions = (Extenstions == "") ? "*.*" : Extenstions;
264 this.EmuBinaryPath = EmuPath;
265 this.EmuOptions = EmuOptions;
266 this.ExternalConfigs = new HashSet<IEmuConfig> ();
267 this.GameTitle = "";
268 this.GameImage = "";
269 this.GameExe = "";
270 this.GameExeArgs = "";
271 }
272 #region IEmuConfig members
273 private string _configpath;
274 private string _configfile;
275 public bool IsFavorites { get { return false; } }
276 public string ConfigPath {
277 get { return _configpath; }
278 set {
279 if (value == null) { throw new NullReferenceException ("config path cannot be null"); }
280 _configpath = value;
281 FileInfo fi = null;
282 try {
283 fi = new FileInfo (_configpath);
284 _configfile = fi.Name;
285 } finally {
286 fi = null;
287 }
288
289 }
290 }
291 public string ConfigFile { get { return _configfile; } set { _configfile = value; } }
292 private string _PlatformNameShort = "";
293 public string PlatformNameShort {
294 get {
295 try {
296 DirectoryInfo t = new DirectoryInfo (EmuRomPath);
297 return (_PlatformNameShort == "") ? string.Format ("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameShort;
298
299 } catch (Exception ex) {
300 gLog.Error.WriteLine ("Unable to retrieve property: PlatformNameShort");
301 gLog.Verbose.Error.WriteLine (ex.ToString ());
302 return Unknown_Platform;
303 }
304 }
305 set { _PlatformNameShort = value == null ? "" : value; }
306 }
307 private string _PlatformNameLong = "";
308 public string PlatformNameLong {
309 get {
310 try {
311 DirectoryInfo t = new DirectoryInfo (EmuRomPath);
312 return (_PlatformNameLong == "") ? string.Format ("{0} (folder={1})", Unknown_Platform, t.Name) : _PlatformNameLong;
313 } catch (Exception ex) {
314 gLog.Error.WriteLine ("Unable to retrieve property: PlatformNameLong");
315 gLog.Verbose.Error.WriteLine (ex.ToString ());
316 return Unknown_Platform;
317 }
318 }
319 set { _PlatformNameLong = value == null ? "" : value; }
320 }
321 public String PlatformImage { get; set; }
322 public string GetFullPlatformImage ()
323 {
324 if (this.PlatformImage.StartsWith ("/") || this.PlatformImage.StartsWith ("\\")) {
325 return OSInfo.FormatPath (String.Format ("{0}", this.PlatformImage));
326 } else {
327 return OSInfo.FormatPath (String.Format ("{0}/{1}", this.PlatformPath, this.PlatformImage));
328 }
329 }
330 public string PlatformPath { get; set; }
331 public string Extenstions { get; set; }
332 public string EmuBinaryPath { 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 HashSet<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 #endregion
344
345 public override string ToString ()
346 {
347 return string.Format ("{2} {0}{3}{1}", "{", "}", PlatformNameLong, PlatformNameShort);
348 }
349 #region IComparable Members
350
351 public int CompareTo (IEmuConfig obj)
352 {
353 return this.PlatformNameLong.CompareTo (obj.PlatformNameLong);
354 }
355 public void RefreshConfig ()
356 {
357 gLog.Debug.WriteLine ("Refreshing config for: {0} from {1}", this.ToString (), OSInfo.FormatPath (string.Format (@"{0}\{1}", PlatformPath, EMU_CONFIG)));
358 this.Create (EmuRomPath);
359 }
360 #endregion
361
362 public void Dispose ()
363 {
364 }
365 }
366 #endregion
367
368 }
369 }

  ViewVC Help
Powered by ViewVC 1.1.22