Lua bytecode builder, cdrom fixes

This commit is contained in:
Jan Racek
2026-03-28 19:41:53 +01:00
parent 275b4e891d
commit 61fbca17a7
5 changed files with 555 additions and 15 deletions

View File

@@ -12,6 +12,13 @@ namespace SplashEdit.RuntimeCode
/// </summary>
public static class PSXSceneWriter
{
/// <summary>
/// Pre-compiled Lua bytecode, keyed by Lua source text.
/// When populated, Write() packs bytecode instead of source text.
/// Set by the build pipeline after running luac_psx, cleared after export.
/// </summary>
public static Dictionary<string, byte[]> CompiledLuaBytecode { get; set; }
/// <summary>
/// All scene data needed to produce a .bin file.
/// Populated by PSXSceneExporter before calling <see cref="Write"/>.
@@ -239,7 +246,12 @@ namespace SplashEdit.RuntimeCode
{
luaOffset.PlaceholderPositions.Add(writer.BaseStream.Position);
writer.Write((int)0); // placeholder
writer.Write((uint)Encoding.UTF8.GetByteCount(luaFile.LuaScript));
// Use compiled bytecode length if available, otherwise source length
if (CompiledLuaBytecode != null && CompiledLuaBytecode.TryGetValue(luaFile.LuaScript, out byte[] bytecode))
writer.Write((uint)bytecode.Length);
else
writer.Write((uint)Encoding.UTF8.GetByteCount(luaFile.LuaScript));
}
// ──────────────────────────────────────────────────────
@@ -429,12 +441,26 @@ namespace SplashEdit.RuntimeCode
// Data sections
// ══════════════════════════════════════════════════════
// Lua data
// Lua data (bytecode if compiled, source text otherwise)
int luaIdx = 0;
log?.Invoke($"CompiledLuaBytecode: {(CompiledLuaBytecode != null ? CompiledLuaBytecode.Count + " entries" : "null")}", LogType.Log);
foreach (LuaFile luaFile in luaFiles)
{
AlignToFourBytes(writer);
luaOffset.DataOffsets.Add(writer.BaseStream.Position);
writer.Write(Encoding.UTF8.GetBytes(luaFile.LuaScript));
if (CompiledLuaBytecode != null && CompiledLuaBytecode.TryGetValue(luaFile.LuaScript, out byte[] bytecode))
{
log?.Invoke($" Lua [{luaIdx}]: using bytecode ({bytecode.Length} bytes)", LogType.Log);
writer.Write(bytecode);
}
else
{
byte[] srcBytes = Encoding.UTF8.GetBytes(luaFile.LuaScript);
log?.Invoke($" Lua [{luaIdx}]: FALLBACK to source ({srcBytes.Length} bytes, script hash={luaFile.LuaScript.GetHashCode()})", LogType.Warning);
writer.Write(srcBytes);
}
luaIdx++;
}
// Mesh data