forked from GoldenJude/BlacksmithTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoneReorder.cs
83 lines (72 loc) · 2.69 KB
/
BoneReorder.cs
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
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace BlacksmithTools
{
[HarmonyPatch]
public static class BoneReorder
{
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.AttachItem))]
[HarmonyPostfix]
static void AttachItemPatch(VisEquipment __instance, GameObject __result, int itemHash)
{
if (!Main.reorderEnabled.Value) return;
if (__result == null) return;
if (__result.name.StartsWith("attach_skin") && ObjectDB.instance.GetItemPrefab(itemHash) != null)
{
BoneReorder.SetSMRBones(__instance, __result, itemHash);
}
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.AttachArmor))]
[HarmonyPostfix]
static void AttachArmorPatch(VisEquipment __instance, List<GameObject> __result, int itemHash)
{
if (!Main.reorderEnabled.Value || __result == null) return;
foreach (GameObject result in __result)
{
if (!result.name.StartsWith("attach_skin")) continue;
BoneReorder.SetSMRBones(__instance, result, itemHash);
}
}
public static void SetSMRBones(VisEquipment ve, GameObject instance, int hash)
{
Util.LogMessage("Reordering bones");
try
{
SkinnedMeshRenderer origsmr = instance.GetComponentInChildren<SkinnedMeshRenderer>();
SkinnedMeshRenderer[] smrs = instance.GetComponentsInChildren<SkinnedMeshRenderer>(true);
foreach (SkinnedMeshRenderer smr in smrs)
{
SetBones(smr, GetBoneNames(origsmr), ve.m_bodyModel.rootBone);
}
}
catch(Exception e)
{
Util.LogMessage(e.Message, BepInEx.Logging.LogLevel.Error);
}
}
public static string[] GetBoneNames(SkinnedMeshRenderer smr)
{
List<string> boneNames = new List<string>();
foreach (Transform bone in smr.bones)
{
boneNames.Add(bone.name);
}
return boneNames.ToArray();
}
public static void SetBones(SkinnedMeshRenderer smr, string[] boneNames, Transform skeletonRoot)
{
Transform[] bones = new Transform[smr.bones.Length];
for (int j = 0; j < bones.Length; j++)
{
bones[j] = Util.FindInChildren(skeletonRoot, boneNames[j]);
}
smr.bones = bones;
smr.rootBone = skeletonRoot;
}
}
}