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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 396 - (show annotations) (download)
Sun Jun 23 23:32:26 2019 UTC (6 months, 3 weeks ago) by william
File size: 11922 byte(s)
use HashSet instead of List
1 using System;
2 using System.Collections.Generic;
3 using Enterprise.Logging;
4 using System.Xml.Linq;
5 using System.IO;
6 using Enterprise.CrossPlatform;
7 using System.Linq;
8 using System.Text;
9 using System.Runtime.InteropServices;
10 using System.Windows.Forms;
11
12 namespace EmuXPortal.Api
13 {
14 public delegate bool AbortEvent ();
15 public interface IExternalEmuConfig : IComparable<IExternalEmuConfig>
16 {
17 string ConfigFile{ get; }
18 string GameTitle{get;}
19 string GameImage{get;}
20 string GameBinary{get;}
21 string GameBinaryArgs{get;}
22 bool GameExists {get;}
23 }
24 [Serializable]
25 public class ExternalEmuConfig : IExternalEmuConfig
26 {
27 public ExternalEmuConfig() {
28 }
29 public ExternalEmuConfig (string config_path)
30 {
31 this.ConfigFile = config_path;
32 }
33 #region IExternalEmuConfig members
34 public bool GameExists { get { return File.Exists(this.GameBinary); }}
35 private string m_ConfigFile;
36 public string ConfigFile{ get { return m_ConfigFile; } set { m_ConfigFile = value; } }
37 private string m_GameTitle = "";
38 public string GameTitle{ get { return m_GameTitle; } set {m_GameTitle =value; }}
39 private string m_GameImage = "";
40 public string GameImage{ get { return m_GameImage; } set {m_GameImage =value; }}
41 private string m_GameBinary = "";
42 public string GameBinary{ get { return m_GameBinary; } set {m_GameBinary =value; }}
43 private string m_GameBinaryArgs = "";
44 public string GameBinaryArgs{ get { return m_GameBinaryArgs; } set {m_GameBinaryArgs =value; }}
45 #endregion
46 public bool ParseConfig ()
47 {
48 FileInfo emu_config = null;
49 try {
50 emu_config = new FileInfo(this.ConfigFile);
51 if (!emu_config.Exists) {
52 gLog.Error.WriteLine ("Could not find external config: {0}", this.ConfigFile);
53 return false;
54 }
55 //gLog.Debug.WriteLine("ConfigFile: {0}", this.ConfigFile);
56 XDocument xdoc = XDocument.Load (emu_config.FullName);
57 var __config = xdoc.Element (InternalConsts.CONFIG_SECTION);
58 if (__config != null && __config.HasElements) {
59 XElement elm = null;
60 elm = __config.Element(InternalConsts.GAMETITLE); // a mandatory element
61 if(elm == null) { throw new FormatException("Could not find GAMETITLE element"); }
62 this.GameTitle = elm.Value;
63 //gLog.Debug.WriteLine("GAMETITLE: {0}", this.GameTitle);
64
65 elm = __config.Element(InternalConsts.GAMEIMAGE); // a mandatory element
66 if(elm == null) { throw new FormatException("Could not find GAMEIMAGE element"); }
67 this.GameImage = elm.Value;
68 //gLog.Debug.WriteLine("GAMEIMAGE: {0}", this.GameImage);
69
70 elm = __config.Element(InternalConsts.GAMEEXE); // a mandatory element
71 if(elm == null) { throw new FormatException("Could not find GAMEEXE element"); }
72 this.GameBinary = elm.Value;
73 //gLog.Debug.WriteLine("GAMEEXE: {0}", this.GameBinary);
74
75 elm = __config.Element(InternalConsts.GAMEEXEARGS); // a mandatory element
76 if(elm == null) { throw new FormatException("Could not find GAMEEXEARGS element"); }
77 this.GameBinaryArgs = elm.Value;
78 //gLog.Debug.WriteLine("GAMEEXEARGS: {0}", this.GameBinaryArgs);
79
80 return true;
81 }
82 else {
83 throw new FormatException("Could not find config element");
84 }
85
86 } catch (Exception ex) {
87 gLog.Error.WriteLine (ex.Message);
88 gLog.Verbose.Error.WriteLine (ex.ToString ());
89 } finally {
90 emu_config = null;
91 }
92 return false;
93 }
94 public int CompareTo (IExternalEmuConfig other)
95 {
96 return this.ConfigFile.ToLower().CompareTo(other.ConfigFile.ToLower());
97 }
98 }
99 public interface IEmuPlatform
100 {
101 string ConfigFile {get;}
102 bool PlatformParsed {get;}
103 string PlatformPath { get; }
104
105 string PlatformNameShort { get; }
106
107 string PlatformNameLong { get; }
108
109 String PlatformImage { get; }
110
111 string Extenstions { get; }
112
113 string EmulatorBinaryPath { get; }
114
115 string EmulatorOptions { get; }
116
117 string EmulatorRomPath { get; }
118
119 bool HasExternalConfigs { get; }
120
121 HashSet<IExternalEmuConfig> ExternalConfigs { get; }
122 HashSet<IEmuRom> Roms { get; set;}
123
124 string ToString();
125 }
126
127 [Serializable]
128 public class EmuPlatform : IEmuPlatform
129 {
130 [NonSerialized]
131 private AbortEvent abort_event;
132
133 [NonSerialized]
134 private bool bupdate_config_only = false;
135 // if this file signifies the emulator configuration
136 public EmuPlatform () { }
137
138 public EmuPlatform (AbortEvent abort_event, bool update_config_only, string platform_path)
139 {
140 this.bupdate_config_only = update_config_only;
141 this.abort_event = abort_event;
142 this.PlatformPath = platform_path;
143 this.EmulatorRomPath = OSInfo.FormatPath (string.Format ("{0}/{1}", platform_path, "Roms"));
144 if (ParseConfig ()) {
145 if (!this.HasExternalConfigs) {
146 if (!EnumerateRoms ()) {
147 this.PlatformParsed = false;
148 } else {
149 this.PlatformParsed = true;
150 }
151 } else {
152 this.PlatformParsed = true;
153 }
154 } else {
155 this.PlatformParsed = false;
156 }
157 }
158
159 #region IEmuPlatform members
160 public override string ToString() { return this.PlatformPath; }
161 private string m_ConfigFile = "";
162 public string ConfigFile { get { return m_ConfigFile; } set { m_ConfigFile = value;}}
163 private bool m_PlatformParsed = false;
164 public bool PlatformParsed { get { return m_PlatformParsed; } set { m_PlatformParsed=value;}}
165 private string m_PlatformPath = "";
166
167 public string PlatformPath { get { return m_PlatformPath; } set { this.m_PlatformPath = value; } }
168
169 private string m_PlatformNameShort = "";
170
171 public string PlatformNameShort { get { return m_PlatformNameShort; } set { this.m_PlatformNameShort = value; } }
172
173 private string m_PlatformNameLong = "";
174
175 public string PlatformNameLong { get { return m_PlatformNameLong; } set { this.m_PlatformNameLong = value; } }
176
177 private string m_PlatformImage = "";
178
179 public String PlatformImage { get { return m_PlatformImage; } set { this.m_PlatformImage = value; } }
180
181 private string m_Extenstions = "*.*";
182
183 public string Extenstions { get { return m_Extenstions; } set { this.m_Extenstions = value; } }
184
185 private string m_EmulatorBinaryPath = "";
186
187 public string EmulatorBinaryPath { get { return m_EmulatorBinaryPath; } set { this.m_EmulatorBinaryPath = value; } }
188
189 private string m_EmulatorOptions = "";
190
191 public string EmulatorOptions { get { return m_EmulatorOptions; } set { this.m_EmulatorOptions = value; } }
192
193 private string m_EmulatorRomPath = "";
194
195 public string EmulatorRomPath { get { return m_EmulatorRomPath; } set { this.m_EmulatorRomPath = value; } }
196
197
198 private bool m_HasExternalConfigs = false;
199 public bool HasExternalConfigs { get { return m_HasExternalConfigs; } set { this.m_HasExternalConfigs = value; } }
200 private HashSet<IEmuRom> m_Roms = new HashSet<IEmuRom> ();
201
202 public HashSet<IEmuRom> Roms {
203 get { return m_Roms; }
204 set { m_Roms = value; }
205 }
206
207 private HashSet<IExternalEmuConfig> m_ExternalConfigs = new HashSet<IExternalEmuConfig>();
208 public HashSet<IExternalEmuConfig> ExternalConfigs { get { return m_ExternalConfigs; } set { this.m_ExternalConfigs = value; } }
209 #endregion
210
211 private bool ParseConfig ()
212 {
213 gLog.Debug.WriteLine ("Parsing Platform path: {0}", this.PlatformPath);
214
215 // read the emu config file
216 FileInfo emu_config = null;
217 try {
218 emu_config = new FileInfo (OSInfo.FormatPath (string.Format ("{0}/{1}", this.PlatformPath, InternalConsts.EMU_CONFIG)));
219 this.ConfigFile = emu_config.FullName;
220 if(!emu_config.Exists) {
221 gLog.Error.WriteLine("Could not find: {0} in path: {1}", InternalConsts.EMU_CONFIG, emu_config.Directory.FullName);
222 return false;
223 }
224 XDocument xdoc = XDocument.Load (emu_config.FullName);
225 var __config = xdoc.Element(InternalConsts.CONFIG_SECTION);
226
227 if (__config != null && __config.HasElements) {
228 XElement elm = __config.Element(InternalConsts.PLATFORMNAMESHORT); // a mandatory element
229 if(elm == null) { throw new FormatException("Could not find PLATFORMNAMESHORT element"); }
230 this.PlatformNameShort = elm.Value;
231 gLog.Debug.WriteLine("PLATFORMNAMESHORT: {0}", this.PlatformNameShort);
232
233 elm = __config.Element(InternalConsts.PLATFORMNAMELONG); // a mandatory element
234 if(elm == null) { throw new FormatException("Could not find PLATFORMNAMELONG element"); }
235 this.PlatformNameLong = elm.Value;
236 gLog.Debug.WriteLine("PLATFORMNAMELONG: {0}", this.PlatformNameLong);
237
238 elm = __config.Element(InternalConsts.PLATFORMIMAGE); // a mandatory element
239 if(elm == null) { throw new FormatException("Could not find PLATFORMIMAGE element"); }
240 this.PlatformImage = elm.Value;
241 gLog.Debug.WriteLine("PLATFORMIMAGE: {0}", this.PlatformImage);
242
243 elm = __config.Element(InternalConsts.EXTENSIONS);
244 if(elm != null) {
245 // not a mandatory element
246 this.Extenstions = elm.Value;
247 gLog.Debug.WriteLine("EXTENSIONS: {0}", this.Extenstions);
248 }
249
250 elm = __config.Element(InternalConsts.EMULATORPATH);
251 if(elm != null) {
252 // not a mandatory element
253 this.EmulatorBinaryPath = elm.Value;
254 gLog.Debug.WriteLine("EMULATORPATH: {0}", this.EmulatorBinaryPath);
255 }
256
257 elm = __config.Element(InternalConsts.EMULATOROPTIONS);
258 if(elm != null) {
259 // not a mandatory element
260 this.EmulatorOptions = elm.Value;
261 gLog.Debug.WriteLine("EMULATOROPTIONS: {0}", this.EmulatorOptions);
262 }
263
264 elm = __config.Element(InternalConsts.EXTERNALCONFIGSPATH);
265 if(elm != null) {
266 // not a mandatory element
267 this.HasExternalConfigs = true;
268 string external_config_path = elm.Value;
269 gLog.Debug.WriteLine("EXTERNALCONFIGSPATH: {0}", external_config_path);
270 if(!ParseExternalConfigs(external_config_path)) {
271 gLog.Error.WriteLine("Failed to parse external configs");
272 return false;
273 }
274 }
275
276 return true;
277 }
278 else {
279 throw new FormatException("Could not find config element");
280 }
281 } catch (Exception ex) {
282 gLog.Error.WriteLine (ex.Message);
283 gLog.Verbose.Error.WriteLine (ex.ToString ());
284 } finally {
285 emu_config = null;
286 }
287 return false;
288 }
289
290 private bool ParseExternalConfigs (string external_config_path)
291 {
292 string searchPattern = InternalConsts.EXTERNAL_EMU_CONFIG_EXTENTION;
293 string path = OSInfo.FormatPath (string.Format ("{0}/{1}", this.PlatformPath, external_config_path));
294 Predicate<string> config_predicate = new Predicate<string> (delegate(string t) {
295 string search_exts = searchPattern.Replace ("*", "");
296 HashSet<string> exts = new HashSet<string> (search_exts.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
297 string ext = Path.GetExtension (t).ToLower ();
298 return exts.Contains (ext) && t.ToLower ().EndsWith (ext);
299 });
300 HashSet<string> configs = (searchPattern == "*.*")
301 ? new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories))
302 : new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories).Where (s => config_predicate (s)));
303
304 if (configs.Count > 0) {
305 foreach (var config in configs) {
306 ExternalEmuConfig eec = new ExternalEmuConfig (config);
307 if (!eec.ParseConfig ()) {
308 throw new Exception (string.Format ("Failed to parse config: {0}", config));
309 } else {
310 this.ExternalConfigs.Add (eec);
311 }
312 }
313 }
314 this.ExternalConfigs = new HashSet<IExternalEmuConfig> (this.ExternalConfigs.OrderBy (s => s.ConfigFile.ToLower ()));
315 return true;
316 }
317 private bool EnumerateRoms ()
318 {
319 if (this.bupdate_config_only) {
320 return true;
321 }
322 string searchPattern = this.Extenstions;
323 string path = this.EmulatorRomPath;
324 HashSet<IEmuRom> roms = new HashSet<IEmuRom>();
325 HashSet<string> images = EmuRom.GetImagesInPath(this.EmulatorRomPath);
326 bool result = EmuRom.EnumerateRoms(this, images,this.abort_event, path,searchPattern, out roms);
327 images.Clear();
328 images=null;
329 this.Roms = roms;
330 return result;
331 }
332
333
334 }
335 }

  ViewVC Help
Powered by ViewVC 1.1.22