ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/trunk/EmuXPortal/Api/RomFavorite.cs
Revision: 238
Committed: Tue Aug 5 04:50:39 2014 UTC (9 years, 4 months ago) by william
File size: 12058 byte(s)
Log Message:
+ use Enterprise.Logging instead of EmuXPortal.Logging

File Contents

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