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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 200 - (show annotations) (download)
Mon Aug 4 12:39:13 2014 UTC (6 years, 1 month ago) by william
File size: 11934 byte(s)
** fix issues with games, that have been favorited
** a favorited game, with an image, cannot be displayed more than once
** also, the platform name was not generated properly for external configs

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

  ViewVC Help
Powered by ViewVC 1.1.22