/[EmuXPortal]/branches/mono/EmuXPortal.Api/Api/PlatformParser.cs
ViewVC logotype

Contents of /branches/mono/EmuXPortal.Api/Api/PlatformParser.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 396 - (show annotations) (download)
Sun Jun 23 23:32:26 2019 UTC (7 months ago) by william
File size: 5338 byte(s)
use HashSet instead of List
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Drawing;
7 using Enterprise.Logging;
8 using Enterprise.CrossPlatform;
9 using System.Diagnostics;
10
11 namespace EmuXPortal.Api
12 {
13 public class FavoritePlatform : IEmuConfig
14 {
15 #region IEmuConfig members
16 public bool IsFavorites { get { return true; } }
17 public string PlatformPath { get { return ""; } }
18 public string ConfigPath { get { return ""; } }
19 public string ConfigFile { get { return ""; } }
20 public string PlatformNameShort { get { return "Favorites"; } }
21 public string PlatformNameLong { get { return "Favorites"; } }
22 public string GetFullPlatformImage () { return ""; }
23 public String PlatformImage { get { return ""; } }
24 public string Extenstions { get { return ""; } }
25 public string EmuBinaryPath { get { return ""; } }
26 public string EmuOptions { get { return ""; } }
27 public string EmuRomPath { get { return ""; } set { } }
28
29 public bool isCachedLocally { get { return false; }}
30 public override string ToString()
31 {
32 return string.Format("{0}", PlatformNameShort);
33 }
34
35 public bool HasExternalConfigs { get { return false; } }
36 public HashSet<IEmuConfig> ExternalConfigs { get { return new HashSet<IEmuConfig>(); } }
37
38
39 public string GameTitle { get { return ""; } }
40 public string GameImage { get { return ""; } }
41 public string GameExe { get { return ""; } }
42 public string GameExeArgs { get { return ""; } }
43
44 public void RefreshConfig() { }
45 public int CompareTo(IEmuConfig obj)
46 {
47 return this.PlatformNameLong.CompareTo(obj.PlatformNameLong);
48 }
49 public void Dispose()
50 {
51 }
52 #endregion
53 }
54
55 public class PlatformParser : ParserEventSource, IDisposable
56 {
57 private IPlatformCache cache;
58 private const string EMU_IGNORE_FILE = "emu_ignore.txt"; // if this file is present, that folder is ignored
59
60 public PlatformParser (IPlatformCache cache, string localrompath, string rompath, ProgressEvent event_action) : base (event_action)
61 {
62 this.cache = cache;
63 if (Directory.Exists (rompath)) {
64 HashSet<string> romdirs = GetRomDirectories (rompath);
65 gLog.Info.WriteLine ("Found {0} EMU Folders", romdirs.Count);
66 this.Platforms = new HashSet<IEmuConfig> ();
67 this.Platforms.Add (new FavoritePlatform ());
68 var emu_platforms = GetEMUConfigFiles (romdirs);
69 foreach (var c in emu_platforms) {
70 this.Platforms.Add (c);
71 }
72 } else {
73 this.Platforms = new HashSet<IEmuConfig> ();
74 HashSet<IEmuConfig> emu_platforms = new HashSet<IEmuConfig>();
75 var cached_platforms = cache.GetCachedPlatforms ();
76 this.Platforms.Add (new FavoritePlatform ());
77 foreach (var cached_platform in cached_platforms) {
78 // load the platform from cache
79 emu_platforms.Add(cache.GetCachedPlatformAsEmuConfig(cached_platform.PlatformPath));
80 }
81 foreach (var c in emu_platforms) {
82 this.Platforms.Add (c);
83 }
84 }
85 this.Platforms = new HashSet<IEmuConfig> (this.Platforms.OrderBy (s => s.PlatformNameLong));
86 }
87
88 public HashSet<IEmuConfig> Platforms { get; private set; }
89
90 private HashSet<string> GetRomDirectories(string path)
91 {
92 //gLog.Info.WriteLine("Searching for EMU Folders in: {0}", OSInfo.FormatPath (path));
93 HashSet<string> dirs = new HashSet<string>();
94 foreach (string dir in Directory.GetDirectories(OSInfo.FormatPath (path)))
95 {
96 DirectoryInfo di = null;
97 try {
98 bool ignore = false;
99 di = new DirectoryInfo (dir);
100 if(!di.Name.StartsWith("."))
101 {
102 foreach (string file in Directory.GetFiles(di.FullName))
103 {
104 FileInfo fi = new FileInfo(file);
105 if (fi.Name.ToLower() == EMU_IGNORE_FILE)
106 {
107 gLog.Warn.WriteLine("\tIgnoreing EMU Folder: {0}", dir);
108 ignore = true;
109 break;
110 }
111 }
112 if (!ignore)
113 {
114 //gLog.Info.WriteLine("\tAdding EMU Folder: {0}", dir);
115 dirs.Add(dir);
116 }
117 }
118 }
119 finally {
120 di = null;
121 }
122 }
123 return dirs;
124 }
125
126 private HashSet<IEmuConfig> GetEMUConfigFiles (HashSet<string> romdirs)
127 {
128 HashSet<IEmuConfig> emuconfigs = new HashSet<IEmuConfig> ();
129 //gLog.Info.WriteLine("Reading EMU Config files");
130 int total_count = romdirs.Count;
131 int count = 0;
132
133 string message = string.Format ("Please Wait Loading {0} Rom Platforms...", total_count);
134 foreach (string dir in romdirs) {
135 IEmuConfig emuconfig = null;
136 if (cache.isPlatformPathCached (dir)) {
137 emuconfig = cache.GetCachedPlatformAsEmuConfig (dir);
138 } else {
139 emuconfig = EmuConfigLoader.Load (dir);
140 }
141 this.OnProgressEvent(this,new ProgressEventArgs(count,total_count, emuconfig, message));
142 emuconfigs.Add(emuconfig);
143 count++;
144 }
145 //gLog.Info.WriteLine("Finished Reading EMU Config files");
146 return emuconfigs;
147 }
148
149
150 public override void Dispose()
151 {
152 /* FIXME: HASHSET FOREACH */
153 //Platforms.ForEach(x => x.Dispose());
154 Platforms.Clear();
155 Platforms = null;
156 }
157 }
158 }

  ViewVC Help
Powered by ViewVC 1.1.22