1
1
using System . Collections . Generic ;
2
2
using HarmonyLib ;
3
- using Nautilus . Utility ;
4
3
5
4
namespace TerrainPatcher
6
5
{
@@ -19,8 +18,7 @@ internal static void Patch(Harmony harmony)
19
18
harmony . Patch ( setMethod , prefix : new HarmonyMethod (
20
19
AccessTools . Method ( typeof ( Array3Patches ) , nameof ( SetPrefix ) )
21
20
) ) ;
22
-
23
- SaveUtils . RegisterOnQuitEvent ( ( ) => { _vanillaToNegativeArrays . Clear ( ) ; } ) ;
21
+ harmony . PatchAll ( typeof ( IngameMenu_QuitGameAsync_Patch ) ) ;
24
22
}
25
23
26
24
private class NegativeEntityCell
@@ -33,7 +31,7 @@ public NegativeEntityCell(Dictionary<Int3, EntityCell> entityCells)
33
31
}
34
32
}
35
33
36
- private static Dictionary < Array3 < EntityCell > , NegativeEntityCell > _vanillaToNegativeArrays =
34
+ private static Dictionary < Array3 < EntityCell > , NegativeEntityCell > vanillaToNegativeArrays =
37
35
new Dictionary < Array3 < EntityCell > , NegativeEntityCell > ( ) ;
38
36
39
37
private static bool GetPrefix (
@@ -49,15 +47,15 @@ ref EntityCell __result
49
47
}
50
48
51
49
// At this point the index is negative, so we'll handle it ourselves.
52
- if ( _vanillaToNegativeArrays . TryGetValue ( __instance , out var negativesArray ) &&
50
+ if ( vanillaToNegativeArrays . TryGetValue ( __instance , out var negativesArray ) &&
53
51
negativesArray . EntityCells . TryGetValue ( new Int3 ( x , y , z ) , out var entityCell ) )
54
52
{
55
53
__result = entityCell ;
56
- Mod . LogDebug ( $ "Get negative entity cell for ({ x } ,{ y } ,{ z } )") ;
54
+ Mod . LogDebug ( $ "Get negative entity cell for ({ x } , { y } , { z } )") ;
57
55
return false ;
58
56
}
59
57
60
- Mod . LogDebug ( $ "Couldn't find negative entity cell for ({ x } ,{ y } ,{ z } )") ;
58
+ Mod . LogDebug ( $ "Couldn't find negative entity cell for ({ x } , { y } , { z } )") ;
61
59
return false ;
62
60
}
63
61
@@ -74,18 +72,29 @@ EntityCell value
74
72
}
75
73
76
74
// At this point the index is negative, so we'll set it to our collection.
77
- if ( ! _vanillaToNegativeArrays . TryGetValue ( __instance , out var negativeEntityCell ) )
75
+ if ( ! vanillaToNegativeArrays . TryGetValue ( __instance , out var negativeEntityCell ) )
78
76
{
79
77
negativeEntityCell = new NegativeEntityCell (
80
78
new Dictionary < Int3 , EntityCell > ( Int3 . equalityComparer )
81
79
) ;
82
- _vanillaToNegativeArrays [ __instance ] = negativeEntityCell ;
80
+ vanillaToNegativeArrays [ __instance ] = negativeEntityCell ;
83
81
}
84
82
85
83
negativeEntityCell . EntityCells [ new Int3 ( x , y , z ) ] = value ;
86
- Mod . LogDebug ( $ "Set negative entity cell for ({ x } ,{ y } ,{ z } )") ;
84
+ Mod . LogDebug ( $ "Set negative entity cell for ({ x } , { y } , { z } )") ;
87
85
88
86
return false ;
89
87
}
88
+
89
+ // Clear `vanillaToNegativeArrays` when the user quits the game.
90
+ [ HarmonyPatch ( typeof ( IngameMenu ) , nameof ( IngameMenu . QuitGameAsync ) ) ]
91
+ internal static class IngameMenu_QuitGameAsync_Patch
92
+ {
93
+ private static void Postfix ( )
94
+ {
95
+ Mod . LogDebug ( "Clearing negative entity cells on quit" ) ;
96
+ vanillaToNegativeArrays . Clear ( ) ;
97
+ }
98
+ }
90
99
}
91
100
}
0 commit comments