Skip to content

Commit

Permalink
fix time trial display
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzo committed Apr 25, 2024
1 parent aa94727 commit 865cccb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"recommendations": [
"muhammad-sammy.csharp",
"esbenp.prettier-vscode",
"mjohns.clang-format",
"seaube.clangformat",
"gaborv.flatbuffers"
]
}
22 changes: 5 additions & 17 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@
"omnisharp.useModernNet": false,
"editor.formatOnSave": true,
"files.autoSave": "onFocusChange",
"clang-format.language.csharp.fallbackStyle": "LLVM",
"[csharp]": {
"editor.defaultFormatter": "mjohns.clang-format"
"editor.defaultFormatter": "seaube.clangformat"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand All @@ -92,20 +91,9 @@
"[yaml]": {
"editor.defaultFormatter": "redhat.vscode-yaml"
},
"clang-format.assumeFilename": "Name.cs",
"clang-format.language.csharp.enable": true,
"clang-format.language.apex.enable": false,
"clang-format.language.c.enable": false,
"clang-format.language.cpp.enable": false,
"clang-format.language.cuda.enable": false,
"clang-format.language.glsl.enable": false,
"clang-format.language.hlsl.enable": false,
"clang-format.language.java.enable": false,
"clang-format.language.javascript.enable": false,
"clang-format.language.objective-cpp.enable": false,
"clang-format.language.proto.enable": false,
"clang-format.language.typescript.enable": false,
"clang-format.language.objective-c.enable": false,
"typescript.tsdk": "node_modules\\typescript\\lib",
"dotnet.defaultSolution": "SlzSpeedrunTools.sln"
"dotnet.defaultSolution": "SlzSpeedrunTools.sln",
"clangFormat.assumeFilename": "Name.cs",
"dotnet.preferCSharpExtension": true,
"dotnet.server.useOmnisharp": true
}
1 change: 0 additions & 1 deletion projects/Bonelab/SpeedrunTimer/src/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public override void OnInitializeMelon() {
PrefCategory = MelonPreferences.CreateCategory(PREF_CATEGORY_ID);
SplitsTimer.OnInitialize();
SaveDeleteImprovements.OnInitialize();
TimeTrialDisplayFix.OnInitialize();

LevelHooks.OnLoad += _timer.OnLoadingScreen;
LevelHooks.OnLevelStart += _timer.OnLevelStart;
Expand Down
83 changes: 28 additions & 55 deletions projects/Bonelab/SpeedrunTimer/src/TimeTrialDisplayFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,44 @@
using SLZ.Marrow.Warehouse;
using SLZ.Bonelab;
using Sst.Utilities;
using System.Collections.Generic;

namespace Sst.SpeedrunTimer {
class TimeTrialDisplayFix : MonoBehaviour {
private static HashSet<string> TIME_TRIAL_LEVELS = new HashSet<string>() {
Utilities.Levels.Barcodes.STREET_PUNCHER,
Utilities.Levels.Barcodes.SPRINT_BRIDGE,
};
class TimeTrialDisplayFix {
public const string LABEL_NAME = "TimeTrialDisplayFix_Label";

private static TimeTrial_GameController _controller;
private static BoneLeaderManager _leaderboardManager;
private static TextMeshPro _tmpLabel;

public static void OnInitialize() { LevelHooks.OnLevelStart += OnLevelStart; }

private static void OnLevelStart(LevelCrate level) {
Dbg.Log($"TimeTrialDisplayFix OnLevelStart");

if (!TIME_TRIAL_LEVELS.Contains(level.Barcode.ID))
return;

Dbg.Log($"TimeTrialDisplayFix TIME_TRIAL_LEVELS");

_controller = GameObject.FindObjectOfType<TimeTrial_GameController>();
if (_controller == null)
return;

_leaderboardManager = GameObject.FindObjectOfType<BoneLeaderManager>();
if (_leaderboardManager == null)
return;

Dbg.Log($"TimeTrialDisplayFix DisplayTimeOnFinish");

_controller.onSessionEnd.AddListener(new Action(OnSessionEnd));
[HarmonyPatch(typeof(BoneLeaderManager),
nameof(BoneLeaderManager.SubmitLeaderboardScore))]
class BoneLeaderManager_SubmitLeaderboardScore_Patch {
[HarmonyPostfix()]
internal static void Postfix(BoneLeaderManager __instance,
uint score) => DisplayTime(__instance, score);
}

public static void OnSessionEnd() {
if (_controller == null || _leaderboardManager == null)
return;

Dbg.Log($"TimeTrialDisplayFix OnSessionEnd: {_controller.tsTimerString}");

var leaderboardTitle = _leaderboardManager.text_TitleBoard;
public static void DisplayTime(BoneLeaderManager leaderManager,
uint milliseconds) {
var leaderboardTitle = leaderManager.text_TitleBoard;
var leaderboardCanvas = leaderboardTitle.transform.parent;

var label = new GameObject("TimeTrialDisplayFix_Label");
label.transform.SetParent(leaderboardCanvas, false);
label.transform.localPosition = new Vector3(0f, 0f, 3f);
var tmp = leaderboardCanvas.Find(LABEL_NAME)?.GetComponent<TextMeshPro>() ??
CreateTimeDisplay(leaderboardCanvas, leaderboardTitle);

_tmpLabel = label.AddComponent<TextMeshPro>();
_tmpLabel.alignment = TextAlignmentOptions.Center;
_tmpLabel.font = leaderboardTitle.font;
_tmpLabel.fontSize = 4f;
_tmpLabel.rectTransform.sizeDelta = new Vector2(10f, 1f);
var time = SplitsRenderer.DurationToString(
TimeSpan.FromMilliseconds(milliseconds));
tmp.SetText($"Time: {time}");
}

[HarmonyPatch(typeof(BaseGameController),
nameof(BaseGameController.UpdateTimeDisplay))]
class BaseGameController_UpdateTimeDisplay_Patch {
[HarmonyPostfix()]
internal static void Postfix(BaseGameController __instance) {
if (__instance == _controller && _tmpLabel != null) {
_tmpLabel.SetText($"Time: {_controller.tsTimerString}");
}
}
private static TextMeshPro CreateTimeDisplay(Transform canvas,
TMP_Text title) {
var go = new GameObject(LABEL_NAME);
go.transform.SetParent(canvas, false);
go.transform.localPosition = new Vector3(0f, 1.2f, 3f);

var tmp = go.AddComponent<TextMeshPro>();
tmp.alignment = TextAlignmentOptions.Center;
tmp.font = title.font;
tmp.fontSize = 4f;
tmp.rectTransform.sizeDelta = new Vector2(10f, 1f);
return tmp;
}
}
}

0 comments on commit 865cccb

Please sign in to comment.