Repository navigation
Expand file tree
/
Copy pathFastMapModSystem.cs
More file actions
706 lines (612 loc) · 25.6 KB
/
Copy pathFastMapModSystem.cs
File metadata and controls
706 lines (612 loc) · 25.6 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
using FastMap.Cache;
using FastMap.Config;
using FastMap.Map;
using System;
using System.Collections.Generic;
#if FASTMAPHITCHDIAGNOSTICS
using System.Diagnostics;
#endif
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Datastructures;
using Vintagestory.API.MathTools;
using Vintagestory.API.Server;
using Vintagestory.GameContent;
namespace FastMap;
public sealed class FastMapModSystem : ModSystem
{
private const string ModId = "fastmap";
private const string TerrainLayerRegistryCode = "chunks";
private const string MapperChunkMapLayerFullName = "Mapper.WorldMap.MapperChunkMapLayer";
private const string ConfigLibConfigSavedEvent = "configlib:fastmap:config-saved";
private const string ConfigLibConfigReloadEvent = "configlib:config-reload";
private ICoreClientAPI? capi;
private Action? levelFinalizeHandler;
private bool terrainSamplerUnavailableLogged;
private bool terrainSamplerAvailableLogged;
#if FASTMAPHITCHDIAGNOSTICS
private long hitchDiagnosticListenerId;
private long lastHitchTickTimestamp;
private long lastHitchDiagnosticLogMs;
private int lastHitchGen0Collections;
private int lastHitchGen1Collections;
private int lastHitchGen2Collections;
#endif
public static FastMapModSystem? Instance { get; private set; }
public FastMapConfig Config { get; private set; } = new();
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Client;
public override double ExecuteOrder() => 1.0;
public override void StartClientSide(ICoreClientAPI api)
{
capi = api;
Instance = this;
Config = FastMapConfig.Load(api);
FastMapStoragePaths.MigrateLegacyRootIfNeeded(api);
RegisterConfigReloadListeners(api);
RegisterClientCommands(api);
#if FASTMAPHITCHDIAGNOSTICS
RegisterHitchDiagnosticListener(api);
#endif
FastMapWorldMapGuard.Install(api.Logger);
FastMapCartographyTableCompatibility.Install(api);
ReplaceTerrainLayerRegistration();
levelFinalizeHandler = () => ReplaceTerrainLayerRegistration();
api.Event.LevelFinalize += levelFinalizeHandler;
}
#if FASTMAPHITCHDIAGNOSTICS
private void RegisterHitchDiagnosticListener(ICoreClientAPI api)
{
if (!Config.EnableHitchDiagnostics || hitchDiagnosticListenerId != 0)
{
return;
}
hitchDiagnosticListenerId = api.Event.RegisterGameTickListener(OnHitchDiagnosticTick, 1);
}
#endif
private void RegisterConfigReloadListeners(ICoreAPI api)
{
api.Event.RegisterEventBusListener(OnConfigLibConfigSaved, filterByEventName: ConfigLibConfigSavedEvent);
api.Event.RegisterEventBusListener(OnConfigLibConfigReload, filterByEventName: ConfigLibConfigReloadEvent);
}
private void RegisterClientCommands(ICoreClientAPI api)
{
RegisterVanillaMapCommands(api);
api.ChatCommands.Create("fastmap")
.WithDescription("FastMap cache tools")
.RequiresPlayer()
.RequiresPrivilege(Privilege.chat)
.BeginSubCommand("cleanupcache")
.WithDescription("Delete old FastMap page-version cache folders")
.WithAdditionalInformation("Uses fastmap.json CleanupKeepLatestPageVersion. When enabled, the newest pages-vN folder in each world cache is preserved.")
.HandleWith(OnCleanupCacheCommand)
.EndSubCommand()
.BeginSubCommand("sealevel")
.WithDescription("Report FastMap sea-level diagnostics")
.WithAdditionalInformation("Shows the world sea level FastMap uses for pregen, plus the terrain sampler height at your current position when available.")
.HandleWith(OnSeaLevelCommand)
.EndSubCommand();
}
private void RegisterVanillaMapCommands(ICoreClientAPI api)
{
api.ChatCommands.GetOrCreate("map")
.BeginSubCommand("purgedb")
.WithDescription("Purge the vanilla map DB and FastMap terrain page cache")
.HandleWith(OnMapPurgeDbCommand)
.EndSubCommand()
.BeginSubCommand("redraw")
.WithDescription("Redraw visible FastMap terrain map chunks")
.HandleWith(OnMapRedrawCommand)
.EndSubCommand();
}
private TextCommandResult OnCleanupCacheCommand(TextCommandCallingArgs args)
{
bool keepLatest = Config.CleanupKeepLatestPageVersion;
FastMapCacheCleanupResult result = FastMapCacheCleanup.CleanupVersionedPageCaches(keepLatest);
string summary = result.ToSummary(keepLatest);
capi?.Logger.Notification("[FastMap] {0}", summary);
foreach (string failure in result.FailureMessages)
{
capi?.Logger.Warning("[FastMap] Cache cleanup failed for {0}", failure);
}
return result.Failures == 0
? TextCommandResult.Success(summary)
: TextCommandResult.Error(summary);
}
private TextCommandResult OnMapPurgeDbCommand(TextCommandCallingArgs args)
{
FastPageMapLayer? layer = FindFastMapLayer();
if (layer == null)
{
return TextCommandResult.Error("FastMap terrain layer is not available.");
}
string message = layer.PurgeVanillaMapDatabaseAndFastMapCache();
capi?.Logger.Notification("[FastMap] {0}", message);
return TextCommandResult.Success(message);
}
private TextCommandResult OnMapRedrawCommand(TextCommandCallingArgs args)
{
FastPageMapLayer? layer = FindFastMapLayer();
if (layer == null)
{
return TextCommandResult.Error("FastMap terrain layer is not available.");
}
string message = layer.RedrawVisibleMapPages();
capi?.Logger.Notification("[FastMap] {0}", message);
return TextCommandResult.Success(message);
}
private TextCommandResult OnSeaLevelCommand(TextCommandCallingArgs args)
{
if (capi == null)
{
return TextCommandResult.Error("FastMap client API is not available.");
}
int seaLevel = capi.World.SeaLevel;
int mapHeight = capi.World.BlockAccessor.MapSizeY;
FastMapModDetectionResult terraPretyDetection = FastMapModDetection.DetectMod(capi, "terraprety");
bool terraPretyLoaded = terraPretyDetection.Present;
int terrainSamplerHeightOffset = Config.TerrainSamplerFallbackHeightOffset
+ (terraPretyLoaded ? Config.TerrainSamplerFallbackTerraPretyHeightOffset : 0);
int waterHeightThreshold = FastMapTerrainWater.WaterHeightThreshold(seaLevel, Config.TerrainSamplerFallbackWaterLevelOffset);
string message = $"FastMap sea level: {seaLevel}; pregen water cutoff: < {waterHeightThreshold}; world height: {mapHeight}; pregen height offset: {terrainSamplerHeightOffset}"
+ $" (global {Config.TerrainSamplerFallbackHeightOffset}, Terra Prety loaded {terraPretyLoaded} via {terraPretyDetection.Source}, Terra Prety {Config.TerrainSamplerFallbackTerraPretyHeightOffset}; water level offset {Config.TerrainSamplerFallbackWaterLevelOffset}).";
if (!terraPretyLoaded)
{
message += $" Terra Prety detection details: {terraPretyDetection.Details}.";
}
EntityPlayer? playerEntity = capi.World.Player?.Entity;
if (playerEntity == null)
{
return TextCommandResult.Success(message);
}
int x = (int)Math.Floor(playerEntity.Pos.X);
int y = (int)Math.Floor(playerEntity.Pos.Y);
int z = (int)Math.Floor(playerEntity.Pos.Z);
message += $" Player block position: {x}, {y}, {z}.";
FastMapTerrainSamplerAdapter? sampler = FastMapTerrainSamplerAdapter.TryCreate(capi);
if (sampler == null)
{
string? installedVersion = FastMapTerrainSamplerAdapter.InstalledTerrainSamplerVersion(capi);
message += installedVersion == null
? " Terrain Sampler: not installed or not loaded."
: $" Terrain Sampler: installed version {installedVersion}, requires {FastMapTerrainSamplerAdapter.MinimumSupportedVersion}+.";
return TextCommandResult.Success(message);
}
try
{
FastMapTerrainSamplerColumn sample = sampler.SampleColumn(x, z);
int adjustedHeight = Math.Clamp(sample.Height + terrainSamplerHeightOffset, 0, Math.Max(0, mapHeight - 1));
int delta = adjustedHeight - seaLevel;
string deltaText = delta > 0 ? $"+{delta}" : delta.ToString();
message += $" Terrain Sampler height here: raw {sample.Height}, pregen {adjustedHeight} ({deltaText} vs sea level).";
}
catch (Exception ex)
{
message += $" Terrain Sampler: sample failed ({ex.GetType().Name}: {ex.Message}).";
}
return TextCommandResult.Success(message);
}
public FastMapMapPieceImportResult ImportMapPieces(IReadOnlyDictionary<FastVec2i, MapPieceDB> pieces, string source = "external")
{
FastPageMapLayer? layer = FindFastMapLayer();
return layer == null
? FastMapMapPieceImportResult.NotHandled
: layer.ImportMapPieces(pieces, source);
}
public bool TryGetMapPieces(IEnumerable<FastVec2i> coords, out Dictionary<FastVec2i, MapPieceDB> pieces)
{
pieces = new Dictionary<FastVec2i, MapPieceDB>();
FastPageMapLayer? layer = FindFastMapLayer();
return layer != null && layer.TryGetMapPieces(coords, out pieces);
}
public bool InvalidateMapPieces(IEnumerable<FastVec2i> coords, string source = "external")
{
FastPageMapLayer? layer = FindFastMapLayer();
return layer != null && layer.InvalidateMapPieces(coords, source);
}
private void OnConfigLibConfigSaved(string eventName, ref EnumHandling handling, IAttribute data)
{
if (!IsOwnConfigEvent(data))
{
return;
}
ReloadConfigSafely();
}
private void OnConfigLibConfigReload(string eventName, ref EnumHandling handling, IAttribute data)
{
if (!IsOwnConfigEvent(data))
{
return;
}
ReloadConfigSafely();
}
private static bool IsOwnConfigEvent(IAttribute data)
{
return (data as ITreeAttribute)?.GetAsString("domain") == ModId;
}
private void ReloadConfigSafely()
{
if (capi == null)
{
return;
}
Config = FastMapConfig.Load(capi);
#if FASTMAPHITCHDIAGNOSTICS
if (Config.EnableHitchDiagnostics)
{
RegisterHitchDiagnosticListener(capi);
}
else if (hitchDiagnosticListenerId != 0)
{
capi.Event.UnregisterGameTickListener(hitchDiagnosticListenerId);
hitchDiagnosticListenerId = 0;
}
#endif
RefreshWorldMapRegistrationsAfterConfigReload();
capi.Logger.Notification("[FastMap] Reloaded config. Live map layer instances were left intact; constructor-only settings apply after the next world load.");
}
private void RefreshWorldMapRegistrationsAfterConfigReload()
{
if (capi == null)
{
return;
}
WorldMapManager? worldMapManager = capi.ModLoader.GetModSystem<WorldMapManager>(true);
if (worldMapManager == null)
{
return;
}
worldMapManager.MapLayerRegistry[TerrainLayerRegistryCode] = typeof(FastPageMapLayer);
worldMapManager.LayerGroupPositions[TerrainLayerRegistryCode] = 0.0;
bool terrainSamplerAvailable = IsTerrainSamplerIntegrationAvailable();
RefreshTerrainSamplerOverlayRegistration<FastMapTerrainFallbackLayer>(
worldMapManager,
"fastmap-terrain-fallback",
2.0,
terrainSamplerAvailable && !Config.DisableTerrainSamplerFallbackLayer);
RefreshTerrainSamplerOverlayRegistration<FastMapRainfallLayer>(worldMapManager, "fastmap-rainfall", 0.15, terrainSamplerAvailable && Config.EnableTerrainSamplerRainfallLayer);
RefreshTerrainSamplerOverlayRegistration<FastMapTemperatureLayer>(worldMapManager, "fastmap-temperature", 0.16, terrainSamplerAvailable && Config.EnableTerrainSamplerTemperatureLayer);
RefreshTerrainSamplerOverlayRegistration<FastMapForestDensityLayer>(worldMapManager, "fastmap-forest-density", 0.17, terrainSamplerAvailable && Config.EnableTerrainSamplerForestDensityLayer);
RefreshTerrainSamplerOverlayRegistration<FastMapShrubDensityLayer>(worldMapManager, "fastmap-shrub-density", 0.18, terrainSamplerAvailable && Config.EnableTerrainSamplerShrubDensityLayer);
if (worldMapManager.MapLayers.Count == 0)
{
capi.Logger.Warning("[FastMap] World map layer list was empty after config reload; rebuilding layer registrations to avoid an empty world map dialog.");
ReplaceTerrainLayerRegistration(recreateFastMapLayer: false);
}
}
private static void RefreshTerrainSamplerOverlayRegistration<T>(
WorldMapManager worldMapManager,
string code,
double position,
bool enabled) where T : MapLayer
{
if (enabled)
{
worldMapManager.MapLayerRegistry[code] = typeof(T);
worldMapManager.LayerGroupPositions[code] = position;
}
else if (FindLayerIndex<T>(worldMapManager) < 0)
{
worldMapManager.MapLayerRegistry.Remove(code);
worldMapManager.LayerGroupPositions.Remove(code);
}
}
#if FASTMAPHITCHDIAGNOSTICS
private void OnHitchDiagnosticTick(float dt)
{
if (capi == null || !Config.EnableHitchDiagnostics)
{
return;
}
MarkFrameProfiler("fastmap-app-hitch-check-begin");
long dtMs = (long)Math.Round(dt * 1000f);
long now = Stopwatch.GetTimestamp();
long previous = lastHitchTickTimestamp;
lastHitchTickTimestamp = now;
long wallMs = previous == 0 ? 0 : (long)((now - previous) * 1000.0 / Stopwatch.Frequency);
long elapsedMs = Math.Max(dtMs, wallMs);
if (elapsedMs < Config.HitchDiagnosticThresholdMilliseconds)
{
MarkFrameProfiler("fastmap-app-hitch-check-end");
return;
}
long nowMs = capi.ElapsedMilliseconds;
if (nowMs - lastHitchDiagnosticLogMs < 1000)
{
MarkFrameProfiler("fastmap-app-hitch-check-end");
return;
}
lastHitchDiagnosticLogMs = nowMs;
int gen0 = GC.CollectionCount(0);
int gen1 = GC.CollectionCount(1);
int gen2 = GC.CollectionCount(2);
int gen0Delta = gen0 - lastHitchGen0Collections;
int gen1Delta = gen1 - lastHitchGen1Collections;
int gen2Delta = gen2 - lastHitchGen2Collections;
lastHitchGen0Collections = gen0;
lastHitchGen1Collections = gen1;
lastHitchGen2Collections = gen2;
capi.Logger.Warning(
"[FastMap] App hitch diagnostic elapsedMs={0}, dtMs={1}, wallMs={2}, gc0Delta={3}, gc1Delta={4}, gc2Delta={5}, managedMemoryMb={6}",
elapsedMs,
dtMs,
wallMs,
gen0Delta,
gen1Delta,
gen2Delta,
GC.GetTotalMemory(false) / (1024 * 1024));
MarkFrameProfiler("fastmap-app-hitch-check-end");
}
private void MarkFrameProfiler(string code)
{
if (capi == null || !Config.EnableHitchDiagnostics || !capi.World.FrameProfiler.Enabled)
{
return;
}
capi.World.FrameProfiler.Mark(code);
}
#endif
private void ReplaceTerrainLayerRegistration(bool recreateFastMapLayer = false)
{
if (capi == null)
{
return;
}
WorldMapManager? worldMapManager = capi.ModLoader.GetModSystem<WorldMapManager>(true);
if (worldMapManager == null)
{
capi.Logger.Warning("[FastMap] WorldMapManager was not available; terrain layer replacement skipped.");
return;
}
if (TryInstallMapperRenderLayer(worldMapManager, recreateFastMapLayer))
{
SyncTerrainSamplerOverlayLayers(worldMapManager, recreateFastMapLayer);
return;
}
worldMapManager.MapLayerRegistry[TerrainLayerRegistryCode] = typeof(FastPageMapLayer);
worldMapManager.LayerGroupPositions[TerrainLayerRegistryCode] = 0.0;
for (int i = 0; i < worldMapManager.MapLayers.Count; i++)
{
MapLayer layer = worldMapManager.MapLayers[i];
if (layer.GetType() == typeof(ChunkMapLayer) || (recreateFastMapLayer && layer is FastPageMapLayer))
{
layer.OnShutDown();
layer.Dispose();
FastPageMapLayer replacement = new(capi, worldMapManager);
replacement.OnLoaded();
worldMapManager.MapLayers[i] = replacement;
}
}
SyncTerrainSamplerOverlayLayers(worldMapManager, recreateFastMapLayer);
}
private void SyncTerrainSamplerOverlayLayers(WorldMapManager worldMapManager, bool recreateExisting)
{
if (capi == null)
{
return;
}
bool terrainSamplerAvailable = IsTerrainSamplerIntegrationAvailable();
SyncTerrainSamplerOverlayLayer<FastMapTerrainFallbackLayer>(
worldMapManager,
"fastmap-terrain-fallback",
2.0,
enabled: terrainSamplerAvailable && !Config.DisableTerrainSamplerFallbackLayer,
recreateExisting);
SyncTerrainSamplerOverlayLayer<FastMapRainfallLayer>(
worldMapManager,
"fastmap-rainfall",
0.15,
terrainSamplerAvailable && Config.EnableTerrainSamplerRainfallLayer,
recreateExisting);
SyncTerrainSamplerOverlayLayer<FastMapTemperatureLayer>(
worldMapManager,
"fastmap-temperature",
0.16,
terrainSamplerAvailable && Config.EnableTerrainSamplerTemperatureLayer,
recreateExisting);
SyncTerrainSamplerOverlayLayer<FastMapForestDensityLayer>(
worldMapManager,
"fastmap-forest-density",
0.17,
terrainSamplerAvailable && Config.EnableTerrainSamplerForestDensityLayer,
recreateExisting);
SyncTerrainSamplerOverlayLayer<FastMapShrubDensityLayer>(
worldMapManager,
"fastmap-shrub-density",
0.18,
terrainSamplerAvailable && Config.EnableTerrainSamplerShrubDensityLayer,
recreateExisting);
}
private bool IsTerrainSamplerIntegrationAvailable()
{
string? installedVersion = capi != null ? FastMapTerrainSamplerAdapter.InstalledTerrainSamplerVersion(capi) : null;
bool versionSupported = capi == null || FastMapTerrainSamplerAdapter.IsInstalledVersionSupported(capi);
bool available = versionSupported && FastMapTerrainSamplerAdapter.TryCreate(capi) != null;
if (available)
{
if (!terrainSamplerAvailableLogged)
{
string pregenStatus = Config.DisableTerrainSamplerFallbackLayer
? "Pregen map layer is disabled by config"
: "Pregen map layer is available";
capi?.Logger.Notification(
"[FastMap] Terrain Sampler integration detected ({0}); {1}; sampler overlay map layers are available.",
installedVersion ?? "unknown version",
pregenStatus);
terrainSamplerAvailableLogged = true;
}
return true;
}
if (!terrainSamplerUnavailableLogged)
{
if (installedVersion == null)
{
capi?.Logger.Notification("[FastMap] Terrain Sampler integration not detected; Pregen and sampler overlay map layers are hidden.");
}
else if (!versionSupported)
{
capi?.Logger.Notification(
"[FastMap] Terrain Sampler {0} detected, but FastMap requires {1}+; Pregen and sampler overlay map layers are hidden.",
installedVersion,
FastMapTerrainSamplerAdapter.MinimumSupportedVersion);
}
else
{
capi?.Logger.Notification("[FastMap] Terrain Sampler integration was detected but not ready; Pregen and sampler overlay map layers are hidden.");
}
terrainSamplerUnavailableLogged = true;
}
return false;
}
private void SyncTerrainSamplerOverlayLayer<T>(
WorldMapManager worldMapManager,
string code,
double position,
bool enabled,
bool recreateExisting) where T : MapLayer
{
if (enabled)
{
worldMapManager.MapLayerRegistry[code] = typeof(T);
worldMapManager.LayerGroupPositions[code] = position;
int existingIndex = FindLayerIndex<T>(worldMapManager);
if (existingIndex >= 0)
{
if (!recreateExisting)
{
return;
}
MapLayer oldLayer = worldMapManager.MapLayers[existingIndex];
oldLayer.OnShutDown();
oldLayer.Dispose();
MapLayer replacement = (MapLayer)Activator.CreateInstance(typeof(T), capi!, worldMapManager)!;
replacement.OnLoaded();
worldMapManager.MapLayers[existingIndex] = replacement;
return;
}
if (worldMapManager.MapLayers.Count > 0)
{
MapLayer layer = (MapLayer)Activator.CreateInstance(typeof(T), capi!, worldMapManager)!;
layer.OnLoaded();
worldMapManager.MapLayers.Add(layer);
}
return;
}
worldMapManager.MapLayerRegistry.Remove(code);
worldMapManager.LayerGroupPositions.Remove(code);
int index = FindLayerIndex<T>(worldMapManager);
if (index >= 0)
{
MapLayer oldLayer = worldMapManager.MapLayers[index];
oldLayer.OnShutDown();
oldLayer.Dispose();
worldMapManager.MapLayers.RemoveAt(index);
}
}
private bool TryInstallMapperRenderLayer(WorldMapManager worldMapManager, bool recreateFastMapLayer)
{
if (!worldMapManager.MapLayerRegistry.TryGetValue(TerrainLayerRegistryCode, out Type? terrainLayerType)
|| terrainLayerType.FullName != MapperChunkMapLayerFullName)
{
return false;
}
int fastMapIndex = FindFastMapLayerIndex(worldMapManager);
if (fastMapIndex >= 0)
{
if (!recreateFastMapLayer)
{
return true;
}
MapLayer oldLayer = worldMapManager.MapLayers[fastMapIndex];
oldLayer.OnShutDown();
oldLayer.Dispose();
FastPageMapLayer replacement = new(capi!, worldMapManager);
replacement.OnLoaded();
worldMapManager.MapLayers[fastMapIndex] = replacement;
return true;
}
if (worldMapManager.MapLayers.Count == 0)
{
return true;
}
int mapperIndex = FindLayerIndexByFullName(worldMapManager, MapperChunkMapLayerFullName);
int insertIndex = mapperIndex >= 0 ? mapperIndex + 1 : worldMapManager.MapLayers.Count;
FastPageMapLayer fastMapLayer = new(capi!, worldMapManager);
fastMapLayer.OnLoaded();
worldMapManager.MapLayers.Insert(insertIndex, fastMapLayer);
capi!.Logger.Notification("[FastMap] Mapper chunk layer detected; FastMap installed as terrain render layer while Mapper keeps map state.");
return true;
}
private static int FindFastMapLayerIndex(WorldMapManager worldMapManager)
{
for (int i = 0; i < worldMapManager.MapLayers.Count; i++)
{
if (worldMapManager.MapLayers[i] is FastPageMapLayer)
{
return i;
}
}
return -1;
}
private FastPageMapLayer? FindFastMapLayer()
{
if (capi == null)
{
return null;
}
WorldMapManager? worldMapManager = capi.ModLoader.GetModSystem<WorldMapManager>(true);
if (worldMapManager == null)
{
return null;
}
for (int i = 0; i < worldMapManager.MapLayers.Count; i++)
{
if (worldMapManager.MapLayers[i] is FastPageMapLayer layer)
{
return layer;
}
}
return null;
}
private static int FindLayerIndexByFullName(WorldMapManager worldMapManager, string fullName)
{
for (int i = 0; i < worldMapManager.MapLayers.Count; i++)
{
if (worldMapManager.MapLayers[i].GetType().FullName == fullName)
{
return i;
}
}
return -1;
}
private static int FindLayerIndex<T>(WorldMapManager worldMapManager) where T : MapLayer
{
for (int i = 0; i < worldMapManager.MapLayers.Count; i++)
{
if (worldMapManager.MapLayers[i] is T)
{
return i;
}
}
return -1;
}
public override void Dispose()
{
if (capi != null && levelFinalizeHandler != null)
{
capi.Event.LevelFinalize -= levelFinalizeHandler;
}
#if FASTMAPHITCHDIAGNOSTICS
if (capi != null && hitchDiagnosticListenerId != 0)
{
capi.Event.UnregisterGameTickListener(hitchDiagnosticListenerId);
}
hitchDiagnosticListenerId = 0;
#endif
levelFinalizeHandler = null;
capi = null;
if (Instance == this)
{
Instance = null;
}
}
}