Skip to content

Commit 14a8d57

Browse files
committed
Restructured a bit
1 parent 6eb1166 commit 14a8d57

2 files changed

Lines changed: 129 additions & 117 deletions

File tree

src/Loader.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,129 @@ public static void LoadAssetBundle(Mod mod, Mod.File file)
647647
AssetBundle.LoadFromMemory(file.bytes)
648648
);
649649
}
650+
651+
internal static void ProcessGameLogicData(GameLogicData gameLogicData, JObject rootObject)
652+
{
653+
try
654+
{
655+
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
656+
{
657+
JObject? token = jtoken.TryCast<JObject>();
658+
if (token != null)
659+
{
660+
string dataType = Util.GetJTokenName(token, 2);
661+
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
662+
{
663+
if (token["idx"] != null && (int)token["idx"] == -1 && typeMapping.shouldCreateCache)
664+
{
665+
Type targetType = typeMapping.type;
666+
string id = Util.GetJTokenName(token);
667+
token["idx"] = Registry.autoidx;
668+
MethodInfo? methodInfo = typeof(EnumCache<>).MakeGenericType(targetType).GetMethod("AddMapping");
669+
if (methodInfo != null)
670+
{
671+
methodInfo.Invoke(null, new object[] { id, Registry.autoidx });
672+
methodInfo.Invoke(null, new object[] { id, Registry.autoidx });
673+
674+
if (Loader.typeHandlers.TryGetValue(targetType, out var handler))
675+
{
676+
handler(token, true);
677+
}
678+
Plugin.logger.LogInfo("Created mapping for " + targetType.ToString() + " with id " + id + " and index " + Registry.autoidx);
679+
Registry.autoidx++;
680+
}
681+
}
682+
}
683+
}
684+
}
685+
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
686+
{
687+
JObject? token = jtoken.TryCast<JObject>();
688+
if (token != null)
689+
{
690+
string dataType = Util.GetJTokenName(token, 2);
691+
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
692+
{
693+
if (Loader.typeHandlers.TryGetValue(typeMapping.type, out var handler))
694+
{
695+
handler(token, false);
696+
}
697+
}
698+
}
699+
}
700+
foreach (System.Collections.Generic.KeyValuePair<int, string> item in Registry.prefabNames)
701+
{
702+
UnitData.Type unitPrefabType = UnitData.Type.Scout;
703+
string prefabId = item.Value;
704+
if (Enum.TryParse(prefabId, out UnitData.Type parsedType))
705+
{
706+
unitPrefabType = parsedType;
707+
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
708+
}
709+
else
710+
{
711+
KeyValuePair<Visual.PrefabInfo, Unit> prefabInfo = Registry.unitPrefabs.FirstOrDefault(kv => kv.Key.name == prefabId);
712+
if (!EqualityComparer<Visual.PrefabInfo>.Default.Equals(prefabInfo.Key, default))
713+
{
714+
PrefabManager.units.TryAdd(item.Key, prefabInfo.Value);
715+
}
716+
else
717+
{
718+
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
719+
}
720+
}
721+
}
722+
foreach (Visual.SkinInfo skin in Registry.skinInfo)
723+
{
724+
if (skin.skinData != null)
725+
gameLogicData.skinData[(SkinType)skin.idx] = skin.skinData;
726+
}
727+
foreach (KeyValuePair<string, string> entry in Main.embarkNames)
728+
{
729+
try
730+
{
731+
UnitData.Type unit = EnumCache<UnitData.Type>.GetType(entry.Key);
732+
UnitData.Type newUnit = EnumCache<UnitData.Type>.GetType(entry.Value);
733+
Main.embarkOverrides[unit] = newUnit;
734+
Plugin.logger.LogInfo($"Embark unit type for {entry.Key} is now {entry.Value}");
735+
}
736+
catch
737+
{
738+
Plugin.logger.LogError($"Embark unit type for {entry.Key} is not valid: {entry.Value}");
739+
}
740+
}
741+
foreach (KeyValuePair<string, string> entry in Main.attractsResourceNames)
742+
{
743+
try
744+
{
745+
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
746+
ResourceData.Type resource = EnumCache<ResourceData.Type>.GetType(entry.Value);
747+
Main.attractsResourceOverrides[improvement] = resource;
748+
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts {entry.Value}");
749+
}
750+
catch
751+
{
752+
Plugin.logger.LogError($"Improvement {entry.Key} resource type is not valid: {entry.Value}");
753+
}
754+
}
755+
foreach (KeyValuePair<string, string> entry in Main.attractsTerrainNames)
756+
{
757+
try
758+
{
759+
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
760+
Polytopia.Data.TerrainData.Type terrain = EnumCache<Polytopia.Data.TerrainData.Type>.GetType(entry.Value);
761+
Main.attractsTerrainOverrides[improvement] = terrain;
762+
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts on {entry.Value}");
763+
}
764+
catch
765+
{
766+
Plugin.logger.LogError($"Improvement {entry.Key} terrain type is not valid: {entry.Value}");
767+
}
768+
}
769+
}
770+
catch (Exception e)
771+
{
772+
Plugin.logger.LogError($"Error on processing modified game logic data : {e.Message}");
773+
}
774+
}
650775
}

src/Managers/Main.cs

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -33,121 +33,7 @@ private static void GameLogicData_Parse(GameLogicData __instance, ref JObject ro
3333
{
3434
if (!fullyInitialized)
3535
{
36-
Load(rootObject);
37-
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
38-
{
39-
JObject? token = jtoken.TryCast<JObject>();
40-
if (token != null)
41-
{
42-
string dataType = Util.GetJTokenName(token, 2);
43-
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
44-
{
45-
if (token["idx"] != null && (int)token["idx"] == -1 && typeMapping.shouldCreateCache)
46-
{
47-
Type targetType = typeMapping.type;
48-
string id = Util.GetJTokenName(token);
49-
token["idx"] = Registry.autoidx;
50-
MethodInfo? methodInfo = typeof(EnumCache<>).MakeGenericType(targetType).GetMethod("AddMapping");
51-
if (methodInfo != null)
52-
{
53-
methodInfo.Invoke(null, new object[] { id, Registry.autoidx });
54-
methodInfo.Invoke(null, new object[] { id, Registry.autoidx });
55-
56-
if (Loader.typeHandlers.TryGetValue(targetType, out var handler))
57-
{
58-
handler(token, true);
59-
}
60-
Plugin.logger.LogInfo("Created mapping for " + targetType.ToString() + " with id " + id + " and index " + Registry.autoidx);
61-
Registry.autoidx++;
62-
}
63-
}
64-
}
65-
}
66-
}
67-
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
68-
{
69-
JObject? token = jtoken.TryCast<JObject>();
70-
if (token != null)
71-
{
72-
string dataType = Util.GetJTokenName(token, 2);
73-
if (Loader.typeMappings.TryGetValue(dataType, out Loader.TypeMapping? typeMapping))
74-
{
75-
if (Loader.typeHandlers.TryGetValue(typeMapping.type, out var handler))
76-
{
77-
handler(token, false);
78-
}
79-
}
80-
}
81-
}
82-
foreach (System.Collections.Generic.KeyValuePair<int, string> item in Registry.prefabNames)
83-
{
84-
UnitData.Type unitPrefabType = UnitData.Type.Scout;
85-
string prefabId = item.Value;
86-
if (Enum.TryParse(prefabId, out UnitData.Type parsedType))
87-
{
88-
unitPrefabType = parsedType;
89-
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
90-
}
91-
else
92-
{
93-
KeyValuePair<Visual.PrefabInfo, Unit> prefabInfo = Registry.unitPrefabs.FirstOrDefault(kv => kv.Key.name == prefabId);
94-
if (!EqualityComparer<Visual.PrefabInfo>.Default.Equals(prefabInfo.Key, default))
95-
{
96-
PrefabManager.units.TryAdd(item.Key, prefabInfo.Value);
97-
}
98-
else
99-
{
100-
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
101-
}
102-
}
103-
}
104-
foreach (Visual.SkinInfo skin in Registry.skinInfo)
105-
{
106-
if (skin.skinData != null)
107-
__instance.skinData[(SkinType)skin.idx] = skin.skinData;
108-
}
109-
foreach (KeyValuePair<string, string> entry in embarkNames)
110-
{
111-
try
112-
{
113-
UnitData.Type unit = EnumCache<UnitData.Type>.GetType(entry.Key);
114-
UnitData.Type newUnit = EnumCache<UnitData.Type>.GetType(entry.Value);
115-
embarkOverrides[unit] = newUnit;
116-
Plugin.logger.LogInfo($"Embark unit type for {entry.Key} is now {entry.Value}");
117-
}
118-
catch
119-
{
120-
Plugin.logger.LogError($"Embark unit type for {entry.Key} is not valid: {entry.Value}");
121-
}
122-
}
123-
foreach (KeyValuePair<string, string> entry in attractsResourceNames)
124-
{
125-
try
126-
{
127-
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
128-
ResourceData.Type resource = EnumCache<ResourceData.Type>.GetType(entry.Value);
129-
attractsResourceOverrides[improvement] = resource;
130-
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts {entry.Value}");
131-
}
132-
catch
133-
{
134-
Plugin.logger.LogError($"Improvement {entry.Key} resource type is not valid: {entry.Value}");
135-
}
136-
}
137-
foreach (KeyValuePair<string, string> entry in attractsTerrainNames)
138-
{
139-
try
140-
{
141-
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
142-
Polytopia.Data.TerrainData.Type terrain = EnumCache<Polytopia.Data.TerrainData.Type>.GetType(entry.Value);
143-
attractsTerrainOverrides[improvement] = terrain;
144-
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts on {entry.Value}");
145-
}
146-
catch
147-
{
148-
Plugin.logger.LogError($"Improvement {entry.Key} terrain type is not valid: {entry.Value}");
149-
}
150-
}
36+
Load(__instance, rootObject);
15137
fullyInitialized = true;
15238
}
15339
}
@@ -408,7 +294,7 @@ internal static void Init()
408294
stopwatch.Stop();
409295
}
410296

411-
internal static void Load(JObject gameLogicdata)
297+
internal static void Load(GameLogicData gameLogicData, JObject json)
412298
{
413299
stopwatch.Start();
414300
Loc.BuildAndLoadLocalization(
@@ -432,7 +318,7 @@ internal static void Load(JObject gameLogicdata)
432318
{
433319
Loader.LoadGameLogicDataPatch(
434320
mod,
435-
gameLogicdata,
321+
json,
436322
JObject.Parse(new StreamReader(new MemoryStream(file.bytes)).ReadToEnd())
437323
);
438324
continue;
@@ -465,6 +351,7 @@ internal static void Load(JObject gameLogicdata)
465351
{
466352
TechItem.techTierFirebaseId.Add($"tech_research_{i}");
467353
}
354+
Loader.ProcessGameLogicData(gameLogicData, json);
468355
stopwatch.Stop();
469356
Plugin.logger.LogInfo($"Loaded all mods in {stopwatch.ElapsedMilliseconds}ms");
470357
}

0 commit comments

Comments
 (0)