forked from JereKuusela/BetterContinents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patcher.cs
455 lines (438 loc) · 16.1 KB
/
Patcher.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using HarmonyLib;
namespace BetterContinents;
public partial class BetterContinents
{
public static void DynamicPatch()
{
PatchHeightmap();
PatchBiomeColor();
PatchGetBaseHeight();
PatchGetBiomeHeight();
PatchGetBiome();
PatchAddRivers();
PatchForestFactorPrefix();
PatchForestFactorPostfix();
PatchHeatPrefix();
PatchMapDropOff();
PatchAshlandGap();
PatchDeepNorthGap();
}
private static int HeightmapGetBiomePatched = 0;
private static void PatchHeightmap()
{
var precision = Settings.EnabledForThisWorld ? Settings.BiomePrecision : 0;
if (precision == HeightmapGetBiomePatched)
return;
Log($"Note: Biome precision feature doesn't work at the moment.");
return;
/*
var method1 = AccessTools.Method(typeof(Heightmap), nameof(Heightmap.GetBiome));
var patch1 = AccessTools.Method(typeof(BetterContinents), nameof(GetBiomePatch));
var method2 = AccessTools.Method(typeof(HeightmapBuilder), nameof(HeightmapBuilder.Build));
var patch2 = AccessTools.Method(typeof(BetterContinents), nameof(BuildPatch));
var method3 = AccessTools.Method(typeof(Heightmap), nameof(Heightmap.GetBiomeColor), [typeof(float), typeof(float)]);
var patch3 = AccessTools.Method(typeof(BetterContinents), nameof(GetBiomeColorPatch));
if (HeightmapGetBiomePatched > 0)
{
Log("Unpatching Heightmap.GetBiome");
HarmonyInstance.Unpatch(method1, patch1);
HarmonyInstance.Unpatch(method2, patch2);
HarmonyInstance.Unpatch(method3, patch3);
HeightmapGetBiomePatched = 0;
}
if (precision > 0)
{
Log($"Patching Heightmap.GetBiome with precision {precision}");
HarmonyInstance.Patch(method1, prefix: new(patch1));
HarmonyInstance.Patch(method2, prefix: new(patch2));
HarmonyInstance.Patch(method3, prefix: new(patch3));
HeightmapGetBiomePatched = precision;
}
foreach (Heightmap hm in Heightmap.Instances)
{
hm.m_buildData = null;
hm.Regenerate();
}
ClutterSystem.instance?.ClearAll();
*/
}
private static bool BiomeColorPatched = false;
private static void PatchBiomeColor()
{
var toPatch = Settings.EnabledForThisWorld && Settings.HasTerrainMap;
var method = AccessTools.Method(typeof(Heightmap), nameof(Heightmap.GetBiomeColor), [typeof(float), typeof(float)]);
var patch = AccessTools.Method(typeof(BetterContinents), nameof(GetBiomeColorPatch));
if (toPatch == BiomeColorPatched)
return;
if (BiomeColorPatched)
{
Log("Unpatching Heightmap.GetBiomeColor");
HarmonyInstance.Unpatch(method, patch);
BiomeColorPatched = false;
}
if (toPatch)
{
Log("Patching Heightmap.GetBiomeColor");
HarmonyInstance.Patch(method, prefix: new(patch));
BiomeColorPatched = true;
}
}
private static int GetBaseHeightPatched = 0;
private static void PatchGetBaseHeight()
{
var patchVersion = 0;
if (Settings.EnabledForThisWorld)
{
switch (Settings.Version)
{
case 1:
case 2:
patchVersion = 1;
break;
case 3:
case 4:
case 5:
case 6:
patchVersion = 2;
break;
default:
// GetBaseHeightV3 doesn't work at all without heightmap which makes testing more difficult.
if (Settings.HasHeightMap || Settings.BaseHeightNoise.NoiseLayers.Count > 0)
patchVersion = 3;
break;
}
}
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetBaseHeight));
var patch1 = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBaseHeightPrefixV1));
var patch2 = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBaseHeightPrefixV2));
var patch3 = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBaseHeightPrefixV3));
if (patchVersion == GetBaseHeightPatched)
return;
if (GetBaseHeightPatched == 1)
{
Log("Unpatching WorldGenerator.GetBaseHeight V1");
HarmonyInstance.Unpatch(method, patch1);
GetBaseHeightPatched = 0;
}
if (GetBaseHeightPatched == 2)
{
Log("Unpatching WorldGenerator.GetBaseHeight V2");
HarmonyInstance.Unpatch(method, patch2);
GetBaseHeightPatched = 0;
}
if (GetBaseHeightPatched == 3)
{
Log("Unpatching WorldGenerator.GetBaseHeight");
HarmonyInstance.Unpatch(method, patch3);
GetBaseHeightPatched = 0;
}
if (patchVersion == 1)
{
Log($"Patching WorldGenerator.GetBaseHeight V1");
HarmonyInstance.Patch(method, prefix: new(patch1));
GetBaseHeightPatched = 1;
}
if (patchVersion == 2)
{
Log($"Patching WorldGenerator.GetBaseHeight V2");
HarmonyInstance.Patch(method, prefix: new(patch2));
GetBaseHeightPatched = 2;
}
if (patchVersion == 3)
{
Log($"Patching WorldGenerator.GetBaseHeight");
HarmonyInstance.Patch(method, prefix: new(patch3));
GetBaseHeightPatched = 3;
}
}
// Three different patches for different cases.
private static bool GetBiomeHeightWithRoughPatched = false;
private static bool GetBiomeHeightWithRoughPaintPatched = false;
private static bool GetBiomeHeightWithHeightPatched = false;
private static bool GetBiomeHeightWithHeightPaintPatched = false;
private static bool GetBiomeHeightWithPaintPatched = false;
private static void PatchGetBiomeHeight()
{
var toHeightPaintPatch = Settings.EnabledForThisWorld;
var toHeightPatch = Settings.EnabledForThisWorld;
var toRoughPaintPatch = Settings.EnabledForThisWorld;
var toRoughPatch = Settings.EnabledForThisWorld;
var toPaintPatch = Settings.EnabledForThisWorld;
if (Settings.HasPaintMap || Settings.HasLavaMap || Settings.HasMossMap)
{
toHeightPatch = false;
toRoughPatch = false;
}
else
{
toHeightPaintPatch = false;
toRoughPaintPatch = false;
toPaintPatch = false;
}
if (Settings.ShouldHeightMapOverrideAll)
{
toRoughPaintPatch = false;
toRoughPatch = false;
}
else
{
toHeightPaintPatch = false;
toHeightPatch = false;
}
if (!Settings.HasRoughMap)
{
toRoughPaintPatch = false;
toRoughPatch = false;
}
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetBiomeHeight));
// 5 different patches depending what is needed.
var patchRough = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightWithRough));
var patchRoughPaint = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightWithRoughPaint));
var patchHeight = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightWithHeight));
var patchHeightPaint = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightWithHeightPaint));
var patchPeint = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightWithPaint));
if (toRoughPatch != GetBiomeHeightWithRoughPatched)
{
if (GetBiomeHeightWithRoughPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight with rough");
HarmonyInstance.Unpatch(method, patchRough);
GetBiomeHeightWithRoughPatched = false;
}
if (toRoughPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight with rough");
HarmonyInstance.Patch(method, postfix: new(patchRough));
GetBiomeHeightWithRoughPatched = true;
}
}
if (toRoughPaintPatch != GetBiomeHeightWithRoughPaintPatched)
{
if (GetBiomeHeightWithRoughPaintPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight with rough and paint");
HarmonyInstance.Unpatch(method, patchRoughPaint);
GetBiomeHeightWithRoughPaintPatched = false;
}
if (toRoughPaintPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight with rough and paint");
HarmonyInstance.Patch(method, postfix: new(patchRoughPaint));
GetBiomeHeightWithRoughPaintPatched = true;
}
}
if (toHeightPatch != GetBiomeHeightWithHeightPatched)
{
if (GetBiomeHeightWithHeightPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight with height");
HarmonyInstance.Unpatch(method, patchHeight);
GetBiomeHeightWithHeightPatched = false;
}
if (toHeightPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight with height");
HarmonyInstance.Patch(method, postfix: new(patchHeight));
GetBiomeHeightWithHeightPatched = true;
}
}
if (toHeightPaintPatch != GetBiomeHeightWithHeightPaintPatched)
{
if (GetBiomeHeightWithHeightPaintPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight with height and paint");
HarmonyInstance.Unpatch(method, patchHeightPaint);
GetBiomeHeightWithHeightPaintPatched = false;
}
if (toHeightPaintPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight with height and paint");
HarmonyInstance.Patch(method, postfix: new(patchHeightPaint));
GetBiomeHeightWithHeightPaintPatched = true;
}
}
if (toPaintPatch != GetBiomeHeightWithPaintPatched)
{
if (GetBiomeHeightWithPaintPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight with paint");
HarmonyInstance.Unpatch(method, patchPeint);
GetBiomeHeightWithPaintPatched = false;
}
if (toPaintPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight with paint");
HarmonyInstance.Patch(method, postfix: new(patchPeint));
GetBiomeHeightWithPaintPatched = true;
}
}
}
private static bool MapDropOffPatched = false;
private static void PatchMapDropOff()
{
var toPatch = Settings.EnabledForThisWorld && Settings.DisableMapEdgeDropoff;
if (toPatch == MapDropOffPatched)
return;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetBiomeHeight));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomeHeightTranspiler));
if (MapDropOffPatched)
{
Log("Unpatching WorldGenerator.GetBiomeHeight (map dropoff)");
HarmonyInstance.Unpatch(method, patch);
MapDropOffPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.GetBiomeHeight (map dropoff)");
HarmonyInstance.Patch(method, transpiler: new(patch));
MapDropOffPatched = true;
}
}
private static bool GetBiomePatched = false;
private static void PatchGetBiome()
{
var toPatch = Settings.EnabledForThisWorld && Settings.HasBiomeMap;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetBiome), [typeof(float), typeof(float), typeof(float), typeof(bool)]);
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetBiomePrefix));
if (toPatch == GetBiomePatched)
return;
if (GetBiomePatched)
{
Log("Unpatching WorldGenerator.GetBiome");
HarmonyInstance.Unpatch(method, patch);
GetBiomePatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.GetBiome");
HarmonyInstance.Patch(method, prefix: new(patch));
GetBiomePatched = true;
}
}
private static bool AddRiversPAtched = false;
private static void PatchAddRivers()
{
var toPatch = Settings.EnabledForThisWorld && !Settings.RiversEnabled;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.AddRivers));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.AddRiversPrefix));
if (toPatch == AddRiversPAtched)
return;
if (AddRiversPAtched)
{
Log("Unpatching WorldGenerator.AddRivers");
HarmonyInstance.Unpatch(method, patch);
AddRiversPAtched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.AddRivers");
HarmonyInstance.Patch(method, prefix: new(patch));
AddRiversPAtched = true;
}
}
private static bool ForestFactorPrefixPatched = false;
private static void PatchForestFactorPrefix()
{
var toPatch = Settings.EnabledForThisWorld && Settings.ForestScale != 1f;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetForestFactor));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetForestFactorPrefix));
if (toPatch == ForestFactorPrefixPatched)
return;
if (ForestFactorPrefixPatched)
{
Log("Unpatching WorldGenerator.GetForestFactor prefix");
HarmonyInstance.Unpatch(method, patch);
ForestFactorPrefixPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.GetForestFactor prefix");
HarmonyInstance.Patch(method, prefix: new(patch));
ForestFactorPrefixPatched = true;
}
}
private static bool ForestFactorPostfixPatched = false;
private static void PatchForestFactorPostfix()
{
var toPatch = Settings.EnabledForThisWorld && (Settings.HasForestMap || Settings.ForestAmountOffset != 0f);
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetForestFactor));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetForestFactorPostfix));
if (toPatch == ForestFactorPostfixPatched)
return;
if (ForestFactorPostfixPatched)
{
Log("Unpatching WorldGenerator.GetForestFactor postfix");
HarmonyInstance.Unpatch(method, patch);
ForestFactorPostfixPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.GetForestFactor postfix");
HarmonyInstance.Patch(method, postfix: new(patch));
ForestFactorPostfixPatched = true;
}
}
private static bool HeatPrefixPatched = false;
private static void PatchHeatPrefix()
{
var toPatch = Settings.EnabledForThisWorld && Settings.HasHeatMap && Settings.HeatMapScale > 0f;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.GetAshlandsOceanGradient), [typeof(float), typeof(float)]);
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.GetAshlandsOceanGradientPrefix));
if (toPatch == HeatPrefixPatched)
return;
if (HeatPrefixPatched)
{
Log("Unpatching WorldGenerator.GetAshlandsOceanGradient prefix");
HarmonyInstance.Unpatch(method, patch);
HeatPrefixPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.GetAshlandsOceanGradient prefix");
HarmonyInstance.Patch(method, prefix: new(patch));
HeatPrefixPatched = true;
}
}
private static bool AshlandsGapPatched = false;
private static void PatchAshlandGap()
{
var toPatch = Settings.EnabledForThisWorld && !Settings.AshlandsGapEnabled;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.CreateAshlandsGap));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.DisableGap));
if (toPatch == AshlandsGapPatched)
return;
if (AshlandsGapPatched)
{
Log("Unpatching WorldGenerator.CreateAshlandsGap");
HarmonyInstance.Unpatch(method, patch);
AshlandsGapPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.CreateAshlandsGap");
HarmonyInstance.Patch(method, prefix: new(patch));
AshlandsGapPatched = true;
}
}
private static bool DeepNorthGapPatched = false;
private static void PatchDeepNorthGap()
{
var toPatch = Settings.EnabledForThisWorld && !Settings.DeepNorthGapEnabled;
var method = AccessTools.Method(typeof(WorldGenerator), nameof(WorldGenerator.CreateDeepNorthGap));
var patch = AccessTools.Method(typeof(WorldGeneratorPatch), nameof(WorldGeneratorPatch.DisableGap));
if (toPatch == DeepNorthGapPatched)
return;
if (DeepNorthGapPatched)
{
Log("Unpatching WorldGenerator.CreateDeepNorthGap");
HarmonyInstance.Unpatch(method, patch);
DeepNorthGapPatched = false;
}
if (toPatch)
{
Log("Patching WorldGenerator.CreateDeepNorthGap");
HarmonyInstance.Patch(method, prefix: new(patch));
DeepNorthGapPatched = true;
}
}
}