forked from FenrixTheFox/DynamicBonesSafety
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBSMod.cs
More file actions
97 lines (78 loc) · 4.27 KB
/
DBSMod.cs
File metadata and controls
97 lines (78 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using DynamicBonesSafety;
using MelonLoader;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnhollowerRuntimeLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
[assembly: MelonGame("VRChat", "VRChat")]
[assembly: MelonInfo(typeof(DBSMod), "Dynamic Bones Safety", "2.1.5", "Fenrix")]
namespace DynamicBonesSafety
{
public class DBSMod : MelonMod
{
public const int DynamicBonesEnumValue = 100;
public static Dictionary<string, bool> CanUseBones = new Dictionary<string, bool>();
private static VRCUiPageSafety _cachedPageSafety;
public static MethodInfo _cachedSelectedSafetyClass;
public static string UiPageSafetySelectedSocial => _cachedSelectedSafetyClass.Invoke(_cachedPageSafety, new object[] { }).ToString();
public override void OnApplicationStart()
{
// Generate Config
if (Directory.Exists("UserData")
&& File.Exists("UserData/DynamicBonesSafety.json"))
{
CanUseBones = JsonConvert.DeserializeObject<Dictionary<string, bool>>(
File.ReadAllText("UserData/DynamicBonesSafety.json"));
}
else
{
Directory.CreateDirectory("UserData");
}
_cachedSelectedSafetyClass = typeof(VRCUiPageSafety).GetProperties().Where(prop => prop.PropertyType.IsEnum).First().GetGetMethod();
ModPatches.DoPatch(HarmonyInstance);
MelonCoroutines.Start(WaitForUiManagerInit());
}
private IEnumerator WaitForUiManagerInit()
{
while (VRCUiManager.prop_VRCUiManager_0 == null) yield return null;
GameObject SafetyUiMenu = GameObject.Find("UserInterface/MenuContent/Screens/Settings_Safety");
_cachedPageSafety = SafetyUiMenu.GetComponent<VRCUiPageSafety>();
Transform SafetyMatrixTogglesTransform = GameObject.Find("UserInterface/MenuContent/Screens/Settings_Safety/_SafetyMatrix/_Toggles").transform;
// Rescale all children to make it not be S Q U I S H
foreach (var t in SafetyMatrixTogglesTransform)
{
t.Cast<Transform>().localScale = new Vector3(0.9f, 0.9f, 0.9f);
}
GameObject DynamicBoneToggle = GameObject.Instantiate(GameObject.Find("UserInterface/MenuContent/Screens/Settings_Safety/_SafetyMatrix/_Toggles/Toggle_CustomAnimations"), SafetyMatrixTogglesTransform);
DynamicBoneToggle.name = "Toggle_DynamicBones";
DynamicBoneToggle.GetComponentInChildren<Text>().text = "Dynamic Bones";
UiSafetyFeatureToggle UiSafetyFeature = DynamicBoneToggle.GetComponent<UiSafetyFeatureToggle>();
UiSafetyFeature.field_Public_String_0 = "<i>Dynamic Bones</i> turns on or off dynamic bones on a user's avatar for the combined <i>Safety Mode</i> and <i>User Trust Rank</i>.";
UiSafetyFeature.GetType().GetProperties().Where(prop => prop.PropertyType.IsEnum).First().SetValue(UiSafetyFeature, DynamicBonesEnumValue);
Toggle BoneToggle = DynamicBoneToggle.GetComponent<Toggle>();
BoneToggle.onValueChanged.AddListener(DelegateSupport.ConvertDelegate<UnityAction<bool>>(new Action<bool>(delegate
{
CanUseBones[UiPageSafetySelectedSocial] = BoneToggle.isOn;
File.WriteAllText("UserData/DynamicBonesSafety.json", JsonConvert.SerializeObject(CanUseBones, Formatting.Indented));
})));
// Load our icon
// Taken directly from UIX
AssetBundle assetBundle;
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DynamicBonesSafety.dbsmod"))
using (var tempStream = new MemoryStream((int)stream.Length))
{
stream.CopyTo(tempStream);
assetBundle = AssetBundle.LoadFromMemory_Internal(tempStream.ToArray(), 0);
assetBundle.hideFlags |= HideFlags.DontUnloadUnusedAsset;
}
DynamicBoneToggle.GetComponentInChildren<RawImage>().texture = assetBundle.LoadAsset_Internal("boneIcon", Il2CppType.Of<Texture2D>()).Cast<Texture2D>();
}
}
}