Skip to content

Commit

Permalink
refactor: Reference new GameInfo and obsolete old GameInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Feb 22, 2025
1 parent db46a30 commit bbaf0e6
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ protected override void HandleMessage(MessageID messageId, BinaryReader reader)
LogVerbose($"Received message SetCustomInfoTemplate: '{customInfoTemplate}'");

TasSettings.InfoCustomTemplate = customInfoTemplate;
GameInfo.Update();
break;

case MessageID.ClearWatchEntityInfo:
LogVerbose("Received message ClearWatchEntityInfo");

InfoWatchEntity.ClearWatchEntities();
GameInfo.Update();
break;

case MessageID.RecordTAS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal static class DebugRcPage {
WriteLine(builder, $"SaveState: {Savestates.IsSaved_Safe}");
WriteLine(builder, $"CurrentFrame: {Manager.Controller.CurrentFrameInTas}");
WriteLine(builder, $"TotalFrames: {Manager.Controller.Inputs.Count}");
WriteLine(builder, $"RoomName: {GameInfo.LevelName}");
WriteLine(builder, $"ChapterTime: {GameInfo.ChapterTime}");
WriteLine(builder, $"RoomName: {TAS.InfoHUD.GameInfo.RoomName}");
WriteLine(builder, $"ChapterTime: {TAS.InfoHUD.GameInfo.ChapterTime}");
WriteLine(builder, "Game Info: ");

var args = Everest.DebugRC.ParseQueryString(c.Request.RawUrl);
Expand Down
13 changes: 0 additions & 13 deletions CelesteTAS-EverestInterop/Source/Gameplay/CenterCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ internal static class CenterCamera {
private static void Load() {
On.Monocle.Engine.RenderCore += On_Engine_RenderCore;
On.Monocle.Commands.Render += On_Commands_Render;
On.Celeste.Level.Render += On_Level_Render;

#if DEBUG
cameraOffset = Engine.Instance.GetDynamicDataInstance().Get<Vector2?>("CelesteTAS_CameraOffset") ?? Vector2.Zero;
Expand All @@ -31,7 +30,6 @@ private static void Load() {
private static void Unload() {
On.Monocle.Engine.RenderCore -= On_Engine_RenderCore;
On.Monocle.Commands.Render -= On_Commands_Render;
On.Celeste.Level.Render -= On_Level_Render;

#if DEBUG
Engine.Instance.GetDynamicDataInstance().Set("CelesteTAS_CameraOffset", cameraOffset);
Expand All @@ -45,23 +43,12 @@ private static void On_Engine_RenderCore(On.Monocle.Engine.orig_RenderCore orig,
orig(self);
RestoreCamera();
}

// Commands check which entity was clicked, which is dependant on the current camera
private static void On_Commands_Render(On.Monocle.Commands.orig_Render orig, Monocle.Commands self) {
AdjustCamera();
orig(self);
RestoreCamera();
}

private static void On_Level_Render(On.Celeste.Level.orig_Render orig, Level self) {
orig(self);

// Show cursor for dragging camera
if (TasSettings.CenterCamera && !Hotkeys.InfoHud.Check && MouseInput.Right.Check) {
InfoMouse.DrawCursor(MouseInput.Position);
}
}

// Disable "Zoom Level" Extended Variant while using center camera, to avoid it causing issues with rendering
private static bool DisableZoomLevelVariant() => TasSettings.CenterCamera;

Expand Down
13 changes: 8 additions & 5 deletions CelesteTAS-EverestInterop/Source/InfoHUD/GameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace TAS.InfoHUD;
public static class GameInfo {
public enum Target { InGameHud, Studio, ExactInfo }

internal static string RoomName => sessionData.Value is { } session ? session.RoomName : string.Empty;
internal static string ChapterTime => sessionData.Value is { } session ? session.ChapterTime : string.Empty;

/// Fetches the info for the current frame
/// May **not** be called during scene.Update! Only before or after Update.
[PublicAPI]
Expand Down Expand Up @@ -230,8 +233,8 @@ private static void OnLevelTransition(Level level, LevelData next, Vector2 direc
private static LazyValue<string> customInfoExactForceAllowCodeExecution = new(QueryExactCustomInfoForceAllowCodeExecution);

// Data which needs to be tracked, because it'll be used in the next frame
private static SubpixelPosition preUpdatePosition, lastPosition;
private static int transitionFrames;
internal static SubpixelPosition preUpdatePosition, lastPosition;
internal static int transitionFrames;

// Kept to reduce allocations
private static readonly StringBuilder builder = new();
Expand Down Expand Up @@ -494,19 +497,19 @@ private static string FormatState(StateMachine stateMachine) {

return name;
}
private static string FormatTime(long ticks) {
public static string FormatTime(long ticks) {
var timeSpan = TimeSpan.FromTicks(ticks);
long frames = ticks / Engine.RawDeltaTime.SecondsToTicks();

return $"{timeSpan.ShortGameplayFormat()}({frames})";
}

private static float ConvertSpeedUnit(float speed, SpeedUnit unit) {
public static float ConvertSpeedUnit(float speed, SpeedUnit unit) {
return unit == SpeedUnit.PixelPerSecond
? speed * Engine.TimeRateB
: speed * Engine.RawDeltaTime * Engine.TimeRateB;
}
private static Vector2 ConvertSpeedUnit(Vector2 speed, SpeedUnit unit) {
public static Vector2 ConvertSpeedUnit(Vector2 speed, SpeedUnit unit) {
return unit == SpeedUnit.PixelPerSecond
? speed * Engine.TimeRateB
: speed * Engine.RawDeltaTime * Engine.TimeRateB;
Expand Down
8 changes: 5 additions & 3 deletions CelesteTAS-EverestInterop/Source/InfoHUD/InfoCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public static string EvaluateTemplate(Template template, int decimals, bool forc
case LuaComponent lua: {
if (TargetQuery.PreventCodeExecution && !forceAllowCodeExecution) {
infoBuilder.Append("<Cannot safely evaluate Lua code during EnforceLegal>");
continue;
}

var result = LuaContext.ExecuteChunk(lua.Chunk, template.Environment);
Expand Down Expand Up @@ -297,6 +298,7 @@ public static string EvaluateTemplate(Template template, int decimals, bool forc
case LuaComponent lua: {
if (TargetQuery.PreventCodeExecution && !forceAllowCodeExecution) {
resultBuilder.Append("<Cannot safely evaluate Lua code during EnforceLegal>");
continue;
}

var result = LuaContext.ExecuteChunk(lua.Chunk, template.Environment);
Expand Down Expand Up @@ -423,7 +425,7 @@ public static string EvaluateTemplate(Template template, int decimals, bool forc
/// Formats a value in seconds into frames
private static bool Formatter_toFrame(object? value, int _, out string formattedValue) {
if (value is float floatValue) {
formattedValue = TAS.GameInfo.ConvertToFrames(floatValue).ToString();
formattedValue = floatValue.ToCeilingFrames().ToString();
return true;
}

Expand All @@ -433,11 +435,11 @@ private static bool Formatter_toFrame(object? value, int _, out string formatted
/// Formats a value in px/s into px/f
private static bool Formatter_toPixelPerFrame(object? value, int decimals, out string formattedValue) {
if (value is float floatValue) {
formattedValue = TAS.GameInfo.ConvertSpeedUnit(floatValue, SpeedUnit.PixelPerFrame).ToFormattedString(decimals);
formattedValue = GameInfo.ConvertSpeedUnit(floatValue, SpeedUnit.PixelPerFrame).ToFormattedString(decimals);
return true;
}
if (value is Vector2 vectorValue) {
formattedValue = TAS.GameInfo.ConvertSpeedUnit(vectorValue, SpeedUnit.PixelPerFrame).ToSimpleString(decimals);
formattedValue = GameInfo.ConvertSpeedUnit(vectorValue, SpeedUnit.PixelPerFrame).ToSimpleString(decimals);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static class SpeedrunToolInterop {
private static bool disallowUnsafeInput;
private static Random? auraRandom;
private static bool betterInvincible = false;
private static SubpixelPosition lastPosition;
private static int transitionFrames;

[Initialize]
private static void Initialize() {
Expand Down Expand Up @@ -93,6 +95,8 @@ static void OnSave(Dictionary<Type, Dictionary<string, object>> savedValues, Lev
disallowUnsafeInput = SafeCommand.DisallowUnsafeInput;
auraRandom = DesyncFixer.AuraHelperSharedRandom.DeepCloneShared();
betterInvincible = Manager.Running && BetterInvincible.Invincible;
lastPosition = InfoHUD.GameInfo.lastPosition;
transitionFrames = InfoHUD.GameInfo.transitionFrames;
}

static void OnLoad(Dictionary<Type, Dictionary<string, object>> savedValues, Level level) {
Expand All @@ -117,6 +121,8 @@ static void OnLoad(Dictionary<Type, Dictionary<string, object>> savedValues, Lev
SafeCommand.DisallowUnsafeInput = disallowUnsafeInput;
DesyncFixer.AuraHelperSharedRandom = auraRandom!.DeepCloneShared();
BetterInvincible.Invincible = Manager.Running && betterInvincible;
InfoHUD.GameInfo.lastPosition = lastPosition;
InfoHUD.GameInfo.transitionFrames = transitionFrames;
}

static void OnClear() {
Expand Down
9 changes: 0 additions & 9 deletions CelesteTAS-EverestInterop/Source/Module/CelesteTasSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ public int PositionDecimals {
get => StudioShared.PositionDecimals;
set {
StudioShared.PositionDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -324,7 +323,6 @@ public int SpeedDecimals {
get => StudioShared.SpeedDecimals;
set {
StudioShared.SpeedDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -333,7 +331,6 @@ public int VelocityDecimals {
get => StudioShared.VelocityDecimals;
set {
StudioShared.VelocityDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -342,7 +339,6 @@ public int AngleDecimals {
get => StudioShared.AngleDecimals;
set {
StudioShared.AngleDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -351,7 +347,6 @@ public int CustomInfoDecimals {
get => StudioShared.CustomInfoDecimals;
set {
StudioShared.CustomInfoDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -360,7 +355,6 @@ public int WatchEntityDecimals {
get => StudioShared.WatchEntityDecimals;
set {
StudioShared.WatchEntityDecimals = Calc.Clamp(value, GameSettings.MinDecimals, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -369,7 +363,6 @@ public int SubpixelIndicatorDecimals {
get => StudioShared.SubpixelIndicatorDecimals;
set {
StudioShared.SubpixelIndicatorDecimals = Calc.Clamp(value, 1, GameSettings.MaxDecimals);
GameInfo.Update();
SyncSettings();
}
}
Expand All @@ -378,15 +371,13 @@ public SpeedUnit SpeedUnit {
get => StudioShared.SpeedUnit;
set {
StudioShared.SpeedUnit = value;
GameInfo.Update();
SyncSettings();
}
}
public SpeedUnit VelocityUnit {
get => StudioShared.VelocityUnit;
set {
StudioShared.VelocityUnit = value;
GameInfo.Update();
SyncSettings();
}
}
Expand Down
39 changes: 20 additions & 19 deletions CelesteTAS-EverestInterop/Source/TAS/ExportGameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,26 @@ private static void ExportInfo(InputFrame inputFrame) {
return;
}

string time = GameInfo.GetChapterTime(level);
string time = InfoHUD.GameInfo.FormatTime(level.Session.Time);
string pos = player.ToSimplePositionString(GetDecimals(TasSettings.PositionDecimals, GameSettings.MaxDecimals));
string speed = player.Speed.ToSimpleString(GetDecimals(TasSettings.SpeedDecimals, GameSettings.MaxDecimals));
string statuses = GameInfo.GetStatuses(level, player);
GameInfo.GetAdjustedLiftBoost(player, out string liftBoost);
if (liftBoost.IsNotEmpty()) {
if (statuses.IsEmpty()) {
statuses = liftBoost;
} else {
statuses += $"\t{liftBoost}";
}
}

statuses += $"\t[{level.Session.Level}]";
// FIXME
// string statuses = GameInfo.GetStatuses(level, player);
// GameInfo.GetAdjustedLiftBoost(player, out string liftBoost);
// if (liftBoost.IsNotEmpty()) {
// if (statuses.IsEmpty()) {
// statuses = liftBoost;
// } else {
// statuses += $"\t{liftBoost}";
// }
// }
//
// statuses += $"\t[{level.Session.Level}]";

output = string.Join("\t",
inputFrame.Line + 1, $"{controller.CurrentFrameInInput}/{inputFrame}", controller.CurrentFrameInTas, time, pos, speed,
PlayerStates.GetCurrentStateName(player),
statuses);
// output = string.Join("\t",
// inputFrame.Line + 1, $"{controller.CurrentFrameInInput}/{inputFrame}", controller.CurrentFrameInTas, time, pos, speed,
// PlayerStates.GetCurrentStateName(player),
// statuses);

foreach (string typeName in trackedEntities.Keys) {
List<Entity> entities = trackedEntities[typeName].Invoke();
Expand All @@ -149,8 +150,8 @@ private static void ExportInfo(InputFrame inputFrame) {
}

foreach (Entity entity in entities) {
output +=
$"\t{typeName}: {entity.ToSimplePositionString(GetDecimals(TasSettings.PositionDecimals, GameSettings.MaxDecimals))}";
// output +=
// $"\t{typeName}: {entity.ToSimplePositionString(GetDecimals(TasSettings.PositionDecimals, GameSettings.MaxDecimals))}";
}
}

Expand All @@ -177,7 +178,7 @@ private static void ExportInfo(InputFrame inputFrame) {
sceneName);
}

streamWriter?.WriteLine(output);
// streamWriter?.WriteLine(output);
streamWriter?.Flush();
}

Expand Down
39 changes: 20 additions & 19 deletions CelesteTAS-EverestInterop/Source/TAS/GameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace TAS;

[Obsolete("Use TAS.InfoHUD.GameInfo", error: true)]
public static class GameInfo {
private static readonly GetDelegate<Level, float> LevelUnpauseTimer = FastReflection.CreateGetDelegate<Level, float>("unpauseTimer");

Expand Down Expand Up @@ -134,25 +135,25 @@ public static string ExactStudioInfoAllowCodeExecution {
private static int FramesPerGameSecond => (int) Math.Round(1 / Engine.RawDeltaTime / Engine.TimeRateB);
private static int FramesPerRealSecond => (int) Math.Round(1 / Engine.RawDeltaTime);

[Load]
private static void Load() {
On.Monocle.Engine.Update += EngineOnUpdate;
On.Monocle.Scene.AfterUpdate += SceneOnAfterUpdate;
Everest.Events.Level.OnTransitionTo += LevelOnOnTransitionTo;
On.Celeste.Level.Update += LevelOnUpdate;
typeof(Player)
.GetMethodInfo(nameof(Player.DashCoroutine))!
.GetStateMachineTarget()!
.IlHook(PlayerOnDashCoroutine);
}

[Unload]
private static void Unload() {
On.Monocle.Engine.Update -= EngineOnUpdate;
On.Monocle.Scene.AfterUpdate -= SceneOnAfterUpdate;
Everest.Events.Level.OnTransitionTo -= LevelOnOnTransitionTo;
On.Celeste.Level.Update -= LevelOnUpdate;
}
// [Load]
// private static void Load() {
// On.Monocle.Engine.Update += EngineOnUpdate;
// On.Monocle.Scene.AfterUpdate += SceneOnAfterUpdate;
// Everest.Events.Level.OnTransitionTo += LevelOnOnTransitionTo;
// On.Celeste.Level.Update += LevelOnUpdate;
// typeof(Player)
// .GetMethodInfo(nameof(Player.DashCoroutine))!
// .GetStateMachineTarget()!
// .IlHook(PlayerOnDashCoroutine);
// }
//
// [Unload]
// private static void Unload() {
// On.Monocle.Engine.Update -= EngineOnUpdate;
// On.Monocle.Scene.AfterUpdate -= SceneOnAfterUpdate;
// Everest.Events.Level.OnTransitionTo -= LevelOnOnTransitionTo;
// On.Celeste.Level.Update -= LevelOnUpdate;
// }

private static void PlayerOnDashCoroutine(ILContext il) {
ILCursor ilCursor = new(il);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void Unload() {
[DisableRun]
private static void UpdateFileTime() {
if (TasStartInfo != null && SaveData.Instance != null && !Manager.Controller.CanPlayback) {
UpdateAllMetadata("FileTime", _ => GameInfo.FormatTime(SaveData.Instance.Time - TasStartInfo.Value.FileTimeTicks));
UpdateAllMetadata("FileTime", _ => InfoHUD.GameInfo.FormatTime(SaveData.Instance.Time - TasStartInfo.Value.FileTimeTicks));
}

TasStartInfo = null;
Expand All @@ -57,7 +57,7 @@ private static void UpdateChapterTime(Level level) {
return;
}

UpdateAllMetadata("ChapterTime", _ => GameInfo.GetChapterTime(level));
UpdateAllMetadata("ChapterTime", _ => InfoHUD.GameInfo.FormatTime(level.Session.Time));
}

public static void UpdateRecordCount(InputController inputController) {
Expand Down Expand Up @@ -94,7 +94,7 @@ private static void MidwayFileTimeCommand(CommandLine commandLine, int studioLin
}

UpdateAllMetadata("MidwayFileTime",
_ => GameInfo.FormatTime(SaveData.Instance.Time - TasStartInfo.Value.FileTimeTicks),
_ => InfoHUD.GameInfo.FormatTime(SaveData.Instance.Time - TasStartInfo.Value.FileTimeTicks),
command => Manager.Controller.CurrentCommands.Contains(command));
}

Expand All @@ -105,7 +105,7 @@ private static void MidwayChapterTimeCommand(CommandLine commandLine, int studio
}

UpdateAllMetadata("MidwayChapterTime",
_ => GameInfo.GetChapterTime(level),
_ => InfoHUD.GameInfo.FormatTime(level.Session.Time),
command => Manager.Controller.CurrentCommands.Contains(command));
}

Expand Down
Loading

0 comments on commit bbaf0e6

Please sign in to comment.