forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBepInExPlugin.cs
383 lines (336 loc) · 16 KB
/
BepInExPlugin.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Backpack
{
[BepInPlugin("aedenthorn.Backpack", "Backpack", "0.3.0")]
public class BepInExPlugin : BaseUnityPlugin
{
private static readonly bool isDebug = true;
private static BepInExPlugin context;
private Harmony harmony;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<string> hotKey;
public static ConfigEntry<string> backpackName;
public static ConfigEntry<string> backpackGUID;
public static ConfigEntry<Vector2> backpackSize;
public static ConfigEntry<float> backpackWeightMult;
public static ConfigEntry<bool> dropInventoryOnDeath;
public static ConfigEntry<bool> createTombStone;
private static GameObject backpack = null;
private static ZDO backpackZDO = null;
private static string backpackObjectPrefix = "Container_Backpack";
private static string backpackObjectName = "";
private static bool saving = false;
private static bool opening = false;
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug)
Debug.Log((pref ? typeof(BepInExPlugin).Namespace + " " : "") + str);
}
private void Awake()
{
context = this;
modEnabled = Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
nexusID = Config.Bind<int>("General", "NexusID", 858, "Nexus mod ID for updates");
hotKey = Config.Bind<string>("General", "HotKey", "b", "Hotkey to open backpack.");
backpackName = Config.Bind<string>("General", "BackpackName", "Backpack", "Display name for backpack.");
backpackGUID = Config.Bind<string>("General", "BackpackGUID", Guid.NewGuid().ToString(), "Unique ID for your backpack (don't change this).");
backpackSize = Config.Bind<Vector2>("General", "BackpackSize", new Vector2(6,3), "Size of backpack (w,h).");
backpackWeightMult = Config.Bind<float>("General", "BackpackWeightMult", 0.5f, "Multiplier for weight of items in backpack (set to 0 to disable backpack weight).");
dropInventoryOnDeath = Config.Bind<bool>("General", "DropInventoryOnDeath", true, "Drop backpack inventory on death");
createTombStone = Config.Bind<bool>("General", "CreateTombStone", true, "If DropInventoryOnDeath then create tombstone rather than just dropping inventory.");
if (!modEnabled.Value)
return;
harmony = new Harmony(Info.Metadata.GUID);
harmony.PatchAll();
}
private void OnDestroy()
{
Dbgl("Destroying plugin");
harmony?.UnpatchAll();
}
private void Update()
{
if (!modEnabled.Value || !Player.m_localPlayer || !ZNetScene.instance)
return;
if (backpack)
{
if (!saving && backpack.transform.parent != Player.m_localPlayer.transform)
{
Dbgl("Moving backpack to player");
backpack.transform.SetParent(Player.m_localPlayer.transform);
InitBackpack();
}
backpack.transform.position = Player.m_localPlayer.transform.position;
if (!AedenthornUtils.IgnoreKeyPresses(true) && AedenthornUtils.CheckKeyDown(hotKey.Value))
{
opening = true;
Traverse.Create(backpack.GetComponent<Container>()).Field("m_nview").GetValue<ZNetView>().ClaimOwnership();
Traverse.Create(backpack.GetComponent<Container>()).Field("m_nview").GetValue<ZNetView>().InvokeRPC("RequestOpen", new object[] { Player.m_localPlayer.GetPlayerID() });
}
}
}
[HarmonyPatch(typeof(FejdStartup), "LoadMainScene")]
static class LoadMainScene_Patch
{
static void Prefix(FejdStartup __instance, List<PlayerProfile> ___m_profiles)
{
if (!modEnabled.Value)
return;
var profile = ___m_profiles.Find(p => p.GetFilename() == (string)typeof(Game).GetField("m_profileFilename", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null));
long id = profile.GetPlayerID();
string name = profile.GetName();
backpackObjectName = backpackObjectPrefix + "_" + backpackGUID.Value + "_" + name;
backpack = null;
backpackZDO = null;
}
}
[HarmonyPatch(typeof(ZNetScene), "Awake")]
static class ZNetScene_Awake_Patch
{
static void Postfix(Dictionary<int, GameObject> ___m_namedPrefabs)
{
if (!modEnabled.Value)
return;
//GameObject go = Instantiate(___m_namedPrefabs["piece_chest".GetStableHashCode()]);
___m_namedPrefabs.Add(backpackObjectName.GetStableHashCode(), ___m_namedPrefabs["piece_chest".GetStableHashCode()]);
}
}
[HarmonyPatch(typeof(ZNetScene), "CreateObject")]
static class CreateObject_Patch
{
static void Postfix(ZDO zdo, GameObject __result)
{
if (!modEnabled.Value || !__result || zdo.GetPrefab() != backpackObjectName.GetStableHashCode())
return;
Dbgl($"Created backpack {__result.name}");
__result.name = backpackObjectName;
__result.GetComponent<Container>().m_name = backpackName.Value;
Traverse.Create(Traverse.Create(__result.GetComponent<Container>()).Field("m_inventory").GetValue<Inventory>()).Field("m_width").SetValue((int)Math.Min(8, backpackSize.Value.x));
Traverse.Create(Traverse.Create(__result.GetComponent<Container>()).Field("m_inventory").GetValue<Inventory>()).Field("m_height").SetValue((int)backpackSize.Value.y);
backpack = __result;
if (Player.m_localPlayer)
{
backpack.transform.SetParent(Player.m_localPlayer.transform);
InitBackpack();
}
}
}
[HarmonyPatch(typeof(Player), "Start")]
static class Player_Start_Patch
{
static void Prefix(Player __instance)
{
if (!modEnabled.Value || !ZNetScene.instance || Player.m_localPlayer == null || __instance.GetPlayerID() != Player.m_localPlayer.GetPlayerID())
return;
saving = false;
if (backpack)
{
Dbgl("Backpack exists, moving to player.");
backpack.transform.SetParent(Player.m_localPlayer.transform);
}
else if(backpackZDO != null)
{
Dbgl($"Creating new backpack from existing ZDO {backpackZDO.GetPrefab()}.");
Traverse.Create(ZNetScene.instance).Method("CreateObject", new object[] { backpackZDO }).GetValue();
return;
}
else
{
Dbgl("Creating new backpack.");
GameObject prefab = ZNetScene.instance.GetPrefab("piece_chest");
backpack = Instantiate(prefab, Player.m_localPlayer.transform);
}
InitBackpack();
}
}
private static void InitBackpack()
{
Dbgl("Initializing backpack.");
backpack.transform.localPosition = Vector3.zero;
backpack.name = backpackObjectName;
if (backpack.GetComponent<WearNTear>())
Destroy(backpack.GetComponent<WearNTear>());
if (backpack.GetComponent<Piece>())
Destroy(backpack.GetComponent<Piece>());
if (backpack.transform.Find("New"))
Destroy(backpack.transform.Find("New").gameObject);
backpack.GetComponent<Container>().m_name = backpackName.Value;
Traverse tc = Traverse.Create(backpack.GetComponent<Container>());
Traverse ti = Traverse.Create(tc.Field("m_inventory").GetValue<Inventory>());
tc.Field("m_nview").GetValue<ZNetView>().ClaimOwnership();
ti.Field("m_name").SetValue(backpackName.Value);
ti.Field("m_width").SetValue((int)Math.Min(8, backpackSize.Value.x));
ti.Field("m_height").SetValue((int)backpackSize.Value.y);
backpackZDO = backpack.GetComponent<ZNetView>().GetZDO();
ResetBackpackSector();
}
[HarmonyPatch(typeof(Container), "Awake")]
static class Container_Awake_Patch
{
static void Prefix(Container __instance)
{
if (!modEnabled.Value || !Player.m_localPlayer)
return;
if(__instance.transform.parent == Player.m_localPlayer.transform)
{
__instance.m_name = backpackName.Value;
__instance.name = backpackObjectName;
Dbgl("Backpack awake.");
}
}
static void Postfix(Container __instance, ZNetView ___m_nview)
{
if (!modEnabled.Value || !Player.m_localPlayer)
return;
if(__instance.transform.parent == Player.m_localPlayer.transform)
{
Dbgl($"Backpack name {__instance.name} hash {backpackObjectName.GetStableHashCode()} {___m_nview.GetZDO().GetPrefab()}");
__instance.name = backpackObjectName;
___m_nview.GetZDO().SetPrefab(backpackObjectName.GetStableHashCode());
}
}
}
[HarmonyPatch(typeof(ZNetScene), "CreateDestroyObjects")]
static class CreateDestroyObjects_Patch
{
static void Prefix(ZNetScene __instance)
{
if (!modEnabled.Value || !Player.m_localPlayer)
return;
ResetBackpackSector();
}
}
[HarmonyPatch(typeof(InventoryGui), "Update")]
static class InventoryGui_Update_Patch
{
static void Postfix(InventoryGui __instance, Animator ___m_animator, Container ___m_currentContainer)
{
if (!modEnabled.Value || !backpack || !Player.m_localPlayer || !___m_animator.GetBool("visible") || !AedenthornUtils.CheckKeyDown(hotKey.Value))
return;
if (opening)
{
opening = false;
return;
}
if(___m_currentContainer == backpack.GetComponent<Container>())
{
Traverse.Create(__instance).Method("CloseContainer").GetValue();
}
else
{
Traverse.Create(backpack.GetComponent<Container>()).Field("m_nview").GetValue<ZNetView>().ClaimOwnership();
Traverse.Create(backpack.GetComponent<Container>()).Field("m_nview").GetValue<ZNetView>().InvokeRPC("RequestOpen", new object[] { Player.m_localPlayer.GetPlayerID() });
}
}
}
[HarmonyPatch(typeof(Inventory), "GetTotalWeight")]
[HarmonyPriority(Priority.Last)]
static class GetTotalWeight_Patch
{
static void Postfix(Inventory __instance, ref float __result)
{
if (!modEnabled.Value || !backpack || !Player.m_localPlayer)
return;
if(__instance == Player.m_localPlayer.GetInventory())
{
if (new StackFrame(2).ToString().IndexOf("OverrideGetTotalWeight") > -1)
{
return;
}
__result += backpack.GetComponent<Container>().GetInventory().GetTotalWeight();
}
else if(__instance == backpack.GetComponent<Container>().GetInventory())
{
__result *= backpackWeightMult.Value;
}
}
}
[HarmonyPatch(typeof(Player), "OnDeath")]
static class OnDeath_Patch
{
static void Prefix(Player __instance)
{
if (!modEnabled.Value || !backpack || !Player.m_localPlayer || __instance.GetPlayerID() != Player.m_localPlayer.GetPlayerID() || backpack.GetComponent<Container>().GetInventory().NrOfItems() == 0)
return;
if (dropInventoryOnDeath.Value)
{
if (createTombStone.Value)
{
GameObject gameObject = Instantiate(__instance.m_tombstone, __instance.GetCenterPoint() + Vector3.forward, __instance.transform.rotation);
gameObject.GetComponent<Container>().GetInventory().MoveInventoryToGrave(backpack.GetComponent<Container>().GetInventory());
TombStone component = gameObject.GetComponent<TombStone>();
PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
component.Setup(playerProfile.GetName(), playerProfile.GetPlayerID());
}
else
Traverse.Create(backpack.GetComponent<Container>()).Method("DropAllItems", new object[] { }).GetValue();
}
Dbgl("Moving backpack to scene on death");
MoveBackpackToScene();
ResetBackpackSector();
}
}
private static void MoveBackpackToScene()
{
saving = true;
backpack.transform.SetParent(Traverse.Create(ZNetScene.instance).Field("m_netSceneRoot").GetValue<GameObject>().transform);
backpack.transform.localPosition = Player.m_localPlayer.transform.localPosition;
}
private static void ResetBackpackSector()
{
if (backpack?.GetComponent<ZNetView>()?.GetZDO() == null)
return;
Vector2i zone = ZoneSystem.instance.GetZone(ZNet.instance.GetReferencePosition());
Traverse.Create(backpack.GetComponent<ZNetView>().GetZDO()).Method("SetSector", new object[] { zone }).GetValue();
}
//[HarmonyPatch(typeof(Game), "SavePlayerProfile")]
static class SavePlayerProfile_Patch
{
static void Prefix()
{
if (!modEnabled.Value)
return;
saving = true;
if (backpack)
{
//Dbgl("Moving backpack to scene on save");
//MoveBackpackToScene();
}
}
}
[HarmonyPatch(typeof(Terminal), "InputText")]
static class InputText_Patch
{
static bool Prefix(Terminal __instance)
{
if (!modEnabled.Value)
return true;
string text = __instance.m_input.text;
if (text.ToLower().Equals($"{typeof(BepInExPlugin).Namespace.ToLower()} reset"))
{
context.Config.Reload();
context.Config.Save();
if (Player.m_localPlayer && backpack)
{
backpack.transform.SetParent(Player.m_localPlayer.transform);
InitBackpack();
}
Traverse.Create(__instance).Method("AddString", new object[] { text }).GetValue();
Traverse.Create(__instance).Method("AddString", new object[] { $"{context.Info.Metadata.Name} config reloaded" }).GetValue();
return false;
}
return true;
}
}
}
}