ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/branches/mono/EmuXPortal/Api/RomFavorite.cs
Revision: 264
Committed: Thu Mar 17 04:23:49 2016 UTC (7 years, 2 months ago) by william
File size: 12676 byte(s)
Log Message:
update emuxportal for building under mono

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.Error.WriteLine("Rom is already favorited: [{0}]", rom.RomTitle);
141 return false;
142 }
143 lst_favorites.Add(new RomFavorite(rom));
144 gLog.Info.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.Error.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
152 return false;
153 }
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 gLog.Info.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
169 return true;
170 }
171 public static bool UpdateFavorites()
172 {
173 bool ret = SaveFavorites();
174 if (!ret)
175 {
176 gLog.Error.WriteLine("Failed to update favorites (failed to save)");
177 }
178 else
179 {
180 ret = Config.ConfigLoader.LoadFavorites();
181 if (!ret)
182 {
183 gLog.Error.WriteLine("Failed to update favorites (failed to load)");
184 }
185 else
186 {
187 gLog.Info.WriteLine("Updated favorites");
188 }
189 }
190 return ret;
191 }
192
193 internal static bool LoadFavorites(string ini)
194 {
195 try
196 {
197 lst_favorites.Clear();
198 gLog.Debug.WriteLine("Loading Favorites: {0}", ini);
199 FileInfo fi = new FileInfo(ini);
200 if (!fi.Exists)
201 {
202 gLog.Error.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
220 bool rom_valid = true;
221
222 FileInfo f_config = new FileInfo(config);
223 FileInfo f_rom = new FileInfo(rom);
224
225 if (!f_config.Exists) { gLog.Warn.WriteLine("Could not find config file: {0}", config); rom_valid = false; } f_config = null;
226 if (!f_rom.Exists) { gLog.Warn.WriteLine("Could not find rom file: {0}", rom); rom_valid = false; } f_rom = null;
227
228 if (rom_valid)
229 {
230 IEmuConfig emu_config = EmuConfigLoader.Load(config, "");
231 var emu_rom = RomLoader.Load(rom, emu_config);
232 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
233 }
234 else
235 {
236 gLog.Warn.WriteLine("Skipping and removing favorite (not found): '{0}'", rom);
237 }
238 }
239 }
240 }
241 }
242 gLog.Info.WriteLine("Loaded Favorites: {0}", ini);
243 SaveFavorites();
244 return true;
245
246 }
247 catch (Exception ex)
248 {
249 gLog.Error.WriteLine("\tFailed to Load Favorites: {0}", ini);
250 gLog.Verbose.Error.WriteLine(ex.ToString());
251 return false;
252 }
253 }
254 internal static bool SaveFavorites()
255 {
256 // clean-up any duplicates
257 lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
258 lst_favorites = lst_favorites.OrderBy(s => s.RomConfig.Config.PlatformNameShort).ThenBy(s => s.DateAdded).Reverse().ToList();
259 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
260 try
261 {
262 gLog.Debug.WriteLine("Saving Favorites: {0}", ini);
263 FileInfo fi = new FileInfo(ini);
264 if (fi.Exists)
265 {
266 gLog.Debug.WriteLine("Favorites file already exists - overwriting: {0}", ini);
267 fi.Delete();
268 }
269 else
270 {
271 gLog.Debug.WriteLine("Favorites file does not exist - creating: {0}", ini);
272 }
273 using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
274 {
275 XmlWriterSettings settings = new XmlWriterSettings();
276 using (XmlWriter writer = XmlWriter.Create(fs, settings))
277 {
278 writer.WriteRaw(System.Environment.NewLine);
279 writer.WriteStartElement(FAVORITES);
280
281 foreach (var rom in lst_favorites)
282 {
283 // config file
284 string rom_config_path = rom.RomConfig.Config.ConfigPath;
285 string rom_file = rom.RomConfig.RomFile;
286 string date = rom.DateAdded.ToString();
287
288 writer.WriteRaw(System.Environment.NewLine);
289
290 writer.WriteStartElement(FAVORITE);
291
292 writer.WriteStartAttribute(EMU_CONFIG);
293 writer.WriteString(rom_config_path);
294 writer.WriteEndAttribute();
295
296 writer.WriteStartAttribute(ROM_FILE);
297 writer.WriteString(rom_file);
298 writer.WriteEndAttribute();
299
300
301 writer.WriteStartAttribute(DATE_ADDED);
302 writer.WriteString(date);
303 writer.WriteEndAttribute();
304
305 writer.WriteEndElement();
306
307 }
308 writer.WriteRaw(System.Environment.NewLine);
309 writer.WriteFullEndElement();
310 }
311 }
312
313 gLog.Info.WriteLine("Saved Favorites: {0}", ini);
314 return true;
315
316 }
317 catch (Exception ex)
318 {
319 gLog.Error.WriteLine("\tFailed to Save Favorites: {0}", ini);
320 gLog.Verbose.Error.WriteLine(ex.ToString());
321 return false;
322 }
323 }
324 #endregion
325 }
326 }