Skip to content

Commit 7a52a3f

Browse files
committed
Rewrote MRB's custom embark gld setting
1 parent c051479 commit 7a52a3f

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/Loader.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ public static void LoadGameLogicDataPatch(Mod mod, JObject gld, JObject patch)
441441
Registry.tribePreviews[Util.GetJTokenName(token)] = preview;
442442
}
443443
}
444+
foreach (JToken jtoken in patch.SelectTokens("$.unitData.*").ToArray())
445+
{
446+
JObject token = jtoken.Cast<JObject>();
447+
if (token["embarksTo"] != null)
448+
{
449+
string unitId = Util.GetJTokenName(token);
450+
string embarkUnitId = token["embarksTo"].ToString();
451+
Main.embarkNames[unitId] = embarkUnitId;
452+
}
453+
}
444454
gld.Merge(patch, new() { MergeArrayHandling = MergeArrayHandling.Replace, MergeNullValueHandling = MergeNullValueHandling.Merge });
445455
Plugin.logger.LogInfo($"Registried patch from {mod.id} mod");
446456
}

src/Managers/Main.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public static class Main
1616
internal static readonly Stopwatch stopwatch = new();
1717
internal static bool fullyInitialized;
1818
internal static bool dependencyCycle;
19+
internal static Dictionary<string, string> embarkNames = new();
20+
internal static Dictionary<UnitData.Type, UnitData.Type> embarkOverrides = new();
21+
internal static bool currentlyEmbarking = false;
1922

2023

2124
[HarmonyPrefix]
@@ -30,6 +33,20 @@ private static void GameLogicData_Parse(GameLogicData __instance, JObject rootOb
3033
if (skin.skinData != null)
3134
__instance.skinData[(SkinType)skin.idx] = skin.skinData;
3235
}
36+
foreach (KeyValuePair<string, string> entry in embarkNames)
37+
{
38+
try
39+
{
40+
UnitData.Type unit = EnumCache<UnitData.Type>.GetType(entry.Key);
41+
UnitData.Type newUnit = EnumCache<UnitData.Type>.GetType(entry.Value);
42+
embarkOverrides[unit] = newUnit;
43+
Plugin.logger.LogInfo($"Embark unit type for {entry.Key} is now {entry.Value}");
44+
}
45+
catch
46+
{
47+
Plugin.logger.LogError($"Embark unit type for {entry.Key} is not valid: {entry.Value}");
48+
}
49+
}
3350
fullyInitialized = true;
3451
}
3552
}
@@ -131,7 +148,7 @@ private static void GameModeScreen_Init(GameModeScreen __instance)
131148

132149
[HarmonyPrefix]
133150
[HarmonyPatch(typeof(TechView), nameof(TechView.CreateNode))]
134-
public static bool TechView_CreateNode(TechView __instance, TechData data, TechItem parentItem, float angle) {
151+
private static bool TechView_CreateNode(TechView __instance, TechData data, TechItem parentItem, float angle) {
135152
float baseAngle = 360 / GameManager.GameState.GameLogicData.GetTechData(TechData.Type.Basic).techUnlocks.Count;
136153
float childAngle = 0f;
137154
if (parentItem != null)
@@ -163,6 +180,37 @@ public static bool TechView_CreateNode(TechView __instance, TechData data, TechI
163180
return false;
164181
}
165182

183+
[HarmonyPrefix]
184+
[HarmonyPatch(typeof(EmbarkAction), nameof(EmbarkAction.Execute))]
185+
private static bool EmbarkAction_Execute_Prefix(EmbarkAction __instance, GameState gameState)
186+
{
187+
currentlyEmbarking = true;
188+
return true;
189+
}
190+
191+
[HarmonyPrefix]
192+
[HarmonyPatch(typeof(ActionUtils), nameof(ActionUtils.TrainUnit))]
193+
private static bool ActionUtils_TrainUnit(ref UnitState __result, GameState gameState, PlayerState playerState, TileData tile, ref UnitData unitData)
194+
{
195+
if (tile == null)
196+
{
197+
return true;
198+
}
199+
if (tile.unit == null)
200+
{
201+
return true;
202+
}
203+
if (currentlyEmbarking)
204+
{
205+
if (embarkOverrides.TryGetValue(tile.unit.type, out UnitData.Type newType))
206+
{
207+
gameState.GameLogicData.TryGetData(newType, out unitData);
208+
}
209+
currentlyEmbarking = false;
210+
}
211+
return true;
212+
}
213+
166214
internal static void Init()
167215
{
168216
stopwatch.Start();

0 commit comments

Comments
 (0)