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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 396 - (show annotations) (download)
Sun Jun 23 23:32:26 2019 UTC (6 months, 3 weeks ago) by william
File size: 6663 byte(s)
use HashSet instead of List
1 using System;
2 using System.IO;
3 using Enterprise.Logging;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Diagnostics;
7 using System.Runtime.InteropServices;
8 using System.Text;
9 using Enterprise.CrossPlatform;
10
11 namespace EmuXPortal.Api
12 {
13 public interface IEmuRom : IComparable<IEmuRom>
14 {
15 IEmuPlatform PlatformConfig{get;}
16 string RomFile { get; }
17 string RomImage { get; }
18 string ToString();
19 bool RomExists { get; }
20
21 bool isCachedLocally{get;}
22 }
23
24 [Serializable]
25 public class EmuRom : IEmuRom
26 {
27 private static readonly string[] VALID_IMAGE_EXTENSIONS = new string[] { ".jpg", ".png", ".gif", ".bmp", ".tiff" };
28 //private static readonly HashSet<string> VALID_IMAGE_EXTENSIONS = new HashSet<string>( new string[] { ".jpg"});
29
30 public static HashSet<string> GetImagesInPath(string path) {
31 HashSet<string> images = new HashSet<string>();
32 Predicate<string> rom_predicate = new Predicate<string> (delegate(string t) {
33 string ext = Path.GetExtension (t).ToLower ();
34 return VALID_IMAGE_EXTENSIONS.Contains (ext) && t.ToLower ().EndsWith (ext);
35 });
36 var files = new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories).Where (s => rom_predicate (s)));
37
38 foreach (var file in files) {
39 if (File.Exists (file)) {
40 images.Add(file);
41 }
42 }
43 return images;
44 }
45
46 public static bool EnumerateRoms (IEmuPlatform platform_config,HashSet<string> found_images, AbortEvent abort_event, string path, string extensions, out HashSet<IEmuRom> roms) {
47 return EnumerateRoms(platform_config, found_images, new HashSet<IEmuRom>(),abort_event,path,extensions,out roms);
48 }
49 public static bool EnumerateRoms (IEmuPlatform platform_config, HashSet<string> found_images, HashSet<IEmuRom> existing_roms, AbortEvent abort_event, string path, string extensions, out HashSet<IEmuRom> roms)
50 {
51 bool result = true;
52 roms = new HashSet<IEmuRom> ();
53
54 Dictionary<string,EmuRom> dict = new Dictionary<string, EmuRom> ();
55 foreach (var t in existing_roms) {
56 if (!dict.ContainsKey (t.RomFile)) {
57 dict.Add (t.RomFile.ToLower (), (EmuRom)t);
58 }
59 }
60
61 Predicate<string> rom_predicate = new Predicate<string> (delegate(string t) {
62 string search_exts = extensions.Replace ("*", "");
63 HashSet<string> exts = new HashSet<string> (search_exts.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
64 string ext = Path.GetExtension (t).ToLower ();
65 return exts.Contains (ext) && t.ToLower ().EndsWith (ext);
66 });
67 var files = from f in (extensions == "*.*")
68 ? new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories))
69 : new HashSet<string> (Directory.GetFiles (path, "*.*", SearchOption.AllDirectories).Where (s => rom_predicate (s)))
70 select f;
71
72 HashSet<string> hash_set = new HashSet<string> ();
73 foreach (var f in files) {
74 hash_set.Add (f);
75 }
76 gLog.Debug.WriteLine ("Found {0} roms", hash_set.Count);
77
78 int index = 0;
79 int total = hash_set.Count ();
80 foreach (var file in hash_set) {
81
82 if (abort_event.Invoke ()) {
83 result = false;
84 gLog.Warn.WriteLine ("user requested abort while processing file: {0}", file);
85 break;
86 }
87
88 if (!dict.ContainsKey (file.ToLower ())) {
89 //gLog.Debug.WriteLine ("Adding new rom: {0}", file);
90 EmuRom emurom = new EmuRom (platform_config, file);
91 emurom.FindRomImage (found_images); // upd ate the image
92 dict.Add (emurom.RomFile.ToLower (), emurom);
93 } else {
94 EmuRom emurom = dict [file.ToLower ()];
95 emurom.PlatformConfig = platform_config;
96 string old_image = emurom.RomImage;
97 emurom.FindRomImage (found_images); // upd ate the image
98 string new_image = emurom.RomImage;
99 if (old_image.ToLower () != new_image.ToLower()) {
100 //gLog.Debug.WriteLine ("Updating rom image: {0}", new_image);
101 dict [file.ToLower ()] = emurom;
102 }
103 }
104
105 double progress = ((double)index / (double)total) * 100.0;
106 if (index % InternalConsts.NUMBER_ENTRIES_BEFORE_OUTPUT == 0) {
107 gLog.ReportProgress (null, new ReportProgressEventArgs ((int)progress, string.Format ("Last Cached Rom: {0}", Win32.CompactPath (file))));
108 //Application.DoEvents ();
109 }
110 index++;
111 }
112
113 foreach (var t in dict) {
114 roms.Add(t.Value);
115 }
116 roms = new HashSet<IEmuRom> (roms.OrderBy (s => s.RomFile.ToLower ()));
117 return result;
118 }
119 public EmuRom (IEmuPlatform platform_config)
120 {
121 this.RomFile = "";
122 this.RomImage = "";
123 }
124
125 public EmuRom (IEmuPlatform platform_config, string romfile)
126 {
127 this.RomFile = romfile;
128 this.RomImage = "";
129 }
130
131 public void FindRomImage (HashSet<string> found_images)
132 {
133
134 this.RomImage = ""; // reset the rom image (it will be updated later if found)
135 FileInfo fi = new FileInfo (this.RomFile);
136 string filename = fi.FullName.Replace (fi.Extension, "");
137
138 HashSet<string> rom_images = new HashSet<string> ();
139 foreach (var image in found_images) {
140 FileInfo fi2 = new FileInfo (image);
141 string image_filename = fi2.FullName.Replace (fi2.Extension, "");
142 if (filename.ToLower () == image_filename.ToLower ()) {
143 rom_images.Add (image);
144 }
145 fi2 = null;
146 }
147
148 if (rom_images.Count > 0) {
149 if (rom_images.Count > 1) {
150 gLog.Warn.WriteLine("Using first image found for rom: {0}", this.RomFile);
151 }
152 this.RomImage = rom_images.ElementAt(0);
153 }
154
155 fi = null;
156 }
157
158
159 #region IEmuRom members
160 public override string ToString() { return this.RomFile; }
161
162 private IEmuPlatform m_PlatformConfig;
163 public IEmuPlatform PlatformConfig{ get { return m_PlatformConfig; } set { m_PlatformConfig = value; }}
164
165 public bool isCachedLocally {
166 get {
167 string local_rom_path = Config.LocalRomPath;
168 string rom_path = this.PlatformConfig.EmulatorRomPath;
169 FileInfo fi = new FileInfo (this.RomFile);
170 string rom_filename = fi.Name;
171 fi = new FileInfo (local_rom_path);
172 string local_path = OSInfo.FormatPath (string.Format ("{0}/{1}/{2}", fi.Directory.FullName, this.PlatformConfig.PlatformNameShort, rom_filename));
173 fi = null;
174 bool exists = File.Exists (local_path);
175 return exists;
176 }
177 }
178 public bool RomExists { get { return this.isCachedLocally || File.Exists(this.RomFile); } }
179 private string m_RomFile;
180
181 public string RomFile { get { return m_RomFile; } set { m_RomFile = value; } }
182
183 private string m_RomImage;
184
185 public string RomImage { get { return m_RomImage; } set { m_RomImage = value; } }
186
187 #endregion
188
189 public int CompareTo (IEmuRom other)
190 {
191 return this.RomFile.ToLower().CompareTo(other.RomFile.ToLower());
192 }
193 }
194 }
195

  ViewVC Help
Powered by ViewVC 1.1.22