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