2011-02-20 06:13:26 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
|
|
|
|
|
namespace BizHawk.MultiClient
|
|
|
|
|
{
|
|
|
|
|
public static class ConfigService
|
|
|
|
|
{
|
|
|
|
|
public static T Load<T>(string filepath) where T : new()
|
|
|
|
|
{
|
|
|
|
|
T config = new T();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(filepath);
|
2011-02-28 06:16:20 +00:00
|
|
|
|
if(file.Exists)
|
|
|
|
|
using (var reader = file.OpenText())
|
|
|
|
|
{
|
|
|
|
|
var s = new JsonSerializer();
|
|
|
|
|
var r = new JsonReader(reader);
|
|
|
|
|
config = (T) s.Deserialize(r, typeof (T));
|
|
|
|
|
}
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
catch { }
|
2011-03-21 06:03:58 +00:00
|
|
|
|
if (config == null) return new T();
|
|
|
|
|
else return config;
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Save(string filepath, object config)
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(filepath);
|
2011-02-20 06:13:26 +00:00
|
|
|
|
using (var writer = file.CreateText())
|
2011-01-11 02:55:51 +00:00
|
|
|
|
{
|
2011-02-20 06:13:26 +00:00
|
|
|
|
var s = new JsonSerializer();
|
|
|
|
|
var w = new JsonWriter(writer) { Formatting = Formatting.Indented };
|
|
|
|
|
s.Serialize(w, config);
|
2011-01-11 02:55:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|