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

Contents of /branches/mono/EmuXPortal.Api/Api/RomFavorite.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: 10555 byte(s)
use HashSet instead of List
1 using Enterprise.Logging;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Xml;
10 using System.Xml.Linq;
11
12 namespace EmuXPortal.Api {
13 public interface IRomFavorite : IComparable<IRomFavorite> {
14 IRomConfig RomConfig { get; }
15 DateTime DateAdded { get; }
16 string ToString ();
17 }
18 public class RomFavorite : IRomFavorite {
19 public RomFavorite (IRomConfig config)
20 {
21 this.RomConfig = config;
22 this.DateAdded = DateTime.Now;
23 }
24 public RomFavorite (IRomConfig config, DateTime added) : this (config)
25 {
26 this.DateAdded = added;
27 }
28 #region IFavoriteRom members
29 public IRomConfig RomConfig { get; private set; }
30 public DateTime DateAdded { get; private set; }
31
32 public override string ToString ()
33 {
34 if (RomConfig == null) {
35 return base.ToString ();
36 } else {
37 string title = RomConfig.RomTitle;
38 string platform = RomConfig.EmuConfig.PlatformNameShort;
39 return string.Format ("[{0}] - {1}", platform, title);
40 }
41 }
42 #endregion
43 #region IComparable<IRomFavorite> members
44 public int CompareTo (IRomFavorite other)
45 {
46 if (other == null || (this.RomConfig == null || other.RomConfig == null)) {
47 return 0;
48 } else {
49 return (this.RomConfig.RomTitle.CompareTo (other.RomConfig.RomTitle));
50 }
51 }
52 #endregion
53 #region constants
54 const string FAVORITES = "favorites";
55 const string FAVORITE = "favorite";
56 const string EMU_CONFIG = "config";
57 const string ROM_FILE = "rom";
58 const string DATE_ADDED = "date_added";
59 #endregion
60
61
62
63 private static HashSet<IRomFavorite> lst_favorites = new HashSet<IRomFavorite> ();
64
65 private class FavoriesComparor : IEqualityComparer<IRomFavorite> {
66 public bool Equals (IRomFavorite x, IRomFavorite y) { return x.RomConfig.RomFile.Equals (y.RomConfig.RomFile); }
67 public int GetHashCode (IRomFavorite obj) { return obj.RomConfig.RomFile.GetHashCode (); }
68 }
69 #region static members
70
71 internal static HashSet<IRomConfig> GetRoms (IPlatformCache cache)
72 {
73 HashSet<IRomConfig> roms = new HashSet<IRomConfig> ();
74 //lst_favorites.ForEach(s => roms.Add(s.RomConfig));
75
76 foreach (var rom in lst_favorites) {
77 IRomConfig rom_config = rom.RomConfig;
78 if (cache.isPlatformPathCached (rom.RomConfig.EmuConfig.PlatformPath)) {
79 var t = cache.GetCachedRom (rom.RomConfig.EmuConfig.PlatformPath, rom.RomConfig.RomFile);
80 if (t != null) {
81 rom_config = t;
82 }
83 }
84 roms.Add (rom_config);
85 }
86 return roms;
87 }
88
89 public static IRomFavorite GetFavoriteInfoFromRom (IRomConfig config)
90 {
91 foreach (var rom in lst_favorites) {
92 if (rom.RomConfig.RomFile == config.RomFile) {
93 return rom;
94 }
95 }
96 throw new InvalidOperationException (string.Format ("Could not find rom: [{0}]", config.ToString ()));
97 }
98 public static string GetRomTitleFromFavorite (IRomFavorite favorite)
99 {
100 return string.Format (favorite.ToString ());
101 }
102 public static string GetRomTitleFromConfig (IRomConfig config) { return GetRomTitleFromFavorite (GetFavoriteInfoFromRom (config)); }
103 public static HashSet<IRomFavorite> GetFavorites ()
104 {
105 HashSet<IRomFavorite> favorites = new HashSet<IRomFavorite> ();
106 foreach (var rom in lst_favorites) {
107 favorites.Add (rom);
108 }
109 return favorites;
110 }
111
112 public static bool IsFavorite (IRomConfig rom)
113 {
114 foreach (var c_rom in lst_favorites) {
115 if (c_rom.RomConfig.RomFile == rom.RomFile) {
116 return true;
117 }
118 }
119 return false;
120 }
121 public static bool AddFavorite (IRomConfig rom)
122 {
123 if (IsFavorite (rom)) {
124 gLog.Error.WriteLine ("Rom is already favorited: [{0}]", rom.RomTitle);
125 return false;
126 }
127 lst_favorites.Add (new RomFavorite (rom));
128 gLog.Info.WriteLine ("Added rom to favorites: [{0}]", rom.RomTitle);
129 return true;
130 }
131 public static bool RemoveFavorite (IRomConfig rom)
132 {
133 if (!IsFavorite (rom)) {
134 gLog.Error.WriteLine ("Rom is not favorited: [{0}]", rom.RomTitle);
135 return false;
136 }
137 HashSet<int> indices = new HashSet<int> ();
138 for (int i = 0; i < lst_favorites.Count; i++) {
139 var k = lst_favorites.ElementAt(i);
140 if (k.RomConfig.RomFile == rom.RomFile) {
141 indices.Add (i);
142 }
143 }
144 foreach (var index in indices) {
145 var i = lst_favorites.ElementAt (index);
146 lst_favorites.Remove(i);
147 }
148
149 gLog.Info.WriteLine ("Removed rom from favorites: [{0}]", rom.RomTitle);
150 return true;
151 }
152 public static bool UpdateFavorites (IPlatformCache cache)
153 {
154 bool ret = SaveFavorites ();
155 if (!ret) {
156 gLog.Error.WriteLine ("Failed to update favorites (failed to save)");
157 } else {
158 ret = Config.ConfigLoader.LoadFavorites (cache);
159 if (!ret) {
160 gLog.Error.WriteLine ("Failed to update favorites (failed to load)");
161 } else {
162 gLog.Info.WriteLine ("Updated favorites");
163 }
164 }
165 return ret;
166 }
167
168 internal static bool LoadFavorites (IPlatformCache cache, string ini)
169 {
170 try {
171 lst_favorites.Clear ();
172 // gLog.Debug.WriteLine("Loading Favorites: {0}", ini);
173 FileInfo fi = new FileInfo (ini);
174 if (!fi.Exists) {
175 //gLog.Error.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
176 return false;
177 }
178
179 XDocument xdoc = XDocument.Load (fi.FullName);
180
181 var favorites = xdoc.Root.Descendants (InternalConsts.ROM_FAVORITE);
182 foreach (var favorite in favorites) {
183 if (favorite.Name.ToString () == InternalConsts.ROM_FAVORITE) {
184 var config = favorite.Attribute (InternalConsts.ROM_CONFIG) == null ? "" : favorite.Attribute (InternalConsts.ROM_CONFIG).Value;
185 var rom = favorite.Attribute (InternalConsts.ROM_FILE) == null ? "" : favorite.Attribute (InternalConsts.ROM_FILE).Value;
186 var date_added = favorite.Attribute (InternalConsts.ROM_DATE_ADDED) == null ? "" : favorite.Attribute (InternalConsts.ROM_DATE_ADDED).Value;
187
188 string rom_path = "";
189 IRomConfig cached_rom = null;
190 bool rom_valid = true;
191 if (string.IsNullOrEmpty (config)) {
192 gLog.Warn.WriteLine ("favorite entry missing config attribute");
193 break;
194 } else {
195 FileInfo f_config = new FileInfo (config);
196 rom_path = f_config.Directory.FullName;
197 cached_rom = cache.GetCachedRom (rom_path, rom);
198 if (!f_config.Exists && cached_rom != null) {
199 gLog.Warn.WriteLine ("using cached data for rom file: {0}", cached_rom.RomFile);
200 rom_valid = true;
201 } else {
202 if (!f_config.Exists) { gLog.Warn.WriteLine ("Could not find config file: {0}", config); rom_valid = false; }
203 }
204 f_config = null;
205
206 }
207 if (string.IsNullOrEmpty (rom)) {
208 gLog.Warn.WriteLine ("favorite entry missing rom attribute");
209 } else {
210 if (cached_rom == null) {
211 FileInfo f_rom = new FileInfo (rom);
212 if (!f_rom.Exists) { gLog.Warn.WriteLine ("Could not find rom file: {0}", rom); rom_valid = false; }
213 f_rom = null;
214 } else {
215 rom_valid = true;
216 }
217 }
218 if (string.IsNullOrEmpty (date_added)) {
219 gLog.Warn.WriteLine ("favorite entry missing data_added attribute");
220 date_added = DateTime.Now.ToString ();
221 }
222
223 if (rom_valid) {
224 IEmuConfig emu_config = null;
225 IRomConfig emu_rom = null;
226 if (cached_rom == null) {
227 emu_config = EmuConfigLoader.Load (config, "");
228 emu_rom = null;
229 if (cache.isPlatformPathCached (emu_config.EmuRomPath)) {
230 emu_rom = cache.GetCachedRom (emu_config.EmuRomPath, rom);
231 } else {
232 emu_rom = RomLoader.Load (rom, "", emu_config);
233 }
234
235 lst_favorites.Add (new RomFavorite (emu_rom, DateTime.Parse (date_added)));
236 } else {
237 emu_config = cached_rom.EmuConfig;
238 emu_rom = cached_rom;
239 lst_favorites.Add (new RomFavorite (emu_rom, DateTime.Parse (date_added)));
240 }
241 } else {
242 gLog.Warn.WriteLine ("Skipping and removing favorite (not found): '{0}'", rom);
243 }
244
245 } else {
246 gLog.Warn.WriteLine ("Unexpected element '{0}' in favorites", favorite.Name);
247 }
248 }
249 lst_favorites = new HashSet<IRomFavorite> (lst_favorites.Distinct (new FavoriesComparor ()).OrderBy (s => s.RomConfig.RomTitle));
250 gLog.Info.WriteLine ("Loaded Favorites: {0}", ini);
251 SaveFavorites ();
252 return true;
253
254 } catch (Exception ex) {
255 gLog.Error.WriteLine ("\tFailed to Load Favorites: {0}", ini);
256 gLog.Verbose.Error.WriteLine (ex.ToString ());
257 return false;
258 }
259 }
260 internal static bool SaveFavorites ()
261 {
262 // clean-up any duplicates
263 HashSet<IRomFavorite> lst_f = new HashSet<IRomFavorite> (lst_favorites.Distinct (new FavoriesComparor ()).OrderBy (s => s.DateAdded));
264 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
265 try {
266 gLog.Debug.WriteLine ("Saving Favorites: {0}", ini);
267 FileInfo fi = new FileInfo (ini);
268 if (fi.Exists) {
269 gLog.Debug.WriteLine ("Favorites file already exists - overwriting: {0}", ini);
270 fi.Delete ();
271 } else {
272 gLog.Debug.WriteLine ("Favorites file does not exist - creating: {0}", ini);
273 }
274 using (FileStream fs = new FileStream (ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) {
275 XmlWriterSettings settings = new XmlWriterSettings ();
276 using (XmlWriter writer = XmlWriter.Create (fs, settings)) {
277 writer.WriteRaw (System.Environment.NewLine);
278 writer.WriteStartElement (FAVORITES);
279
280 foreach (var rom in lst_favorites) {
281 // config file
282 string rom_config_path = rom.RomConfig.EmuConfig.ConfigPath;
283 string rom_file = rom.RomConfig.RomFile;
284 string date = rom.DateAdded.ToString ();
285
286 writer.WriteRaw (System.Environment.NewLine);
287
288 writer.WriteStartElement (FAVORITE);
289
290 writer.WriteStartAttribute (EMU_CONFIG);
291 writer.WriteString (rom_config_path);
292 writer.WriteEndAttribute ();
293
294 writer.WriteStartAttribute (ROM_FILE);
295 writer.WriteString (rom_file);
296 writer.WriteEndAttribute ();
297
298
299 writer.WriteStartAttribute (DATE_ADDED);
300 writer.WriteString (date);
301 writer.WriteEndAttribute ();
302
303 writer.WriteEndElement ();
304
305 }
306 writer.WriteRaw (System.Environment.NewLine);
307 writer.WriteFullEndElement ();
308 }
309 }
310
311 gLog.Info.WriteLine ("Saved Favorites: {0}", ini);
312 return true;
313
314 } catch (Exception ex) {
315 gLog.Error.WriteLine ("\tFailed to Save Favorites: {0}", ini);
316 gLog.Verbose.Error.WriteLine (ex.ToString ());
317 return false;
318 }
319 }
320 #endregion
321 }
322 }

  ViewVC Help
Powered by ViewVC 1.1.22