Files
Pal3.Unity/Assets/Scripts/Pal3.Game/State/SceneStateManager.cs
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

216 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.Game.State
{
using System;
using System.Collections.Generic;
using Command;
using Command.Extensions;
using Core.Command;
using Core.DataReader.Scn;
using Core.Primitives;
public sealed class SceneObjectStateOverride
{
// In-game state overrides
public bool? IsActivated { get; set; }
// ScnObjectInfo overrides
public byte? SwitchState { get; set; }
public byte? TimesCount { get; set; }
public byte? LayerIndex { get; set; }
public GameBoxVector3? GameBoxPosition { get; set; }
public float? GameBoxYRotation { get; set; }
// Special object states
#if PAL3
public int? BidirectionalPushableObjectState { get; set; }
#elif PAL3A
public int? ThreePhaseSwitchPreviousState { get; set; }
public int? ThreePhaseSwitchCurrentState { get; set; }
#endif
public ScnObjectInfo ApplyOverrides(ScnObjectInfo objectInfo)
{
if (SwitchState.HasValue) objectInfo.SwitchState = SwitchState.Value;
if (TimesCount.HasValue) objectInfo.Times = TimesCount.Value;
if (LayerIndex.HasValue) objectInfo.LayerIndex = LayerIndex.Value;
if (GameBoxPosition.HasValue) objectInfo.GameBoxPosition = GameBoxPosition.Value;
if (GameBoxYRotation.HasValue) objectInfo.GameBoxYRotation = GameBoxYRotation.Value;
return objectInfo;
}
public IEnumerable<ICommand> ToCommands((string cityName, string sceneName, int objectId) info)
{
if (IsActivated.HasValue)
{
yield return new SceneSaveGlobalObjectActivationStateCommand(
info.cityName, info.sceneName, info.objectId, IsActivated.Value);
}
if (SwitchState.HasValue)
{
yield return new SceneSaveGlobalObjectSwitchStateCommand(
info.cityName, info.sceneName, info.objectId, SwitchState.Value);
}
if (TimesCount.HasValue)
{
yield return new SceneSaveGlobalObjectTimesCountCommand(
info.cityName, info.sceneName, info.objectId, TimesCount.Value);
}
if (LayerIndex.HasValue)
{
yield return new SceneSaveGlobalObjectLayerIndexCommand(
info.cityName, info.sceneName, info.objectId, LayerIndex.Value);
}
if (GameBoxPosition.HasValue)
{
yield return new SceneSaveGlobalObjectPositionCommand(
info.cityName, info.sceneName, info.objectId,
GameBoxPosition.Value.X, GameBoxPosition.Value.Y, GameBoxPosition.Value.Z);
}
if (GameBoxYRotation.HasValue)
{
yield return new SceneSaveGlobalObjectYRotationCommand(
info.cityName, info.sceneName, info.objectId, GameBoxYRotation.Value);
}
#if PAL3
if (BidirectionalPushableObjectState.HasValue)
{
yield return new SceneSaveGlobalBidirectionalPushableObjectStateCommand(
info.cityName, info.sceneName, info.objectId, BidirectionalPushableObjectState.Value);
}
#elif PAL3A
if (ThreePhaseSwitchPreviousState.HasValue && ThreePhaseSwitchCurrentState.HasValue)
{
yield return new SceneSaveGlobalThreePhaseSwitchStateCommand(
info.cityName, info.sceneName, info.objectId,
ThreePhaseSwitchPreviousState.Value, ThreePhaseSwitchCurrentState.Value);
}
#endif
}
}
public sealed class SceneStateManager : IDisposable,
ICommandExecutor<SceneSaveGlobalObjectActivationStateCommand>,
ICommandExecutor<SceneSaveGlobalObjectSwitchStateCommand>,
ICommandExecutor<SceneSaveGlobalObjectTimesCountCommand>,
ICommandExecutor<SceneSaveGlobalObjectLayerIndexCommand>,
ICommandExecutor<SceneSaveGlobalObjectPositionCommand>,
ICommandExecutor<SceneSaveGlobalObjectYRotationCommand>,
#if PAL3
ICommandExecutor<SceneSaveGlobalBidirectionalPushableObjectStateCommand>,
#elif PAL3A
ICommandExecutor<SceneSaveGlobalThreePhaseSwitchStateCommand>,
#endif
ICommandExecutor<ResetGameStateCommand>
{
private readonly Dictionary<(string cityName, string sceneName, int objectId), SceneObjectStateOverride>
_sceneObjectStateOverrides = new ();
public SceneStateManager()
{
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
public void Dispose()
{
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public Dictionary<(string cityName, string sceneName, int objectId), SceneObjectStateOverride> GetSceneObjectStateOverrides()
{
return _sceneObjectStateOverrides;
}
public bool TryGetSceneObjectStateOverride(string cityName,
string sceneName,
int objectId,
out SceneObjectStateOverride stateOverride)
{
(string, string, int objectId) key = (cityName.ToLower(), sceneName.ToLower(), objectId);
if (_sceneObjectStateOverrides.TryGetValue(key, out SceneObjectStateOverride objectStateOverride))
{
stateOverride = objectStateOverride;
return true;
}
else
{
stateOverride = default;
return false;
}
}
private void InitKeyIfNotExists((string cityName, string sceneName, int objectId) key)
{
_sceneObjectStateOverrides.TryAdd(key, new SceneObjectStateOverride());
}
public void Execute(SceneSaveGlobalObjectActivationStateCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].IsActivated = command.IsActivated;
}
public void Execute(SceneSaveGlobalObjectSwitchStateCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].SwitchState = command.SwitchState;
}
public void Execute(SceneSaveGlobalObjectTimesCountCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].TimesCount = command.TimesCount;
}
public void Execute(SceneSaveGlobalObjectLayerIndexCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].LayerIndex = command.LayerIndex;
}
public void Execute(SceneSaveGlobalObjectPositionCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].GameBoxPosition =
new GameBoxVector3(command.GameBoxXPosition, command.GameBoxYPosition, command.GameBoxZPosition);
}
public void Execute(SceneSaveGlobalObjectYRotationCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].GameBoxYRotation = command.GameBoxYRotation;
}
#if PAL3
public void Execute(SceneSaveGlobalBidirectionalPushableObjectStateCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].BidirectionalPushableObjectState = command.State;
}
#elif PAL3A
public void Execute(SceneSaveGlobalThreePhaseSwitchStateCommand command)
{
(string, string, int ObjectId) key = (command.CityName.ToLower(), command.SceneName.ToLower(), command.ObjectId);
InitKeyIfNotExists(key);
_sceneObjectStateOverrides[key].ThreePhaseSwitchPreviousState = command.PreviousState;
_sceneObjectStateOverrides[key].ThreePhaseSwitchCurrentState = command.CurrentState;
}
#endif
public void Execute(ResetGameStateCommand command)
{
_sceneObjectStateOverrides.Clear();
}
}
}