-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPatches.cs
311 lines (275 loc) · 11 KB
/
Patches.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using BattleTech;
using BattleTech.Rendering;
using BattleTech.Save;
using BattleTech.Save.SaveGameStructure.Messages;
using BattleTech.UI;
using Harmony;
using UnityEngine;
using static ScorchedEarth.ScorchedEarth;
// ReSharper disable UnusedMember.Local
// ReSharper disable NotAccessedVariable
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable InconsistentNaming
namespace ScorchedEarth
{
internal class Patches
{
internal static void Init()
{
try
{
var terrainDecal = AccessTools.Inner(typeof(FootstepManager), "TerrainDecal");
var original = AccessTools.Constructor(terrainDecal,
new[] {typeof(Vector3), typeof(Quaternion), typeof(Vector3), typeof(float)});
var transpiler = AccessTools.Method(typeof(Patches), nameof(TerrainDecal_Ctor_Transpiler));
harmony.Patch(original, null, null, new HarmonyMethod(transpiler));
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
catch (Exception ex)
{
Log(ex);
}
}
private static IEnumerable<CodeInstruction> TerrainDecal_Ctor_Transpiler(
IEnumerable<CodeInstruction> instructions)
{
var codes = instructions.ToList();
codes[9].opcode = OpCodes.Ldc_R4;
codes[9].operand = float.MaxValue;
return codes.AsEnumerable();
}
[HarmonyPatch(typeof(BTCustomRenderer), "OnPreCull")]
public static class BTCustomRenderer_OnPreCull_Patch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
// replace DrawDecals method call with our own and add argument for CustomCommandBuffers (for speed)
// note that this is using the publicized assembly https://github.com/CabbageCrow/AssemblyPublicizer
var codes = instructions.ToList();
var original = AccessTools.Method(typeof(BTCustomRenderer), nameof(BTCustomRenderer.DrawDecals));
var replacement = AccessTools.Method(typeof(Patches), nameof(DrawDecals),
new[] {typeof(BTCustomRenderer), typeof(Camera), typeof(BTCustomRenderer.CustomCommandBuffers)});
var index = codes.FindIndex(c => c.operand is MethodInfo methodInfo && methodInfo == original);
codes.Insert(index, new CodeInstruction(OpCodes.Ldloc_1));
codes[index + 1].operand = replacement;
return codes.AsEnumerable();
}
}
public static void DrawDecals(object instance, Camera camera, BTCustomRenderer.CustomCommandBuffers customCommandBuffers)
{
//var timer = new Stopwatch();
//timer.Restart();
var deferredDecalsBuffer = customCommandBuffers.deferredDecalsBuffer;
var skipDecals = ((BTCustomRenderer) instance).skipDecals;
if (!skipDecals)
{
BTDecal.DecalController.ProcessCommandBuffer(deferredDecalsBuffer, camera);
}
int numFootsteps;
deferredDecalsBuffer.SetGlobalFloat(BTCustomRenderer.Uniforms._FootstepScale, 1f);
var footsteps = Helpers.ProcessFootsteps(out numFootsteps);
foreach (var chunk in footsteps.Where(x => x != null))
{
deferredDecalsBuffer.DrawMeshInstanced(
BTDecal.DecalMesh.DecalMeshFull,
0,
FootstepManager.Instance.footstepMaterial,
0,
chunk,
chunkSize);
}
int numScorches;
var scorches = Helpers.ProcessScorches(out numScorches);
foreach (var chunk in scorches.Where(x => x != null))
{
deferredDecalsBuffer.DrawMeshInstanced(
BTDecal.DecalMesh.DecalMeshFull,
0,
FootstepManager.Instance.scorchMaterial,
0,
chunk,
chunkSize);
}
//var time = timer.ElapsedTicks;
//if (time > 200000)
//{
// Log("SLOW: " + timer.ElapsedTicks);
// Log(new string('*', 100));
//}
}
}
[HarmonyPatch(typeof(FootstepManager), nameof(FootstepManager.AddFootstep))]
public static class FootstepManager_AddFootstep_Patch
{
public static bool Prefix(Vector3 position, List<FootstepManager.TerrainDecal> ____footstepList)
{
try
{
Log($"Footstep count: {____footstepList.Count}/{____footstepList.Capacity}");
if (____footstepList.All(terrainDecal => Helpers.Distance(terrainDecal.transformMatrix, position) > FootstepDistance))
{
// FIFO logic, only act when needed
if (____footstepList.Count == FootstepManager.maxDecals)
{
Log("maxDecals exceeded");
____footstepList.RemoveAt(0);
Log("oldest footstep removed");
}
Log("Adding footstep");
return true;
}
}
catch (Exception ex)
{
Log(ex);
}
Log("======================================================== dropped footstep");
return false;
}
}
// draws scorches without logic checks, also providing FIFO by removing the first scorch element as needed
[HarmonyPatch(typeof(FootstepManager), nameof(FootstepManager.AddScorch))]
public class FootstepManager_AddScorch_Patch
{
public static bool Prefix(Vector3 position, List<FootstepManager.TerrainDecal> ____scorchList)
{
try
{
Log($"Scorch count: {____scorchList.Count}/{____scorchList.Capacity}");
// ReSharper disable once PossibleNullReferenceException
if (____scorchList.All(terrainDecal =>
Helpers.Distance(terrainDecal.transformMatrix, position) > DecalDistance))
{
// FIFO logic, only act when needed
if (____scorchList.Count == FootstepManager.maxDecals)
{
Log("maxDecals exceeded");
____scorchList.RemoveAt(0);
Log("oldest scorch removed");
}
return true;
}
}
catch (Exception ex)
{
Log(ex);
}
Log("======================================================== dropped scorch");
return false;
}
}
[HarmonyPatch(typeof(GameInstance), "SaveComplete")]
public static class GameInstance_SaveComplete_Patch
{
public static void Postfix(StructureSaveComplete message)
{
try
{
if (!modSettings.SaveState)
{
return;
}
Log("GameInstance_SaveComplete_Patch");
Helpers.CleanupSaves();
if (!Directory.Exists(modSettings.SaveDirectory))
{
Directory.CreateDirectory(modSettings.SaveDirectory);
}
var filename = modSettings.SaveDirectory + "/" + message.Slot.FileID.Substring(4) + ".gzip";
var results = new List<IList>
{
Helpers.ExtractDecals("scorchList"),
Helpers.ExtractDecals("footstepList")
};
Helpers.SaveDecals(results, filename);
}
catch (Exception ex)
{
Log(ex);
}
}
}
public class Hydrate
{
private static string fileID;
// save the fileID here because it's not easily available at Briefing.InitializeContractComplete
[HarmonyPatch(typeof(GameInstance), "Load")]
public static class GameInstance_Load_Patch
{
public static void Postfix(GameInstanceSave save)
{
if (!modSettings.SaveState)
{
return;
}
fileID = save.FileID;
}
}
[HarmonyPatch(typeof(CombatGameState), nameof(CombatGameState._Init))]
public static class CombatGameState__Init_Patch
{
private static bool completed;
public static void Postfix(CombatGameState __instance)
{
if (!__instance.WasFromSave && !completed)
{
Log("Clearing fileID");
fileID = "";
completed = true;
}
}
}
[HarmonyPatch(typeof(Briefing), "InitializeContractComplete")]
public static class Briefing_InitializeContractComplete_Patch
{
public static void Postfix()
{
try
{
if (!modSettings.SaveState)
{
return;
}
if (string.IsNullOrEmpty(fileID))
{
Log("New instance");
return;
}
var filename = modSettings.SaveDirectory + "/" + fileID.Substring(4) + ".gzip";
if (!Directory.Exists(modSettings.SaveDirectory) ||
!File.Exists(filename))
{
Log("SaveState disabled, or missing data");
return;
}
var results = new List<IList>();
Log("Hydrate scorches and footsteps");
// first element is always scorches, 2nd footsteps
results.Add(Helpers.RecreateDecals(filename, "scorchList", 0));
results.Add(Helpers.RecreateDecals(filename, "footstepList", 1));
var scorchList = Traverse.Create(FootstepManager.Instance).Property("scorchList").GetValue<IList>();
var footstepList = Traverse.Create(FootstepManager.Instance).Property("footstepList").GetValue<IList>();
foreach (var decal in results[0])
{
scorchList.Add(decal);
}
foreach (var decal in results[1])
{
footstepList.Add(decal);
}
}
catch (Exception ex)
{
Log(ex);
}
}
}
}
}