-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBodypartSystem.cs
More file actions
190 lines (163 loc) · 7.15 KB
/
BodypartSystem.cs
File metadata and controls
190 lines (163 loc) · 7.15 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BlacksmithTools
{
[HarmonyPatch]
public static class BodypartSystem
{
public static Dictionary<string, List<bodyPart>> bodypartSettings = new Dictionary<string, List<bodyPart>>();
public static Dictionary<string, List<int>> bodypartSettingsAsBones = new Dictionary<string, List<int>>();
//to be implemented later
//public static Dictionary<string, List<string>> bodyPartSettingsOverrride = new Dictionary<string, List<string>>();
public enum bodyPart
{
Head,
Torso,
ArmUpperLeft,
ArmLowerLeft,
HandLeft,
ArmUpperRight,
ArmLowerRight,
HandRight,
LegUpperLeft,
LegLowerLeft,
FootLeft,
LegUpperRight,
LegLowerRight,
FootRight,
All,
Beard,
Hair
}
//attaching controller
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.Awake))]
[HarmonyPostfix]
static void VisEqAwakePatch(VisEquipment __instance)
{
if (!Main.bodyHidingEnabled.Value) return;
if (!__instance.m_isPlayer) return;
if (__instance.m_bodyModel?.sharedMesh == null) return;
if (!__instance.m_bodyModel.sharedMesh.isReadable) return;
//attach controller
BodyPartController ctrl = __instance.gameObject.AddComponent<BodyPartController>();
ctrl.Setup(__instance);
}
public static void BindConfigs()
{
string[] files = Directory.GetFiles(Paths.ConfigPath);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
if (fileName.StartsWith("bsmith."))
{
//get item name from file name
string itemName = fileName.Remove(0, 7);
itemName = itemName.Remove(itemName.Length - 4, 4);
Util.LogMessage("Loaded configuration file for " + itemName, BepInEx.Logging.LogLevel.Message);
//create entry for dictionary if one doesnt already exist
if (bodypartSettingsAsBones.ContainsKey(itemName)) return;
bodypartSettingsAsBones.Add(itemName, new List<int>());
//load part name list
ConfigFile cfg = new ConfigFile(file, true);
ConfigEntry<string> partList = cfg.Bind("Body Parts", "List", "", "List of body parts to hide, delimited by a semilocor. List of valid values on mod page");
//convert bodypart list to bone index array
string[] splitPartNames = partList.Value.Split(';');
for (int i = 0; i < splitPartNames.Length; i++)
{
bodypartSettingsAsBones[itemName].AddRange(Util.BodyPartToBoneIndexes(splitPartNames[i]));
}
//parse bone index list to array
ConfigEntry<string> boneIndexList = cfg.Bind("Body Parts", "Bone List", "", "List of bone indexes, body model geometry weighted to these bones will be hidden, delimited by a semilocor. List of valid values on mod page");
string[] explodedBoneIndexCfg = boneIndexList.Value.Split(';');
int boneIndex;
for (int i = 0; i < explodedBoneIndexCfg.Length; i++)
{
if (int.TryParse(explodedBoneIndexCfg[i], out boneIndex)) bodypartSettingsAsBones[itemName].Add(boneIndex);
}
Util.LogMessage(bodypartSettingsAsBones[itemName].Count + " bones for " + itemName, BepInEx.Logging.LogLevel.Message);
}
}
PartCfgToBoneindexes();
CleanupCfgs();
}
//convert bodypartSettings dictionary to bone inedexes and add to bodypartSettingsAsBones
public static void PartCfgToBoneindexes()
{
foreach (string itemName in bodypartSettings.Keys)
{
if(!bodypartSettingsAsBones.ContainsKey(itemName))
{
bodypartSettingsAsBones.Add(itemName, new List<int>());
}
bodypartSettingsAsBones[itemName].AddRange(Util.BodyPartToBoneIndexes(bodypartSettings[itemName].ToArray()));
}
}
public static void CleanupCfgs()
{
foreach (KeyValuePair<string, List<bodyPart>> cfg in bodypartSettings)
{
bodypartSettingsAsBones[cfg.Key] = new List<int>(bodypartSettingsAsBones[cfg.Key].Distinct().ToArray());
}
}
static void EquipmentChanged(VisEquipment viseq)
{
if (!Main.bodyHidingEnabled.Value) return;
viseq.GetComponent<BodyPartController>()?.FullUpdate();
Util.LogMessage("Equipment changed ", BepInEx.Logging.LogLevel.Message);
}
//uhhh body part updates
#region vile but thats how valheim code is
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetRightHandEquipped))]
[HarmonyPostfix]
static void SetRightHandPatch(VisEquipment __instance, bool __result)
{
if(__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetLeftHandEquipped))]
[HarmonyPostfix]
static void SetLeftHandPatch(VisEquipment __instance, bool __result)
{
if (__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetChestEquipped))]
[HarmonyPostfix]
static void SetChestPatch(VisEquipment __instance, bool __result, int hash)
{
if (__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetLegEquipped))]
[HarmonyPostfix]
static void SetLegPatch(VisEquipment __instance, bool __result, int hash)
{
if (__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetHelmetEquipped))]
[HarmonyPostfix]
static void SetHelmetPatch(VisEquipment __instance, bool __result)
{
if (__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetShoulderEquipped))]
[HarmonyPostfix]
static void SetShoulderPtach(VisEquipment __instance, bool __result)
{
if (__result) EquipmentChanged(__instance);
}
[HarmonyPatch(typeof(VisEquipment), nameof(VisEquipment.SetUtilityEquipped))]
[HarmonyPostfix]
static void SetUtilityPatch(VisEquipment __instance, bool __result)
{
if (__result) EquipmentChanged(__instance);
}
#endregion
}
}