/[EmuXPortal]/trunk/EmuXPortal/Api/RomParser.cs
ViewVC logotype

Annotation of /trunk/EmuXPortal/Api/RomParser.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (hide annotations) (download)
Mon Aug 4 06:31:48 2014 UTC (6 years, 3 months ago) by william
File size: 4119 byte(s)

1 william 38 //#define LIST_ADDED_ROMS // when defined will log all found rom files
2     using System;
3 william 18 using System.Collections.Generic;
4     using System.Linq;
5     using System.Text;
6     using EmuXPortal.Logging;
7     using System.IO;
8 william 38 using System.Diagnostics;
9 william 18
10     namespace EmuXPortal.Api
11     {
12 william 105 public class RomParser :IDisposable
13 william 18 {
14 william 85 private IEmuConfig Config { get; set; }
15 william 112 public RomParser(logger log, IEmuConfig config)
16 william 18 {
17 william 85 this.Config = config;
18 william 175 if (!this.Config.IsFavorites)
19     {
20     List<IRomConfigPair> roms = GetRoms(log);
21     log.WriteLine("Found {0} Roms", roms.Count);
22     this.Roms = GenerateRomConfig(log, roms, config);
23     }
24     else
25     {
26     this.Roms = RomFavorite.GetRoms(log);
27     log.WriteLine("Found {0} Roms", this.Roms.Count);
28     }
29 william 18 }
30     public List<IRomConfig> Roms { get; private set; }
31 william 112 private List<IRomConfigPair> GetRoms(logger log)
32 william 18 {
33 william 85 if (Config.HasExternalConfigs) { return GetRomsEX(); }
34     string path = Config.EmuRomPath; string searchPattern = Config.Extenstions;
35 william 112 log.WriteLine("Searching for Roms in Folder: {0}", path);
36 william 107 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 william 38 #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 william 85 List<IRomConfigPair> rompairlist = new List<IRomConfigPair>();
56     foreach (string rom in roms)
57     {
58     rompairlist.Add(new RomConfigPair(rom, Config));
59     }
60 william 38 #endregion
61 william 85 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) { roms.Add(new RomConfigPair(config.GameExe, config)); }
71    
72 william 18 return roms;
73     }
74 william 85
75 william 112 private List<IRomConfig> GenerateRomConfig(logger log, List<IRomConfigPair> roms, IEmuConfig config)
76 william 18 {
77     List<IRomConfig> romconfigs = new List<IRomConfig>();
78 william 112 log.WriteLine("Generating Rom Configs");
79     foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(log, rom.RomFile, rom.RomConfig)); }
80 william 18 return romconfigs;
81     }
82 william 85
83     private interface IRomConfigPair
84     {
85     string RomFile { get; }
86     IEmuConfig RomConfig { get; }
87     }
88     private class RomConfigPair : IRomConfigPair
89     {
90     public RomConfigPair() : this("", EmuConfigLoader.Empty) { }
91     public RomConfigPair(string romFile, IEmuConfig romConfig) { RomFile = romFile; RomConfig = romConfig; }
92     #region IRomConfigPair members
93     public string RomFile { get; private set; }
94     public IEmuConfig RomConfig { get; private set; }
95     #endregion
96     }
97 william 105
98     public void Dispose()
99     {
100     Roms.ForEach(x => x.Dispose());
101     Roms.Clear();
102     Roms = null;
103     }
104 william 18 }
105     }

  ViewVC Help
Powered by ViewVC 1.1.22