very crappy RecentFiles save/load to config

This commit is contained in:
beirich 2011-01-18 03:40:53 +00:00
parent 1f192a6530
commit ab8b3972c9
5 changed files with 49 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{DD448B37-BA3F-4544-9754-5406E8094723}</ProjectGuid>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BizHawk.MultiClient</RootNamespace>
<AssemblyName>BizHawk.MultiClient</AssemblyName>
@ -81,9 +81,11 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="NameStateForm.resx">
<DependentUpon>NameStateForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>

View File

@ -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";

View File

@ -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<T>(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.
}

View File

@ -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();

View File

@ -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<string> recentlist; //List of recent files
public RecentFiles() : this(8) {}
public RecentFiles(int max)
{
recentlist = new List<string>();
@ -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("\"", ""));
}
}
}