Skip to content

Commit

Permalink
Add pause/resume API
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed Jun 1, 2023
1 parent b652ac5 commit a2de3c3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion BaboonAPI/BaboonAPI.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<ItemGroup>
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="TromboneChamp.GameLibs" Version="1.9.3" />
<PackageReference Include="TromboneChamp.GameLibs" Version="1.11.0-beta.1" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions BaboonAPI/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type BaboonPlugin() =
typeof<TrackTitlePatches>
typeof<LoaderPatch>
typeof<GameControllerPatch>
typeof<PausePatches>
typeof<SaverLoaderPatch>
typeof<TrackScorePatches>
] |> List.iter harmony.PatchAll
Expand Down
17 changes: 17 additions & 0 deletions BaboonAPI/api/TrackRegistry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ type public TromboneTrack =
/// Whether this track is visible in the track selector
abstract IsVisible: unit -> bool

/// <summary>
/// LoadedTromboneTrack extension for pause/resume functionality
/// </summary>
/// <remarks>
/// Implementing this class allows you to react to the pause and resume
/// </remarks>
type public PauseAware =
/// <summary>Can this track be resumed after a pause?</summary>
/// <remarks>If false, the curtains will close on pause.</remarks>
abstract CanResume: bool

/// Called when this track is paused. Use this to pause backgrounds or other features.
abstract OnPause: unit -> unit

/// Called when this track is resumed (after the countdown).
abstract OnResume: unit -> unit

/// <summary>
/// Event-based API for registering new tracks.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions BaboonAPI/patch/BaseTracksLoaderPatch.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ type internal BaseGameLoadedTrack(trackref: string, bundle: AssetBundle) =
()

member this.trackref = trackref

interface PauseAware with
member this.CanResume = true

member this.OnPause() = ()

member this.OnResume() = ()

type internal BaseGameTrack(data: string[]) =
interface TromboneTrack with
Expand Down
32 changes: 32 additions & 0 deletions BaboonAPI/patch/GameControllerPatch.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ type private GameControllerExtension() =
// Start background task next frame
instance.StartCoroutine("loadAssetBundleResources") |> ignore

// Set up pause/resume functionality
instance.track_is_pausable <-
match l with
| :? PauseAware as pauseable -> pauseable.CanResume // `track_is_pausable` actually controls resuming
| _ -> false

// Usually this should be cleaned up by Unload, but let's just make sure...
match loadedTrack with
| Some prev ->
Expand All @@ -70,6 +76,18 @@ type private GameControllerExtension() =

static member LoadChart(trackref: string): SavedLevel =
(TrackAccessor.fetchTrack trackref).LoadChart()

static member PauseTrack() =
match loadedTrack with
| Some (:? PauseAware as pa) ->
pa.OnPause()
| _ -> ()

static member ResumeTrack() =
match loadedTrack with
| Some (:? PauseAware as pa) ->
pa.OnResume()
| _ -> ()

static member Unload() =
match loadedTrack with
Expand Down Expand Up @@ -150,3 +168,17 @@ type GameControllerPatch() =
___mySoundAssetBundle <- null

false

[<HarmonyPatch>]
type PausePatches() =
[<HarmonyPostfix>]
[<HarmonyPatch(typeof<PauseCanvasController>, "showPausePanel")>]
static member PausePostfix(__instance: PauseCanvasController) =
GameControllerExtension.PauseTrack()
()

[<HarmonyPostfix>]
[<HarmonyPatch(typeof<PauseCanvasController>, "resumeFromPause")>]
static member ResumePostfix(__instance: PauseCanvasController) =
GameControllerExtension.ResumeTrack()
()

0 comments on commit a2de3c3

Please sign in to comment.