-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllWeapons.cs
243 lines (214 loc) · 9.64 KB
/
AllWeapons.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
231
232
233
234
235
236
237
238
239
240
241
242
243
// This file is part of MoonoMod and is licenced under the GNU GPL v3.0.
// See LICENSE file for full text.
// Copyright © 2024 Michael Ripley
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using UnityEngine;
namespace MoonoMod
{
internal class AllWeapons
{
// this is the same as the vanilla list, except the circular upgrade weapons "SHINING BLADE" and "SHADOW BLADE" are removed, as they have numbers appended if they have nonzero weapon XP which breaks Kira's check.
private readonly static HashSet<string> SPECIAL_WEAPONS = new(new string[]
{
"JOTUNN SLAYER",
"DARK GREATSWORD",
"DOUBLE CROSSBOW",
"ELFEN LONGSWORD",
"FIRE SWORD",
"HERITAGE SWORD",
"IRON TORCH",
"LYRIAN GREATSWORD",
"MARAUDER BLACK FLAIL",
"POISON CLAW",
"SAINT ISHII",
"SILVER RAPIER",
"STEEL CLUB",
"STEEL LANCE",
});
// there's 51 distinct weapons you can technically have. Also you can have duplicates... Kira checks for 48. Which 3 didn't they want to count?
// Kira probably doesn't know about the drop duping bug that would let you have both the CURSEBRAND and the POISONGUARD, so really it's just 50 weapons you can have.
private readonly static HashSet<string> TERMINAL_WEAPONS = new(new string[] {
"EMPTY", // included for completeness
"AXE OF HARMING",
"BATTLE AXE",
"BLADE OF JUSZTINA",
"BLADE OF OPHELIA",
"BLESSED WIND",
"BRITTLE ARMING SWORD", // numeric suffix
"CORRUPTED DAGGER",
"CURSED BLADE",
"DARK GREATSWORD",
"DARK RAPIER",
"DOUBLE CROSSBOW",
"ELFEN BOW",
"ELFEN LONGSWORD",
"FIRE SWORD",
"FISHING SPEAR",
"GOLDEN KHOPESH",
"GOLDEN SICKLE",
"HALBERD",
"HERITAGE SWORD",
"ICE SICKLE",
"IRON TORCH",
"JAILORS CANDLE",
"JOTUNN SLAYER",
"LIMBO", // might be considered secret?
"LUCID BLADE",
"LYRIAN GREATSWORD",
"MARAUDER BLACK FLAIL",
"MOONLIGHT",
"POISON CLAW",
"PRIVATEER MUSKET",
"RITUAL DAGGER",
"SAINT ISHII",
"SERPENT FANG",
"SILVER RAPIER",
"SKELETON AXE",
"STEEL CLUB",
"STEEL LANCE",
"STEEL NEEDLE",
"STEEL SPEAR",
"SUCSARIAN DAGGER",
"SUCSARIAN SPEAR",
"TWISTED STAFF",
"VAMPIRE HUNTER SWORD",
"WAND OF POWER",
"WOLFRAM GREATSWORD",
"WOODEN SHIELD",
});
// My best guess as to the weapons Kira didn't want to count in the 48
private readonly static HashSet<string> LOSABLE_WEAPONS = new(new string[] {
"DEATH SCYTHE", // numeric suffix. Might be considered secret?. Lost when you restore Death.
"HAMMER OF CRUELTY", // Lost if you return it to Garrat.
});
// player must have AT LEAST one of these two
private readonly static HashSet<string> OBSIDIAN_WEAPONS = new(new string[] {
"OBSIDIAN CURSEBRAND", // numeric suffix
"OBSIDIAN POISONGUARD", // numeric suffix
});
// player must have one of these two
private readonly static HashSet<string> SHADOW_SHINING_BLADE = new(new string[] {
"SHADOW BLADE", // numeric suffix
"SHINING BLADE", // numeric suffix
});
// if remains on 0 and isn't updated, we'll just not do the relevant patches
private static int TOTAL_WEAPON_COUNT = 0;
// fun fact: I count 50 obtainable weapons. Maybe 51 if you can get both an Obsidian Cursebrand and Obsidian Poisonguard to drop. So I have no idea where Kira got 48 from.
private readonly static int EXPECTED_TOTAL_WEAPONS = 48;
// this matches kira's expected count of 15 (at least after I solved the "SHINING BLADE"/"SHADOW BLADE" debacle)
private readonly static int EXPECTED_SPECIAL_WEAPONS = SPECIAL_WEAPONS.Count;
internal static void Init()
{
try
{
TOTAL_WEAPON_COUNT = ComputeTotalWeaponCount();
}
catch (TranspilerException e)
{
MoonoMod.Logger!.LogWarning($"Disabling total weapon count fix due to error:\n{e}");
}
if (TOTAL_WEAPON_COUNT != EXPECTED_TOTAL_WEAPONS)
{
MoonoMod.Logger!.LogWarning($"Found evidence of {TOTAL_WEAPON_COUNT} weapons, but expected {EXPECTED_TOTAL_WEAPONS}. Kira may have added new weapons or fixed the weapon count bug this mod fixes.");
}
}
// get total weapon count in a dynamic way. This is non-trivial, because the weapons are unloaded in an asset bundle and Unity 2020.3.4f1 has no way to enumerate them.
// instead we'll just read how many weapons Kira thinks there are, because they've got a hardcoded count lying around.
private static int ComputeTotalWeaponCount()
{
MethodInfo hasAllWeapons = AccessTools.DeclaredMethod(typeof(CONTROL), nameof(CONTROL.CheckForAllWeps));
List<CodeInstruction> codes = PatchProcessor.GetOriginalInstructions(hasAllWeapons);
// sliding window search for ldc.i4.s, blt.s
for (int index = codes.Count - 1; index > 0; index -= 1)
{
if ((codes[index - 1].opcode == OpCodes.Ldc_I4 || codes[index - 1].opcode == OpCodes.Ldc_I4_S) && (codes[index].opcode == OpCodes.Blt || codes[index].opcode == OpCodes.Blt_S))
{
object totalWeaponCount = codes[index - 1].operand;
try
{
// handle normal LDC
return (int)totalWeaponCount;
}
catch
{
}
try
{
// handle short-form LDC
return (sbyte)totalWeaponCount;
}
catch
{
}
throw new TranspilerException($"Could not extract int from {totalWeaponCount.GetType()} when trying to read total weapon count");
}
}
throw new TranspilerException("Could not read total weapon count");
}
private static bool ShouldPatchWeaponCount()
{
return MoonoMod.fixAllWeaponCheck!.Value && TOTAL_WEAPON_COUNT != 0;
}
private static bool HasAllWeapons(CONTROL control)
{
string[] weapons = control.CURRENT_PL_DATA.WEPS;
HashSet<string> weaponSet = new();
int weapon_count = -1; // presumably Kira's way of dealing with EMPTY. It's terrible, but here I am doing it too.
int special_weapon_count = 0;
for (int index = 0; index < weapons.Length && weapons[index] != null && weapons[index] != ""; index += 1)
{
string weaponName = Util.TrimTrailingNumbers(weapons[index]);
weaponSet.Add(weaponName);
// log ALL weapons
if (MoonoMod.debugInventory?.Value ?? false)
{
MoonoMod.Logger!.LogMessage($"you have {weapons[index]} = {weaponName}");
}
weapon_count += 1;
if (SPECIAL_WEAPONS.Contains(weapons[index]))
{
special_weapon_count += 1;
}
}
if (MoonoMod.debugLogs?.Value ?? false)
{
bool anyObsidian = weaponSet.Overlaps(OBSIDIAN_WEAPONS);
bool anyShadowShining = weaponSet.Overlaps(SHADOW_SHINING_BLADE);
HashSet<string> missingWeapons = new(TERMINAL_WEAPONS);
missingWeapons.ExceptWith(weaponSet);
MoonoMod.Logger!.LogInfo($"You have {weapon_count} / {TOTAL_WEAPON_COUNT} weapons, and {special_weapon_count} / {EXPECTED_SPECIAL_WEAPONS} special weapons. obsidian={weaponSet.Overlaps(OBSIDIAN_WEAPONS)} shadowShining={anyShadowShining} missing={missingWeapons.Count}");
if (MoonoMod.debugInventory?.Value ?? false)
{
foreach (string missingWeapon in missingWeapons)
{
MoonoMod.Logger!.LogInfo($"missing {missingWeapon}");
}
}
return anyObsidian && anyShadowShining && missingWeapons.Count == 0;
}
else
{
return weaponSet.Overlaps(OBSIDIAN_WEAPONS) && weaponSet.Overlaps(SHADOW_SHINING_BLADE) && weaponSet.IsSupersetOf(TERMINAL_WEAPONS);
}
}
[HarmonyPatch]
private static class HarmonyPatches
{
// Fix the achievment check against all weapons to count Shadow/Shining blade
[HarmonyPrefix]
[HarmonyPatch(typeof(CONTROL), nameof(CONTROL.CheckForAllWeps))]
private static bool CheckForAllWeps(CONTROL __instance, GameObject ___ACHY)
{
if (!ShouldPatchWeaponCount())
{
return true; // run original method
}
___ACHY?.transform.GetChild(0).gameObject.SetActive(HasAllWeapons(__instance));
return false; // skip original method
}
}
}
}