-
Notifications
You must be signed in to change notification settings - Fork 1
/
Patches.cs
160 lines (147 loc) · 6.68 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
using System;
using System.Collections.Generic;
using System.Linq;
using BattleTech;
using BattleTech.UI;
using BattleTech.UI.Tooltips;
using Harmony;
using SVGImporter;
using static Abilifier.Mod;
// ReSharper disable InconsistentNaming
namespace Abilifier
{
public class Patches
{
[HarmonyPatch(typeof(SGBarracksAdvancementPanel), "Initialize")]
public static class SGBarracksAdvancementPanel_Initialize_Patch
{
public static void Prefix()
{
try
{
Helpers.PreloadIcons();
Helpers.InsertAbilities();
}
catch (Exception ex)
{
Log(ex);
}
}
}
[HarmonyPatch(typeof(SGBarracksSkillPip), "Initialize")]
public static class SGBarracksSkillPip_Initialize_Patch
{
public static void Prefix(int index, ref AbilityDef ability)
{
// prevent Ability icons appearing at non-tier locations
// the actual AbilityDef is going to be provided later anyway
if (index != 4 && index != 7 && ability != null)
{
Trace($"nulling {ability.ReqSkill}|{index}");
ability = null;
}
}
}
// rewrite of original
[HarmonyPatch(typeof(SGBarracksAdvancementPanel), "OnValueClick")]
public static class SGBarracksAdvancementPanel_OnValueClick_Patch
{
public static bool Prefix(
SGBarracksAdvancementPanel __instance,
Pilot ___curPilot,
List<SGBarracksSkillPip> ___gunPips,
List<SGBarracksSkillPip> ___pilotPips,
List<SGBarracksSkillPip> ___gutPips,
List<SGBarracksSkillPip> ___tacPips,
string type,
int value)
{
try
{
___curPilot.AddExperience(0, "", 100000);
var sim = UnityGameInstance.BattleTechGame.Simulation;
var pips = new Dictionary<string, List<SGBarracksSkillPip>>
{
{"Gunnery", ___gunPips},
{"Piloting", ___pilotPips},
{"Guts", ___gutPips},
{"Tactics", ___tacPips},
};
// removal of pip
if (___curPilot.StatCollection.GetValue<int>(type) > value)
{
Trace($"Removing {type} {value}");
Trace(pips[type][value].Ability);
Helpers.SetTempPilotSkill(type, value, -sim.GetLevelCost(value));
___curPilot.pilotDef.abilityDefNames.Do(Trace);
Log("\n");
Helpers.ForceResetCharacter(__instance);
Traverse.Create(__instance).Method("ForceResetCharacter").GetValue();
return false;
}
// add non-ability pip
if (!Traverse.Create(pips[type][value]).Field("hasAbility").GetValue<bool>())
{
Trace("Non-ability pip");
Helpers.SetTempPilotSkill(type, value, sim.GetLevelCost(value));
___curPilot.pilotDef.abilityDefNames.Do(Trace);
Trace("\n");
return false;
}
// Ability pips
// build complete list of defs from HBS and imported json
var abilityDefs = Helpers.ModAbilities
.Where(x => x.ReqSkillLevel == value + 1 && x.ReqSkill.ToString() == type).ToList();
var abilityDictionaries = sim.AbilityTree.Select(x => x.Value).ToList();
foreach (var abilityDictionary in abilityDictionaries)
{
abilityDefs.AddRange(abilityDictionary[value].Where(x => x.ReqSkill.ToString() == type));
}
// don't create choice popups with 1 option
if (abilityDefs.Count <= 1)
{
Trace($"Single ability for {type}|{value}, skipping");
Helpers.SetTempPilotSkill(type, value, sim.GetLevelCost(value));
return false;
}
// prevents which were ability buttons before other primaries were selected from being abilities
// not every ability button is visible all the time
var curButton = Traverse.Create(pips[type][value]).Field("curButton").GetValue<HBSDOTweenToggle>();
var skillButton = Traverse.Create(pips[type][value]).Field("skillButton").GetValue<HBSDOTweenToggle>();
if (curButton != skillButton)
{
Trace(new string('=', 50));
Trace("curButton != skillButton");
Helpers.SetTempPilotSkill(type, value, sim.GetLevelCost(value));
return false;
}
// dynamic buttons based on available abilities
var popup = GenericPopupBuilder
.Create("", "Select an ability")
.AddFader();
popup.AlwaysOnTop = true;
var pip = pips[type][value];
foreach (var abilityDef in abilityDefs)
{
popup.AddButton(abilityDef.Description.Name,
() =>
{
// have to change the Ability so SetPips later, SetActiveAbilityVisible works
Traverse.Create(pip).Field("thisAbility").SetValue(abilityDef);
Traverse.Create(pip).Field("abilityIcon").GetValue<SVGImage>().vectorGraphics = abilityDef.AbilityIcon;
Traverse.Create(pip).Field("AbilityTooltip").GetValue<HBSTooltip>()
.SetDefaultStateData(TooltipUtilities.GetStateDataFromObject(abilityDef.Description));
Helpers.SetTempPilotSkill(type, value, sim.GetLevelCost(value), abilityDef);
});
}
popup.Render();
}
catch (Exception ex)
{
Log(ex);
}
return false;
}
}
}
}