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

259 lines
9.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.Scn
{
using System;
using System.IO;
using Contract.Enums;
using Primitives;
public sealed class ScnFileReader : IFileReader<ScnFile>
{
public ScnFile Read(IBinaryReader reader, int codepage)
{
char[] header = reader.ReadChars(4);
string headerStr = new (header[..^1]);
if (headerStr != "SCN")
{
throw new InvalidDataException("Invalid SCN(.scn) file: header != SCN");
}
short version = reader.ReadInt16();
short numberOfNpc = reader.ReadInt16();
uint npcDataOffset = reader.ReadUInt32();
short numberOfObjects = reader.ReadInt16();
uint objectDataOffset = reader.ReadUInt32();
ScnSceneInfo sceneInfo = new()
{
CityName = reader.ReadString(32, codepage),
SceneName = reader.ReadString(32, codepage),
Model = reader.ReadString(32, codepage),
SceneType = (SceneType) reader.ReadInt32(),
LightMap = reader.ReadInt32(),
SkyBox = reader.ReadUInt32(),
Reserved = reader.ReadUInt32s(6),
};
reader.Seek(npcDataOffset, SeekOrigin.Begin);
ScnNpcInfo[] npcInfos = new ScnNpcInfo[numberOfNpc];
for (int i = 0; i < numberOfNpc; i++)
{
npcInfos[i] = ReadNpcInfo(reader, codepage);
}
reader.Seek(objectDataOffset, SeekOrigin.Begin);
ScnObjectInfo[] objInfos = new ScnObjectInfo[numberOfObjects];
for (int i = 0; i < numberOfObjects; i++)
{
ScnObjectInfo sceneObject = ReadObjectInfo(reader, codepage);
objInfos[i] = sceneObject;
}
return new ScnFile(sceneInfo, npcInfos, objInfos);
}
private static ScnNpcInfo ReadNpcInfo(IBinaryReader reader, int codepage)
{
return new ScnNpcInfo()
{
Id = reader.ReadByte(),
Type = (ActorType)reader.ReadByte(),
Name = reader.ReadString(32, codepage),
Texture = reader.ReadString(34, codepage),
FacingDirection = reader.ReadSingle(),
LayerIndex = reader.ReadInt32(),
GameBoxXPosition = reader.ReadSingle(),
GameBoxZPosition = reader.ReadSingle(),
InitActive = reader.ReadInt32(),
InitBehaviour = (ActorBehaviourType) reader.ReadInt32(),
ScriptId = reader.ReadUInt32(),
GameBoxYPosition = reader.ReadSingle(),
InitAction = reader.ReadString(16, codepage),
MonsterIds = reader.ReadUInt32s(3),
NumberOfMonsters = reader.ReadByte(),
MonsterCanRespawn = reader.ReadByte(),
PaddingBytes = reader.ReadBytes(2), // Empty padding bytes to complete 4-byte alignment
Path = new ScnPath()
{
NumberOfWaypoints = reader.ReadInt32(),
GameBoxWaypoints = reader.ReadGameBoxVector3s(16)
},
NoTurn = reader.ReadUInt32(),
LoopAction = reader.ReadUInt32(),
GameBoxMoveSpeed = reader.ReadUInt32(),
Reserved = reader.ReadUInt32s(29),
};
}
private static ScnObjectInfo ReadObjectInfo(IBinaryReader reader, int codepage)
{
byte id = reader.ReadByte();
byte initActive = reader.ReadByte();
byte times = reader.ReadByte();
byte switchState = reader.ReadByte();
string name = reader.ReadString(32, codepage);
byte triggerType = reader.ReadByte();
byte isNonBlocking = reader.ReadByte();
_ = reader.ReadBytes(2); // Empty padding bytes to complete 4-byte alignment
GameBoxVector3 gameBoxPosition = reader.ReadGameBoxVector3();
float yRotation = reader.ReadSingle();
GameBoxRect tileMapTriggerRect = new()
{
Left = reader.ReadInt32(),
Top = reader.ReadInt32(),
Right = reader.ReadInt32(),
Bottom = reader.ReadInt32(),
};
SceneObjectType type = (SceneObjectType)reader.ReadByte();
byte saveState = reader.ReadByte();
byte layerIndex = reader.ReadByte();
ObjectElementType elementType = (ObjectElementType)reader.ReadByte();
#if PAL3
int[] parameters = reader.ReadInt32s(6);
#elif PAL3A
int[] parameters = Array.ConvertAll(reader.ReadSingles(6), Convert.ToInt32);
#endif
#if PAL3A
uint notUsed = reader.ReadUInt32();
#endif
byte requireSpecialAction = reader.ReadByte();
ushort requireItem = reader.ReadUInt16();
_ = reader.ReadByte(); // Empty padding byte to complete 4-byte alignment
ushort requireMoney = reader.ReadUInt16();
ushort requireLevel = reader.ReadUInt16();
ushort requireAttackValue = reader.ReadUInt16();
byte requireAllMechanismsSolved = reader.ReadByte();
string failedMessage = reader.ReadString(16, codepage);
_ = reader.ReadByte(); // Empty padding byte to complete 4-byte alignment
#if PAL3A
ushort linkedObjectGroupId = reader.ReadUInt16();
_ = reader.ReadBytes(2); // Empty padding bytes to complete 4-byte alignment
#endif
uint scriptId = reader.ReadUInt32();
ScnPath path = new()
{
NumberOfWaypoints = reader.ReadInt32(),
GameBoxWaypoints = reader.ReadGameBoxVector3s(16)
};
ushort linkedObjectId = reader.ReadUInt16();
string dependentSceneName = reader.ReadString(32, codepage);
byte dependentObjectId = reader.ReadByte();
_ = reader.ReadByte(); // Empty padding byte to complete 4-byte alignment
GameBoxVector3 gameBoxBoundsMin = reader.ReadGameBoxVector3();
GameBoxVector3 gameBoxBoundsMax = reader.ReadGameBoxVector3();
float xRotation = reader.ReadSingle();
#if PAL3A
float zRotation = reader.ReadSingle();
#endif
string sfxName = reader.ReadString(8, codepage);
uint effectModelType = reader.ReadUInt32();
uint scriptActivated = reader.ReadUInt32();
uint scriptMoved = reader.ReadUInt32();
#if PAL3A
uint canOnlyBeTriggeredOnce = reader.ReadUInt32();
#endif
#if PAL3
uint[] reserved = reader.ReadUInt32s(52);
#elif PAL3A
uint[] reserved = reader.ReadUInt32s(44);
#endif
return new ScnObjectInfo()
{
Id = id,
InitActive = initActive,
Times = times,
SwitchState = switchState,
Name = name,
TriggerType = triggerType,
IsNonBlocking = isNonBlocking,
GameBoxPosition = gameBoxPosition,
GameBoxYRotation = yRotation,
TileMapTriggerRect = tileMapTriggerRect,
Type = type,
SaveState = saveState,
LayerIndex = layerIndex,
ElementType = elementType,
Parameters = parameters,
#if PAL3A
NotUsed = notUsed,
#endif
RequireSpecialAction = requireSpecialAction,
RequireItem = requireItem,
RequireMoney = requireMoney,
RequireLevel = requireLevel,
RequireAttackValue = requireAttackValue,
RequireAllMechanismsSolved = requireAllMechanismsSolved,
FailedMessage = failedMessage,
#if PAL3A
LinkedObjectGroupId = linkedObjectGroupId,
#endif
ScriptId = scriptId,
Path = path,
LinkedObjectId = linkedObjectId,
DependentSceneName = dependentSceneName,
DependentObjectId = dependentObjectId,
GameBoxBoundsMin = gameBoxBoundsMin,
GameBoxBoundsMax = gameBoxBoundsMax,
GameBoxXRotation = xRotation,
#if PAL3A
GameBoxZRotation = zRotation,
#endif
SfxName = sfxName,
EffectModelType = effectModelType,
ScriptActivated = scriptActivated,
ScriptMoved = scriptMoved,
#if PAL3A
CanOnlyBeTriggeredOnce = canOnlyBeTriggeredOnce,
#endif
Reserved = reserved,
};
}
}
}