Framework/Lua/Diagnostics/LuaSpike.cs
using System;
namespace RoleplayFramework.Lua.Diagnostics;
/// <summary>
/// Phase 0 integration spike: loads a real .lua file from the mounted content filesystem,
/// runs it in a fresh realm state, and calls back into it from C#.
/// Run from the editor: <c>return RoleplayFramework.Lua.Diagnostics.LuaSpike.Run();</c>
/// </summary>
public static class LuaSpike
{
public const string SpikePath = "lua/spike/sh_spike.lua";
public static string Run()
{
if ( !FileSystem.Mounted.FileExists( SpikePath ) )
return $"FAIL: '{SpikePath}' not found on FileSystem.Mounted";
var source = FileSystem.Mounted.ReadAllText( SpikePath );
var output = new System.Text.StringBuilder();
var L = new LuaState( "shared" );
L.Globals.Set( "REALM", "shared" );
L.PrintHandler = line =>
{
Log.Info( $"[lua:{L.Name}] {line}" );
output.AppendLine( line );
};
try
{
var module = L.DoString( source, SpikePath )[0];
var describe = L.Index( module, "Describe" );
var payroll = L.Index( module, "TotalPayroll" );
var counts = new LuaTable();
counts.Set( "Citizen", 10 );
counts.Set( "Police Officer", 2 );
var jobs = L.Call( describe )[0].AsString;
var total = L.Call( payroll, counts )[0].Number;
output.AppendLine( $"Describe(): {jobs}" );
output.AppendLine( $"TotalPayroll(10 citizens, 2 police) = {total} (expected 600)" );
output.Append( total == 600 ? "PASS" : "FAIL" );
}
catch ( LuaException e )
{
output.Append( $"FAIL: {e.Message}" );
}
return output.ToString();
}
}