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 EmuXPortal.Logging; |
7 |
using System.IO; |
8 |
using System.Diagnostics; |
9 |
|
10 |
namespace EmuXPortal.Api |
11 |
{ |
12 |
public class RomParser |
13 |
{ |
14 |
private IEmuConfig Config { get; set; } |
15 |
public RomParser(IEmuConfig config) |
16 |
{ |
17 |
this.Config = config; |
18 |
List<IRomConfigPair> roms = GetRoms(); |
19 |
logger.WriteLine("Found {0} Roms", roms.Count); |
20 |
this.Roms = GenerateRomConfig(roms,config); |
21 |
} |
22 |
public List<IRomConfig> Roms { get; private set; } |
23 |
private List<IRomConfigPair> GetRoms() |
24 |
{ |
25 |
if (Config.HasExternalConfigs) { return GetRomsEX(); } |
26 |
string path = Config.EmuRomPath; string searchPattern = Config.Extenstions; |
27 |
logger.WriteLine("Searching for Roms in Folder: {0}", path); |
28 |
List<string> roms = (searchPattern == "*.*") |
29 |
? new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) |
30 |
: new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => searchPattern.Contains(Path.GetExtension(s).ToLower()))); |
31 |
#region LIST_ADDED_ROMS |
32 |
#if LIST_ADDED_ROMS |
33 |
foreach (string rom in roms) |
34 |
{ |
35 |
FileInfo fi = new FileInfo(rom); |
36 |
logger.WriteLine("\tAdding: {0}", fi.Name); |
37 |
} |
38 |
#endif |
39 |
List<IRomConfigPair> rompairlist = new List<IRomConfigPair>(); |
40 |
foreach (string rom in roms) |
41 |
{ |
42 |
rompairlist.Add(new RomConfigPair(rom, Config)); |
43 |
} |
44 |
#endregion |
45 |
return rompairlist; |
46 |
} |
47 |
|
48 |
private List<IRomConfigPair> GetRomsEX() |
49 |
{ |
50 |
List<IEmuConfig> configs = Config.ExternalConfigs; |
51 |
List<IRomConfigPair> roms = new List<IRomConfigPair>(); |
52 |
|
53 |
|
54 |
foreach (IEmuConfig config in configs) { roms.Add(new RomConfigPair(config.GameExe, config)); } |
55 |
|
56 |
return roms; |
57 |
} |
58 |
|
59 |
private List<IRomConfig> GenerateRomConfig(List<IRomConfigPair> roms, IEmuConfig config) |
60 |
{ |
61 |
List<IRomConfig> romconfigs = new List<IRomConfig>(); |
62 |
logger.WriteLine("Generating Rom Configs"); |
63 |
foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(rom.RomFile, rom.RomConfig)); } |
64 |
return romconfigs; |
65 |
} |
66 |
|
67 |
private interface IRomConfigPair |
68 |
{ |
69 |
string RomFile { get; } |
70 |
IEmuConfig RomConfig { get; } |
71 |
} |
72 |
private class RomConfigPair : IRomConfigPair |
73 |
{ |
74 |
public RomConfigPair() : this("", EmuConfigLoader.Empty) { } |
75 |
public RomConfigPair(string romFile, IEmuConfig romConfig) { RomFile = romFile; RomConfig = romConfig; } |
76 |
#region IRomConfigPair members |
77 |
public string RomFile { get; private set; } |
78 |
public IEmuConfig RomConfig { get; private set; } |
79 |
#endregion |
80 |
} |
81 |
} |
82 |
} |