BizHawk/BizHawk.MultiClient/ConfigService.cs

39 lines
1.0 KiB
C#
Raw Normal View History

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);
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 { }
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
}
}
}
}