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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 178 - (show annotations) (download)
Mon Aug 4 07:00:27 2014 UTC (6 years, 1 month ago) by william
File size: 11285 byte(s)

1 using EmuXPortal.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(logger log)
74 {
75 List<IRomConfig> roms = new List<IRomConfig>();
76 lst_favorites.ForEach(s => roms.Add(s.RomConfig));
77 return roms;
78 }
79
80 public static IRomFavorite GetFavoriteInfoFromRom(IRomConfig config)
81 {
82 foreach (var rom in lst_favorites)
83 {
84 if (rom.RomConfig.RomFile == config.RomFile)
85 {
86 return rom;
87 }
88 }
89 throw new InvalidOperationException(string.Format("Could not find rom: [{0}]", config.ToString()));
90 }
91 public static string GetRomTitleFromFavorite(IRomFavorite favorite)
92 {
93 return string.Format(favorite.ToString());
94 }
95 public static string GetRomTitleFromConfig(IRomConfig config) { return GetRomTitleFromFavorite(GetFavoriteInfoFromRom(config)); }
96 public static List<IRomFavorite> GetFavorites()
97 {
98 List<IRomFavorite> favorites = new List<IRomFavorite>();
99 foreach (var rom in lst_favorites)
100 {
101 favorites.Add(rom);
102 }
103 return favorites;
104 }
105
106 public static bool IsFavorite(logger log,IRomConfig rom)
107 {
108 foreach (var c_rom in lst_favorites)
109 {
110 if (c_rom.RomConfig.RomFile == rom.RomFile)
111 {
112 return true;
113 }
114 }
115 return false;
116 }
117 public static bool AddFavorite(logger log, IRomConfig rom)
118 {
119 if (IsFavorite(log,rom))
120 {
121 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
122 return false;
123 }
124 lst_favorites.Add(new RomFavorite(rom));
125 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
126 return true;
127 }
128 public static bool RemoveFavorite(logger log, IRomConfig rom)
129 {
130 if (!IsFavorite(log, rom))
131 {
132 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
133 return false;
134 }
135 //lst_favorites.Remove(new RomFavorite(rom));
136
137 IRomFavorite f_rom = new RomFavorite(rom);
138 List<int> indices = new List<int>();
139 for (int i = 0; i < lst_favorites.Count; i++)
140 {
141 var k = lst_favorites[i];
142 if (k.RomConfig.RomFile == rom.RomFile)
143 {
144 indices.Add(lst_favorites.IndexOf(k));
145 }
146 }
147 foreach (var index in indices)
148 {
149 lst_favorites.RemoveAt(index);
150 }
151
152 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
153 return true;
154 }
155 public static bool UpdateFavorites(logger log)
156 {
157 bool ret = SaveFavorites(log);
158 if (!ret)
159 {
160 log.WriteLine("Failed to update favorites (failed to save)");
161 }
162 else
163 {
164 ret = Config.ConfigLoader.LoadFavorites(log);
165 if (!ret)
166 {
167 log.WriteLine("Failed to update favorites (failed to load)");
168 }
169 else
170 {
171 log.WriteLine("Updated favorites");
172 }
173 }
174 return ret;
175 }
176
177 internal static bool LoadFavorites(logger log, string ini)
178 {
179 try
180 {
181 lst_favorites.Clear();
182 log.WriteLine("Loading Favorites: {0}", ini);
183 FileInfo fi = new FileInfo(ini);
184 if (!fi.Exists)
185 {
186 log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
187 return false;
188 }
189 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
190 {
191 using (XmlReader reader = XmlReader.Create(fs))
192 {
193 reader.ReadToDescendant(FAVORITES);
194 while (reader.Read())
195 {
196 if (reader.HasAttributes && reader.Name == FAVORITE)
197 {
198 int count = reader.AttributeCount;
199 string config = reader.GetAttribute(EMU_CONFIG);
200 string rom = reader.GetAttribute(ROM_FILE);
201 string added = reader.GetAttribute(DATE_ADDED);
202 if (added == null) { added = DateTime.Now.ToString(); }
203 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
204 var emu_rom = RomLoader.Load(log, rom, emu_config);
205 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
206 }
207 }
208 }
209 }
210 log.WriteLine("Loaded Favorites: {0}", ini);
211 SaveFavorites(log);
212 return true;
213
214 }
215 catch (Exception ex)
216 {
217 log.WriteLine("\tFailed to Load Favorites: {0}", ini);
218 Console.WriteLine(ex.ToString());
219 log.WriteLine("Error: {0}", ex.ToString());
220 return false;
221 }
222 }
223 internal static bool SaveFavorites(logger log)
224 {
225 // clean-up any duplicates
226 lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
227 lst_favorites = lst_favorites.OrderBy(s => s.RomConfig.Config.PlatformNameShort).ThenBy(s => s.DateAdded).Reverse().ToList();
228 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
229 try
230 {
231 log.WriteLine("Saving Favorites: {0}", ini);
232 FileInfo fi = new FileInfo(ini);
233 if (fi.Exists)
234 {
235 log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
236 fi.Delete();
237 }
238 else
239 {
240 log.WriteLine("Favorites file does not exist - creating: {0}", ini);
241 }
242 using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
243 {
244 XmlWriterSettings settings = new XmlWriterSettings();
245 using (XmlWriter writer = XmlWriter.Create(fs, settings))
246 {
247 writer.WriteRaw(System.Environment.NewLine);
248 writer.WriteStartElement(FAVORITES);
249
250 foreach (var rom in lst_favorites)
251 {
252 // config file
253 string rom_config_path = rom.RomConfig.Config.ConfigPath;
254 string rom_file = rom.RomConfig.RomFile;
255 string date = rom.DateAdded.ToString();
256
257 writer.WriteRaw(System.Environment.NewLine);
258
259 writer.WriteStartElement(FAVORITE);
260
261 writer.WriteStartAttribute(EMU_CONFIG);
262 writer.WriteString(rom_config_path);
263 writer.WriteEndAttribute();
264
265 writer.WriteStartAttribute(ROM_FILE);
266 writer.WriteString(rom_file);
267 writer.WriteEndAttribute();
268
269
270 writer.WriteStartAttribute(DATE_ADDED);
271 writer.WriteString(date);
272 writer.WriteEndAttribute();
273
274 writer.WriteEndElement();
275
276 }
277 writer.WriteRaw(System.Environment.NewLine);
278 writer.WriteFullEndElement();
279 }
280 }
281
282 log.WriteLine("Saved Favorites: {0}", ini);
283 return true;
284
285 }
286 catch (Exception ex)
287 {
288 log.WriteLine("\tFailed to Save Favorites: {0}", ini);
289 Console.WriteLine(ex.ToString());
290 log.WriteLine("Error: {0}", ex.ToString());
291 return false;
292 }
293 }
294 #endregion
295 }
296 }

  ViewVC Help
Powered by ViewVC 1.1.22