Load/Save settings from <support dir>/settings.yaml

This commit is contained in:
Paul Chote
2010-07-10 14:58:50 +12:00
parent b7eb4193d4
commit 04bb5b0a4b
4 changed files with 64 additions and 28 deletions

View File

@@ -133,6 +133,19 @@ namespace OpenRA.FileFormats
f => new MiniYaml(FormatValue(o, f)))); f => new MiniYaml(FormatValue(o, f))));
} }
public static MiniYaml SaveDifferences(object o, object from)
{
if (o.GetType() != from.GetType())
throw new InvalidOperationException("FieldLoader: can't diff objects of different types");
var fields = o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(f => FormatValue(o,f) != FormatValue(from,f));
return new MiniYaml(null, fields.ToDictionary(
f => f.Name,
f => new MiniYaml(FormatValue(o, f))));
}
public static string FormatValue(object o, FieldInfo f) public static string FormatValue(object o, FieldInfo f)
{ {
var v = f.GetValue(o); var v = f.GetValue(o);

View File

@@ -563,14 +563,17 @@ namespace OpenRA
{ {
AppDomain.CurrentDomain.AssemblyResolve += FileSystem.ResolveAssembly; AppDomain.CurrentDomain.AssemblyResolve += FileSystem.ResolveAssembly;
LoadUserSettings(settings); var defaultSupport = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
+ Path.DirectorySeparatorChar + "OpenRA";
SupportDir = settings.GetValue("SupportDir", defaultSupport);
Settings = new UserSettings(settings);
Log.LogPath = SupportDir + "Logs" + Path.DirectorySeparatorChar; Log.LogPath = SupportDir + "Logs" + Path.DirectorySeparatorChar;
Log.AddChannel("perf", "perf.log", false, false); Log.AddChannel("perf", "perf.log", false, false);
Log.AddChannel("debug", "debug.log", false, false); Log.AddChannel("debug", "debug.log", false, false);
Log.AddChannel("sync", "syncreport.log", true, true); Log.AddChannel("sync", "syncreport.log", true, true);
LobbyInfo.GlobalSettings.Mods = Settings.InitialMods; LobbyInfo.GlobalSettings.Mods = Settings.InitialMods;
// Load the default mod to access required files // Load the default mod to access required files
@@ -605,19 +608,6 @@ namespace OpenRA
} }
static void LoadUserSettings(Settings settings)
{
Settings = new UserSettings();
var settingsFile = settings.GetValue("settings", "settings.ini");
FileSystem.Mount("./");
if (FileSystem.Exists(settingsFile))
FieldLoader.Load(Settings,
new IniFile(FileSystem.Open(settingsFile)).GetSection("Settings"));
FileSystem.UnmountAll();
Settings.AddSettings(settings);
}
static bool quit; static bool quit;
internal static void Run() internal static void Run()
{ {
@@ -644,16 +634,22 @@ namespace OpenRA
Chrome.rootWidget.OpenWindow("MAINMENU_BG"); Chrome.rootWidget.OpenWindow("MAINMENU_BG");
} }
static string baseSupportDir = null;
public static string SupportDir public static string SupportDir
{ {
get { set {
var dir = Settings.SupportDir; var dir = value;
// Expand paths relative to the personal directory // Expand paths relative to the personal directory
if (dir.ElementAt(0) == '~') if (dir.ElementAt(0) == '~')
dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + dir.Substring(1); dir = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + dir.Substring(1);
return dir + Path.DirectorySeparatorChar;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
baseSupportDir = dir + Path.DirectorySeparatorChar;
} }
get {return baseSupportDir;}
} }

View File

@@ -18,15 +18,15 @@
*/ */
#endregion #endregion
using OpenRA.FileFormats;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using System;
using System.IO; using System.IO;
using System.Collections.Generic;
namespace OpenRA.GameRules namespace OpenRA.GameRules
{ {
public class UserSettings public class UserSettings
{ {
public readonly string SupportDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
+ Path.DirectorySeparatorChar + "OpenRA" + Path.DirectorySeparatorChar;
// Debug settings // Debug settings
public bool UnitDebug = false; public bool UnitDebug = false;
public bool PathDebug = true; public bool PathDebug = true;
@@ -58,11 +58,32 @@ namespace OpenRA.GameRules
public readonly bool InternetServer = true; public readonly bool InternetServer = true;
public readonly string MasterServer = "http://open-ra.org/master/"; public readonly string MasterServer = "http://open-ra.org/master/";
public void AddSettings(Settings settings) string SettingsFile;
UserSettings defaults;
public UserSettings() {}
public UserSettings(Settings args)
{ {
defaults = new UserSettings();
SettingsFile = Game.SupportDir + "settings.yaml";
if (File.Exists(SettingsFile))
{
System.Console.WriteLine("Loading settings file {0}",SettingsFile);
var yaml = MiniYaml.FromFile(SettingsFile);
FieldLoader.Load(this, yaml["Settings"]);
}
foreach (var f in this.GetType().GetFields()) foreach (var f in this.GetType().GetFields())
if (settings.Contains(f.Name)) if (args.Contains(f.Name))
OpenRA.FileFormats.FieldLoader.LoadField( this, f.Name, settings.GetValue(f.Name, "") ); OpenRA.FileFormats.FieldLoader.LoadField( this, f.Name, args.GetValue(f.Name, "") );
}
public void Save()
{
Dictionary<string, MiniYaml> root = new Dictionary<string, MiniYaml>();
root.Add("Settings", FieldSaver.SaveDifferences(this, defaults));
root.WriteToFile(SettingsFile);
} }
} }
} }

View File

@@ -15,24 +15,28 @@ namespace OpenRA.Widgets.Delegates
r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_UNITDEBUG").Checked = () => {return Game.Settings.UnitDebug;}; r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_UNITDEBUG").Checked = () => {return Game.Settings.UnitDebug;};
r.GetWidget("SETTINGS_CHECKBOX_UNITDEBUG").OnMouseDown = mi => { r.GetWidget("SETTINGS_CHECKBOX_UNITDEBUG").OnMouseDown = mi => {
Game.Settings.UnitDebug ^= true; Game.Settings.UnitDebug ^= true;
Game.Settings.Save();
return true; return true;
}; };
r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_PATHDEBUG").Checked = () => {return Game.Settings.PathDebug;}; r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_PATHDEBUG").Checked = () => {return Game.Settings.PathDebug;};
r.GetWidget("SETTINGS_CHECKBOX_PATHDEBUG").OnMouseDown = mi => { r.GetWidget("SETTINGS_CHECKBOX_PATHDEBUG").OnMouseDown = mi => {
Game.Settings.PathDebug ^= true; Game.Settings.PathDebug ^= true;
Game.Settings.Save();
return true; return true;
}; };
r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_INDEXDEBUG").Checked = () => {return Game.Settings.IndexDebug;}; r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_INDEXDEBUG").Checked = () => {return Game.Settings.IndexDebug;};
r.GetWidget("SETTINGS_CHECKBOX_INDEXDEBUG").OnMouseDown = mi => { r.GetWidget("SETTINGS_CHECKBOX_INDEXDEBUG").OnMouseDown = mi => {
Game.Settings.IndexDebug ^= true; Game.Settings.IndexDebug ^= true;
Game.Settings.Save();
return true; return true;
}; };
r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_PERFDEBUG").Checked = () => {return Game.Settings.PerfDebug;}; r.GetWidget<CheckboxWidget>("SETTINGS_CHECKBOX_PERFDEBUG").Checked = () => {return Game.Settings.PerfDebug;};
r.GetWidget("SETTINGS_CHECKBOX_PERFDEBUG").OnMouseDown = mi => { r.GetWidget("SETTINGS_CHECKBOX_PERFDEBUG").OnMouseDown = mi => {
Game.Settings.PerfDebug ^= true; Game.Settings.PerfDebug ^= true;
Game.Settings.Save();
return true; return true;
}; };
@@ -40,6 +44,7 @@ namespace OpenRA.Widgets.Delegates
r.GetWidget("SETTINGS_CHECKBOX_SYNCREPORTS").OnMouseDown = mi => r.GetWidget("SETTINGS_CHECKBOX_SYNCREPORTS").OnMouseDown = mi =>
{ {
Game.Settings.RecordSyncReports ^= true; Game.Settings.RecordSyncReports ^= true;
Game.Settings.Save();
return true; return true;
}; };
@@ -48,6 +53,7 @@ namespace OpenRA.Widgets.Delegates
{ {
Game.Settings.MusicPlayer ^= true; Game.Settings.MusicPlayer ^= true;
r.GetWidget("MUSIC_BG").Visible = Game.Settings.MusicPlayer; r.GetWidget("MUSIC_BG").Visible = Game.Settings.MusicPlayer;
Game.Settings.Save();
return true; return true;
}; };