@@ -20,23 +20,25 @@ namespace PolyMod;
2020
2121public static class Loader
2222{
23- internal static Dictionary < string , Type > typeMappings = new ( )
23+ internal record TypeMapping ( Type type , bool shouldCreateCache = true ) ;
24+ internal static Dictionary < string , TypeMapping > typeMappings = new ( )
2425 {
25- { "tribeData" , typeof ( TribeData . Type ) } ,
26- { "techData" , typeof ( TechData . Type ) } ,
27- { "unitData" , typeof ( UnitData . Type ) } ,
28- { "improvementData" , typeof ( ImprovementData . Type ) } ,
29- { "terrainData" , typeof ( Polytopia . Data . TerrainData . Type ) } ,
30- { "resourceData" , typeof ( ResourceData . Type ) } ,
31- { "taskData" , typeof ( TaskData . Type ) } ,
32- { "tribeAbility" , typeof ( TribeAbility . Type ) } ,
33- { "unitAbility" , typeof ( UnitAbility . Type ) } ,
34- { "improvementAbility" , typeof ( ImprovementAbility . Type ) } ,
35- { "playerAbility" , typeof ( PlayerAbility . Type ) } ,
36- { "weaponData" , typeof ( UnitData . WeaponEnum ) }
26+ { "tribeData" , new TypeMapping ( typeof ( TribeData . Type ) ) } ,
27+ { "techData" , new TypeMapping ( typeof ( TechData . Type ) ) } ,
28+ { "unitData" , new TypeMapping ( typeof ( UnitData . Type ) ) } ,
29+ { "improvementData" , new TypeMapping ( typeof ( ImprovementData . Type ) ) } ,
30+ { "terrainData" , new TypeMapping ( typeof ( Polytopia . Data . TerrainData . Type ) ) } ,
31+ { "resourceData" , new TypeMapping ( typeof ( ResourceData . Type ) ) } ,
32+ { "taskData" , new TypeMapping ( typeof ( TaskData . Type ) ) } ,
33+ { "tribeAbility" , new TypeMapping ( typeof ( TribeAbility . Type ) ) } ,
34+ { "unitAbility" , new TypeMapping ( typeof ( UnitAbility . Type ) ) } ,
35+ { "improvementAbility" , new TypeMapping ( typeof ( ImprovementAbility . Type ) ) } ,
36+ { "playerAbility" , new TypeMapping ( typeof ( PlayerAbility . Type ) ) } ,
37+ { "weaponData" , new TypeMapping ( typeof ( UnitData . WeaponEnum ) ) } ,
38+ { "skinData" , new TypeMapping ( typeof ( SkinData ) , false ) }
3739 } ;
3840 internal static List < GameModeButtonsInformation > gamemodes = new ( ) ;
39- private static readonly Dictionary < Type , Action < JObject , bool > > typeHandlers = new ( )
41+ internal static readonly Dictionary < Type , Action < JObject , bool > > typeHandlers = new ( )
4042 {
4143 [ typeof ( TribeData . Type ) ] = new ( ( token , duringEnumCacheCreation ) =>
4244 {
@@ -49,6 +51,31 @@ public static class Loader
4951 }
5052 else
5153 {
54+ if ( token [ "skins" ] != null ) // Doesnt work, and I do not know why! Good luck.
55+ {
56+ JArray skins = token [ "skins" ] . Cast < JArray > ( ) ;
57+ List < JToken > skinValues = skins . _values . ToArray ( ) . ToList ( ) ;
58+ foreach ( var skin in skinValues )
59+ {
60+ string skinValue = skin . ToString ( ) ;
61+ if ( ! Enum . TryParse < SkinType > ( CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( skinValue ) , out _ ) )
62+ {
63+ EnumCache < SkinType > . AddMapping ( skinValue . ToLowerInvariant ( ) , ( SkinType ) Registry . autoidx ) ;
64+ EnumCache < SkinType > . AddMapping ( skinValue . ToLowerInvariant ( ) , ( SkinType ) Registry . autoidx ) ;
65+ Registry . skinInfo . Add ( new Visual . SkinInfo ( Registry . autoidx , skinValue , null ) ) ;
66+ Plugin . logger . LogInfo ( "Created mapping for skinType with id " + skinValue + " and index " + Registry . autoidx ) ;
67+ Registry . autoidx ++ ;
68+ }
69+ }
70+ foreach ( var skin in Registry . skinInfo )
71+ {
72+ if ( skins . _values . Contains ( skin . id ) )
73+ {
74+ skins . _values . Remove ( skin . id ) ;
75+ skins . _values . Add ( skin . idx ) ;
76+ }
77+ }
78+ }
5279 if ( token [ "preview" ] != null )
5380 {
5481 Visual . PreviewTile [ ] preview = JsonSerializer . Deserialize < Visual . PreviewTile [ ] > ( token [ "preview" ] . ToString ( ) ) ! ;
@@ -128,7 +155,26 @@ public static class Loader
128155 }
129156 PrefabManager . resources . TryAdd ( ( ResourceData . Type ) Registry . autoidx , PrefabManager . resources [ resourcePrefabType ] ) ;
130157 }
131- } )
158+ } ) ,
159+
160+ [ typeof ( SkinData ) ] = new ( ( token , duringEnumCacheCreation ) =>
161+ {
162+ string id = Util . GetJTokenName ( token ) ;
163+ int index = Registry . skinInfo . FindIndex ( t => t . id == id ) ;
164+ if ( Registry . skinInfo . ElementAtOrDefault ( index ) != null )
165+ {
166+ SkinData skinData = new ( ) ;
167+ if ( token [ "color" ] != null )
168+ {
169+ skinData . color = ( int ) token [ "color" ] ;
170+ }
171+ if ( token [ "language" ] != null )
172+ {
173+ skinData . language = token [ "language" ] . ToString ( ) ;
174+ }
175+ Registry . skinInfo [ index ] = new Visual . SkinInfo ( Registry . skinInfo [ index ] . idx , Registry . skinInfo [ index ] . id , skinData ) ;
176+ }
177+ } ) ,
132178 } ;
133179
134180 public record GameModeButtonsInformation ( int gameModeIndex , UIButtonBase . ButtonAction action , int ? buttonIndex , Sprite ? sprite ) ;
@@ -144,7 +190,13 @@ public static void AddGameModeButton(string id, UIButtonBase.ButtonAction action
144190 public static void AddPatchDataType ( string typeId , Type type )
145191 {
146192 if ( ! typeMappings . ContainsKey ( typeId ) )
147- typeMappings . Add ( typeId , type ) ;
193+ typeMappings . Add ( typeId , new TypeMapping ( type ) ) ;
194+ }
195+
196+ public static void AddPatchDataType ( string typeId , Type type , bool shouldCreateCache )
197+ {
198+ if ( ! typeMappings . ContainsKey ( typeId ) )
199+ typeMappings . Add ( typeId , new TypeMapping ( type , shouldCreateCache ) ) ;
148200 }
149201
150202 internal static void LoadMods ( Dictionary < string , Mod > mods )
@@ -571,50 +623,6 @@ public static void LoadGameLogicDataPatch(Mod mod, JObject gld, JObject patch)
571623 {
572624 try
573625 {
574- foreach ( JToken jtoken in patch . SelectTokens ( "$.*.*" ) . ToArray ( ) )
575- {
576- JObject ? token = jtoken . TryCast < JObject > ( ) ;
577- if ( token != null )
578- {
579- string dataType = Util . GetJTokenName ( token , 2 ) ;
580- if ( typeMappings . TryGetValue ( dataType , out Type ? targetType ) )
581- {
582- if ( token [ "idx" ] != null && ( int ) token [ "idx" ] == - 1 )
583- {
584- string id = Util . GetJTokenName ( token ) ;
585- token [ "idx" ] = Registry . autoidx ;
586- MethodInfo ? methodInfo = typeof ( EnumCache < > ) . MakeGenericType ( targetType ) . GetMethod ( "AddMapping" ) ;
587- if ( methodInfo != null )
588- {
589- methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
590- methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
591-
592- if ( typeHandlers . TryGetValue ( targetType , out var handler ) )
593- {
594- handler ( token , true ) ;
595- }
596- Plugin . logger . LogInfo ( "Created mapping for " + targetType . ToString ( ) + " with id " + id + " and index " + Registry . autoidx ) ;
597- Registry . autoidx ++ ;
598- }
599- }
600- }
601- }
602- }
603- foreach ( JToken jtoken in patch . SelectTokens ( "$.*.*" ) . ToArray ( ) )
604- {
605- JObject ? token = jtoken . TryCast < JObject > ( ) ;
606- if ( token != null )
607- {
608- string dataType = Util . GetJTokenName ( token , 2 ) ;
609- if ( typeMappings . TryGetValue ( dataType , out Type ? targetType ) )
610- {
611- if ( typeHandlers . TryGetValue ( targetType , out var handler ) )
612- {
613- handler ( token , false ) ;
614- }
615- }
616- }
617- }
618626 gld = JsonMerger . Merge ( gld , patch ) ;
619627 Plugin . logger . LogInfo ( $ "Registered patch from { mod . id } mod") ;
620628 }
0 commit comments