-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added saving mod options. Moved a lot of initialization to InitialScene
- Loading branch information
Showing
12 changed files
with
249 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Assembly-CSharp.Courier.mm/Courier/Module/CourierModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
namespace Mod.Courier.Module { | ||
public class CourierModule { | ||
|
||
// Called in InitialScene, before anything happens | ||
public virtual void Load() { | ||
|
||
} | ||
|
||
// Called in BootGame, after Managers are created | ||
public virtual void Initialize() { | ||
|
||
} | ||
|
||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
Assembly-CSharp.Courier.mm/Courier/Module/PostmanModule.cs
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
Assembly-CSharp.Courier.mm/Courier/Save/BooleanOptionSaveMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
namespace Mod.Courier.Save { | ||
public class BooleanOptionSaveMethod : OptionSaveMethod { | ||
public Func<bool> GetBooleanValue; | ||
public Action<bool> SetBooleanValue; | ||
|
||
public BooleanOptionSaveMethod(string optionKey, Func<bool> GetBooleanValue, Action<bool> SetBooleanValue) { | ||
this.optionKey = optionKey; | ||
this.GetBooleanValue = GetBooleanValue; | ||
this.SetBooleanValue = SetBooleanValue; | ||
} | ||
|
||
public override string Save() { | ||
return GetBooleanValue?.Invoke().ToString(); | ||
} | ||
|
||
public override void Load(string load) { | ||
if (bool.TryParse(load, out bool res)) { | ||
SetBooleanValue?.Invoke(res); | ||
} | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
Assembly-CSharp.Courier.mm/Courier/Save/ModdedOptionsSave.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Mod.Courier.Save { | ||
[Serializable] | ||
public class ModdedOptionsSave { | ||
public static ModdedOptionsSave Instance { set; get; } | ||
|
||
public OptionPair[] Options; | ||
|
||
static ModdedOptionsSave() { | ||
Instance = new ModdedOptionsSave(); | ||
} | ||
|
||
public string GetJson() { | ||
return JsonUtility.ToJson(this); | ||
} | ||
|
||
public void LoadOptions() { | ||
if (Options == null) return; | ||
foreach (OptionPair option in Options) { | ||
for (int i = 0; i < Courier.UI.OptionButtons.Count; i++) { | ||
if (!string.IsNullOrEmpty(Courier.UI.OptionButtons[i].SaveMethod.optionKey) && Courier.UI.OptionButtons[i].SaveMethod.optionKey.Equals(option.optionKey)) { | ||
Courier.UI.OptionButtons[i].SaveMethod.Load(option.optionValue); | ||
} | ||
} | ||
|
||
for (int i = 0; i < Courier.UI.ModOptionButtons.Count; i++) { | ||
if (!string.IsNullOrEmpty(Courier.UI.ModOptionButtons[i].SaveMethod.optionKey) && Courier.UI.ModOptionButtons[i].SaveMethod.optionKey.Equals(option.optionKey)) { | ||
Courier.UI.ModOptionButtons[i].SaveMethod.Load(option.optionValue); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void UpdateOptionsData() { | ||
OptionPair[] allOptions = new OptionPair[Courier.UI.OptionButtons.Count + Courier.UI.ModOptionButtons.Count]; | ||
int numSavableOptions = 0; | ||
|
||
for(int i = 0; i < Courier.UI.OptionButtons.Count; i++) { | ||
string val = Courier.UI.OptionButtons[i].SaveMethod.Save(); | ||
|
||
if (!string.IsNullOrEmpty(Courier.UI.OptionButtons[i].SaveMethod.optionKey) && !string.IsNullOrEmpty(val)) { | ||
allOptions[i] = new OptionPair { optionKey = Courier.UI.OptionButtons[i].SaveMethod.optionKey, optionValue = val }; | ||
numSavableOptions++; | ||
} | ||
} | ||
|
||
for (int i = 0; i < Courier.UI.ModOptionButtons.Count; i++) { | ||
string val = Courier.UI.ModOptionButtons[i].SaveMethod.Save(); | ||
|
||
if (!string.IsNullOrEmpty(Courier.UI.ModOptionButtons[i].SaveMethod.optionKey) && !string.IsNullOrEmpty(val)) { | ||
allOptions[Courier.UI.OptionButtons.Count + i] = new OptionPair { optionKey = Courier.UI.ModOptionButtons[i].SaveMethod.optionKey, optionValue = val }; | ||
numSavableOptions++; | ||
} | ||
} | ||
|
||
Options = new OptionPair[numSavableOptions]; | ||
for(int i = 0, option = 0; i < allOptions.Length; i++) { | ||
if(allOptions[i] != null) { | ||
Options[option] = allOptions[i]; | ||
option++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[Serializable] | ||
public class OptionPair { | ||
[SerializeField] | ||
public string optionKey; | ||
|
||
[SerializeField] | ||
public string optionValue; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Assembly-CSharp.Courier.mm/Courier/Save/OptionSaveMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
namespace Mod.Courier.Save { | ||
public class OptionSaveMethod { | ||
public string optionKey; | ||
|
||
public virtual string Save() { | ||
return null; | ||
} | ||
|
||
public virtual void Load(string load) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it | ||
|
||
using System; | ||
using Mod.Courier; | ||
|
||
public class patch_InitialScene : InitialScene { | ||
private extern void orig_Start(); | ||
private void Start() { | ||
Courier.Boot(); | ||
Courier.LoadMods(); | ||
|
||
orig_Start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it | ||
|
||
using System; | ||
using Mod.Courier; | ||
using Mod.Courier.Save; | ||
|
||
public class patch_SaveGame : SaveGame { | ||
public extern void orig_LoadOptions(); | ||
public new void LoadOptions() { | ||
orig_LoadOptions(); | ||
ModdedOptionsSave.Instance.LoadOptions(); | ||
} | ||
|
||
public extern void orig_UpdateOptionsData(); | ||
public new void UpdateOptionsData() { | ||
orig_UpdateOptionsData(); | ||
ModdedOptionsSave.Instance.UpdateOptionsData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it | ||
|
||
using System; | ||
using System.IO; | ||
using Mod.Courier; | ||
using Mod.Courier.Save; | ||
using UnityEngine; | ||
|
||
public class patch_SaveLoadStandalone : SaveLoadStandalone { | ||
public extern void orig_Save(SaveGame saveGame); | ||
public override void Save(SaveGame saveGame) { | ||
string json = ModdedOptionsSave.Instance.GetJson(); | ||
PlayerPrefs.SetString("ModSave", json); | ||
string path = Application.persistentDataPath + "/ModSave.json"; | ||
try { | ||
using (StreamWriter streamWriter = new StreamWriter(path, false)) { | ||
streamWriter.WriteLine(json); | ||
streamWriter.Close(); | ||
} | ||
} catch (Exception) { | ||
CourierLogger.Log("Mod Options Save", "An error occured while saving the file, retry."); | ||
try { | ||
File.Delete(path); | ||
using (StreamWriter streamWriter2 = new StreamWriter(path, false)) { | ||
streamWriter2.WriteLine(json); | ||
streamWriter2.Close(); | ||
} | ||
} catch (Exception) { | ||
CourierLogger.Log("Mod Options Save", "Retry Failed, continue without saving."); | ||
} | ||
} | ||
orig_Save(saveGame); | ||
} | ||
|
||
public extern void orig_Load(); | ||
public override void Load() { | ||
LoadModOptions(); | ||
orig_Load(); | ||
} | ||
|
||
public static void LoadModOptions() { | ||
ModdedOptionsSave moddedSave = null; | ||
string text = string.Empty; | ||
try { | ||
text = File.ReadAllText(Application.persistentDataPath + "/ModSave.json"); | ||
if (string.IsNullOrEmpty(text)) { | ||
throw new Exception("Modded SaveLoadStandalone::Load : Modded save file is empty."); | ||
} | ||
text = text.Replace("\u008c\u008b", string.Empty); | ||
moddedSave = JsonUtility.FromJson<ModdedOptionsSave>(text); | ||
} catch (Exception) { | ||
try { | ||
if (PlayerPrefs.HasKey("ModSave")) { | ||
text = PlayerPrefs.GetString("ModSave"); | ||
} | ||
moddedSave = JsonUtility.FromJson<ModdedOptionsSave>(text); | ||
} catch (Exception e) { | ||
CourierLogger.Log(LogType.Exception, "Mod Options Load", "Error while reading modded save from the registry."); | ||
e.LogDetailed("Mod Options Load"); | ||
moddedSave = null; | ||
} | ||
} | ||
if (moddedSave != null) { | ||
ModdedOptionsSave.Instance = moddedSave; | ||
} | ||
ModdedOptionsSave.Instance.LoadOptions(); | ||
} | ||
} |