Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModSettings update #20

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ! Source Project/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion ! Template Project/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion BetterCasePicker/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion BombTimeExtender/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Broken Buttons/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Cheap Checkout/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Creation/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Encrypted Equations/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Minesweeper/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion MissionMaker/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion PacingExtender/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
57 changes: 36 additions & 21 deletions PacingExtender/PacingExtenderAssembly/ModSettings.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.IO;
using UnityEngine;

class ModConfig<T>
{
public ModConfig(string name)
public ModConfig(string filename)
{
_filename = name;
SettingsPath = Path.Combine(Path.Combine(Application.persistentDataPath, "Modsettings"), filename + ".json");
}

string _filename = null;
readonly string SettingsPath = null;

string SettingsPath
{
get
{
return Path.Combine(Path.Combine(Application.persistentDataPath, "Modsettings"), _filename + ".json");
}
}

private string SerializeSettings(T settings)
public string SerializeSettings(T settings)
{
return JsonConvert.SerializeObject(settings, Formatting.Indented);
return JsonConvert.SerializeObject(settings, Formatting.Indented, new StringEnumConverter());
}

public T Settings
static readonly object settingsFileLock = new object();

public T Settings
{
get
{
try
{
if (!File.Exists(SettingsPath))
lock(settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(Activator.CreateInstance<T>()));
}
if (!File.Exists(SettingsPath))
{
File.WriteAllText(SettingsPath, SerializeSettings(Activator.CreateInstance<T>()));
}

return JsonConvert.DeserializeObject<T>(File.ReadAllText(SettingsPath));
T deserialized = JsonConvert.DeserializeObject<T>(
File.ReadAllText(SettingsPath),
new JsonSerializerSettings { Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) => args.ErrorContext.Handled = true }
);
return deserialized != null ? deserialized : Activator.CreateInstance<T>();
}
}
catch
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
}
Expand All @@ -48,8 +52,19 @@ public T Settings
{
if (value.GetType() == typeof(T))
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
lock (settingsFileLock)
{
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion Skewed Slots/Assets/Shared_Assets/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public T Settings
}
catch(Exception e)
{
Debug.LogFormat("An exception has occured while attempting to read the settings from {0}\nDefault settings will be used for the type of {1}.", SettingsPath, typeof(T).ToString());
Debug.LogException(e);
return Activator.CreateInstance<T>();
}
Expand All @@ -53,7 +54,15 @@ public T Settings
{
lock (settingsFileLock)
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
try
{
File.WriteAllText(SettingsPath, SerializeSettings(value));
}
catch (Exception e)
{
Debug.LogFormat("Failed to write to {0}", SettingsPath);
Debug.LogException(e);
}
}
}
}
Expand Down
Loading