-
Notifications
You must be signed in to change notification settings - Fork 13
/
Patches.cs
230 lines (205 loc) · 7.97 KB
/
Patches.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BTD_Mod_Helper;
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Extensions;
using HarmonyLib;
using Il2CppAssets.Scripts;
using Il2CppAssets.Scripts.Models.Towers;
using Il2CppAssets.Scripts.Models.Towers.Upgrades;
using Il2CppAssets.Scripts.Simulation;
using Il2CppAssets.Scripts.Simulation.Towers;
using Il2CppAssets.Scripts.Unity.Bridge;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.TowerSelectionMenu;
using Il2CppNinjaKiwi.Common.ResourceUtils;
namespace AscendedUpgrades;
/// <summary>
/// Allow ascended upgrades to show up in all 3 paths
/// </summary>
[HarmonyPatch(typeof(TowerSelectionMenu), nameof(TowerSelectionMenu.IsUpgradePathClosed))]
internal class TowerSelectionMenu_IsUpgradePathClosed
{
[HarmonyPrefix]
internal static bool Prefix(TowerSelectionMenu __instance, ref bool __result)
{
if (__instance.selectedTower.tower.HasAscendedUpgrades())
{
__result = false;
return false;
}
return true;
}
}
/// <summary>
/// Fix v38.1 inlining of TowerSelectionMenu.IsUpgradePathClosed method
/// </summary>
[HarmonyPatch(typeof(UpgradeObject), nameof(UpgradeObject.UpdateVisuals))]
internal static class UpgradeObject_UpdateVisuals
{
[HarmonyPrefix]
private static bool Prefix(UpgradeObject __instance, int path, bool upgradeClicked)
{
if (ModHelper.HasMod("UltimateCrosspathing") || ModHelper.HasMod("PathsPlusPlus")) return false;
if (__instance.towerSelectionMenu.IsUpgradePathClosed(path))
{
__instance.upgradeButton.SetUpgradeModel(null);
}
__instance.CheckLocked();
var maxTier = __instance.CheckBlockedPath();
var maxTierRestricted = __instance.CheckRestrictedPath();
__instance.SetTier(__instance.tier, maxTier, maxTierRestricted);
__instance.currentUpgrade.UpdateVisuals();
__instance.upgradeButton.UpdateVisuals(path, upgradeClicked);
return false;
}
}
/// <summary>
/// Make Ascended Upgrades show up when available
/// </summary>
[HarmonyPatch(typeof(UpgradeObject), nameof(UpgradeObject.LoadUpgrades))]
internal static class UpgradeObject_LoadUpgrades
{
[HarmonyPostfix]
private static void Postfix(UpgradeObject __instance)
{
var ascendedPips = __instance.transform.GetComponentInChildren<AscendedPips>();
if (ascendedPips == null)
{
ascendedPips = AscendedPips.Create(__instance);
}
if (!__instance.tts.tower.HasAscendedUpgrades() || __instance.path >= 3)
{
ascendedPips.SetAmount(0);
return;
}
var gameModel = InGame.instance.bridge.Model;
var upgradeId = AscendedUpgrade.IdByPath[__instance.path];
__instance.upgradeButton.SetUpgradeModel(gameModel.GetUpgrade(upgradeId));
var tower = __instance.towerSelectionMenu.selectedTower.tower;
ascendedPips.SetAmount(Math.Min(tower.GetAscendedStacks().First(pair => pair.Key.Path == __instance.path).Value,
AscendedUpgradesMod.MaxUpgradePips));
}
}
/// <summary>
/// Fixes a minor visual glitch where the wrong info can appear for one frame
/// </summary>
[HarmonyPatch(typeof(UpgradeObject), nameof(UpgradeObject.IncreaseTier))]
internal static class UpgradeObject_IncreaseTier
{
[HarmonyPrefix]
private static bool Prefix(UpgradeObject __instance) => !__instance.tts.tower.HasAscendedUpgrades();
}
/// <summary>
/// Change the look of the upgrade background for ascended upgrades
/// </summary>
[HarmonyPatch]
internal static class UpgradeButton_Visuals
{
private static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(UpgradeButton), nameof(UpgradeButton.UpdateVisuals));
yield return AccessTools.Method(typeof(UpgradeButton), nameof(UpgradeButton.LoadBackground));
yield return AccessTools.Method(typeof(UpgradeButton), nameof(UpgradeButton.CheckCash));
}
[HarmonyPostfix]
private static void Postfix(UpgradeButton __instance)
{
var upgradeId = __instance.upgrade?.name ?? "";
if (AscendedUpgrade.IdByPath.ContainsValue(upgradeId) &&
__instance.upgradeStatus == UpgradeButton.UpgradeStatus.Purchasable)
{
ResourceLoader.LoadSpriteFromSpriteReferenceAsync(
ModContent.GetSpriteReference<AscendedUpgradesMod>("AscendedArrowBtn"),
__instance.background);
}
}
}
[HarmonyPatch(typeof(TowerSelectionMenu), nameof(TowerSelectionMenu.UpgradeTower), typeof(UpgradeModel), typeof(int),
typeof(float), typeof(double))]
internal static class TowerSelectionMenu_UpgradeTower
{
[HarmonyPrefix]
private static void Prefix(UpgradeModel upgrade)
{
UnityToSimulation_UpgradeTower_Impl.current = upgrade;
UnityToSimulation_UpgradeTower_Impl.cash = InGame.instance.GetCash();
}
}
/// <summary>
/// Make sure that an Ascended Upgrade goes through with the noise / sound, and also applies its effects
/// </summary>
[HarmonyPatch(typeof(UnityToSimulation), nameof(UnityToSimulation.UpgradeTower_Impl))]
internal static class UnityToSimulation_UpgradeTower_Impl
{
internal static UpgradeModel? current;
internal static double cash;
[HarmonyPostfix]
private static void Postfix(UnityToSimulation __instance, ObjectId id, int pathIndex, int inputId)
{
if (current == null || pathIndex >= 3) return;
var towerManager = __instance.simulation.towerManager;
var tower = towerManager.GetTowerById(id);
if (!tower.HasAscendedUpgrades()) return;
var cost = towerManager.GetTowerUpgradeCost(tower, pathIndex, 5);
if (current.name.StartsWith(nameof(AscendedUpgrade)) && cost <= cash)
{
towerManager.UpgradeTower(inputId, tower, tower.rootModel.Cast<TowerModel>(), pathIndex, cost);
InGame.instance.SetCash(cash - cost);
#if DEBUG
ModHelper.Msg<AscendedUpgradesMod>($"Doing ascended upgrade {pathIndex} with cost {cost}");
#endif
var ascendedUpgrade = AscendedUpgrade.ByPath[pathIndex];
var stacks = tower.GetAscendedStacks()[ascendedUpgrade];
ascendedUpgrade.Apply(tower, stacks + 1, 1);
}
}
}
/// <summary>
/// Undo possible upgrade cost changes if SharedTowerScaling is enabled
/// </summary>
[HarmonyPatch(typeof(TowerManager), nameof(TowerManager.DestroyTower))]
internal static class TowerManager_DestroyTower
{
[HarmonyPrefix]
private static void Prefix(Tower tower)
{
if (tower.ParentId.Id != -1) return;
foreach (var (ascendedUpgrade, stacks) in tower.GetAscendedStacks())
{
ascendedUpgrade.UnApply(stacks);
}
}
}
/// <summary>
/// Change the ascended upgrade cost based on how many have been purchased for the tower
/// </summary>
[HarmonyPatch(typeof(Simulation), nameof(Simulation.GetSimulationBehaviorDiscount))]
internal static class Simulation_GetSimulationBehaviorDiscount
{
[HarmonyPostfix]
private static void Postfix(Tower tower, int path, ref float __result)
{
if (tower.HasAscendedUpgrades() && !AscendedUpgradesMod.SharedTowerScaling)
{
var mult = AscendedUpgradesMod.IncreaseUpgradeCost / (float) AscendedUpgradesMod.BaseUpgradeCost;
var stacks = tower.GetAscendedStacks();
var amount = AscendedUpgradesMod.SharedUpgradeScaling
? stacks.Sum(pair => pair.Value)
: stacks.Where(pair => pair.Key.Path == path).Select(pair => pair.Value).SingleOrDefault();
__result -= amount * mult;
}
}
}
[HarmonyPatch(typeof(BuffIcon), nameof(BuffIcon.Show))]
internal static class BuffIcon_Show
{
[HarmonyPrefix]
private static void Prefix(BuffQuery buff)
{
if (!buff.buffIndicator.name.Contains(nameof(AscendedUpgrades))) return;
buff.availableBuffCount = buff.timedMutator.mutator.AscendedStackCount();
}
}