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

LocalPlayerPrefsMod: Add new cli parameter --localplayerprefs= #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 15 additions & 8 deletions LocalPlayerPrefs/LocalPlayerPrefsMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LocalPlayerPrefs
{
public class LocalPlayerPrefsMod : MelonMod
{
private const string FileName = "UserData/PlayerPrefs.json";
private string fileName = "UserData/PlayerPrefs.json";

private readonly List<Delegate> myPinnedDelegates = new List<Delegate>();
private readonly ConcurrentDictionary<string, object> myPrefs = new ConcurrentDictionary<string, object>();
Expand All @@ -39,18 +39,25 @@ public class LocalPlayerPrefsMod : MelonMod

public override void OnApplicationStart()
{
foreach (string arg in Environment.GetCommandLineArgs())
{
if (arg.StartsWith("--localplayerprefs="))
{
fileName = arg.Split(new char[] { '=' }, 2)[1];
}
}
try
{
if (File.Exists(FileName))
if (File.Exists(fileName))
{
var dict = (ProxyObject) JSON.Load(File.ReadAllText(FileName));
var dict = (ProxyObject) JSON.Load(File.ReadAllText(fileName));
foreach (var keyValuePair in dict) myPrefs[keyValuePair.Key] = ToObject(keyValuePair.Key, keyValuePair.Value);
MelonLogger.Msg($"Loaded {dict.Count} prefs from PlayerPrefs.json");
MelonLogger.Msg($"Loaded {dict.Count} prefs from {fileName}");
}
}
catch (Exception ex)
{
MelonLogger.Error($"Unable to load PlayerPrefs.json: {ex}");
MelonLogger.Error($"Unable to load {fileName}: {ex}");
}

HookICall<TrySetFloatDelegate>(nameof(PlayerPrefs.TrySetFloat), TrySetFloat);
Expand Down Expand Up @@ -90,13 +97,13 @@ private object ToObject(string key, Variant value)
public override void OnSceneWasLoaded(int buildIndex, string name)
{
Save();
MelonLogger.Msg("Saved PlayerPrefs.json on level load");
MelonLogger.Msg($"Saved {fileName} on level load");
}

public override void OnApplicationQuit()
{
Save();
MelonLogger.Msg("Saved PlayerPrefs.json on exit");
MelonLogger.Msg($"Saved {fileName} on exit");
}

private bool HasKey(IntPtr keyPtr)
Expand Down Expand Up @@ -128,7 +135,7 @@ private void Save()
{
lock (mySaveLock)
{
File.WriteAllText(FileName, JSON.Dump(myPrefs, EncodeOptions.PrettyPrint));
File.WriteAllText(fileName, JSON.Dump(myPrefs, EncodeOptions.PrettyPrint));
}
}
catch (IOException ex)
Expand Down