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 |
{ |
12 |
public class RomParser :IDisposable |
13 |
{ |
14 |
private IEmuConfig Config { get; set; } |
15 |
public RomParser(IEmuConfig config) |
16 |
{ |
17 |
this.Config = config; |
18 |
if (!this.Config.IsFavorites) |
19 |
{ |
20 |
List<IRomConfigPair> roms = GetRoms(); |
21 |
gLog.Debug.WriteLine("Found {0} Roms", roms.Count); |
22 |
this.Roms = GenerateRomConfig(roms, config); |
23 |
} |
24 |
else |
25 |
{ |
26 |
this.Roms = RomFavorite.GetRoms(); |
27 |
gLog.Debug.WriteLine("Found {0} Roms", this.Roms.Count); |
28 |
} |
29 |
} |
30 |
public List<IRomConfig> Roms { get; private set; } |
31 |
private List<IRomConfigPair> GetRoms() |
32 |
{ |
33 |
if (Config.HasExternalConfigs) { return GetRomsEX(); } |
34 |
string path = Config.EmuRomPath; string searchPattern = Config.Extenstions; |
35 |
gLog.Debug.WriteLine("Searching for Roms in Folder: {0}", path); |
36 |
Predicate<string> rom_predicate = new Predicate<string>(delegate(string t) |
37 |
{ |
38 |
string search_exts = searchPattern.Replace("*", ""); |
39 |
List<string> exts = new List<string>(search_exts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); |
40 |
string ext = Path.GetExtension(t).ToLower(); |
41 |
return exts.Contains(ext) && t.ToLower().EndsWith(ext); |
42 |
}); |
43 |
List<string> roms = (searchPattern == "*.*") |
44 |
? new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) |
45 |
: new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => rom_predicate(s))); |
46 |
|
47 |
#region LIST_ADDED_ROMS |
48 |
#if LIST_ADDED_ROMS |
49 |
foreach (string rom in roms) |
50 |
{ |
51 |
FileInfo fi = new FileInfo(rom); |
52 |
logger.WriteLine("\tAdding: {0}", fi.Name); |
53 |
} |
54 |
#endif |
55 |
List<IRomConfigPair> rompairlist = new List<IRomConfigPair>(); |
56 |
foreach (string rom in roms) |
57 |
{ |
58 |
rompairlist.Add(new RomConfigPair(rom, Config)); |
59 |
} |
60 |
#endregion |
61 |
return rompairlist; |
62 |
} |
63 |
|
64 |
private List<IRomConfigPair> GetRomsEX() |
65 |
{ |
66 |
List<IEmuConfig> configs = Config.ExternalConfigs; |
67 |
List<IRomConfigPair> roms = new List<IRomConfigPair>(); |
68 |
|
69 |
|
70 |
foreach (IEmuConfig config in configs) |
71 |
{ |
72 |
var reconfig = FixExternalConfig(Config, config); |
73 |
roms.Add(new RomConfigPair(reconfig.GameExe, reconfig)); |
74 |
} |
75 |
|
76 |
return roms; |
77 |
} |
78 |
private IEmuConfig FixExternalConfig(IEmuConfig parent_config, IEmuConfig child_config) |
79 |
{ |
80 |
string PlatformNameShort = child_config.PlatformNameShort.Contains("Unknown") ? parent_config.PlatformNameShort != "" ? parent_config.PlatformNameShort : "" : child_config.PlatformNameShort; |
81 |
string PlatformNameLong = child_config.PlatformNameLong.Contains("Unknown") ? parent_config.PlatformNameLong != "" ? parent_config.PlatformNameLong : "" : child_config.PlatformNameLong; |
82 |
string PlatformImage = ""; |
83 |
string Extenstions = child_config.Extenstions == "" ? parent_config.Extenstions != "" ? parent_config.Extenstions : "" : child_config.Extenstions; |
84 |
string EmuPath = child_config.EmuPath == "" ? parent_config.EmuPath != "" ? parent_config.EmuPath : "" : child_config.EmuPath; |
85 |
string EmuOptions = child_config.EmuOptions == "" ? parent_config.EmuOptions != "" ? parent_config.EmuOptions : "" : child_config.EmuOptions; |
86 |
|
87 |
|
88 |
|
89 |
EmuXPortal.Api.EmuConfigLoader.EmuConfig c = new EmuConfigLoader.EmuConfig(PlatformNameShort, PlatformNameLong, PlatformImage,Extenstions, EmuPath, EmuOptions); |
90 |
c.EmuRomPath = string.IsNullOrEmpty(child_config.EmuRomPath) ? string.IsNullOrEmpty(parent_config.EmuRomPath) ? "" : parent_config.EmuRomPath : child_config.EmuRomPath; |
91 |
|
92 |
|
93 |
c.GameExe = child_config.GameExe; |
94 |
c.GameExeArgs = child_config.GameExeArgs; |
95 |
c.GameImage = child_config.GameImage; |
96 |
c.GameTitle = child_config.GameTitle; |
97 |
c.ConfigPath = child_config.ConfigPath; |
98 |
c.GameImage = child_config.GameImage; |
99 |
return c; |
100 |
} |
101 |
private List<IRomConfig> GenerateRomConfig(List<IRomConfigPair> roms, IEmuConfig config) |
102 |
{ |
103 |
List<IRomConfig> romconfigs = new List<IRomConfig>(); |
104 |
gLog.Debug.WriteLine("Generating Rom Configs"); |
105 |
foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(rom.RomFile, rom.RomConfig)); } |
106 |
return romconfigs; |
107 |
} |
108 |
|
109 |
private interface IRomConfigPair |
110 |
{ |
111 |
string RomFile { get; } |
112 |
IEmuConfig RomConfig { get; } |
113 |
} |
114 |
private class RomConfigPair : IRomConfigPair |
115 |
{ |
116 |
public RomConfigPair() : this("", EmuConfigLoader.Empty) { } |
117 |
public RomConfigPair(string romFile, IEmuConfig romConfig) { RomFile = romFile; RomConfig = romConfig; } |
118 |
#region IRomConfigPair members |
119 |
public string RomFile { get; private set; } |
120 |
public IEmuConfig RomConfig { get; private set; } |
121 |
#endregion |
122 |
} |
123 |
|
124 |
public void Dispose() |
125 |
{ |
126 |
Roms.ForEach(x => x.Dispose()); |
127 |
Roms.Clear(); |
128 |
Roms = null; |
129 |
} |
130 |
} |
131 |
} |