@@ -11,14 +11,17 @@ namespace PolyMod;
1111/// <summary>
1212/// A collection of utility methods for the PolyMod framework.
1313/// </summary>
14- internal static class Util
14+ public static class Util
1515{
16+ internal const string PLACEHOLDER = "placeholder" ;
17+ internal const string HIDDEN = "hidden" ;
18+ internal static Dictionary < string , string > cachedReversedSpriteDataNames = new ( ) ;
1619 /// <summary>
1720 /// Wraps a managed type for use in the Il2Cpp runtime.
1821 /// </summary>
1922 /// <typeparam name="T">The type to wrap.</typeparam>
2023 /// <returns>The Il2Cpp representation of the type.</returns>
21- internal static Il2CppSystem . Type WrapType < T > ( ) where T : class
24+ public static Il2CppSystem . Type WrapType < T > ( ) where T : class
2225 {
2326 if ( ! ClassInjector . IsTypeRegisteredInIl2Cpp < T > ( ) )
2427 ClassInjector . RegisterTypeInIl2Cpp < T > ( ) ;
@@ -30,7 +33,7 @@ internal static Il2CppSystem.Type WrapType<T>() where T : class
3033 /// </summary>
3134 /// <param name="data">The object to hash.</param>
3235 /// <returns>The hexadecimal string representation of the hash.</returns>
33- internal static string Hash ( object data )
36+ public static string Hash ( object data )
3437 {
3538 return Convert . ToHexString ( SHA256 . HashData ( Encoding . UTF8 . GetBytes ( data . ToString ( ) ! ) ) ) ;
3639 }
@@ -41,7 +44,7 @@ internal static string Hash(object data)
4144 /// <param name="token">The JToken.</param>
4245 /// <param name="n">The part of the path to retrieve, from the end.</param>
4346 /// <returns>The name of the JToken.</returns>
44- internal static string GetJTokenName ( JToken token , int n = 1 )
47+ public static string GetJTokenName ( JToken token , int n = 1 )
4548 {
4649 return token . Path . Split ( '.' ) [ ^ n ] ;
4750 }
@@ -51,7 +54,7 @@ internal static string GetJTokenName(JToken token, int n = 1)
5154 /// </summary>
5255 /// <param name="self">The Il2CppSystem.Version to convert.</param>
5356 /// <returns>The equivalent System.Version.</returns>
54- internal static Version Cast ( this Il2CppSystem . Version self )
57+ public static Version Cast ( this Il2CppSystem . Version self )
5558 {
5659 return new ( self . ToString ( ) ) ;
5760 }
@@ -62,7 +65,7 @@ internal static Version Cast(this Il2CppSystem.Version self)
6265 /// <typeparam name="T">The result type of the Task.</typeparam>
6366 /// <param name="self">The Task to unwrap.</param>
6467 /// <returns>The result of the Task.</returns>
65- internal static T UnwrapAsync < T > ( this Task < T > self )
68+ public static T UnwrapAsync < T > ( this Task < T > self )
6669 {
6770 return self . GetAwaiter ( ) . GetResult ( ) ;
6871 }
@@ -72,7 +75,7 @@ internal static T UnwrapAsync<T>(this Task<T> self)
7275 /// </summary>
7376 /// <param name="self">The Version to modify.</param>
7477 /// <returns>A new Version with the revision set to -1.</returns>
75- internal static Version CutRevision ( this Version self )
78+ public static Version CutRevision ( this Version self )
7679 {
7780 return new ( self . Major , self . Minor , self . Build ) ;
7881 }
@@ -84,7 +87,7 @@ internal static Version CutRevision(this Version self)
8487 /// <param name="version1">The first version string.</param>
8588 /// <param name="version2">The second version string.</param>
8689 /// <returns>True if version1 is older or equal to version2, false otherwise.</returns>
87- internal static bool IsVersionOlderOrEqual ( this string version1 , string version2 )
90+ public static bool IsVersionOlderOrEqual ( this string version1 , string version2 )
8891 {
8992 Version version1_ = new ( version1 . Split ( '-' ) [ 0 ] ) ;
9093 Version version2_ = new ( version2 . Split ( '-' ) [ 0 ] ) ;
@@ -107,11 +110,49 @@ internal static bool IsVersionOlderOrEqual(this string version1, string version2
107110 /// <param name="tribe">The tribe.</param>
108111 /// <param name="skin">The skin.</param>
109112 /// <returns>The style name.</returns>
110- internal static string GetStyle ( TribeType tribe , SkinType skin )
113+ public static string GetStyle ( TribeType tribe , SkinType skin )
111114 {
112115 return skin != SkinType . Default ? EnumCache < SkinType > . GetName ( skin ) : EnumCache < TribeType > . GetName ( tribe ) ;
113116 }
114117
118+ internal static void CacheReversedSpriteDataNames ( )
119+ {
120+ List < TerrainData . Type > terrains = Enum . GetValues ( typeof ( TerrainData . Type ) ) . Cast < TerrainData . Type > ( ) . ToList ( ) ;
121+ foreach ( var terrain in terrains )
122+ {
123+ string terrainToString = SpriteData . TerrainToString ( terrain ) ;
124+
125+ if ( terrainToString == HIDDEN )
126+ continue ;
127+
128+ string terrainName = EnumCache < TerrainData . Type > . GetName ( terrain ) ;
129+ if ( terrain == TerrainData . Type . Wetland )
130+ terrainName = EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Field ) + "_flooded" ;
131+
132+ cachedReversedSpriteDataNames [ terrainToString ] = terrainName ;
133+ }
134+ List < ResourceData . Type > resources = Enum . GetValues ( typeof ( ResourceData . Type ) ) . Cast < ResourceData . Type > ( ) . ToList ( ) ;
135+ foreach ( var resource in resources )
136+ {
137+ string resourceToString = SpriteData . ResourceToString ( resource ) ;
138+
139+ if ( resourceToString == PLACEHOLDER )
140+ continue ;
141+
142+ cachedReversedSpriteDataNames [ resourceToString ] = EnumCache < ResourceData . Type > . GetName ( resource ) ;
143+ }
144+ List < ImprovementData . Type > improvements = Enum . GetValues ( typeof ( ImprovementData . Type ) ) . Cast < ImprovementData . Type > ( ) . ToList ( ) ;
145+ foreach ( var improvement in improvements )
146+ {
147+ string improvementToString = SpriteData . ImprovementToString ( improvement ) ;
148+
149+ if ( improvementToString == PLACEHOLDER )
150+ continue ;
151+
152+ cachedReversedSpriteDataNames [ improvementToString ] = EnumCache < ImprovementData . Type > . GetName ( improvement ) ;
153+ }
154+ }
155+
115156 /// <summary>
116157 /// Formats a sprite name to match the enum naming convention.
117158 /// This is a workaround for inconsistencies in the game's sprite naming.
@@ -123,49 +164,20 @@ internal static string GetStyle(TribeType tribe, SkinType skin)
123164 // This method is necessary because the sprite names in the game's data do not always
124165 // match the names in the corresponding enums. This method replaces the hardcoded sprite
125166 // names with the enum names to ensure consistency.
126- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_AQUA_FARM , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Aquafarm ) ) ;
127- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_ATOLL , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Atoll ) ) ;
128- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_BURN_FOREST , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . BurnForest ) ) ;
129- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_CLEAR_FOREST , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . ClearForest ) ) ;
130- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_CUSTOMS_HOUSE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . CustomsHouse ) ) ;
131- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_FARM , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Farm ) ) ;
132- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_FOREST_TEMPLE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . ForestTemple ) ) ;
133- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_FORGE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Forge ) ) ;
134- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_GROW_FOREST , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . GrowForest ) ) ;
135- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_ICE_BANK , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . IceBank ) ) ;
136- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_ICE_PORT , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Outpost ) ) ;
137- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_ICE_TEMPLE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . IceTemple ) ) ;
138- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_LUMBER_HUT , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . LumberHut ) ) ;
139- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_MARKET , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Market ) ) ;
140- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_MINE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Mine ) ) ;
141- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_MOUNTAIN_TEMPLE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . MountainTemple ) ) ;
142- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_PORT , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Port ) ) ;
143- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_ROAD , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Road ) ) ;
144- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_RUIN , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Ruin ) ) ;
145- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_SANCTUARY , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Sanctuary ) ) ;
146- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_SAWMILL , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Sawmill ) ) ;
147- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_TEMPLE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Temple ) ) ;
148- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_WATER_TEMPLE , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . WaterTemple ) ) ;
149- baseName = baseName . Replace ( SpriteData . IMPROVEMENT_WINDMILL , EnumCache < ImprovementData . Type > . GetName ( ImprovementData . Type . Windmill ) ) ;
150-
151- baseName = baseName . Replace ( SpriteData . RESOURCE_AQUACROP , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . AquaCrop ) ) ;
152- baseName = baseName . Replace ( SpriteData . RESOURCE_CROP , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Crop ) ) ;
153- baseName = baseName . Replace ( SpriteData . RESOURCE_FISH , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Fish ) ) ;
154- baseName = baseName . Replace ( SpriteData . RESOURCE_FRUIT , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Fruit ) ) ;
155- baseName = baseName . Replace ( SpriteData . RESOURCE_GAME , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Game ) ) ;
156- baseName = baseName . Replace ( SpriteData . RESOURCE_METAL , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Metal ) ) ;
157- baseName = baseName . Replace ( SpriteData . RESOURCE_SPORES , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Spores ) ) ;
158- baseName = baseName . Replace ( SpriteData . RESOURCE_STARFISH , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Starfish ) ) ;
159- baseName = baseName . Replace ( SpriteData . RESOURCE_WHALE , EnumCache < ResourceData . Type > . GetName ( ResourceData . Type . Whale ) ) ;
160-
161- baseName = baseName . Replace ( SpriteData . TILE_FIELD , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Field ) ) ;
162- baseName = baseName . Replace ( SpriteData . TILE_FOREST , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Forest ) ) ;
163- baseName = baseName . Replace ( SpriteData . TILE_ICE , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Ice ) ) ;
164- baseName = baseName . Replace ( SpriteData . TILE_MOUNTAIN , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Mountain ) ) ;
165- baseName = baseName . Replace ( SpriteData . TILE_OCEAN , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Ocean ) ) ;
166- baseName = baseName . Replace ( SpriteData . TILE_UNKNOWN , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Field ) ) ;
167- baseName = baseName . Replace ( SpriteData . TILE_WATER , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Water ) ) ;
168- baseName = baseName . Replace ( SpriteData . TILE_WETLAND , EnumCache < TerrainData . Type > . GetName ( TerrainData . Type . Field ) + "_flooded" ) ;
167+ foreach ( var key in cachedReversedSpriteDataNames . Keys )
168+ {
169+ if ( baseName . Contains ( SpriteData . TILE_WETLAND ) && key == SpriteData . TILE_WETLAND )
170+ {
171+ Console . Write ( baseName ) ;
172+ baseName = baseName . Replace ( key , cachedReversedSpriteDataNames [ key ] ) ;
173+ Console . Write ( baseName ) ;
174+ }
175+ else
176+ {
177+ baseName = baseName . Replace ( key , cachedReversedSpriteDataNames [ key ] ) ;
178+ }
179+
180+ }
169181
170182 baseName = baseName . Replace ( "UI_" , "" ) ;
171183 return baseName ;
0 commit comments