Added lua exporting

This commit is contained in:
2025-04-09 22:20:52 +02:00
parent 765575d7ae
commit b3da188438
10 changed files with 161 additions and 34 deletions

11
Runtime/LuaFile.cs Normal file
View File

@@ -0,0 +1,11 @@
using UnityEngine;
namespace Splashedit.RuntimeCode
{
[CreateAssetMenu(fileName = "NewLuaScript", menuName = "Lua Script", order = 1)]
public class LuaFile : ScriptableObject
{
public TextAsset luaScript;
}
}

2
Runtime/LuaFile.cs.meta Normal file
View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e3b07239f3beb7a87ad987c3fedae9c1

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Splashedit.RuntimeCode;
using UnityEngine;
namespace SplashEdit.RuntimeCode
@@ -7,6 +8,7 @@ namespace SplashEdit.RuntimeCode
public class PSXObjectExporter : MonoBehaviour
{
public PSXBPP BitDepth = PSXBPP.TEX_8BIT; // Defines the bit depth of the texture (e.g., 4BPP, 8BPP)
public LuaFile luaFile;
public List<PSXTexture2D> Textures { get; set; } = new List<PSXTexture2D>(); // Stores the converted PlayStation-style texture
public PSXMesh Mesh { get; set; } // Stores the converted PlayStation-style mesh

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Splashedit.RuntimeCode;
using UnityEditor;
using UnityEngine;
@@ -83,6 +84,10 @@ namespace SplashEdit.RuntimeCode
string path = EditorUtility.SaveFilePanel("Select Output File", "", "output", "bin");
int totalFaces = 0;
// Lists for lua data offsets.
List<long> luaOffsetPlaceholderPositions = new List<long>();
List<long> luaDataOffsets = new List<long>();
// Lists for mesh data offsets.
List<long> meshOffsetPlaceholderPositions = new List<long>();
List<long> meshDataOffsets = new List<long>();
@@ -100,6 +105,7 @@ namespace SplashEdit.RuntimeCode
List<long> navmeshDataOffsets = new List<long>();
int clutCount = 0;
List<LuaFile> luaFiles = new List<LuaFile>();
// Cluts
foreach (TextureAtlas atlas in _atlases)
@@ -113,12 +119,26 @@ namespace SplashEdit.RuntimeCode
}
}
// Lua files
foreach (PSXObjectExporter exporter in _exporters)
{
if (exporter.luaFile != null)
{
//if not contains
if (!luaFiles.Contains(exporter.luaFile))
{
luaFiles.Add(exporter.luaFile);
}
}
}
using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))
{
// Header
writer.Write('S'); // 1 byte // 1
writer.Write('P'); // 1 byte // 2
writer.Write((ushort)1); // 2 bytes - version // 4
writer.Write((ushort)luaFiles.Count); // 2 bytes - padding // 6
writer.Write((ushort)_exporters.Length); // 2 bytes // 6
writer.Write((ushort)_navmeshes.Length); // 8
writer.Write((ushort)_atlases.Length); // 2 bytes // 10
@@ -134,6 +154,16 @@ namespace SplashEdit.RuntimeCode
writer.Write((ushort)PSXTrig.ConvertCoordinateToPSX(_playerHeight, GTEScaling)); // 26
writer.Write((ushort)0);
writer.Write((ushort)0);
// Lua file section
foreach (LuaFile luaFile in luaFiles)
{
// Write placeholder for lua file data offset and record its position.
luaOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
writer.Write((int)0); // 4-byte placeholder for mesh data offset.
writer.Write((uint)luaFile.luaScript.text.Length);
}
// GameObject section (exporters)
foreach (PSXObjectExporter exporter in _exporters)
@@ -159,7 +189,15 @@ namespace SplashEdit.RuntimeCode
writer.Write((int)rotationMatrix[2, 2]);
writer.Write((ushort)exporter.Mesh.Triangles.Count);
writer.Write((ushort)0);
if (exporter.luaFile != null)
{
int index = luaFiles.IndexOf(exporter.luaFile);
writer.Write((short)index);
}
else
{
writer.Write((short)-1);
}
}
// Navmesh metadata section
@@ -205,6 +243,17 @@ namespace SplashEdit.RuntimeCode
// Start of data section
// Lua data section: Write lua file data for each exporter.
foreach (LuaFile luaFile in luaFiles)
{
AlignToFourBytes(writer);
// Record the current offset for this lua file's data.
long luaDataOffset = writer.BaseStream.Position;
luaDataOffsets.Add(luaDataOffset);
writer.Write(luaFile.luaScript.bytes);
}
// Mesh data section: Write mesh data for each exporter.
foreach (PSXObjectExporter exporter in _exporters)
{
@@ -337,6 +386,20 @@ namespace SplashEdit.RuntimeCode
}
// Bacfill the lua data offsets into the metadata section.
if (luaOffsetPlaceholderPositions.Count == luaDataOffsets.Count)
{
for (int i = 0; i < luaOffsetPlaceholderPositions.Count; i++)
{
writer.Seek((int)luaOffsetPlaceholderPositions[i], SeekOrigin.Begin);
writer.Write((int)luaDataOffsets[i]);
}
}
else
{
Debug.LogError("Mismatch between metadata lua offset placeholders and lua data blocks!");
}
// Backfill the mesh data offsets into the metadata section.
if (meshOffsetPlaceholderPositions.Count == meshDataOffsets.Count)
{