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

Contents of /branches/mono/EmuXPortal.Api/Api/RomParser.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: 7662 byte(s)
use HashSet instead of List
1 //#define LIST_ADDED_ROMS // when defined will log all found rom files
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.IO;
7 using System.Diagnostics;
8 using Enterprise.Logging;
9
10 namespace EmuXPortal.Api {
11 public class RomParser : ParserEventSource, IDisposable {
12 private IPlatformCache cache;
13 private IEmuConfig Config { get; set; }
14
15 public IEmuConfig GetConfig ()
16 {
17 return this.Config;
18 }
19 public RomParser (IPlatformCache cache, IEmuConfig config, ProgressEvent event_action) : base (event_action)
20 {
21 this.cache = cache;
22 this.Config = config;
23 if (!this.Config.IsFavorites) {
24 gLog.Info.WriteLine ("Getting Roms for platform: {0}", config.PlatformNameShort);
25 HashSet<IRomConfigPair> roms = new HashSet<IRomConfigPair> ();
26 bool platform_is_cached = cache.isPlatformPathCached (config.PlatformPath);
27 if (platform_is_cached) {
28 var cached_platform = cache.GetCachedPlatform (config.PlatformPath);
29 if (cached_platform.HasExternalConfigs) {
30 HashSet<IExternalEmuConfig> ieecs = cached_platform.ExternalConfigs;
31 foreach (IExternalEmuConfig ieec in ieecs) {
32 IEmuConfig iec = new EmuConfigLoader.EmuConfig (ieec);
33 iec.EmuRomPath = cached_platform.EmulatorRomPath;
34 var reconfig = FixExternalConfig (Config, iec);
35 roms.Add (new RomConfigPair (reconfig.GameExe, reconfig.GameImage, reconfig));
36 }
37 } else {
38 HashSet<IEmuRom> iers = cached_platform.Roms;
39 foreach (var ier in iers) {
40 roms.Add (new RomConfigPair (ier.RomFile, ier.RomImage, Config));
41 }
42 }
43 } else {
44 roms = GetRoms ();
45
46 }
47 gLog.Info.WriteLine ("Found {0} Roms", roms.Count);
48 if (!platform_is_cached) {
49 // don't need to generate any rom config if the platform was cached
50 this.Roms = GenerateRomConfig (roms, config);
51 } else {
52 HashSet<IRomConfig> romconfigs = new HashSet<IRomConfig> ();
53 foreach (IRomConfigPair rom in roms) {
54 RomLoader.RomConfig crom = new RomLoader.RomConfig (rom.RomFile, rom.RomImage, rom.RomConfig);
55 romconfigs.Add (crom);
56 }
57 this.Roms = romconfigs;
58 }
59 } else {
60 gLog.Info.WriteLine ("Getting Roms for platform: {0}", config.PlatformNameShort);
61 this.Roms = RomFavorite.GetRoms (cache);
62 gLog.Info.WriteLine ("Found {0} Roms", this.Roms.Count);
63 }
64 }
65 public HashSet<IRomConfig> Roms { get; private set; }
66 private HashSet<IRomConfigPair> GetRoms ()
67 {
68 if (Config.HasExternalConfigs) { return GetRomsEX (); }
69 string path = Config.EmuRomPath; string searchPattern = Config.Extenstions;
70 gLog.Info.WriteLine ("Searching for Roms in Folder: {0}", path);
71 Predicate<string> rom_predicate = new Predicate<string> (delegate (string t) {
72 string search_exts = searchPattern.Replace ("*", "");
73 HashSet<string> exts = new HashSet<string> (search_exts.Split (new char [] { ';' }, StringSplitOptions.RemoveEmptyEntries));
74 string ext = Path.GetExtension (t).ToLower ();
75 return exts.Contains (ext) && t.ToLower ().EndsWith (ext);
76 });
77 HashSet<string> roms = (searchPattern == "*.*")
78 ? new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories))
79 : new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories).Where (s => rom_predicate (s)));
80
81 #region LIST_ADDED_ROMS
82 #if LIST_ADDED_ROMS
83 foreach (string rom in roms)
84 {
85 FileInfo fi = new FileInfo(rom);
86 logger.WriteLine("\tAdding: {0}", fi.Name);
87 }
88 #endif
89 HashSet<IRomConfigPair> rompairlist = new HashSet<IRomConfigPair> ();
90 foreach (string rom in roms) {
91 /* TODO:
92 1) Do we leave the image paramter emptry?
93 -or-
94 2) Do we need to assign an image for the rom?
95 */
96 rompairlist.Add (new RomConfigPair (rom, "", Config));
97 }
98 #endregion
99 return rompairlist;
100 }
101
102 private HashSet<IRomConfigPair> GetRomsEX ()
103 {
104 HashSet<IEmuConfig> configs = Config.ExternalConfigs;
105 HashSet<IRomConfigPair> roms = new HashSet<IRomConfigPair> ();
106
107
108 foreach (IEmuConfig config in configs) {
109 var reconfig = FixExternalConfig (Config, config);
110 roms.Add (new RomConfigPair (reconfig.GameExe, reconfig.GameImage, reconfig));
111 }
112
113 return roms;
114 }
115 private IEmuConfig FixExternalConfig (IEmuConfig parent_config, IEmuConfig child_config)
116 {
117 string PlatformNameShort = child_config.PlatformNameShort.Contains ("Unknown") ? parent_config.PlatformNameShort != "" ? parent_config.PlatformNameShort : "" : child_config.PlatformNameShort;
118 string PlatformNameLong = child_config.PlatformNameLong.Contains ("Unknown") ? parent_config.PlatformNameLong != "" ? parent_config.PlatformNameLong : "" : child_config.PlatformNameLong;
119 string PlatformImage = "";
120 string Extenstions = child_config.Extenstions == "" ? parent_config.Extenstions != "" ? parent_config.Extenstions : "" : child_config.Extenstions;
121 string EmuBinaryPath = child_config.EmuBinaryPath == "" ? parent_config.EmuBinaryPath != "" ? parent_config.EmuBinaryPath : "" : child_config.EmuBinaryPath;
122 string EmuOptions = child_config.EmuOptions == "" ? parent_config.EmuOptions != "" ? parent_config.EmuOptions : "" : child_config.EmuOptions;
123
124
125
126 EmuXPortal.Api.EmuConfigLoader.EmuConfig c = new EmuConfigLoader.EmuConfig (PlatformNameShort, PlatformNameLong, PlatformImage, Extenstions, EmuBinaryPath, EmuOptions);
127 c.EmuRomPath = string.IsNullOrEmpty (child_config.EmuRomPath) ? string.IsNullOrEmpty (parent_config.EmuRomPath) ? "" : parent_config.EmuRomPath : child_config.EmuRomPath;
128
129
130 c.GameExe = child_config.GameExe;
131 c.GameExeArgs = child_config.GameExeArgs;
132 c.GameImage = child_config.GameImage;
133 c.GameTitle = child_config.GameTitle;
134 c.ConfigPath = child_config.ConfigPath;
135 return c;
136 }
137 private HashSet<IRomConfig> GenerateRomConfig (HashSet<IRomConfigPair> roms, IEmuConfig config)
138 {
139 int total_count = roms.Count;
140 int count = 0;
141 int update_modulus = (int)Math.Ceiling (0.01 * (double)total_count);
142 HashSet<IRomConfig> romconfigs = new HashSet<IRomConfig> ();
143 //gLog.Debug.WriteLine("Generating Rom Configs");
144 foreach (IRomConfigPair rom in roms) {
145 IRomConfig crom = RomLoader.Load (rom.RomFile, rom.RomImage, rom.RomConfig);
146 if (crom != null) {
147 romconfigs.Add (crom);
148 } else {
149 gLog.Warn.WriteLine ("Failed to add rom: '{0}' from config: '{1}'", rom.RomFile, rom.RomConfig);
150 }
151 if (count % update_modulus == 0) {
152 string message = string.Format ("Please Wait Loading {0} Roms {1} of {2} ...", config.PlatformNameLong, count, total_count);
153 this.OnProgressEvent (this, new ProgressEventArgs (count, total_count, crom, message));
154 }
155 count++;
156 }
157 return romconfigs;
158 }
159
160 private interface IRomConfigPair {
161 string RomFile { get; }
162 string RomImage { get; }
163 IEmuConfig RomConfig { get; }
164 string ToString ();
165 }
166 private class RomConfigPair : IRomConfigPair {
167 public RomConfigPair () : this ("", "", EmuConfigLoader.Empty) { }
168 public RomConfigPair (string romFile, string romImage, IEmuConfig romConfig)
169 {
170 RomFile = romFile;
171 RomImage = romImage;
172 RomConfig = romConfig;
173 }
174 #region IRomConfigPair members
175 public string RomFile { get; set; }
176 public string RomImage { get; set; }
177 public IEmuConfig RomConfig { get; set; }
178 public override string ToString ()
179 {
180 return this.RomFile;
181 }
182 #endregion
183 }
184
185 public override void Dispose ()
186 {
187 /* FIXME: HASHSET FOREACH */
188 //Roms.ForEach (x => x.Dispose ());
189 Roms.Clear ();
190 Roms = null;
191 }
192 }
193 }

  ViewVC Help
Powered by ViewVC 1.1.22