Files
Jiaqi Liu d1b2c05eb4 Optimize resource loading and fix PAL3A script execution (#92)
* 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
2026-11-13 09:28:31 +08:00

242 lines
8.2 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
{
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Primitives;
/// <summary>
/// Binary reader interface for reading data from binary file
/// with extension methods for reading various data types.
/// </summary>
public interface IBinaryReader : IDisposable
{
public long Position { get; }
public long Length { get; }
public void Seek(long offset, SeekOrigin seekOrigin);
public short ReadInt16();
public int ReadInt32();
public long ReadInt64();
public ushort ReadUInt16();
public uint ReadUInt32();
public ulong ReadUInt64();
public float ReadSingle();
public double ReadDouble();
public bool ReadBoolean();
public byte ReadByte();
public byte[] ReadBytes(int count);
#region Helper Extensions (Default Implementation)
public byte[] ReadExactly(int count)
{
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
if (count > Length - Position) throw new EndOfStreamException();
byte[] bytes = ReadBytes(count);
if (bytes.Length != count) throw new EndOfStreamException();
return bytes;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float[] ReadSingles(int count)
{
var array = new float[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadSingle();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double[] ReadDoubles(int count)
{
var array = new double[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadDouble();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short[] ReadInt16s(int count)
{
var array = new short[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadInt16();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort[] ReadUInt16s(int count)
{
var array = new ushort[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadUInt16();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int[] ReadInt32s(int count)
{
var array = new int[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadInt32();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint[] ReadUInt32s(int count)
{
var array = new uint[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadUInt32();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long[] ReadInt64s(int count)
{
var array = new long[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadInt64();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong[] ReadUInt64s(int count)
{
var array = new ulong[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadUInt64();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T[] ReadEnums<T>(int count) where T : Enum
{
var array = new T[count];
for (var i = 0; i < count; i++)
{
array[i] = (T)Enum.ToObject(typeof(T), ReadInt32());
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char[] ReadChars(int count)
{
return Encoding.ASCII.GetChars(ReadExactly(count));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(int count) => new (ReadChars(count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(int count, int codepage)
{
var strBytes = ReadExactly(count);
int length = Array.IndexOf(strBytes, (byte)0);
if (length == -1) length = strBytes.Length; // If no null byte is found, use the full length
return Encoding.GetEncoding(codepage).GetString(strBytes, 0, length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GameBoxVector2 ReadGameBoxVector2() => new (ReadSingle(), ReadSingle());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GameBoxVector2[] ReadGameBoxVector2s(int count)
{
var array = new GameBoxVector2[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadGameBoxVector2();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GameBoxVector3 ReadGameBoxVector3() => new (ReadSingle(), ReadSingle(), ReadSingle());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GameBoxVector3[] ReadGameBoxVector3s(int count)
{
var array = new GameBoxVector3[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadGameBoxVector3();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Color32 ReadColor32() => new (ReadByte(), ReadByte(), ReadByte(), ReadByte());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Color32[] ReadColor32s(int count)
{
var array = new Color32[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadColor32();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Color ReadColor() => new (ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Color[] ReadColors(int count)
{
var array = new Color[count];
for (var i = 0; i < count; i++)
{
array[i] = ReadColor();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Read(Type type)
{
return type switch
{
not null when type == typeof(bool) => ReadBoolean(),
not null when type == typeof(byte) => ReadByte(),
not null when type == typeof(short) => ReadInt16(),
not null when type == typeof(ushort) => ReadUInt16(),
not null when type == typeof(int) => ReadInt32(),
not null when type == typeof(uint) => ReadUInt32(),
not null when type == typeof(long) => ReadInt64(),
not null when type == typeof(ulong) => ReadUInt64(),
not null when type == typeof(float) => ReadSingle(),
not null when type == typeof(double) => ReadDouble(),
not null when type == typeof(GameBoxVector2) => ReadGameBoxVector2(),
not null when type == typeof(GameBoxVector3) => ReadGameBoxVector3(),
not null when type == typeof(Color32) => ReadColor32(),
not null when type == typeof(Color) => ReadColor(),
_ => throw new NotSupportedException($"Type {type} is not supported.")
};
}
#endregion
}
}