From ab8b3972c93b151914203c1a5624bd72ef3a3bdb Mon Sep 17 00:00:00 2001 From: beirich Date: Tue, 18 Jan 2011 03:40:53 +0000 Subject: [PATCH] very crappy RecentFiles save/load to config --- .../BizHawk.MultiClient.csproj | 4 ++- BizHawk.MultiClient/Config.cs | 1 + BizHawk.MultiClient/ConfigService.cs | 15 ++++++++++ BizHawk.MultiClient/MainForm.cs | 14 ++++------ BizHawk.MultiClient/RecentFiles.cs | 28 ++++++++++++++++--- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/BizHawk.MultiClient/BizHawk.MultiClient.csproj b/BizHawk.MultiClient/BizHawk.MultiClient.csproj index 73361a7f8d..519058259f 100644 --- a/BizHawk.MultiClient/BizHawk.MultiClient.csproj +++ b/BizHawk.MultiClient/BizHawk.MultiClient.csproj @@ -6,7 +6,7 @@ 9.0.21022 2.0 {DD448B37-BA3F-4544-9754-5406E8094723} - WinExe + Exe Properties BizHawk.MultiClient BizHawk.MultiClient @@ -81,9 +81,11 @@ MainForm.cs + Designer NameStateForm.cs + Designer ResXFileCodeGenerator diff --git a/BizHawk.MultiClient/Config.cs b/BizHawk.MultiClient/Config.cs index f2d06a0ae7..f40c28eb11 100644 --- a/BizHawk.MultiClient/Config.cs +++ b/BizHawk.MultiClient/Config.cs @@ -6,6 +6,7 @@ public bool SoundEnabled = true; public string LastRomPath = "."; public bool AutoLoadMostRecentRom = false; //TODO: eventually make a class or struct for all the auto-loads, which will include recent roms, movies, etc, as well as autoloading any modeless dialog + public RecentFiles RecentRoms = new RecentFiles(8); // Client Hotkey Bindings public string HardResetBinding = "LeftShift+Tab"; diff --git a/BizHawk.MultiClient/ConfigService.cs b/BizHawk.MultiClient/ConfigService.cs index 0c3f23f3a1..9743a18b91 100644 --- a/BizHawk.MultiClient/ConfigService.cs +++ b/BizHawk.MultiClient/ConfigService.cs @@ -3,6 +3,11 @@ using System.IO; namespace BizHawk.MultiClient { + public interface IConfigSerializable + { + void Deserialize(string str); + } + public static class ConfigService { public static T Load(string filepath) where T : new() @@ -50,6 +55,16 @@ namespace BizHawk.MultiClient field.SetValue(config, bool.Parse(value)); else if (fieldType == typeof(char)) field.SetValue(config, char.Parse(value)); + else + { + var iface = fieldType.GetInterface("IConfigSerializable"); + if (iface != null) + { + IConfigSerializable i = (IConfigSerializable) Activator.CreateInstance(fieldType); + i.Deserialize(value); + field.SetValue(config, i); + } + } } catch { } // If anything fails to parse, just move on / use defaults, don't crash. } diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index c6f3528830..792b1245fe 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -14,8 +14,6 @@ namespace BizHawk.MultiClient { private Control renderTarget; private RetainedViewportPanel retainedPanel; - private RecentFiles RecentRoms = new RecentFiles(8); - private int SaveSlot = 0; //Saveslot sytem @@ -198,7 +196,7 @@ namespace BizHawk.MultiClient Global.Emulator.LoadGame(game); Text = game.Name; ResetRewindBuffer(); - RecentRoms.Add(file.FullName); + Global.Config.RecentRoms.Add(file.FullName); if (File.Exists(game.SaveRamPath)) LoadSaveRam(); } @@ -587,7 +585,7 @@ namespace BizHawk.MultiClient private void clearToolStripMenuItem_Click(object sender, EventArgs e) { - RecentRoms.Clear(); + Global.Config.RecentRoms.Clear(); } private void UpdateAutoLoadRecentRom() @@ -683,15 +681,15 @@ namespace BizHawk.MultiClient //repopulate it with an up to date list recentROMToolStripMenuItem.DropDownItems.Clear(); - if (RecentRoms.IsEmpty()) + if (Global.Config.RecentRoms.IsEmpty()) { recentROMToolStripMenuItem.DropDownItems.Add("None"); } else { - for (int x = 0; x < RecentRoms.Length(); x++) + for (int x = 0; x < Global.Config.RecentRoms.Length(); x++) { - string path = RecentRoms.GetRecentFileByPosition(x); + string path = Global.Config.RecentRoms.GetRecentFileByPosition(x); var item = new ToolStripMenuItem(); item.Text = path; item.Click += (o, ev) => LoadRom(path); @@ -703,7 +701,7 @@ namespace BizHawk.MultiClient var clearitem = new ToolStripMenuItem(); clearitem.Text = "&Clear"; - clearitem.Click += (o, ev) => RecentRoms.Clear(); + clearitem.Click += (o, ev) => Global.Config.RecentRoms.Clear(); recentROMToolStripMenuItem.DropDownItems.Add(clearitem); var auto = new ToolStripMenuItem(); diff --git a/BizHawk.MultiClient/RecentFiles.cs b/BizHawk.MultiClient/RecentFiles.cs index 5bd1ebdc28..ae51c5c3ec 100644 --- a/BizHawk.MultiClient/RecentFiles.cs +++ b/BizHawk.MultiClient/RecentFiles.cs @@ -1,15 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Text; namespace BizHawk.MultiClient { - class RecentFiles + public class RecentFiles : IConfigSerializable { private int MAX_RECENT_FILES; //Maximum number of files private List recentlist; //List of recent files + public RecentFiles() : this(8) {} public RecentFiles(int max) { recentlist = new List(); @@ -84,5 +83,26 @@ namespace BizHawk.MultiClient { return recentlist[position]; } + + public override string ToString() + { + var sb = new StringBuilder(); + sb.Append(MAX_RECENT_FILES); + sb.Append("@"); + foreach (string file in recentlist) + sb.AppendFormat("\"{0}\"|", file); + return sb.ToString(); + } + + public void Deserialize(string str) + { + var sections = str.Split('@'); + MAX_RECENT_FILES = int.Parse(sections[0]); + var files = sections[1].Split('|'); + recentlist.Clear(); + foreach (string file in files) + if (string.IsNullOrEmpty(file) == false) + recentlist.Add(file.Replace("\"", "")); + } } }