Skip to content

Commit

Permalink
feat(SyncCheck): Detect desnycs caused by unsafe-actions or failed as…
Browse files Browse the repository at this point in the history
…sertions
  • Loading branch information
psyGamer committed Nov 30, 2024
1 parent 95d3bd5 commit 8cb8380
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
28 changes: 25 additions & 3 deletions CelesteTAS-EverestInterop/Source/SyncCheck/SyncChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ internal static class SyncChecker {
private static string resultFile = string.Empty;

private static List<string> wrongTimes = [];
private static bool wasUnsafe = false;
private static bool wasAssert = false;

private static SyncCheckResult result = new();

Expand All @@ -45,7 +47,7 @@ public static void SetResultFile(string file) {
resultFile = file;
}

/// Indicates the current TAS has finished executing
/// Indicates that the current TAS has finished executing
public static void ReportRunFinished() {
if (!Active) {
return;
Expand All @@ -55,7 +57,13 @@ public static void ReportRunFinished() {

// Check for desyncs
SyncCheckResult.Entry entry;
if (Engine.Scene is not (Level { Completed: true } or LevelExit or AreaComplete)) {
if (wasUnsafe) {
// Performed unsafe action in safe-mode
entry = new SyncCheckResult.Entry(InputController.StudioTasFilePath, SyncCheckResult.Status.UnsafeAction, GameInfo.ExactStatus);
} else if (wasAssert) {
// Assertion failure
entry = new SyncCheckResult.Entry(InputController.StudioTasFilePath, SyncCheckResult.Status.AssertFailed, GameInfo.ExactStatus);
} else if (Engine.Scene is not (Level { Completed: true } or LevelExit or AreaComplete)) {
// TAS did not finish
GameInfo.Update(updateVel: false);
entry = new SyncCheckResult.Entry(InputController.StudioTasFilePath, SyncCheckResult.Status.NotFinished, GameInfo.ExactStatus);
Expand All @@ -81,12 +89,24 @@ public static void ReportRunFinished() {
}
}

/// Indicates a time command was updated with another time
/// Indicates that a time command was updated with another time
public static void ReportWrongTime(string filePath, int fileLine, string oldTime, string newTime) {
Logger.Error("CelesteTAS/SyncCheck", $"Detected wrong time in file '{filePath}' line {fileLine}: '{oldTime}' vs '{newTime}'");
wrongTimes.Add($"{filePath}\t{fileLine}\t{oldTime}\t{newTime}");
}

/// Indicates that an unsafe action was performed in safe-mode
public static void ReportUnsafeAction() {
Logger.Error("CelesteTAS/SyncCheck", $"Detected unsafe action");
wasUnsafe = true;
}

/// Indicates that an Assert-command failed
public static void ReportAssertFailed(string lineText, string filePath, int fileLine, string expected, string actual) {
Logger.Error("CelesteTAS/SyncCheck", $"Detected failed assertion '{lineText}' in file '{filePath}' line {fileLine}: Expected '{expected}', got '{actual}'");
wasAssert = true;
}

[Initialize]
private static void Initialize() {
On.Celeste.Celeste.OnSceneTransition += On_Celeste_OnSceneTransition;
Expand Down Expand Up @@ -118,6 +138,8 @@ private static void On_Celeste_OnSceneTransition(On.Celeste.Celeste.orig_OnScene
private static void CheckFile(string file) {
// Reset state
wrongTimes.Clear();
wasUnsafe = false;
wasAssert = false;

Logger.Info("CelesteTAS/SyncCheck", $"Starting check for file: '{file}'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using TAS.EverestInterop.InfoHUD;
using TAS.SyncCheck;
using TAS.Utils;

namespace TAS.Input.Commands;
Expand Down Expand Up @@ -73,6 +74,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected equal: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -83,6 +85,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected not equal: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -93,6 +96,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected contain: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -102,6 +106,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected not contain: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -111,6 +116,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected starts with: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -120,6 +126,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected not starts with: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -129,6 +136,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected ends with: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand All @@ -138,6 +146,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
Expected not ends with: {expected}
But was: {actual}"
""";
SyncChecker.ReportAssertFailed(commandLine.OriginalText, filePath, fileLine, expected, actual);
AbortTas($"{prefix}{failureMessage}", true, 4f);
}
break;
Expand Down
5 changes: 4 additions & 1 deletion CelesteTAS-EverestInterop/Source/TAS/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public static void Update() {
if (item is TextMenu.Header {Title: { } title} &&
(title == Dialog.Clean("OPTIONS_TITLE") || title == Dialog.Clean("MENU_VARIANT_TITLE") ||
title == Dialog.Clean("MODOPTIONS_EXTENDEDVARIANTS_PAUSEMENU_BUTTON").ToUpperInvariant()) ||
item is TextMenuExt.HeaderImage {Image: "menu/everest"}) {
item is TextMenuExt.HeaderImage {Image: "menu/everest"})
{
// TODO: Retrieve file-path and file-line
SyncChecker.ReportUnsafeAction();
DisableRun();
}
}
Expand Down

0 comments on commit 8cb8380

Please sign in to comment.