Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Added toggle for saving calibrations and updated to MelonLoader 0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RequiDev committed Jul 20, 2021
1 parent 85281df commit 0afa577
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
5 changes: 2 additions & 3 deletions FBT Saver/FBT Saver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
<Reference Include="Il2Cppmscorlib">
<HintPath>S:\Games\steamapps\common\VRChat\MelonLoader\Managed\Il2Cppmscorlib.dll</HintPath>
</Reference>
<Reference Include="MelonLoader.ModHandler, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>S:\Games\steamapps\common\VRChat\MelonLoader\MelonLoader.ModHandler.dll</HintPath>
<Reference Include="MelonLoader">
<HintPath>S:\Games\steamapps\common\VRChat\MelonLoader\MelonLoader.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
27 changes: 15 additions & 12 deletions FBT Saver/FBTSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Harmony;
using HarmonyLib;
using MelonLoader;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
Expand All @@ -17,7 +17,7 @@ public static class BuildInfo
public const string Name = "FBT Saver";
public const string Author = "Requi";
public const string Company = "RequiDev";
public const string Version = "1.1.6";
public const string Version = "1.1.7";
public const string DownloadLink = "https://github.com/RequiDev/FBTSaver";
}

Expand All @@ -31,13 +31,14 @@ private class FbtCalibration
}

private static Dictionary<string, FbtCalibration> _savedCalibrations;
private static MelonPreferences_Entry<bool> _saveCalibrations;

private const string CalibrationsDirectory = "UserData/FBTSaver/";
private const string CalibrationsFile = "calibrations.json";

public override void OnApplicationStart()
{
var instance = HarmonyInstance.Create("FBTSaver");
var instance = new HarmonyLib.Harmony("FBTSaver");

Directory.CreateDirectory(CalibrationsDirectory);

Expand All @@ -47,19 +48,19 @@ public override void OnApplicationStart()
JsonConvert.DeserializeObject<Dictionary<string, FbtCalibration>>(
File.ReadAllText($"{CalibrationsDirectory}{CalibrationsFile}"));

MelonLogger.Log($"Loaded {_savedCalibrations.Count} calibrations from disk.");
MelonLogger.Msg($"Loaded {_savedCalibrations.Count} calibrations from disk.");
}
else
{
MelonLogger.Log($"No saved calibrations found. Creating new.");
MelonLogger.Msg($"No saved calibrations found. Creating new.");
_savedCalibrations = new Dictionary<string, FbtCalibration>(128);
File.WriteAllText($"{CalibrationsDirectory}{CalibrationsFile}", JsonConvert.SerializeObject(_savedCalibrations, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore // otherwise it gets fucky on Vector3.normalized.normalized...
}));
}

MelonLogger.Log("Patching IsCalibratedForAvatar...");
MelonLogger.Msg("Patching IsCalibratedForAvatar...");

var methods = typeof(VRCTrackingSteam).GetMethods();
foreach (var methodInfo in methods)
Expand All @@ -75,19 +76,21 @@ public override void OnApplicationStart()
}
}

_saveCalibrations = MelonPreferences.CreateCategory("FBT Saver").CreateEntry("SaveCalibrations", true, "Save Calibrations", "Save your calibrations to disk and re-use them on each avatar.");

if (MelonHandler.Mods.Find(m => m.Info.Name == "UI Expansion Kit") != null)
{
MelonLogger.Log("Adding UIX Button to clear saved calibrations...");
ExpansionKitApi.GetExpandedMenu(ExpandedMenu.AvatarMenu).AddSimpleButton("Clear Saved FBT Calibrations",
MelonLogger.Msg("Adding UIX Button to clear saved calibrations...");
ExpansionKitApi.GetExpandedMenu(ExpandedMenu.AvatarMenu).AddSimpleButton("Clear saved FBT calibrations",
() =>
{
_savedCalibrations.Clear();
File.Delete($"{CalibrationsDirectory}{CalibrationsFile}");
MelonLogger.Log("Cleared Saved Calibrations");
MelonLogger.Msg("Cleared saved calibrations");
});
}

MelonLogger.Log("Done!");
MelonLogger.Msg("Done!");
}

private static void PerformCalibration(ref VRCTrackingSteam __instance, Animator __0, bool __1, bool __2)
Expand All @@ -111,7 +114,7 @@ private static void PerformCalibration(ref VRCTrackingSteam __instance, Animator
catch(Exception e)
{
File.WriteAllText($"{CalibrationsDirectory}error.log", e.Message);
MelonLogger.LogError(
MelonLogger.Error(
$"Could not save current calibration to file! Created error.log in /UserData/FTBSaver. Please create an issue on GitHub or message Requi in the VRChat Modding Group Discord with that file.");
}
}
Expand All @@ -125,7 +128,7 @@ private static bool IsCalibratedForAvatar(ref VRCTrackingSteam __instance, ref b
}

var avatarId = __0;
if (_savedCalibrations.ContainsKey(avatarId))
if (_saveCalibrations.Value && _savedCalibrations.ContainsKey(avatarId))
{
var savedCalib = _savedCalibrations[avatarId];
__instance.field_Public_Transform_10.localPosition = savedCalib.LeftFoot.Key;
Expand Down

0 comments on commit 0afa577

Please sign in to comment.