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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 105 - (hide annotations) (download)
Wed Aug 22 02:36:47 2012 UTC (8 years, 2 months ago) by william
File size: 3345 byte(s)
**** memory optimization (bulk WIP)

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 29 public RomParser(IEmuConfig config)
16 william 18 {
17 william 85 this.Config = config;
18     List<IRomConfigPair> roms = GetRoms();
19 william 18 logger.WriteLine("Found {0} Roms", roms.Count);
20 william 29 this.Roms = GenerateRomConfig(roms,config);
21 william 18 }
22     public List<IRomConfig> Roms { get; private set; }
23 william 85 private List<IRomConfigPair> GetRoms()
24 william 18 {
25 william 85 if (Config.HasExternalConfigs) { return GetRomsEX(); }
26     string path = Config.EmuRomPath; string searchPattern = Config.Extenstions;
27 william 38 logger.WriteLine("Searching for Roms in Folder: {0}", path);
28     List<string> roms = (searchPattern == "*.*")
29     ? new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
30 william 26 : new List<string>(Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => searchPattern.Contains(Path.GetExtension(s).ToLower())));
31 william 38 #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 william 85 List<IRomConfigPair> rompairlist = new List<IRomConfigPair>();
40     foreach (string rom in roms)
41     {
42     rompairlist.Add(new RomConfigPair(rom, Config));
43     }
44 william 38 #endregion
45 william 85 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 william 18 return roms;
57     }
58 william 85
59     private List<IRomConfig> GenerateRomConfig(List<IRomConfigPair> roms, IEmuConfig config)
60 william 18 {
61     List<IRomConfig> romconfigs = new List<IRomConfig>();
62     logger.WriteLine("Generating Rom Configs");
63 william 85 foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(rom.RomFile, rom.RomConfig)); }
64 william 18 return romconfigs;
65     }
66 william 85
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 william 105
82     public void Dispose()
83     {
84     Roms.ForEach(x => x.Dispose());
85     Roms.Clear();
86     Roms = null;
87     }
88 william 18 }
89     }

  ViewVC Help
Powered by ViewVC 1.1.22