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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (show annotations) (download)
Thu Aug 23 02:10:16 2012 UTC (8 years, 2 months ago) by william
File size: 3825 byte(s)
+ ensure we only read files that have the correct rom file pattern: ie .iso, .rom,.bin, etc)

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 :IDisposable
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 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 #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 List<IRomConfigPair> rompairlist = new List<IRomConfigPair>();
48 foreach (string rom in roms)
49 {
50 rompairlist.Add(new RomConfigPair(rom, Config));
51 }
52 #endregion
53 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 return roms;
65 }
66
67 private List<IRomConfig> GenerateRomConfig(List<IRomConfigPair> roms, IEmuConfig config)
68 {
69 List<IRomConfig> romconfigs = new List<IRomConfig>();
70 logger.WriteLine("Generating Rom Configs");
71 foreach (IRomConfigPair rom in roms) { romconfigs.Add(RomLoader.Load(rom.RomFile, rom.RomConfig)); }
72 return romconfigs;
73 }
74
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
90 public void Dispose()
91 {
92 Roms.ForEach(x => x.Dispose());
93 Roms.Clear();
94 Roms = null;
95 }
96 }
97 }

  ViewVC Help
Powered by ViewVC 1.1.22