remove code repetition
2
This commit is contained in:
@@ -87,29 +87,23 @@ namespace SplashEdit.RuntimeCode
|
|||||||
|
|
||||||
void ExportFile()
|
void ExportFile()
|
||||||
{
|
{
|
||||||
|
|
||||||
string path = EditorUtility.SaveFilePanel("Select Output File", "", "output", "bin");
|
string path = EditorUtility.SaveFilePanel("Select Output File", "", "output", "bin");
|
||||||
int totalFaces = 0;
|
int totalFaces = 0;
|
||||||
|
|
||||||
// Lists for lua data offsets.
|
// Lists for lua data offsets.
|
||||||
List<long> luaOffsetPlaceholderPositions = new List<long>();
|
OffsetData luaOffset = new();
|
||||||
List<long> luaDataOffsets = new List<long>();
|
|
||||||
|
|
||||||
// Lists for mesh data offsets.
|
// Lists for mesh data offsets.
|
||||||
List<long> meshOffsetPlaceholderPositions = new List<long>();
|
OffsetData meshOffset = new();
|
||||||
List<long> meshDataOffsets = new List<long>();
|
|
||||||
|
|
||||||
// Lists for atlas data offsets.
|
// Lists for atlas data offsets.
|
||||||
List<long> atlasOffsetPlaceholderPositions = new List<long>();
|
OffsetData atlasOffset = new();
|
||||||
List<long> atlasDataOffsets = new List<long>();
|
|
||||||
|
|
||||||
// Lists for clut data offsets.
|
// Lists for clut data offsets.
|
||||||
List<long> clutOffsetPlaceholderPositions = new List<long>();
|
OffsetData clutOffset = new();
|
||||||
List<long> clutDataOffsets = new List<long>();
|
|
||||||
|
|
||||||
// Lists for navmesh data offsets.
|
// Lists for navmesh data offsets.
|
||||||
List<long> navmeshOffsetPlaceholderPositions = new List<long>();
|
OffsetData navmeshOffset = new();
|
||||||
List<long> navmeshDataOffsets = new List<long>();
|
|
||||||
|
|
||||||
int clutCount = 0;
|
int clutCount = 0;
|
||||||
List<LuaFile> luaFiles = new List<LuaFile>();
|
List<LuaFile> luaFiles = new List<LuaFile>();
|
||||||
@@ -167,7 +161,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
foreach (LuaFile luaFile in luaFiles)
|
foreach (LuaFile luaFile in luaFiles)
|
||||||
{
|
{
|
||||||
// Write placeholder for lua file data offset and record its position.
|
// Write placeholder for lua file data offset and record its position.
|
||||||
luaOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
luaOffset.OffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
||||||
writer.Write((int)0); // 4-byte placeholder for mesh data offset.
|
writer.Write((int)0); // 4-byte placeholder for mesh data offset.
|
||||||
writer.Write((uint)luaFile.LuaScript.Length);
|
writer.Write((uint)luaFile.LuaScript.Length);
|
||||||
}
|
}
|
||||||
@@ -176,7 +170,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
foreach (PSXObjectExporter exporter in _exporters)
|
foreach (PSXObjectExporter exporter in _exporters)
|
||||||
{
|
{
|
||||||
// Write placeholder for mesh data offset and record its position.
|
// Write placeholder for mesh data offset and record its position.
|
||||||
meshOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
meshOffset.OffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
||||||
writer.Write((int)0); // 4-byte placeholder for mesh data offset.
|
writer.Write((int)0); // 4-byte placeholder for mesh data offset.
|
||||||
|
|
||||||
// Write object's transform
|
// Write object's transform
|
||||||
@@ -211,7 +205,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
foreach (PSXNavMesh navmesh in _navmeshes)
|
foreach (PSXNavMesh navmesh in _navmeshes)
|
||||||
{
|
{
|
||||||
// Write placeholder for navmesh raw data offset.
|
// Write placeholder for navmesh raw data offset.
|
||||||
navmeshOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
navmeshOffset.OffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
||||||
writer.Write((int)0); // 4-byte placeholder for navmesh data offset.
|
writer.Write((int)0); // 4-byte placeholder for navmesh data offset.
|
||||||
|
|
||||||
writer.Write((ushort)navmesh.Navmesh.Count);
|
writer.Write((ushort)navmesh.Navmesh.Count);
|
||||||
@@ -222,7 +216,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
foreach (TextureAtlas atlas in _atlases)
|
foreach (TextureAtlas atlas in _atlases)
|
||||||
{
|
{
|
||||||
// Write placeholder for texture atlas raw data offset.
|
// Write placeholder for texture atlas raw data offset.
|
||||||
atlasOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
atlasOffset.OffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
||||||
writer.Write((int)0); // 4-byte placeholder for atlas data offset.
|
writer.Write((int)0); // 4-byte placeholder for atlas data offset.
|
||||||
|
|
||||||
writer.Write((ushort)atlas.Width);
|
writer.Write((ushort)atlas.Width);
|
||||||
@@ -238,7 +232,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
{
|
{
|
||||||
if (texture.ColorPalette != null)
|
if (texture.ColorPalette != null)
|
||||||
{
|
{
|
||||||
clutOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
clutOffset.OffsetPlaceholderPositions.Add(writer.BaseStream.Position);
|
||||||
writer.Write((int)0); // 4-byte placeholder for clut data offset.
|
writer.Write((int)0); // 4-byte placeholder for clut data offset.
|
||||||
writer.Write((ushort)texture.ClutPackingX); // 2 bytes
|
writer.Write((ushort)texture.ClutPackingX); // 2 bytes
|
||||||
writer.Write((ushort)texture.ClutPackingY); // 2 bytes
|
writer.Write((ushort)texture.ClutPackingY); // 2 bytes
|
||||||
@@ -256,21 +250,11 @@ namespace SplashEdit.RuntimeCode
|
|||||||
AlignToFourBytes(writer);
|
AlignToFourBytes(writer);
|
||||||
// Record the current offset for this lua file's data.
|
// Record the current offset for this lua file's data.
|
||||||
long luaDataOffset = writer.BaseStream.Position;
|
long luaDataOffset = writer.BaseStream.Position;
|
||||||
luaDataOffsets.Add(luaDataOffset);
|
luaOffset.DataOffsets.Add(luaDataOffset);
|
||||||
|
|
||||||
writer.Write(Encoding.UTF8.GetBytes(luaFile.LuaScript));
|
writer.Write(Encoding.UTF8.GetBytes(luaFile.LuaScript));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mesh data section: Write mesh data for each exporter.
|
|
||||||
foreach (PSXObjectExporter exporter in _exporters)
|
|
||||||
{
|
|
||||||
AlignToFourBytes(writer);
|
|
||||||
// Record the current offset for this exporter's mesh data.
|
|
||||||
long meshDataOffset = writer.BaseStream.Position;
|
|
||||||
meshDataOffsets.Add(meshDataOffset);
|
|
||||||
|
|
||||||
totalFaces += exporter.Mesh.Triangles.Count;
|
|
||||||
|
|
||||||
void writeVertexPosition(PSXVertex v)
|
void writeVertexPosition(PSXVertex v)
|
||||||
{
|
{
|
||||||
writer.Write((short)v.vx);
|
writer.Write((short)v.vx);
|
||||||
@@ -302,6 +286,18 @@ namespace SplashEdit.RuntimeCode
|
|||||||
action(tri.Vertexes[i]);
|
action(tri.Vertexes[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mesh data section: Write mesh data for each exporter.
|
||||||
|
foreach (PSXObjectExporter exporter in _exporters)
|
||||||
|
{
|
||||||
|
AlignToFourBytes(writer);
|
||||||
|
// Record the current offset for this exporter's mesh data.
|
||||||
|
long meshDataOffset = writer.BaseStream.Position;
|
||||||
|
meshOffset.DataOffsets.Add(meshDataOffset);
|
||||||
|
|
||||||
|
totalFaces += exporter.Mesh.Triangles.Count;
|
||||||
|
|
||||||
|
|
||||||
foreach (Tri tri in exporter.Mesh.Triangles)
|
foreach (Tri tri in exporter.Mesh.Triangles)
|
||||||
{
|
{
|
||||||
int expander = 16 / ((int)tri.Texture.BitDepth);
|
int expander = 16 / ((int)tri.Texture.BitDepth);
|
||||||
@@ -336,10 +332,11 @@ namespace SplashEdit.RuntimeCode
|
|||||||
{
|
{
|
||||||
AlignToFourBytes(writer);
|
AlignToFourBytes(writer);
|
||||||
long navmeshDataOffset = writer.BaseStream.Position;
|
long navmeshDataOffset = writer.BaseStream.Position;
|
||||||
navmeshDataOffsets.Add(navmeshDataOffset);
|
navmeshOffset.DataOffsets.Add(navmeshDataOffset);
|
||||||
|
|
||||||
foreach (PSXNavMeshTri tri in navmesh.Navmesh)
|
foreach (PSXNavMeshTri tri in navmesh.Navmesh)
|
||||||
{
|
{
|
||||||
|
// Write vertices coordinates
|
||||||
writer.Write((int)tri.v0.vx);
|
writer.Write((int)tri.v0.vx);
|
||||||
writer.Write((int)tri.v0.vy);
|
writer.Write((int)tri.v0.vy);
|
||||||
writer.Write((int)tri.v0.vz);
|
writer.Write((int)tri.v0.vz);
|
||||||
@@ -361,7 +358,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
AlignToFourBytes(writer);
|
AlignToFourBytes(writer);
|
||||||
// Record the current offset for this atlas's data.
|
// Record the current offset for this atlas's data.
|
||||||
long atlasDataOffset = writer.BaseStream.Position;
|
long atlasDataOffset = writer.BaseStream.Position;
|
||||||
atlasDataOffsets.Add(atlasDataOffset);
|
atlasOffset.DataOffsets.Add(atlasDataOffset);
|
||||||
|
|
||||||
// Write the atlas's raw texture data.
|
// Write the atlas's raw texture data.
|
||||||
for (int y = 0; y < atlas.vramPixels.GetLength(1); y++)
|
for (int y = 0; y < atlas.vramPixels.GetLength(1); y++)
|
||||||
@@ -382,7 +379,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
{
|
{
|
||||||
AlignToFourBytes(writer);
|
AlignToFourBytes(writer);
|
||||||
long clutDataOffset = writer.BaseStream.Position;
|
long clutDataOffset = writer.BaseStream.Position;
|
||||||
clutDataOffsets.Add(clutDataOffset);
|
clutOffset.DataOffsets.Add(clutDataOffset);
|
||||||
|
|
||||||
foreach (VRAMPixel color in texture.ColorPalette)
|
foreach (VRAMPixel color in texture.ColorPalette)
|
||||||
{
|
{
|
||||||
@@ -393,70 +390,24 @@ namespace SplashEdit.RuntimeCode
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bacfill the lua data offsets into the metadata section.
|
writeOffset(writer, luaOffset, "lua");
|
||||||
if (luaOffsetPlaceholderPositions.Count == luaDataOffsets.Count)
|
writeOffset(writer, meshOffset, "mesh");
|
||||||
{
|
writeOffset(writer, navmeshOffset, "navmesh");
|
||||||
for (int i = 0; i < luaOffsetPlaceholderPositions.Count; i++)
|
writeOffset(writer, atlasOffset, "atlas");
|
||||||
{
|
writeOffset(writer, clutOffset, "clut");
|
||||||
writer.Seek((int)luaOffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
|
||||||
writer.Write((int)luaDataOffsets[i]);
|
|
||||||
}
|
}
|
||||||
}
|
Debug.Log(totalFaces);
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("Mismatch between metadata lua offset placeholders and lua data blocks!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backfill the mesh data offsets into the metadata section.
|
private void writeOffset(BinaryWriter writer, OffsetData data, string type)
|
||||||
if (meshOffsetPlaceholderPositions.Count == meshDataOffsets.Count)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < meshOffsetPlaceholderPositions.Count; i++)
|
// Backfill the data offsets into the metadata section.
|
||||||
|
if (data.OffsetPlaceholderPositions.Count == data.DataOffsets.Count)
|
||||||
{
|
{
|
||||||
writer.Seek((int)meshOffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
for (int i = 0; i < data.OffsetPlaceholderPositions.Count; i++)
|
||||||
writer.Write((int)meshDataOffsets[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
Debug.LogError("Mismatch between metadata mesh offset placeholders and mesh data blocks!");
|
writer.Seek((int)data.OffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
||||||
}
|
writer.Write((int)data.DataOffsets[i]);
|
||||||
|
|
||||||
// Backfill the navmesh offsets into the metadata section.
|
|
||||||
if (navmeshOffsetPlaceholderPositions.Count == navmeshDataOffsets.Count)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < navmeshOffsetPlaceholderPositions.Count; i++)
|
|
||||||
{
|
|
||||||
writer.Seek((int)navmeshOffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
|
||||||
writer.Write((int)navmeshDataOffsets[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("Mismatch between metadata mesh offset placeholders and mesh data blocks!");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Backfill the atlas data offsets into the metadata section.
|
|
||||||
if (atlasOffsetPlaceholderPositions.Count == atlasDataOffsets.Count)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < atlasOffsetPlaceholderPositions.Count; i++)
|
|
||||||
{
|
|
||||||
writer.Seek((int)atlasOffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
|
||||||
writer.Write((int)atlasDataOffsets[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError("Mismatch between atlas offset placeholders and atlas data blocks!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backfill the clut data offsets into the metadata section.
|
|
||||||
if (clutOffsetPlaceholderPositions.Count == clutDataOffsets.Count)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < clutOffsetPlaceholderPositions.Count; i++)
|
|
||||||
{
|
|
||||||
writer.Seek((int)clutOffsetPlaceholderPositions[i], SeekOrigin.Begin);
|
|
||||||
writer.Write((int)clutDataOffsets[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -464,8 +415,7 @@ namespace SplashEdit.RuntimeCode
|
|||||||
Debug.LogError("Mismatch between clut offset placeholders and clut data blocks!");
|
Debug.LogError("Mismatch between clut offset placeholders and clut data blocks!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.Log(totalFaces);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AlignToFourBytes(BinaryWriter writer)
|
void AlignToFourBytes(BinaryWriter writer)
|
||||||
{
|
{
|
||||||
@@ -483,4 +433,10 @@ namespace SplashEdit.RuntimeCode
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class OffsetData
|
||||||
|
{
|
||||||
|
public List<long> OffsetPlaceholderPositions = new List<long>();
|
||||||
|
public List<long> DataOffsets = new List<long>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user