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

save / import system changes #232

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions source/Patches/CustomOption/Base.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using HarmonyLib;
using UnityEngine;

namespace TownOfUs.CustomOption
{
Expand Down Expand Up @@ -81,5 +84,25 @@ protected internal void Set(object value, bool SendRpc = true)
{
}
}

public static void UpdateStoredIfNewVersion()
{
//updates files with added / removed settings if it was written on a past version
for (int i = 1; i < 6; i++)
{
if (File.Exists(Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{i}")))
{
Patches.ImportButton.LegacyImportSlot(i);
Patches.ExportButton.ExportSlot(i);
File.Delete(Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{i}"));
continue;
}
if (File.Exists(Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{i}.json")))
{
if (Patches.ImportButton.ImportSlot(i)) Patches.ExportButton.ExportSlot(i);
}
}
AllOptions.Do(x => x.Value = x.DefaultValue);
}
}
}
39 changes: 20 additions & 19 deletions source/Patches/CustomOption/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Il2CppSystem.Text;
using Reactor.Utilities;
using Reactor.Utilities.Extensions;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using UnityEngine;
using Object = UnityEngine.Object;
using System.Text.Json;

namespace TownOfUs.CustomOption
{
Expand Down Expand Up @@ -109,33 +109,34 @@ protected internal void ToDo()
__instance.Children = new Il2CppReferenceArray<OptionBehaviour>(options.ToArray());
}

private void ExportSlot(int slotId)
public void ExportSlot(int slotId)
{
System.Console.WriteLine(slotId);
System.Console.WriteLine($"writing slot {slotId}");

var dictie = new Dictionary<string, string>();

var builder = new StringBuilder();
Dictionary<string, object> data = [];
data.Add("Version", TownOfUs.VersionString);
foreach (var option in AllOptions)
{
if (option.Type == CustomOptionType.Button || option.Type == CustomOptionType.Header) continue;
builder.AppendLine(option.Name);
builder.AppendLine(option.Value.ToString());
if (option.Type is CustomOptionType.String or CustomOptionType.Toggle or CustomOptionType.Number)
{
data.Add(option.Name, option.Value);
}
}


var text = Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{slotId}-temp");
var json = JsonSerializer.Serialize(data, options: new() { WriteIndented = true });
var temp = Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{slotId}-temp.json");
try
{
File.WriteAllText(text, builder.ToString());
var text2 = Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{slotId}");
File.Delete(text2);
File.Move(text, text2);
Cancel(FlashGreen);
File.WriteAllText(temp, json);
var finalfile = Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{slotId}.json");
File.Delete(finalfile);
File.Move(temp, finalfile);
if (LobbyBehaviour.Instance) Cancel(FlashGreen);
}
catch
catch (Exception e)
{
Cancel(FlashRed);
Logger<TownOfUs>.Error(e);
if (LobbyBehaviour.Instance) Cancel(FlashRed);
}
}

Expand Down
2 changes: 2 additions & 0 deletions source/Patches/CustomOption/Generate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,8 @@ public static void GenerateAll()
Underdog = new CustomHeaderOption(num++, MultiMenu.modifiers, "<color=#FF0000FF>Underdog</color>");
UnderdogKillBonus = new CustomNumberOption(num++, MultiMenu.modifiers, "Kill Cooldown Bonus", 5f, 2.5f, 10f, 2.5f, CooldownFormat);
UnderdogIncreasedKC = new CustomToggleOption(num++, MultiMenu.modifiers, "Increased Kill Cooldown When 2+ Imps", true);

CustomOption.UpdateStoredIfNewVersion();
}
}
}
61 changes: 53 additions & 8 deletions source/Patches/CustomOption/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using UnityEngine;
using Object = UnityEngine.Object;
using System.Text.Json;

namespace TownOfUs.CustomOption
{
Expand Down Expand Up @@ -63,7 +64,7 @@ protected internal IEnumerator CancelCoro(Func<IEnumerator> flashCoro)
Loading.Do = () => { };
Loading.Setting.Cast<ToggleOption>().TitleText.text = "Loading...";

__instance.Children = new[] {Loading.Setting};
__instance.Children = new[] { Loading.Setting };


yield return new WaitForSeconds(0.5f);
Expand Down Expand Up @@ -107,10 +108,59 @@ protected internal void ToDo()
__instance.Children = new Il2CppReferenceArray<OptionBehaviour>(options.ToArray());
}

private void ImportSlot(int slotId)
// returns true if the current mod version is higher than the version stored or stored version is absent
public bool ImportSlot(int slotId)
{
System.Console.WriteLine(slotId);
System.Console.WriteLine($"reading slot {slotId}");

Dictionary<string, object> data = new();
try
{
var path = Path.Combine(Application.persistentDataPath, $"GameSettings-Slot{slotId}.json");
var text = File.ReadAllText(path);
data = JsonSerializer.Deserialize<Dictionary<string, object>>(text);
}
catch (Exception e)
{
Logger<TownOfUs>.Error(e);
if (LobbyBehaviour.Instance) Cancel(FlashRed);
return false;
}

foreach (var storedoption in data)
{
var option = AllOptions.FirstOrDefault(x => x.Name.Equals(storedoption.Key));
if (option != null)
{
switch (option.Type)
{
case CustomOptionType.Number:
option.Set(((JsonElement)storedoption.Value).Deserialize<float>(), false);
break;
case CustomOptionType.Toggle:
option.Set(((JsonElement)storedoption.Value).Deserialize<bool>(), false);
break;
case CustomOptionType.String:
option.Set(((JsonElement)storedoption.Value).Deserialize<int>(), false);
break;
}
}
}

if (LobbyBehaviour.Instance)
{
Rpc.SendRpc();
Cancel(FlashGreen);
}

if (!data.ContainsKey("Version")) return true;
return TownOfUs.Version.CompareTo(new Version(((JsonElement)data["Version"]).Deserialize<string>())) > 0;
}


// Used for earlier save system, kept for migrating purposes, do not use except in CustomOption.UpdateStoredIfNewVersion()
public void LegacyImportSlot(int slotId)
{
string text;

try
Expand All @@ -120,7 +170,6 @@ private void ImportSlot(int slotId)
}
catch
{
Cancel(FlashRed);
return;
}

Expand Down Expand Up @@ -160,10 +209,6 @@ private void ImportSlot(int slotId)
break;
}
}

Rpc.SendRpc();

Cancel(FlashGreen);
}


Expand Down