@@ -54,8 +54,19 @@ public record SkinInfo(int idx, string id, SkinData? skinData);
5454 /// <summary>A dictionary of custom widths for basic popups.</summary>
5555 public static Dictionary < int , int > basicPopupWidths = new ( ) ;
5656 /// <summary>Represents information about a unit prefab.</summary>
57- public record UnitPrefabInfo ( string type , string tribe , string skin ) ;
58- public static Dictionary < UnitPrefabInfo , int > skinnedHashKeys = new ( ) ;
57+ public struct UnitPrefabInfo
58+ {
59+ public UnitPrefabInfo ( string type , string tribe , string skin )
60+ {
61+ this . type = type ;
62+ this . tribe = tribe ;
63+ this . skin = skin ;
64+ }
65+ public string type ;
66+ public string tribe ;
67+ public string skin ;
68+ }
69+ public static Dictionary < UnitPrefabInfo , Unit > customPrefabs = new ( ) ;
5970 private static bool firstTimeOpeningPreview = true ;
6071 private static UnitData . Type currentUnitTypeUI = UnitData . Type . None ;
6172 private static TribeType attackerTribe = TribeType . None ;
@@ -234,118 +245,22 @@ private static void SkinVisualsRenderer_SkinWorldObject(
234245 }
235246
236247 [ HarmonyPostfix ]
237- [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . SetupData ) ) ]
238- private static void PrefabManager_SetupData ( PrefabManager __instance )
239- {
240- PrefabManager . units . Clear ( ) ;
241- PrefabManager . unitUIOverrides . Clear ( ) ;
242-
243- foreach ( PrefabManager . UnitPrefabData unitPrefabData in __instance . unitPrefabs )
244- {
245- PrefabManager . units . Add (
246- PrefabManager . GetSkinnedHashKey ( unitPrefabData . type , unitPrefabData . tribe , unitPrefabData . skin ) ,
247- unitPrefabData . prefab
248- ) ;
249- }
250-
251- foreach ( PrefabManager . UnitPrefabData unitUIPrefabData in __instance . unitUIOverridePrefabs )
252- {
253- PrefabManager . unitUIOverrides . Add (
254- PrefabManager . GetSkinnedHashKey ( unitUIPrefabData . type , unitUIPrefabData . tribe , unitUIPrefabData . skin ) ,
255- unitUIPrefabData . prefab
256- ) ;
257- }
258- }
259-
260- [ HarmonyPrefix ]
261- [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . GetSkinnedHashKey ) ) ]
262- private static bool PrefabManager_GetSkinnedHashKey ( ref int __result , UnitData . Type type , TribeType tribe , SkinType skin )
248+ [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . GetPrefab ) ,
249+ typeof ( UnitData . Type ) , typeof ( TribeType ) , typeof ( SkinType )
250+ ) ]
251+ private static void PrefabManager_GetPrefab ( ref Unit __result , UnitData . Type type , TribeType tribe , SkinType skin )
263252 {
264253 UnitPrefabInfo unitPrefabInfo = new (
265254 EnumCache < UnitData . Type > . GetName ( type ) ,
266255 EnumCache < TribeType > . GetName ( tribe ) ,
267256 EnumCache < SkinType > . GetName ( skin )
268257 ) ;
269- if ( ! skinnedHashKeys . ContainsKey ( unitPrefabInfo ) )
270- {
271- int key = skinnedHashKeys . Keys . Count ;
272- skinnedHashKeys . Add ( unitPrefabInfo , key ) ;
273- }
274- __result = skinnedHashKeys [ unitPrefabInfo ] ;
275- return false ;
276- }
277-
278- [ HarmonyPrefix ]
279- [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . HasUIUnitOverride ) ) ]
280- public static bool PrefabManager_HasUIUnitOverride ( ref bool __result , UnitData . Type unitType , TribeType tribe , SkinType skin )
281- {
282- int skinnedHashKey = PrefabManager . GetSkinnedHashKey ( unitType , tribe , skin ) ;
283- __result = PrefabManager . unitUIOverrides . ContainsKey ( skinnedHashKey ) ;
284- return false ;
285- }
286-
287- [ HarmonyPrefix ]
288- [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . GetUnitOrUiOverride ) ) ]
289- public static bool PrefabManager_GetUnitOrUiOverride ( ref Unit __result , UnitData . Type unitType , TribeType tribe , SkinType skin )
290- {
291- int skinnedHashKey = PrefabManager . GetSkinnedHashKey ( unitType , tribe , skin ) ;
292- if ( PrefabManager . unitUIOverrides . TryGetValue ( skinnedHashKey , out var prefab ) )
293- {
294- __result = prefab ;
295- return false ;
296- }
297- __result = PrefabManager . GetPrefab ( unitType , tribe , skin ) ;
298- return false ;
299- }
300-
301- [ HarmonyPrefix ]
302- [ HarmonyPatch ( typeof ( PrefabManager ) , nameof ( PrefabManager . GetPrefab ) , typeof ( UnitData . Type ) , typeof ( TribeType ) , typeof ( SkinType ) ) ]
303- public static bool GetPrefab ( ref Unit __result , UnitData . Type type , TribeType tribe , SkinType skin )
304- {
305- if ( SeasonManager . IsChristmas ( ) )
306- {
307- Unit prefab = PrefabManager . GetPrefab ( type , OverrideCondition . Christmas ) ;
308- if ( prefab != null )
309- {
310- __result = prefab ;
311- return false ;
312- }
313- }
314- int skinnedHashKey = PrefabManager . GetSkinnedHashKey ( type , tribe , skin ) ;
315- if ( PrefabManager . units . TryGetValue ( skinnedHashKey , out var value ) )
316- {
317- __result = value ;
318- return false ;
319- }
320- if ( skin != SkinType . Default || tribe != TribeType . None )
258+ if ( customPrefabs . ContainsKey (
259+ unitPrefabInfo
260+ ) )
321261 {
322- int skinnedHashKey2 = PrefabManager . GetSkinnedHashKey ( type , TribeType . None , skin ) ;
323- int skinnedHashKey3 = PrefabManager . GetSkinnedHashKey ( type , tribe , SkinType . Default ) ;
324- int skinnedHashKey4 = PrefabManager . GetSkinnedHashKey ( type , TribeType . None , SkinType . Default ) ;
325- int num = 0 ;
326- if ( PrefabManager . units . ContainsKey ( skinnedHashKey2 ) )
327- {
328- num = skinnedHashKey2 ;
329- }
330- else if ( PrefabManager . units . ContainsKey ( skinnedHashKey3 ) )
331- {
332- num = skinnedHashKey3 ;
333- }
334- else if ( PrefabManager . units . ContainsKey ( skinnedHashKey4 ) )
335- {
336- num = skinnedHashKey4 ;
337- }
338- if ( num != 0 )
339- {
340- Unit unit = PrefabManager . units [ num ] ;
341- PrefabManager . units . Add ( skinnedHashKey , unit ) ;
342- __result = unit ;
343- return false ;
344- }
262+ __result = customPrefabs [ unitPrefabInfo ] ;
345263 }
346- Plugin . logger . LogInfo ( $ "Couldn't find prefab for type: { type } ") ;
347- __result = PrefabManager . units [ 0 ] ;
348- return false ;
349264 }
350265
351266 #endregion
0 commit comments