Skip to content

Commit

Permalink
Prerelease 5.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed Dec 21, 2015
1 parent be877f9 commit 9928171
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
66 changes: 46 additions & 20 deletions Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@

namespace AA2Install
{
static class Configuration
public static class Configuration
{
public static string ReadSetting(string key)
public static string ReadSetting(string key, Stream configStream = null)
{
try
{
if (!File.Exists(Paths.CONFIG))
return null;
string json;

string json = File.ReadAllText(Paths.CONFIG);
if (configStream != null)
{
using (BinaryReader br = new BinaryReader(configStream))
json = Encoding.Unicode.GetString(br.ReadBytes((int)configStream.Length));
}
else
{
if (!File.Exists(Paths.CONFIG))
return null;
json = File.ReadAllText(Paths.CONFIG);
}

var appSettings = JsonConvert.DeserializeObject<SerializableDictionary<string>>(json);

if (!appSettings.ContainsKey(key))
Expand Down Expand Up @@ -58,6 +68,31 @@ public static bool WriteSetting(string key, string value)
return false;
}
}
public static bool WriteSetting(string key, string value, Stream input, out MemoryStream output)
{
try
{
Trace.WriteLine(key + " : " + value);
string json;

using (StreamReader sr = new StreamReader(input))
json = sr.ReadToEnd();

var settings = JsonConvert.DeserializeObject<SerializableDictionary<string>>(json);
if (settings == null)
settings = new SerializableDictionary<string>();
settings[key] = value;

output = new MemoryStream(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(settings)));
return true;
}
catch (Exception ex)
{
Trace.WriteLine("Error writing app settings: " + ex.Message);
output = null;
return false;
}
}

/// <summary>
/// Serialize an object, useful for saving as a key/value pair
Expand Down Expand Up @@ -97,33 +132,24 @@ public static T DeserializeObject<T>(string toDeserialize)
/// <param name="key">Key of item</param>
/// <returns>Value of key in type bool</returns>
public static bool getBool(string key) => bool.Parse(ReadSetting(key) ?? "False");
public static bool getBool(string key, Stream stream) => bool.Parse(ReadSetting(key, stream) ?? "False");
/// <summary>
/// Saves a list of installed mods to the "MODS" key
/// </summary>
/// <param name="list">List of installed mods</param>
public static void saveMods(Dictionary<string, Mod> list)
public static void saveMods(SerializableDictionary<Mod> list)
{
SerializableDictionary<Mod> s = new SerializableDictionary<Mod>();
foreach (string key in list.Keys)
{
s[key] = list[key];
}
WriteSetting("MODS", SerializeObject(s));
WriteSetting("MODS", SerializeObject(list));
}
/// <summary>
/// Loads a list of installed mods from the "MODS" key
/// </summary>
/// <returns>List of installed mods</returns>
public static Dictionary<string, Mod> loadMods()
public static SerializableDictionary<Mod> loadMods()
{
if (ReadSetting("MODS") == null) { return new Dictionary<string, Mod>(); }
Dictionary<string, Mod> d = new Dictionary<string, Mod>();
if (ReadSetting("MODS") == null) { return new SerializableDictionary<Mod>(); }
SerializableDictionary<Mod> s = DeserializeObject<SerializableDictionary<Mod>>(ReadSetting("MODS"));
foreach (string key in s.Keys)
{
d[key] = s[key];
}
return d;
return s;
}
#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion formMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace AA2Install
{
public partial class formMain : Form
{
public Dictionary<string, Mod> modDict = new Dictionary<string, Mod>();
public SerializableDictionary<Mod> modDict = new SerializableDictionary<Mod>();

#region Console

Expand Down

0 comments on commit 9928171

Please sign in to comment.