Skip to content

Commit 8314d32

Browse files
committed
Fix tryToLoadLevel patch
1 parent 8d63576 commit 8314d32

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

BaboonAPI/patch/GameControllerPatch.fs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type private GameControllerExtension() =
106106
[<HarmonyPatch>]
107107
type GameControllerPatch() =
108108
static let freeplay_f = AccessTools.Field(typeof<GameController>, "freeplay")
109+
static let file_exists_m = AccessTools.Method(typeof<System.IO.File>, "Exists")
109110

110111
[<HarmonyTranspiler>]
111112
[<HarmonyPatch(typeof<GameController>, "Start")>]
@@ -154,13 +155,28 @@ type GameControllerPatch() =
154155
[<HarmonyTranspiler>]
155156
[<HarmonyPatch(typeof<GameController>, "tryToLoadLevel")>]
156157
static member LoadChartTranspiler(instructions: CodeInstruction seq): CodeInstruction seq =
157-
let matcher =
158-
CodeMatcher(instructions)
158+
let matcher = CodeMatcher(instructions)
159+
160+
let existsLabels =
161+
matcher
159162
.MatchForward(false, [|
160-
CodeMatch OpCodes.Ldarg_2
161-
CodeMatch OpCodes.Brtrue
163+
CodeMatch OpCodes.Ldloc_0
164+
CodeMatch (fun ins -> ins.Calls(file_exists_m))
165+
CodeMatch OpCodes.Brfalse
162166
|])
163-
.ThrowIfInvalid("Could not find start of injection point in GameController#tryToLoadLevel")
167+
.ThrowIfInvalid("Could not find File.Exists call in GameController#tryToLoadLevel")
168+
.Labels
169+
matcher
170+
.RemoveInstructions(3)
171+
.AddLabels(existsLabels)
172+
173+
// Find the start of the injection point
174+
.MatchForward(false, [|
175+
CodeMatch OpCodes.Ldarg_2
176+
CodeMatch OpCodes.Brtrue
177+
|])
178+
.ThrowIfInvalid("Could not find start of injection point in GameController#tryToLoadLevel")
179+
|> ignore
164180

165181
let startPos = matcher.Pos
166182
let startLabels = matcher.Labels

0 commit comments

Comments
 (0)