Skip to content

Commit

Permalink
tweak: Support [[ Lua Code ]] syntax of Custom Infos with EvalLua com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
psyGamer committed Feb 28, 2025
1 parent bd8f02c commit da9f751
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CelesteTAS-EverestInterop/Source/InfoHUD/InfoCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class InfoCustom {

private static readonly Regex TargetQueryRegex = new(@"\{(.*?)\}", RegexOptions.Compiled);
private static readonly Regex TableRegex = new(@"\|\|(.*?)\|\|", RegexOptions.Compiled);
private static readonly Regex LuaRegex = new(@"\[\[(.+?)\]\]", RegexOptions.Compiled);
internal static readonly Regex LuaRegex = new(@"\[\[(.+?)\]\]", RegexOptions.Compiled);

/// Should return true if the value was successfully formatted, otherwise false
private delegate bool ValueFormatter(object? value, int decimals, out string formattedValue);
Expand Down
9 changes: 8 additions & 1 deletion CelesteTAS-EverestInterop/Source/Lua/LuaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ namespace TAS.Lua;

/// Compiled Lua code which can be executed
internal readonly struct LuaContext : IDisposable {
private const string Prologue = """
const System typeof clr.System
const Celeste typeof clr.Celeste
const Monocle typeof clr.Monocle
const Vector2 typeof clr.Microsoft.Xna.Framework.Vector2
""";

[Load]
private static void Load() {
Expand Down Expand Up @@ -58,7 +65,7 @@ public static Result<LuaContext, string> Compile(string code, string name = "Cel
/// Compiles Lua text code into an executable chunk
public static Result<NeoLua.LuaChunk, string> CompileChunk(NeoLua.Lua lua, string code, string name = "CelesteTAS_LuaContext") {
try {
var chunk = lua.CompileChunk(code, name, new NeoLua.LuaCompileOptions { DebugEngine = NeoLua.LuaExceptionDebugger.Default } );
var chunk = lua.CompileChunk(Prologue + code, name, new NeoLua.LuaCompileOptions { DebugEngine = NeoLua.LuaExceptionDebugger.Default } );
return Result<NeoLua.LuaChunk, string>.Ok(chunk);
} catch (NeoLua.LuaException ex) {
ex.LogException("Lua compilation error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.RegularExpressions;
using TAS.Lua;
using TAS.Module;
using TAS.InfoHUD;
using TAS.Utils;

namespace TAS.Input.Commands;
Expand Down Expand Up @@ -109,12 +110,16 @@ private static void ClearInputs() {
[TasCommand(CommandName, LegalInFullGame = false, ExecuteTiming = ExecuteTiming.Parse | ExecuteTiming.Runtime, MetaDataProvider = typeof(Meta))]
private static void EvalLua(CommandLine commandLine, int studioLine, string filePath, int fileLine) {
if (Command.Parsing) {
if (commandLine.Arguments.Length != 1) {
// Support [[ Lua Code ]] syntax, like Custom Info
string code = InfoCustom.LuaRegex.Replace(string.Join(commandLine.ArgumentSeparator, commandLine.Arguments), match => match.Groups[1].Value);

if (string.IsNullOrWhiteSpace(code)) {
AbortTas("Expected Lua code");
return;
}


var ctx = LuaContext.Compile(string.Join(commandLine.ArgumentSeparator, commandLine.Arguments), CommandName);
var ctx = LuaContext.Compile(code, CommandName);
if (ctx.Failure) {
AbortTas($"Invalid Lua code: {ctx.Error}");
return;
Expand Down

0 comments on commit da9f751

Please sign in to comment.