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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 112 - (hide annotations) (download)
Thu Aug 23 04:13:19 2012 UTC (8 years, 2 months ago) by william
File size: 3863 byte(s)
OPTIMIZE LOGGING

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 112 List<IRomConfigPair> roms = GetRoms(log);
19     log.WriteLine("Found {0} Roms", roms.Count);
20     this.Roms = GenerateRomConfig(log, roms,config);
21 william 18 }
22     public List<IRomConfig> Roms { get; private set; }
23 william 112 private List<IRomConfigPair> GetRoms(logger log)
24 william 18 {
25 william 85 if (Config.HasExternalConfigs) { return GetRomsEX(); }
26     string path = Config.EmuRomPath; string searchPattern = Config.Extenstions;
27 william 112 log.WriteLine("Searching for Roms in Folder: {0}", path);
28 william 107 Predicate<string> rom_predicate = new Predicate<string>(delegate(string t)
29     {
30     string search_exts = searchPattern.Replace("*", "");
31     List<string> exts = new List<string>(search_exts.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
32     string ext = Path.GetExtension(t).ToLower();
33     return exts.Contains(ext) && t.ToLower().EndsWith(ext);
34     });
35     List<string> roms = (searchPattern == "*.*")
36     ? new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
37     : new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => rom_predicate(s)));
38    
39 william 38 #region LIST_ADDED_ROMS
40     #if LIST_ADDED_ROMS
41     foreach (string rom in roms)
42     {
43     FileInfo fi = new FileInfo(rom);
44     logger.WriteLine("\tAdding: {0}", fi.Name);
45     }
46     #endif
47 william 85 List<IRomConfigPair> rompairlist = new List<IRomConfigPair>();
48     foreach (string rom in roms)
49     {
50     rompairlist.Add(new RomConfigPair(rom, Config));
51     }
52 william 38 #endregion
53 william 85 return rompairlist;
54     }
55    
56     private List<IRomConfigPair> GetRomsEX()
57     {
58     List<IEmuConfig> configs = Config.ExternalConfigs;
59     List<IRomConfigPair> roms = new List<IRomConfigPair>();
60    
61    
62     foreach (IEmuConfig config in configs) { roms.Add(new RomConfigPair(config.GameExe, config)); }
63    
64 william 18 return roms;
65     }
66 william 85
67 william 112 private List<IRomConfig> GenerateRomConfig(logger log, List<IRomConfigPair> roms, IEmuConfig config)
68 william 18 {
69     List<IRomConfig> romconfigs = new List<IRomConfig>();
70 william 112 log.WriteLine("Generating Rom Configs");
71     foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(log, rom.RomFile, rom.RomConfig)); }
72 william 18 return romconfigs;
73     }
74 william 85
75     private interface IRomConfigPair
76     {
77     string RomFile { get; }
78     IEmuConfig RomConfig { get; }
79     }
80     private class RomConfigPair : IRomConfigPair
81     {
82     public RomConfigPair() : this("", EmuConfigLoader.Empty) { }
83     public RomConfigPair(string romFile, IEmuConfig romConfig) { RomFile = romFile; RomConfig = romConfig; }
84     #region IRomConfigPair members
85     public string RomFile { get; private set; }
86     public IEmuConfig RomConfig { get; private set; }
87     #endregion
88     }
89 william 105
90     public void Dispose()
91     {
92     Roms.ForEach(x => x.Dispose());
93     Roms.Clear();
94     Roms = null;
95     }
96 william 18 }
97     }

  ViewVC Help
Powered by ViewVC 1.1.22