Skip to content

Commit 8120691

Browse files
committed
Fix
1 parent 8e49012 commit 8120691

File tree

4 files changed

+53
-48
lines changed

4 files changed

+53
-48
lines changed

src/Loader.cs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -836,63 +836,71 @@ internal static void CreateMappings(JObject rootObject)
836836
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
837837
{
838838
JObject? token = jtoken.TryCast<JObject>();
839-
if (token != null)
839+
if (token == null)
840+
continue;
841+
842+
string dataType = Util.GetJTokenName(token, 2);
843+
if (!typeMappings.TryGetValue(dataType, out TypeMapping? typeMapping))
844+
continue;
845+
846+
if (token["idx"] == null || !typeMapping.shouldCreateCache)
847+
continue;
848+
849+
Type targetType = typeMapping.type;
850+
if (!targetType.IsEnum)
840851
{
841-
string dataType = Util.GetJTokenName(token, 2);
842-
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
843-
{
844-
if (token["idx"] != null && typeMapping.shouldCreateCache)
845-
{
846-
Type targetType = typeMapping.type;
847-
if (!targetType.IsEnum)
848-
{
849-
Plugin.logger.LogWarning($"Type {targetType.FullName} is not an enum, skipping!");
850-
continue;
851-
}
852-
string id = Util.GetJTokenName(token);
853-
if((int)token["idx"] == -1)
854-
{
855-
token["idx"] = Registry.autoidx;
856-
}
857-
else if(PolyMod.Plugin.config.allowUnsafeIndexes)
858-
{
859-
Array values = Enum.GetValues(targetType);
860-
861-
var maxValue = values.Cast<int>().Max();
862-
863-
if(maxValue >= (int)token["idx"])
864-
{
865-
continue;
866-
}
867-
}
868-
869-
MethodInfo? methodInfo = typeof(EnumCache<>).MakeGenericType(targetType).GetMethod("AddMapping");
870-
if (methodInfo != null)
871-
{
872-
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
873-
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
874-
if (Loader.typeHandlers.TryGetValue(targetType, out var handler))
875-
{
876-
handler(token, true);
877-
}
878-
Plugin.logger.LogInfo("Created mapping for " + targetType.ToString() + " with id " + id + " and index " + (int)token["idx"]);
879-
}
852+
Plugin.logger.LogWarning($"Type {targetType.FullName} is not an enum, skipping!");
853+
continue;
854+
}
880855

881-
if((int)token["idx"] == Registry.autoidx)
882-
Registry.autoidx++;
883-
}
856+
string id = Util.GetJTokenName(token);
857+
858+
if((int)token["idx"] == -1)
859+
{
860+
token["idx"] = Registry.autoidx;
861+
Registry.autoidx++;
862+
}
863+
else if(Plugin.config.allowUnsafeIndexes)
864+
{
865+
Array values = Enum.GetValues(targetType);
866+
867+
var maxValue = values.Cast<int>().Max();
868+
869+
if(maxValue >= (int)token["idx"])
870+
{
871+
continue;
884872
}
885873
}
874+
else
875+
{
876+
continue;
877+
}
878+
879+
MethodInfo? methodInfo = typeof(EnumCache<>).MakeGenericType(targetType).GetMethod("AddMapping");
880+
if (methodInfo == null)
881+
{
882+
Plugin.logger.LogWarning($"Missing AddMapping method for {targetType.FullName}");
883+
continue;
884+
}
885+
886+
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
887+
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
888+
889+
if (typeHandlers.TryGetValue(targetType, out var handler))
890+
{
891+
handler(token, true);
892+
}
893+
Plugin.logger.LogInfo("Created mapping for " + targetType.ToString() + " with id " + id + " and index " + (int)token["idx"]);
886894
}
887895
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
888896
{
889897
JObject? token = jtoken.TryCast<JObject>();
890898
if (token != null)
891899
{
892900
string dataType = Util.GetJTokenName(token, 2);
893-
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
901+
if (typeMappings.TryGetValue(dataType, out TypeMapping? typeMapping))
894902
{
895-
if (Loader.typeHandlers.TryGetValue(typeMapping.type, out var handler))
903+
if (typeHandlers.TryGetValue(typeMapping.type, out var handler))
896904
{
897905
handler(token, false);
898906
}

src/Managers/Compatibility.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using HarmonyLib;
2-
using Polytopia.IO;
32
using System.Text;
43
using UnityEngine;
54
using UnityEngine.EventSystems;

src/Managers/Loc.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Il2CppInterop.Runtime.InteropTypes.Arrays;
44
using System.Reflection;
55
using LibCpp2IL;
6-
using Polytopia.Data;
76

87
namespace PolyMod.Managers;
98

src/Registry.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using LibCpp2IL;
22
using PolyMod.Managers;
3-
using Polytopia.Data;
43
using PolytopiaBackendBase.Game;
54
using UnityEngine;
65
using PolytopiaBackendBase.Common;

0 commit comments

Comments
 (0)