@@ -181,21 +181,8 @@ internal record TypeMapping(Type type, bool shouldCreateCache = true);
181181
182182 [ typeof ( SkinData ) ] = new ( ( token , duringEnumCacheCreation ) =>
183183 {
184- string id = Util . GetJTokenName ( token ) ;
185- int index = Registry . skinInfo . FindIndex ( t => t . id == id ) ;
186- if ( Registry . skinInfo . ElementAtOrDefault ( index ) != null )
187- {
188- SkinData skinData = new ( ) ;
189- if ( token [ "color" ] != null )
190- {
191- skinData . color = ( int ) token [ "color" ] ;
192- }
193- if ( token [ "language" ] != null )
194- {
195- skinData . language = token [ "language" ] . ToString ( ) ;
196- }
197- Registry . skinInfo [ index ] = new Visual . SkinInfo ( Registry . skinInfo [ index ] . idx , Registry . skinInfo [ index ] . id , skinData ) ;
198- }
184+ var prop = token . Parent . Cast < JProperty > ( ) ;
185+ prop . Replace ( new JProperty ( prop . Name . ToLower ( ) , prop . Value ) ) ;
199186 } ) ,
200187 } ;
201188
@@ -488,7 +475,7 @@ public static void LoadLocalizationFile(Mod mod, Mod.File file)
488475 }
489476 catch ( Exception e )
490477 {
491- Plugin . logger . LogError ( $ "Error on loading locatization from { mod . id } mod: { e . Message } ") ;
478+ Plugin . logger . LogError ( $ "Error on loading locatization from { mod . id } mod: { e . StackTrace } ") ;
492479 }
493480 }
494481
@@ -569,7 +556,7 @@ public static void UpdateSprite(string name)
569556 }
570557 catch ( Exception e )
571558 {
572- Plugin . logger . LogError ( $ "Error on loading sprite data from { mod . id } mod: { e . Message } ") ;
559+ Plugin . logger . LogError ( $ "Error on loading sprite data from { mod . id } mod: { e . StackTrace } ") ;
573560 return null ;
574561 }
575562 }
@@ -628,7 +615,7 @@ public static void LoadPrefabInfoFile(Mod mod, Mod.File file)
628615 }
629616 catch ( Exception e )
630617 {
631- Plugin . logger . LogError ( $ "Error on loading prefab info from { mod . id } mod: { e . Message } ") ;
618+ Plugin . logger . LogError ( $ "Error on loading prefab info from { mod . id } mod: { e . StackTrace } ") ;
632619 }
633620 }
634621
@@ -741,7 +728,7 @@ public static void LoadGameLogicDataPatch(Mod mod, JObject gld, JObject patch)
741728 }
742729 catch ( Exception e )
743730 {
744- Plugin . logger . LogError ( $ "Error on loading patch from { mod . id } mod: { e . Message } ") ;
731+ Plugin . logger . LogError ( $ "Error on loading patch from { mod . id } mod: { e . StackTrace } ") ;
745732 mod . status = Mod . Status . Error ;
746733 }
747734 }
@@ -768,124 +755,135 @@ internal static void ProcessGameLogicData(GameLogicData gameLogicData, JObject r
768755 {
769756 try
770757 {
771- foreach ( JToken jtoken in rootObject . SelectTokens ( "$.*.*" ) . ToArray ( ) )
758+ CreateMappings ( rootObject ) ;
759+ ProcessPrefabs ( ) ;
760+ ProcessEmbark ( ) ;
761+ ProcessAttract ( ) ;
762+ }
763+ catch ( Exception e )
764+ {
765+ Plugin . logger . LogError ( $ "Error on processing modified game logic data : { e . StackTrace } ") ;
766+ }
767+ }
768+ internal static void CreateMappings ( JObject rootObject )
769+ {
770+ foreach ( JToken jtoken in rootObject . SelectTokens ( "$.*.*" ) . ToArray ( ) )
771+ {
772+ JObject ? token = jtoken . TryCast < JObject > ( ) ;
773+ if ( token != null )
772774 {
773- JObject ? token = jtoken . TryCast < JObject > ( ) ;
774- if ( token != null )
775+ string dataType = Util . GetJTokenName ( token , 2 ) ;
776+ if ( Loader . typeMappings . TryGetValue ( dataType , out Loader . TypeMapping ? typeMapping ) )
775777 {
776- string dataType = Util . GetJTokenName ( token , 2 ) ;
777- if ( Loader . typeMappings . TryGetValue ( dataType , out Loader . TypeMapping ? typeMapping ) )
778+ if ( token [ "idx" ] != null && ( int ) token [ "idx" ] == - 1 && typeMapping . shouldCreateCache )
778779 {
779- if ( token [ "idx" ] != null && ( int ) token [ "idx" ] == - 1 && typeMapping . shouldCreateCache )
780+ Type targetType = typeMapping . type ;
781+ string id = Util . GetJTokenName ( token ) ;
782+ token [ "idx" ] = Registry . autoidx ;
783+ MethodInfo ? methodInfo = typeof ( EnumCache < > ) . MakeGenericType ( targetType ) . GetMethod ( "AddMapping" ) ;
784+ if ( methodInfo != null )
780785 {
781- Type targetType = typeMapping . type ;
782- string id = Util . GetJTokenName ( token ) ;
783- token [ "idx" ] = Registry . autoidx ;
784- MethodInfo ? methodInfo = typeof ( EnumCache < > ) . MakeGenericType ( targetType ) . GetMethod ( "AddMapping" ) ;
785- if ( methodInfo != null )
786+ methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
787+ methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
788+
789+ if ( Loader . typeHandlers . TryGetValue ( targetType , out var handler ) )
786790 {
787- methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
788- methodInfo . Invoke ( null , new object [ ] { id , Registry . autoidx } ) ;
789-
790- if ( Loader . typeHandlers . TryGetValue ( targetType , out var handler ) )
791- {
792- handler ( token , true ) ;
793- }
794- Plugin . logger . LogInfo ( "Created mapping for " + targetType . ToString ( ) + " with id " + id + " and index " + Registry . autoidx ) ;
795- Registry . autoidx ++ ;
791+ handler ( token , true ) ;
796792 }
793+ Plugin . logger . LogInfo ( "Created mapping for " + targetType . ToString ( ) + " with id " + id + " and index " + Registry . autoidx ) ;
794+ Registry . autoidx ++ ;
797795 }
798796 }
799797 }
800798 }
801- foreach ( JToken jtoken in rootObject . SelectTokens ( "$.*.*" ) . ToArray ( ) )
799+ }
800+ foreach ( JToken jtoken in rootObject . SelectTokens ( "$.*.*" ) . ToArray ( ) )
801+ {
802+ JObject ? token = jtoken . TryCast < JObject > ( ) ;
803+ if ( token != null )
802804 {
803- JObject ? token = jtoken . TryCast < JObject > ( ) ;
804- if ( token != null )
805+ string dataType = Util . GetJTokenName ( token , 2 ) ;
806+ if ( Loader . typeMappings . TryGetValue ( dataType , out Loader . TypeMapping ? typeMapping ) )
805807 {
806- string dataType = Util . GetJTokenName ( token , 2 ) ;
807- if ( Loader . typeMappings . TryGetValue ( dataType , out Loader . TypeMapping ? typeMapping ) )
808+ if ( Loader . typeHandlers . TryGetValue ( typeMapping . type , out var handler ) )
808809 {
809- if ( Loader . typeHandlers . TryGetValue ( typeMapping . type , out var handler ) )
810- {
811- handler ( token , false ) ;
812- }
810+ handler ( token , false ) ;
813811 }
814812 }
815813 }
816- foreach ( System . Collections . Generic . KeyValuePair < int , string > item in Registry . prefabNames )
814+ }
815+ }
816+ internal static void ProcessPrefabs ( )
817+ {
818+ foreach ( System . Collections . Generic . KeyValuePair < int , string > item in Registry . prefabNames )
819+ {
820+ UnitData . Type unitPrefabType = UnitData . Type . Scout ;
821+ string prefabId = item . Value ;
822+ if ( Enum . TryParse ( prefabId , out UnitData . Type parsedType ) )
823+ {
824+ unitPrefabType = parsedType ;
825+ PrefabManager . units . TryAdd ( item . Key , PrefabManager . units [ ( int ) unitPrefabType ] ) ;
826+ }
827+ else
817828 {
818- UnitData . Type unitPrefabType = UnitData . Type . Scout ;
819- string prefabId = item . Value ;
820- if ( Enum . TryParse ( prefabId , out UnitData . Type parsedType ) )
829+ KeyValuePair < Visual . PrefabInfo , Unit > prefabInfo = Registry . unitPrefabs . FirstOrDefault ( kv => kv . Key . name == prefabId ) ;
830+ if ( ! EqualityComparer < Visual . PrefabInfo > . Default . Equals ( prefabInfo . Key , default ) )
821831 {
822- unitPrefabType = parsedType ;
823- PrefabManager . units . TryAdd ( item . Key , PrefabManager . units [ ( int ) unitPrefabType ] ) ;
832+ PrefabManager . units . TryAdd ( item . Key , prefabInfo . Value ) ;
824833 }
825834 else
826835 {
827- KeyValuePair < Visual . PrefabInfo , Unit > prefabInfo = Registry . unitPrefabs . FirstOrDefault ( kv => kv . Key . name == prefabId ) ;
828- if ( ! EqualityComparer < Visual . PrefabInfo > . Default . Equals ( prefabInfo . Key , default ) )
829- {
830- PrefabManager . units . TryAdd ( item . Key , prefabInfo . Value ) ;
831- }
832- else
833- {
834- PrefabManager . units . TryAdd ( item . Key , PrefabManager . units [ ( int ) unitPrefabType ] ) ;
835- }
836+ PrefabManager . units . TryAdd ( item . Key , PrefabManager . units [ ( int ) unitPrefabType ] ) ;
836837 }
837838 }
838- foreach ( Visual . SkinInfo skin in Registry . skinInfo )
839+ }
840+ }
841+ internal static void ProcessEmbark ( )
842+ {
843+ foreach ( KeyValuePair < string , string > entry in Main . embarkNames )
844+ {
845+ try
839846 {
840- if ( skin . skinData != null )
841- gameLogicData . skinData [ ( SkinType ) skin . idx ] = skin . skinData ;
847+ UnitData . Type unit = EnumCache < UnitData . Type > . GetType ( entry . Key ) ;
848+ UnitData . Type newUnit = EnumCache < UnitData . Type > . GetType ( entry . Value ) ;
849+ Main . embarkOverrides [ unit ] = newUnit ;
850+ Plugin . logger . LogInfo ( $ "Embark unit type for { entry . Key } is now { entry . Value } ") ;
842851 }
843- foreach ( KeyValuePair < string , string > entry in Main . embarkNames )
852+ catch
844853 {
845- try
846- {
847- UnitData . Type unit = EnumCache < UnitData . Type > . GetType ( entry . Key ) ;
848- UnitData . Type newUnit = EnumCache < UnitData . Type > . GetType ( entry . Value ) ;
849- Main . embarkOverrides [ unit ] = newUnit ;
850- Plugin . logger . LogInfo ( $ "Embark unit type for { entry . Key } is now { entry . Value } ") ;
851- }
852- catch
853- {
854- Plugin . logger . LogError ( $ "Embark unit type for { entry . Key } is not valid: { entry . Value } ") ;
855- }
854+ Plugin . logger . LogError ( $ "Embark unit type for { entry . Key } is not valid: { entry . Value } ") ;
856855 }
857- foreach ( KeyValuePair < string , string > entry in Main . attractsResourceNames )
856+ }
857+ }
858+ internal static void ProcessAttract ( )
859+ {
860+ foreach ( KeyValuePair < string , string > entry in Main . attractsResourceNames )
861+ {
862+ try
858863 {
859- try
860- {
861- ImprovementData . Type improvement = EnumCache < ImprovementData . Type > . GetType ( entry . Key ) ;
862- ResourceData . Type resource = EnumCache < ResourceData . Type > . GetType ( entry . Value ) ;
863- Main . attractsResourceOverrides [ improvement ] = resource ;
864- Plugin . logger . LogInfo ( $ "Improvement { entry . Key } now attracts { entry . Value } ") ;
865- }
866- catch
867- {
868- Plugin . logger . LogError ( $ "Improvement { entry . Key } resource type is not valid: { entry . Value } ") ;
869- }
864+ ImprovementData . Type improvement = EnumCache < ImprovementData . Type > . GetType ( entry . Key ) ;
865+ ResourceData . Type resource = EnumCache < ResourceData . Type > . GetType ( entry . Value ) ;
866+ Main . attractsResourceOverrides [ improvement ] = resource ;
867+ Plugin . logger . LogInfo ( $ "Improvement { entry . Key } now attracts { entry . Value } ") ;
870868 }
871- foreach ( KeyValuePair < string , string > entry in Main . attractsTerrainNames )
869+ catch
872870 {
873- try
874- {
875- ImprovementData . Type improvement = EnumCache < ImprovementData . Type > . GetType ( entry . Key ) ;
876- Polytopia . Data . TerrainData . Type terrain = EnumCache < Polytopia . Data . TerrainData . Type > . GetType ( entry . Value ) ;
877- Main . attractsTerrainOverrides [ improvement ] = terrain ;
878- Plugin . logger . LogInfo ( $ "Improvement { entry . Key } now attracts on { entry . Value } ") ;
879- }
880- catch
881- {
882- Plugin . logger . LogError ( $ "Improvement { entry . Key } terrain type is not valid: { entry . Value } ") ;
883- }
871+ Plugin . logger . LogError ( $ "Improvement { entry . Key } resource type is not valid: { entry . Value } ") ;
884872 }
885873 }
886- catch ( Exception e )
874+ foreach ( KeyValuePair < string , string > entry in Main . attractsTerrainNames )
887875 {
888- Plugin . logger . LogError ( $ "Error on processing modified game logic data : { e . Message } ") ;
876+ try
877+ {
878+ ImprovementData . Type improvement = EnumCache < ImprovementData . Type > . GetType ( entry . Key ) ;
879+ Polytopia . Data . TerrainData . Type terrain = EnumCache < Polytopia . Data . TerrainData . Type > . GetType ( entry . Value ) ;
880+ Main . attractsTerrainOverrides [ improvement ] = terrain ;
881+ Plugin . logger . LogInfo ( $ "Improvement { entry . Key } now attracts on { entry . Value } ") ;
882+ }
883+ catch
884+ {
885+ Plugin . logger . LogError ( $ "Improvement { entry . Key } terrain type is not valid: { entry . Value } ") ;
886+ }
889887 }
890888 }
891889}
0 commit comments