-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryFixMod.cs
More file actions
208 lines (192 loc) · 6.94 KB
/
InventoryFixMod.cs
File metadata and controls
208 lines (192 loc) · 6.94 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Reflection;
using Assets.Scripts.Inventory;
using Assets.Scripts.Objects;
using Assets.Scripts.Objects.Entities;
using Assets.Scripts.Objects.Items;
using Assets.Scripts.Serialization;
using Assets.Scripts.UI;
using HarmonyLib;
using StationeersMods.Interface;
using UnityEngine;
using UnityEngine.UI;
namespace inventoryfixmod
{
[StationeersMod("InventoryFixMod", "InventoryFixMod", "0.1.3")]
class InventoryFixMod : ModBehaviour
{
public override void OnLoaded(ContentHandler contentHandler)
{
Harmony harmony = new Harmony("InventoryFixMod");
harmony.PatchAll();
}
}
[HarmonyPatch(typeof(InventoryWindowManager))]
public static class InventoryPatch
{
static List<SlotDisplayButton> AllButtons;
static Action<SlotDisplay> OnPlayerInteract;
static FieldInfo windowField;
static InventoryPatch()
{
AllButtons = typeof(InventoryWindowManager).GetField("AllButtons", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null) as List<SlotDisplayButton>;
var method = typeof(SlotDisplay).GetMethod("OnPlayerInteract", BindingFlags.Instance | BindingFlags.NonPublic);
OnPlayerInteract = (Action<SlotDisplay>)Delegate.CreateDelegate(typeof(Action<SlotDisplay>), null, method);
windowField = typeof(SlotDisplay).GetField("_localInventoryWindow", BindingFlags.Instance | BindingFlags.NonPublic);
}
public static (int, int, bool) WindowToPathData(InventoryWindow window)
{
var fullPath = 0ul;
var pathLen = 0;
var slot = window.ParentSlot;
while (slot != null && slot.Get() != InventoryWindowManager.Instance.Parent)
{
// add 1 to slot index so we distinguish end of path from slot 0
fullPath <<= 8;
fullPath |= (ulong)(slot.SlotIndex + 1) & 0xFF;
slot = (slot.Parent as DynamicThing)?.ParentSlot;
pathLen++;
}
if (pathLen > 7)
{
return (0, 0, false);
}
fullPath |= 0xFF00000000000000; // fill top byte to mark as new path format
return ((int)fullPath, (int)(fullPath >> 32), true);
}
public static List<int> PathDataToPath(int slotId, int stringHash)
{
const ulong topByte = 0xFF00000000000000;
var fullPath = ((ulong)slotId & 0xFFFFFFFF) | ((ulong)stringHash << 32);
if ((fullPath & topByte) != topByte)
{
// old style path
var window = InventoryWindowManager.Instance.Windows.Find(w => w.ParentSlot.StringHash == stringHash);
if (window == null)
{
return null;
}
return new List<int> { window.ParentSlot.SlotIndex };
}
fullPath &= ~topByte;
var path = new List<int>();
while (fullPath != 0)
{
path.Add((int)(fullPath & 0xFF) - 1);
fullPath >>= 8;
}
return path;
}
static InventoryWindow GetWindowByPath(List<int> path)
{
if (path == null || path.Count == 0)
{
return null;
}
var curThing = InventoryWindowManager.Instance.Parent as Thing;
InventoryWindow curWindow = null;
foreach (var slotIndex in path)
{
if (curThing == null || slotIndex >= curThing?.Slots?.Count)
{
// invalid index
return null;
}
var slot = curThing.Slots[slotIndex];
if (slot.Display == null)
{
return null;
}
OnPlayerInteract(slot.Display); // setup window if needed
curThing = slot.Get();
curWindow = windowField.GetValue(slot.Display) as InventoryWindow;
}
return curWindow;
}
[HarmonyPatch(nameof(InventoryWindowManager.GenerateUISaveData)), HarmonyPrefix]
static bool GenerateUISaveData(InventoryWindowManager __instance, ref UserInterfaceSaveData __result)
{
UserInterfaceSaveData saveData = new UserInterfaceSaveData();
foreach (InventoryWindow window in __instance.Windows)
{
if (window.GameObject == null) continue;
var (id, hash, ok) = WindowToPathData(window);
if (!ok) continue;
saveData.OpenSlots.Add(new WindowSaveData()
{
SlotId = id,
StringHash = hash,
IsOpen = window.IsVisible,
IsUndocked = window.IsUndocked,
Position = window.RectTransform.position
});
}
saveData.SelectedButton = InventoryWindowManager.CurrentButtonIndex;
saveData.ActiveHandSlot = InventoryWindowManager.ActiveHand?.SlotIndex ?? 0;
__result = saveData;
return false;
}
[HarmonyPatch(nameof(InventoryWindowManager.LoadUserInterfaceData)), HarmonyPrefix]
static bool LoadUserInterfaceData(UserInterfaceSaveData userInterfaceSaveData)
{
if (userInterfaceSaveData == null)
return false;
foreach (WindowSaveData openSlot in userInterfaceSaveData.OpenSlots)
{
var window = GetWindowByPath(PathDataToPath(openSlot.SlotId, openSlot.StringHash));
if (window != null)
{
window.SetVisible(openSlot.IsOpen);
if (openSlot.IsUndocked)
{
LayoutRebuilder.ForceRebuildLayoutImmediate(window.RectTransform);
window.Undocked();
window.RectTransform.position = (Vector3)openSlot.Position;
window.ClampToScreen();
}
}
}
var currentScollButton = InventoryWindowManager.CurrentScollButton;
if (AllButtons.Count > 0)
{
if (userInterfaceSaveData.SelectedButton >= AllButtons.Count || userInterfaceSaveData.SelectedButton < 0)
userInterfaceSaveData.SelectedButton = 0;
InventoryWindowManager.CurrentScollButton = AllButtons[userInterfaceSaveData.SelectedButton];
InventoryWindowManager.CurrentScollButton.RefreshAnimation(false);
}
if (currentScollButton)
currentScollButton.RefreshAnimation(false);
if (InventoryWindowManager.ActiveHand.SlotIndex == userInterfaceSaveData.ActiveHandSlot)
{
if (InventoryWindowManager.ActiveHand.Get() is Tablet occupant)
occupant.InActiveHand();
InventoryManager.OnActiveEvent();
}
if (InventoryWindowManager.ActiveHand.SlotIndex != userInterfaceSaveData.ActiveHandSlot)
Human.LocalHuman.SwapHands();
InventoryWindowManager.WorldXmlUISaveData = null;
return false;
}
[HarmonyPatch(nameof(InventoryWindowManager.ToggleWindows)), HarmonyPrefix]
static bool ToggleWindows(bool show, InventoryWindowManager __instance)
{
foreach (var window in __instance.Windows)
{
if (window == null)
continue;
window.GameObject?.SetActive(show && window.IsVisible);
}
return false;
}
}
[HarmonyPatch(typeof(InventoryWindow))]
static class InventoryWindowPatch
{
[HarmonyPatch(nameof(InventoryWindow.SetVisible)), HarmonyPostfix]
static void SetVisible(InventoryWindow __instance, bool isVisble)
{
__instance.RectTransform.gameObject.SetActive(isVisble);
}
}
}