mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 09:28:07 +08:00
* Optimize DDS texture creation and static POL meshes Separate file decoding from Unity upload through format-neutral texture data and explicit world, sprite and skybox policies. Validate DDS layouts, preserve alpha coverage and use native BC uploads with software RGBA fallback. Compact referenced POL vertices and batch nearby static opaque groups sharing materials. Keep water and dynamic geometry separate, with source ranges retained only in the Editor. Add DDS layout and POL index regression tests. Validation: PAL3 and PAL3A each passed 24 Editor tests, eight combined Game and ResourceViewer style regressions, 936 paired skybox comparisons, and both macOS Player builds on Metal. Editor provenance and Core tests are excluded from Players. * Add core protocol regression tests and copyright checks Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation. Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries. * Implement PAL3A command handlers and isolate script execution Correct PAL3A parameter signatures and add handlers for fortune selection, skybox sequences, actor facing and attacks, team and progression state, camera constraints, and battle preparation flags. Give script instances separate locals, advance grouped waits concurrently, and pause PAL3A parents only for their directly decoded child calls. Cancel stale interactions and save replay on reset, preserve finite action semantics, and restore camera orientation after releasing horizontal constraints. Persist supported state in v2 saves and add protocol, runtime, and progression regressions. Full combat gameplay remains outside this change. Validation: PAL3 274/274 and PAL3A 291/291 EditMode tests; 18 PAL3 and 22 PAL3A runtime checks. All 77,889 PAL3A instruction boundaries match EXE signatures. * Run CI only for pull requests and upgrade build actions
148 lines
5.7 KiB
C#
148 lines
5.7 KiB
C#
// ------------------------------------------------------------------------
|
|
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ------------------------------------------------------------------------
|
|
|
|
namespace Pal3.Core.DataReader.Nav
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
using Contract.Enums;
|
|
using Primitives;
|
|
using Utilities;
|
|
|
|
public sealed class NavFileReader : IFileReader<NavFile>
|
|
{
|
|
public NavFile Read(IBinaryReader reader, int codepage)
|
|
{
|
|
char[] header = reader.ReadChars(4);
|
|
string headerStr = new string(header[..^1]);
|
|
|
|
if (headerStr != "NAV")
|
|
{
|
|
throw new InvalidDataException("Invalid NAV(.nav) file: header != NAV");
|
|
}
|
|
|
|
byte version = reader.ReadByte();
|
|
if (version != 1 && version != 2)
|
|
throw new InvalidDataException($"Invalid NAV version: {version}");
|
|
byte numberOfLayers = reader.ReadByte();
|
|
uint tileOffset = reader.ReadUInt32();
|
|
uint faceOffset = reader.ReadUInt32();
|
|
|
|
reader.Seek(tileOffset, SeekOrigin.Begin);
|
|
NavLayer[] layers = new NavLayer[numberOfLayers];
|
|
for (int i = 0; i < numberOfLayers; i++)
|
|
{
|
|
layers[i] = ReadLayerData(reader, version);
|
|
}
|
|
|
|
reader.Seek(faceOffset, SeekOrigin.Begin);
|
|
NavMeshData[] meshData = new NavMeshData[numberOfLayers];
|
|
for (int i = 0; i < numberOfLayers; i++)
|
|
{
|
|
meshData[i] = ReadMeshData(reader);
|
|
}
|
|
|
|
return new NavFile(layers, meshData);
|
|
}
|
|
|
|
private static NavLayer ReadLayerData(IBinaryReader reader, byte version)
|
|
{
|
|
GameBoxRect[] portals = Array.Empty<GameBoxRect>();
|
|
|
|
if (version == 2)
|
|
{
|
|
portals = new GameBoxRect[8];
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
portals[i] = new GameBoxRect()
|
|
{
|
|
Left = reader.ReadInt32(),
|
|
Top = reader.ReadInt32(),
|
|
Right = reader.ReadInt32(),
|
|
Bottom = reader.ReadInt32(),
|
|
};
|
|
}
|
|
}
|
|
|
|
GameBoxVector3 gameBoxMaxWorldPosition = reader.ReadGameBoxVector3();
|
|
GameBoxVector3 gameBoxMinWorldPosition = reader.ReadGameBoxVector3();
|
|
|
|
int width = reader.ReadInt32();
|
|
int height = reader.ReadInt32();
|
|
|
|
long mapSize = (long)width * height;
|
|
if (width <= 0 || height <= 0 || mapSize > int.MaxValue || mapSize > (reader.Length - reader.Position) / 8)
|
|
throw new InvalidDataException($"Invalid NAV dimensions: {width}x{height}");
|
|
int navMapSize = (int)mapSize;
|
|
|
|
NavTile[] tiles = new NavTile[navMapSize];
|
|
for (int i = 0; i < navMapSize; i++)
|
|
{
|
|
tiles[i] = new NavTile
|
|
{
|
|
GameBoxYPosition = reader.ReadSingle(),
|
|
DistanceToNearestObstacle = reader.ReadByte(), //---| 1
|
|
FloorType = (FloorType) reader.ReadByte() //----| 2
|
|
};
|
|
_ = reader.ReadBytes(2); //-----------------------------| ^ to complete 4-byte alignment
|
|
}
|
|
|
|
return new NavLayer()
|
|
{
|
|
Portals = portals,
|
|
GameBoxMaxWorldPosition = gameBoxMaxWorldPosition,
|
|
GameBoxMinWorldPosition = gameBoxMinWorldPosition,
|
|
Width = width,
|
|
Height = height,
|
|
Tiles = tiles
|
|
};
|
|
}
|
|
|
|
private static NavMeshData ReadMeshData(IBinaryReader reader)
|
|
{
|
|
ushort numberOfVertices = reader.ReadUInt16();
|
|
ushort numberOfFaces = reader.ReadUInt16();
|
|
|
|
GameBoxVector3[] gameBoxVertices = reader.ReadGameBoxVector3s(numberOfVertices);
|
|
|
|
int[] gameBoxTriangles = new int[numberOfFaces * 3];
|
|
for (int i = 0; i < numberOfFaces; i++)
|
|
{
|
|
int index = i * 3;
|
|
gameBoxTriangles[index] = reader.ReadUInt16();
|
|
gameBoxTriangles[index + 1] = reader.ReadUInt16();
|
|
gameBoxTriangles[index + 2] = reader.ReadUInt16();
|
|
}
|
|
|
|
// Some of the nav meshes in the original game are upside down (don't know why),
|
|
// so we need to flip them if that's the case. You might argue that we can just
|
|
// make the mesh double sided, but that would cause some other issues (trust me).
|
|
{
|
|
GameBoxVector3[] normals = CoreUtility.CalculateNormals(gameBoxVertices, gameBoxTriangles);
|
|
|
|
for (int i = 0; i < numberOfFaces; i++)
|
|
{
|
|
int index = i * 3;
|
|
|
|
// Determine if the face is pointing downwards.
|
|
if (normals[gameBoxTriangles[index]].Y +
|
|
normals[gameBoxTriangles[index + 1]].Y +
|
|
normals[gameBoxTriangles[index + 2]].Y < 0)
|
|
{
|
|
// Change the winding order of the face to make it point upwards.
|
|
(gameBoxTriangles[index], gameBoxTriangles[index + 1]) =
|
|
(gameBoxTriangles[index + 1], gameBoxTriangles[index]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return new NavMeshData()
|
|
{
|
|
GameBoxVertices = gameBoxVertices,
|
|
GameBoxTriangles = gameBoxTriangles,
|
|
};
|
|
}
|
|
}
|
|
} |