-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarmonyPatches.cs
364 lines (334 loc) · 15 KB
/
HarmonyPatches.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
using Harmony;
using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using HarmonyIOLoader.NPC;
using HarmonyIOLoader.Loot;
using HarmonyIOLoader.APC;
using Network;
using System.Reflection;
namespace HarmonyIOLoader
{
[HarmonyPatch(typeof(WorldSerialization), "GetMap", new Type[] { typeof(string) })]
class GetMapPatch
{
static void Prefix(ref WorldSerialization __instance)
{
if (MapDataDecompressor.IsLoaded) return;
if (__instance == null) return;
foreach (var map in __instance.world.maps)
{
if (map != null && !MapDataDecompressor.VanillaMapNames.Contains(map.name))
{
var ioData = MapDataDecompressor.Deserialize<SerializedIOData>(map.data, out bool flag);
if (flag)
{
MapDataDecompressor.IOMapName = map.name;
IOEntityProcessor.SerializedIOData = ioData;
if (Config.DEBUG) Logger.LogMessage($"Found matching IO map name - {map.name}");
Logger.LogMessage($"Sucessfully found {IOEntityProcessor.SerializedIOData.entities.Count} IO entities");
continue;
}
var pathData = MapDataDecompressor.Deserialize<SerializedPathList>(map.data, out flag);
if (flag)
{
MapDataDecompressor.OceanPathMapName = map.name;
OceanPathProcessor.serializedPathList = pathData;
if (Config.DEBUG) Logger.LogMessage($"Found matching ocean path map name - {map.name}");
Logger.LogMessage($"Sucessfully found {pathData.vectorData.Count} ocean path nodes");
continue;
}
var npcData = MapDataDecompressor.Deserialize<SerializedNPCData>(map.data, out flag);
if(flag)
{
MapDataDecompressor.NPCMapName = map.name;
NPCProcessor.serializedNPCData = npcData;
if(Config.DEBUG) Logger.LogMessage($"Found matching NPC spawner map name - {map.name}");
Logger.LogMessage($"Sucessfully found {npcData.npcSpawners.Count} NPC spawners");
NPCProcessor.Process();
continue;
}
var apcData = MapDataDecompressor.Deserialize<SerializedAPCPathList>(map.data, out flag);
if(flag)
{
MapDataDecompressor.APCMapName = map.name;
APCProcessor.serializedAPCPathList = apcData;
if (Config.DEBUG) Logger.LogMessage($"Found matching APC spawner map name - {map.name}");
Logger.LogMessage($"Successfully found {apcData.paths.Count} APC spawners");
APCProcessor.Process();
continue;
}
}
}
MapDataDecompressor.IsLoaded = true;
}
}
#region Spawn
[HarmonyPatch(typeof(BaseNetworkable), "Spawn")]
class SpawnPatch
{
static void Postfix(ref BaseNetworkable __instance)
{
if (!DeployableProcessor.SaveLoaded && DeployableProcessor.baseEntityInfoList != null)
{
bool isInList = false;
for (int i = 0; i < DeployableProcessor.baseEntityInfoList.Count; i++)
{
DeployableProcessor.BaseEntityInfo elem = DeployableProcessor.baseEntityInfoList[i];
if (__instance.prefabID == elem.prefabId && __instance.transform.position == elem.position)
{
isInList = true;
}
}
if (isInList)
DeployableProcessor.ProcessEntity(__instance);
}
if (__instance != null && !(__instance is Signage) && __instance is IOEntity && IOEntityProcessor.SerializedIOData != null)
{
foreach (var sIOEnt in IOEntityProcessor.SerializedIOData.entities)
{
if (IOEntityProcessor.AreEqual(__instance, sIOEnt))
{
IOEntityProcessor.entityList.Add(__instance);
if (IOEntityProcessor.entityList.Count == IOEntityProcessor.SerializedIOData?.entities.Count)
{
Logger.LogMessage("Starting to process IO entities...");
IOEntityProcessor.Process();
}
}
}
}
}
}
[HarmonyPatch(typeof(World), "Spawn", new Type[] { typeof(string), typeof(Prefab), typeof(Vector3), typeof(Quaternion), typeof(Vector3) })]
class PrefabSpawnPatch
{
static bool Prefix(ref string category, ref Prefab prefab, ref Vector3 position, ref Quaternion rotation, ref Vector3 scale)
{
if (!prefab.Object) return false;
if (!World.Cached)
{
prefab.ApplyTerrainPlacements(position, rotation, scale);
prefab.ApplyTerrainModifiers(position, rotation, scale);
}
var go = prefab.Spawn(position, rotation, scale, true);
if (go)
{
go.SetHierarchyGroup(category, true, false);
}
var entity = go.GetComponent<BaseEntity>();
if (entity != null && !DeployableProcessor.SaveLoaded && !(entity is LootContainer))
{
entity.enableSaving = false;
DeployableProcessor.ProcessEntity(entity);
DeployableProcessor.baseEntityInfoList.Add(new DeployableProcessor.BaseEntityInfo(entity));
}
if (DeskKeycardSpawner.SpawnerNames.Contains(go.name))
{
int i = 0;
while (i < go.transform.childCount)
{
Transform child = go.transform.GetChild(i);
if (!StringEx.Contains(child.name, "card_spawner", CompareOptions.IgnoreCase))
{
i++;
}
else
{
var component = child.GetComponent<SpawnGroup>();
if (component != null)
{
var spawnClock = typeof(SpawnGroup).GetField("spawnClock", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(component) as LocalClock;
if (spawnClock != null && component.WantsTimedSpawn()) spawnClock.events.Clear();
var spawnGroups = typeof(SpawnHandler).GetField("SpawnGroups", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(SingletonComponent<SpawnHandler>.Instance) as List<ISpawnGroup>;
if(spawnGroups != null) spawnGroups.Remove(component);
DeskKeycardSpawner.KeycardManagers.Add(component.gameObject.AddComponent<DeskKeycardSpawner.KeycardSpawnerManager>());
break;
}
break;
}
}
}
return false;
}
}
#endregion
#region Kill
[HarmonyPatch(typeof(BaseCombatEntity), "OnAttacked", new Type[] { typeof(HitInfo) })]
class OnAttackedPatch
{
static bool Prefix(ref BaseCombatEntity __instance, ref HitInfo info)
{
try
{
if (__instance != null && info != null)
{
if (__instance is SamSite || __instance is Landmine || __instance is BearTrap || __instance is Barricade)
return true;
if (DeployableProcessor.entityList.Contains(__instance))
return false;
if (__instance is AutoTurret && IOEntityProcessor.entityList.Contains(__instance as AutoTurret) && __instance.HasComponent<AutoTurretManager>())
return false;
}
if (IOEntityProcessor.entityList.Contains(__instance)) return false;
return true;
} catch(Exception ex)
{
Logger.LogMessage("Caught exception " + ex);
return true;
}
}
}
[HarmonyPatch(typeof(BaseNetworkable), "Kill", new Type[] { typeof(BaseNetworkable.DestroyMode) })]
class KillPatch
{
static bool Prefix(ref BaseNetworkable __instance)
{
try {
//JunkPileProcessor.OnEntityKilled(__instance);
if (!(__instance is BaseCombatEntity)) return true;
var entity = __instance as BaseCombatEntity;
if (entity != null && DeployableProcessor.entityList.Contains(entity) && (entity is Landmine || entity is BearTrap || entity is Barricade))
{
if (!DeployableProcessor.SaveLoaded)
{
DeployableProcessor.SpawnEntities(new DeployableProcessor.BaseCombatEntityInfo(entity));
}
else
{
DeployableProcessor.SpawnEntities(new DeployableProcessor.BaseCombatEntityInfo(entity), 120, 360);
}
DeployableProcessor.entityList.Remove(entity);
}
if (DeployableProcessor.entityList.Contains((BaseCombatEntity)__instance)) return false;
//IO
if (!(__instance is IOEntity)) return true;
var IOEnt = __instance as IOEntity;
if (IOEnt is Signage) return true;
if (IOEntityProcessor.entityList.Contains(IOEnt)) return false;
for (int i = 0; i < IOEntityProcessor.SerializedIOData.entities.Count; i++)
{
if (IOEntityProcessor.AreEqual(IOEnt, IOEntityProcessor.SerializedIOData.entities[i]))
{
return false;
}
}
return true;
} catch(Exception ex)
{
Logger.LogMessage("Caught exception: " + ex);
return true;
}
}
}
#endregion
#region AutoTurret
[HarmonyPatch(typeof(Item), "UseItem", new Type[] { typeof(int) })]
class UseItemPatch
{
static void Prefix(ref Item __instance, ref int amountToConsume)
{
if (amountToConsume <= 0) return;
var parent = __instance.parent;
var ent = (parent?.entityOwner);
if (!(ent == null) && (ent is AutoTurret && IOEntityProcessor.entityList.Contains(ent as AutoTurret)))
{
AutoTurretManager component = ent.GetComponent<AutoTurretManager>();
if (component != null && component.UnlimitedAmmo)
{
__instance.amount += amountToConsume * 2;
}
}
}
}
[HarmonyPatch(typeof(AutoTurret), "AddSelfAuthorize", new Type[] { typeof(BaseEntity.RPCMessage) })]
class TurretAuthorizePatch
{
static bool Prefix(ref AutoTurret __instance, ref BaseEntity.RPCMessage rpc)
{
if (IOEntityProcessor.entityList.Contains(__instance) && !rpc.player.IsAdmin) return false;
return true;
}
}
#endregion
#region CargoshipPath
[HarmonyPatch(typeof(BaseBoat), "GenerateOceanPatrolPath", new Type[] { typeof(float), typeof(float) })]
class GenerateOceanPatrolPathPatch
{
static bool Prefix(ref List<Vector3> __result)
{
var pathNodesList = OceanPathProcessor.Process();
if (pathNodesList == null) return true;
__result = pathNodesList;
return false;
}
}
#endregion
#region Misc
[HarmonyPatch(typeof(SaveRestore), "Load", new Type[] { typeof(string), typeof(bool) })]
class OnSaveLoadPatch
{
static void Postfix(ref bool __result)
{
DeployableProcessor.SaveLoaded = true;
if(__result) foreach(var manager in DeskKeycardSpawner.KeycardManagers) manager.SpawnImmidiate();
if (Config.DEBUG) Logger.LogWarning("! HarmonyIOLoader is compiled with debug ON!");
}
}
[HarmonyPatch(typeof(StabilityEntity), "StabilityCheck")]
class OnStabilityCheckPatch
{
static bool Prefix(ref StabilityEntity __instance)
{
if (__instance != null && DeployableProcessor.entityList.Contains(__instance))
return false;
return true;
}
}
#endregion
#region Debugging
[HarmonyPatch(typeof(ConsoleNetwork), "OnClientCommand", new Type[] { typeof(Message) })]
class OnClientCommandPatch
{
static bool Prefix(ref Message packet)
{
if (packet.read.Unread > ConVar.Server.maxpacketsize_command)
{
Debug.LogWarning("Dropping client command due to size");
return false;
}
string text = packet.read.StringRaw(1048576U);
if (packet.connection == null || !packet.connection.connected)
{
Debug.LogWarning("Client without connection tried to run command: " + text);
return false;
}
var player = BasePlayer.FindByID(packet.connection.userid);
if (player == null) return false;
if (Config.DEBUG)
{
switch (text)
{
case "loader.apc.spawn":
CommandHandler.SpawnBradley(player);
return false;
case "loader.apc.show":
CommandHandler.ShowBradleyPath(player);
return false;
case "loader.ocean.show":
CommandHandler.ShowOceanPath(player);
return false;
case "loader.npc.show":
CommandHandler.ShowNPCSpawners(player);
return false;
}
}
string text2 = ConsoleSystem.Run(ConsoleSystem.Option.Server.FromConnection(packet.connection).Quiet(), text, Array.Empty<object>());
if (!string.IsNullOrEmpty(text2))
typeof(ConsoleNetwork).GetMethod("SendClientReply", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { packet.connection, text2 });
return false;
}
}
#endregion
}