-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
297 lines (264 loc) · 9.62 KB
/
Plugin.cs
File metadata and controls
297 lines (264 loc) · 9.62 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
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
using System;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using ScavShrapnelMod.Core;
using ScavShrapnelMod.Logic;
using ScavShrapnelMod.Net;
using ScavShrapnelMod.Patches;
using ScavShrapnelMod.Helpers;
namespace ScavShrapnelMod
{
/// <summary>
/// BepInEx plugin entry point.
///
/// DETECTION ARCHITECTURE:
/// Layer 1: Harmony Prefix/Postfix — WorldGeneration.CreateExplosion (public static)
/// Layer 2: Harmony PatchAll — DestroyBackupPatch, WorldLoadPatch, etc.
/// Layer 3: MonoBehaviour polling — TurretShotMonitor, GunShotMonitor
/// (Harmony can't intercept private Update() on Unity 2022.3)
/// Layer 4: MonoBehaviour polling — TurretDeathMonitor (health ≤ 0)
/// </summary>
[BepInPlugin(Guid, Name, Version)]
public sealed class Plugin : BaseUnityPlugin
{
public const string Guid = "ScavShrapnelMod";
public const string Name = "ScavShrapnelMod";
public const string Version = "0.9.2";
internal static BepInEx.Logging.ManualLogSource Log;
internal static bool CommandsRegistered;
internal static bool VisualsWarmed;
private Harmony _harmony;
private static bool _resetNotificationShown;
private void Awake()
{
Log = Logger;
Logger.LogInfo("╔═══════════════════════════════════════╗");
Logger.LogInfo("║ Shrapnel Overhaul Mod v" + Version + " ║");
Logger.LogInfo("║ Realistic Explosion Fragments ║");
Logger.LogInfo("╚═══════════════════════════════════════╝");
ShrapnelConfig.Bind(Config);
GameVersionChecker.Check();
_harmony = new Harmony(Guid);
ApplyPatches();
CreateCommandRegistrar();
}
private void ApplyPatches()
{
// LAYER 1: WorldGeneration.CreateExplosion (manual — needs priority)
try
{
var targetMethod = AccessTools.Method(
typeof(WorldGeneration),
nameof(WorldGeneration.CreateExplosion),
new[] { typeof(ExplosionParams) });
if (targetMethod != null)
{
_harmony.Patch(
targetMethod,
prefix: new HarmonyMethod(
AccessTools.Method(
typeof(CreateExplosionPatch),
nameof(CreateExplosionPatch.Prefix)))
{ priority = Priority.Low },
postfix: new HarmonyMethod(
AccessTools.Method(
typeof(CreateExplosionPatch),
nameof(CreateExplosionPatch.Postfix)))
{ priority = Priority.Last });
Logger.LogInfo(
"[Patch] ✓ Layer 1: WorldGeneration.CreateExplosion");
}
else
{
Logger.LogError(
"[Patch] ✗ Layer 1: CreateExplosion not found!");
}
}
catch (Exception e)
{
Logger.LogError($"[Patch] ✗ Layer 1: {e.Message}");
}
// LAYER 2: PatchAll (DestroyBackupPatch, ConsoleStartPatch,
// WorldLoadPatch, PlayerCameraStartPatch)
try
{
_harmony.PatchAll();
Logger.LogInfo("[Patch] ✓ PatchAll complete");
}
catch (Exception e)
{
Logger.LogError(
$"[Patch] PatchAll FAILED: {e.Message}\n{e.StackTrace}");
}
// LAYER 3: Shot detectors (MonoBehaviour — proven stable)
ShotDetectorPatches.CreateDetectors();
// LAYER 4: Turret death monitor
CreateMonoBehaviour<TurretDeathMonitor>("TurretDeathMonitor");
Logger.LogInfo("[Patch] ✓ All detection layers active");
}
/// <summary>
/// Creates a persistent DontDestroyOnLoad MonoBehaviour.
/// </summary>
private void CreateMonoBehaviour<T>(string name) where T : MonoBehaviour
{
try
{
var go = new GameObject($"ShrapnelMod_{name}")
{
hideFlags = HideFlags.HideAndDontSave
};
DontDestroyOnLoad(go);
go.AddComponent<T>();
Logger.LogInfo($"[Patch] ✓ {name} created");
}
catch (Exception e)
{
Logger.LogError($"[Patch] ✗ {name}: {e.Message}");
}
}
private void CreateCommandRegistrar()
{
try
{
var registrar = new GameObject("ShrapnelMod_Registrar")
{
hideFlags = HideFlags.HideAndDontSave
};
DontDestroyOnLoad(registrar);
registrar.AddComponent<ConsoleCommandRegistrar>();
}
catch (Exception e)
{
Logger.LogError($"[Init] Failed to create registrar: {e}");
}
}
private void OnDestroy()
{
_harmony?.UnpatchSelf();
}
internal static void RegisterCommands()
{
if (CommandsRegistered) return;
try
{
Console.Register();
CommandsRegistered = true;
Log.LogInfo("[Console] Commands registered successfully");
Console.Log(
$"v{Version} loaded! Type 'help' to see shrapnel_ commands.");
}
catch (Exception e)
{
Log.LogError($"[Console] Registration failed: {e}");
}
}
internal static void WarmVisuals()
{
if (VisualsWarmed) return;
try
{
ShrapnelVisuals.PreWarm();
AshParticlePoolManager.Initialize();
ParticlePoolManager.Initialize();
ShrapnelSpawnLogic.ResetThrottle();
VisualsWarmed = true;
if (MultiplayerHelper.IsNetworkRunning)
ShrapnelNetSync.Initialize();
Log.LogInfo(
$"[Init] Warmed. AshPools={AshParticlePoolManager.Initialized}" +
$" SparkPool={ParticlePoolManager.Initialized}");
}
catch (Exception e)
{
Log.LogError(
$"[Init] PreWarm FAILED: {e.Message}\n{e.StackTrace}");
}
}
internal static void OnWorldLoad()
{
ShrapnelNetSync.Shutdown();
VisualsWarmed = false;
ShrapnelVisuals.ResetMaterials();
AshParticlePoolManager.Shutdown();
ParticlePoolManager.Shutdown();
ShrapnelSpawnLogic.ResetThrottle();
// WHY: Clear stale GunScript/TurretScript references from previous world
ShotDetectorPatches.ForceRescan();
WarmVisuals();
ShowConfigResetNotification();
}
private static void ShowConfigResetNotification()
{
if (_resetNotificationShown) return;
_resetNotificationShown = true;
string notification = ShrapnelConfig.GetResetNotification();
if (notification == null) return;
Log.LogWarning(notification);
Console.Log(notification);
}
}
internal sealed class ConsoleCommandRegistrar : MonoBehaviour
{
private void Update()
{
if (Plugin.CommandsRegistered)
{
Destroy(gameObject);
return;
}
if (ConsoleScript.instance != null &&
ConsoleScript.Commands != null &&
ConsoleScript.Commands.Count > 0)
{
Plugin.RegisterCommands();
Destroy(gameObject);
}
}
}
[HarmonyPatch(typeof(ConsoleScript), "Start")]
internal static class ConsoleStartPatch
{
[HarmonyPostfix]
static void Postfix()
{
if (ConsoleScript.Commands?.Count > 0
&& !Plugin.CommandsRegistered)
Plugin.RegisterCommands();
}
}
[HarmonyPatch(typeof(WorldGeneration), "UpdateChunkVisibility")]
internal static class WorldLoadPatch
{
private static WorldGeneration _lastWarmedWorld;
[HarmonyPostfix]
static void Postfix()
{
try
{
var world = WorldGeneration.world;
if (world == null || world.generatingWorld) return;
if (world == _lastWarmedWorld) return;
_lastWarmedWorld = world;
Plugin.OnWorldLoad();
Plugin.Log.LogInfo(
"[WorldLoad] Visuals pre-warmed on world load");
}
catch { }
}
}
[HarmonyPatch(typeof(PlayerCamera), "Start")]
internal static class PlayerCameraStartPatch
{
[HarmonyPostfix]
static void Postfix()
{
if (!Plugin.VisualsWarmed)
{
Plugin.WarmVisuals();
Plugin.Log.LogInfo(
"[PlayerCamera] Visuals pre-warmed on camera start");
}
}
}
}