Skip to content

Commit

Permalink
Added saving mod options. Moved a lot of initialization to InitialScene
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokemia committed Apr 5, 2020
1 parent 60022c1 commit 3337652
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 13 deletions.
9 changes: 8 additions & 1 deletion Assembly-CSharp.Courier.mm/Assembly-CSharp.Courier.mm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<Compile Include="Patches\BootGame.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Courier\Courier.cs" />
<Compile Include="Courier\Module\PostmanModule.cs" />
<Compile Include="Courier\Module\CourierModule.cs" />
<Compile Include="Courier\Helpers\ReflectionHelper.cs" />
<Compile Include="Courier\Courier.Events.cs" />
<Compile Include="Patches\PlayerController.cs" />
Expand Down Expand Up @@ -155,6 +155,12 @@
<Compile Include="Patches\CreditScreen.cs" />
<Compile Include="Patches\WeakReference.cs" />
<Compile Include="Courier\Helpers\SaveLoadJSON.cs" />
<Compile Include="Patches\SaveGame.cs" />
<Compile Include="Patches\SaveLoadStandalone.cs" />
<Compile Include="Courier\Save\OptionSaveMethod.cs" />
<Compile Include="Courier\Save\ModdedOptionsSave.cs" />
<Compile Include="Courier\Save\BooleanOptionSaveMethod.cs" />
<Compile Include="Patches\InitialScene.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Patches\" />
Expand All @@ -163,6 +169,7 @@
<Folder Include="Courier\Helpers\" />
<Folder Include="Courier\UI\" />
<Folder Include="Courier\GFX\" />
<Folder Include="Courier\Save\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
7 changes: 7 additions & 0 deletions Assembly-CSharp.Courier.mm/Courier/Courier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public static void LoadDLL(string path) {
IEnumerable<Type> modules = FindDerivedTypes(asm, typeof(CourierModule));

foreach (Type moduleType in modules) {
CourierLogger.Log("ModLoader", "Loading module class " + moduleType);
object o = asm.CreateInstance(moduleType.FullName, false, BindingFlags.ExactBinding, null, new object[] { }, null, null);
(o as CourierModule).Load();
Modules.Add(o as CourierModule);
Expand All @@ -216,6 +217,12 @@ public static void LoadDLL(string path) {
}
}

public static void InitMods() {
foreach(CourierModule mod in Modules) {
mod.Initialize();
}
}

internal static void SetupCourierSpriteParams() {
spriteParamsSetup = true;
ResourceHelper.SpriteConfig["Mod.Courier.UI.mod_options_frame"] = new SpriteParams { pixelsPerUnit = 20, border = new Vector4(15, 15, 15, 15)};
Expand Down
16 changes: 16 additions & 0 deletions Assembly-CSharp.Courier.mm/Courier/Module/CourierModule.cs
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 Assembly-CSharp.Courier.mm/Courier/Module/PostmanModule.cs

This file was deleted.

23 changes: 23 additions & 0 deletions Assembly-CSharp.Courier.mm/Courier/Save/BooleanOptionSaveMethod.cs
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 Assembly-CSharp.Courier.mm/Courier/Save/ModdedOptionsSave.cs
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 Assembly-CSharp.Courier.mm/Courier/Save/OptionSaveMethod.cs
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) {

}
}
}
2 changes: 2 additions & 0 deletions Assembly-CSharp.Courier.mm/Courier/UI/OptionsButtonInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Mod.Courier.Save;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
Expand All @@ -12,6 +13,7 @@ public abstract class OptionsButtonInfo {
public GameObject gameObject;
public View addedTo;
public Func<bool> IsEnabled;
public OptionSaveMethod SaveMethod = new OptionSaveMethod();

protected OptionsButtonInfo(Func<string> GetText, UnityAction onClick) {
this.GetText = GetText;
Expand Down
4 changes: 2 additions & 2 deletions Assembly-CSharp.Courier.mm/Patches/BootGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
public class patch_BootGame : BootGame {
private extern void orig_Start();
private void Start() {
Courier.Boot();
Courier.LoadMods();
Courier.InitMods();

orig_Start();
}

// This might need to be moved somewhere more global
void OnApplicationQuit() {
Courier.Quit();
}
Expand Down
14 changes: 14 additions & 0 deletions Assembly-CSharp.Courier.mm/Patches/InitialScene.cs
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();
}
}
19 changes: 19 additions & 0 deletions Assembly-CSharp.Courier.mm/Patches/SaveGame.cs
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();
}
}
68 changes: 68 additions & 0 deletions Assembly-CSharp.Courier.mm/Patches/SaveLoadStandalone.cs
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();
}
}

0 comments on commit 3337652

Please sign in to comment.