mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +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
280 lines
10 KiB
C#
280 lines
10 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.Scene
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using Actor.Controllers;
|
|
using Command;
|
|
using Command.Extensions;
|
|
using Core.Command;
|
|
using Core.Command.SceCommands;
|
|
using Core.Contract.Constants;
|
|
using Core.DataReader.Sce;
|
|
using Core.DataReader.Scn;
|
|
using Core.Utilities;
|
|
using Data;
|
|
using Engine.Core.Abstraction;
|
|
using Engine.Core.Implementation;
|
|
using Engine.Extensions;
|
|
using Engine.Logging;
|
|
using GameSystems.Combat.Scene;
|
|
using Newtonsoft.Json;
|
|
using Script;
|
|
using State;
|
|
using UnityEngine;
|
|
|
|
public sealed class SceneManager : IDisposable,
|
|
ICommandExecutor<SceneLoadCommand>,
|
|
ICommandExecutor<SceneObjectDoNotLoadFromSaveStateCommand>,
|
|
ICommandExecutor<ResetGameStateCommand>
|
|
{
|
|
private readonly GameResourceProvider _resourceProvider;
|
|
private readonly ISceneObjectFactory _sceneObjectFactory;
|
|
private readonly SceneStateManager _sceneStateManager;
|
|
private readonly ScriptManager _scriptManager;
|
|
private readonly IGameEntity _cameraEntity;
|
|
|
|
private IGameEntity _currentSceneRoot;
|
|
private IGameEntity _currentCombatSceneRoot;
|
|
|
|
private GameScene _currentGameScene;
|
|
private CombatScene _currentCombatScene;
|
|
|
|
private readonly HashSet<int> _sceneObjectIdsToNotLoadFromSaveState = new ();
|
|
|
|
public SceneManager(GameResourceProvider resourceProvider,
|
|
ISceneObjectFactory sceneObjectFactory,
|
|
SceneStateManager sceneStateManager,
|
|
ScriptManager scriptManager,
|
|
IGameEntity cameraEntity)
|
|
{
|
|
_resourceProvider = Requires.IsNotNull(resourceProvider, nameof(resourceProvider));
|
|
_sceneObjectFactory = Requires.IsNotNull(sceneObjectFactory, nameof(sceneObjectFactory));
|
|
_sceneStateManager = Requires.IsNotNull(sceneStateManager, nameof(sceneStateManager));
|
|
_scriptManager = Requires.IsNotNull(scriptManager, nameof(scriptManager));
|
|
_cameraEntity = Requires.IsNotNull(cameraEntity, nameof(cameraEntity));
|
|
CommandExecutorRegistry<ICommand>.Instance.Register(this);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
DisposeCurrentScene();
|
|
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
|
|
}
|
|
|
|
public GameScene GetCurrentScene()
|
|
{
|
|
return _currentGameScene;
|
|
}
|
|
|
|
public CombatScene GetCurrentCombatScene()
|
|
{
|
|
return _currentCombatScene;
|
|
}
|
|
|
|
public IGameEntity GetSceneRootGameEntity()
|
|
{
|
|
if (_currentCombatSceneRoot != null)
|
|
{
|
|
return _currentCombatSceneRoot;
|
|
}
|
|
else if (_currentSceneRoot != null)
|
|
{
|
|
return _currentSceneRoot;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public GameScene LoadScene(string sceneCityName, string sceneName)
|
|
{
|
|
Stopwatch timer = Stopwatch.StartNew();
|
|
|
|
DisposeCurrentScene();
|
|
|
|
ScnFile scnFile = _resourceProvider.GetGameResourceFile<ScnFile>(
|
|
FileConstants.GetScnFileVirtualPath(sceneCityName, sceneName));
|
|
|
|
Pal3.Instance.Execute(new ScenePreLoadingNotification(scnFile.SceneInfo));
|
|
|
|
EngineLogger.Log($"Loading scene: {JsonConvert.SerializeObject(scnFile.SceneInfo)}");
|
|
|
|
_currentSceneRoot = GameEntityFactory.Create($"Scene_{sceneCityName}_{sceneName}",
|
|
null, worldPositionStays: false);
|
|
_currentGameScene = _currentSceneRoot.AddComponent<GameScene>();
|
|
_currentGameScene.Init(_resourceProvider,
|
|
_sceneObjectFactory,
|
|
_sceneStateManager,
|
|
_resourceProvider.GetMaterialManager().UsesToonRendering,
|
|
_cameraEntity,
|
|
_sceneObjectIdsToNotLoadFromSaveState);
|
|
_currentGameScene.Load(scnFile, _currentSceneRoot);
|
|
|
|
// Must to clear the exclude list after loading the scene.
|
|
_sceneObjectIdsToNotLoadFromSaveState.Clear();
|
|
|
|
// Add scene script if exists.
|
|
SceFile sceFile = _resourceProvider.GetGameResourceFile<SceFile>(
|
|
FileConstants.GetSceneSceFileVirtualPath(sceneCityName));
|
|
|
|
Pal3.Instance.Execute(
|
|
_scriptManager.TryAddSceneScript(sceFile, $"_{sceneCityName}_{sceneName}", out var sceneScriptId)
|
|
? new ScenePostLoadingNotification(scnFile.SceneInfo, sceneScriptId)
|
|
: new ScenePostLoadingNotification(scnFile.SceneInfo, ScriptConstants.InvalidScriptId));
|
|
|
|
EngineLogger.Log($"Scene loaded in {timer.ElapsedMilliseconds} ms");
|
|
|
|
// Also a good time to collect garbage
|
|
System.GC.Collect();
|
|
|
|
return _currentGameScene;
|
|
}
|
|
|
|
public CombatScene LoadCombatScene(string combatSceneName)
|
|
{
|
|
Stopwatch timer = Stopwatch.StartNew();
|
|
|
|
if (_currentCombatSceneRoot != null) UnloadCombatScene();
|
|
|
|
try
|
|
{
|
|
HideCurrentScene();
|
|
_currentCombatSceneRoot = GameEntityFactory.Create($"CombatScene_{combatSceneName}",
|
|
null, worldPositionStays: false);
|
|
_currentCombatScene = _currentCombatSceneRoot.AddComponent<CombatScene>();
|
|
_currentCombatScene.Init(_resourceProvider);
|
|
_currentCombatScene.Load(_currentCombatSceneRoot,
|
|
combatSceneName,
|
|
_resourceProvider.GetMaterialManager().UsesToonRendering);
|
|
}
|
|
catch
|
|
{
|
|
UnloadCombatScene();
|
|
throw;
|
|
}
|
|
|
|
EngineLogger.Log($"CombatScene loaded in {timer.ElapsedMilliseconds} ms");
|
|
|
|
// Also a good time to collect garbage
|
|
System.GC.Collect();
|
|
|
|
return _currentCombatScene;
|
|
}
|
|
|
|
public void UnloadCombatScene()
|
|
{
|
|
DisposeCombatScene();
|
|
ShowCurrentScene();
|
|
}
|
|
|
|
private void DisposeCombatScene()
|
|
{
|
|
if (_currentCombatSceneRoot?.NativeObject is GameObject root && root != null)
|
|
{
|
|
root.SetActive(false);
|
|
_currentCombatSceneRoot.Destroy();
|
|
}
|
|
|
|
_currentCombatSceneRoot = null;
|
|
_currentCombatScene = null;
|
|
}
|
|
|
|
private readonly IList<Renderer> _temporarilyDisabledRenderers = new List<Renderer>();
|
|
private readonly IList<GameObject> _temporarilyDisabledGameObjects = new List<GameObject>();
|
|
private void HideCurrentScene()
|
|
{
|
|
_currentGameScene?.SetLightingActive(false);
|
|
_temporarilyDisabledRenderers.Clear();
|
|
_temporarilyDisabledGameObjects.Clear();
|
|
|
|
foreach (ActorMovementController movementController in
|
|
_currentSceneRoot.GetComponentsInChildren<ActorMovementController>())
|
|
{
|
|
movementController.PauseMovement();
|
|
}
|
|
|
|
foreach (Renderer renderer in _currentSceneRoot.GetComponentsInChildren<Renderer>())
|
|
{
|
|
if (renderer.enabled)
|
|
{
|
|
renderer.enabled = false;
|
|
_temporarilyDisabledRenderers.Add(renderer);
|
|
}
|
|
}
|
|
|
|
foreach (ParticleSystem particleSystem in _currentSceneRoot.GetComponentsInChildren<ParticleSystem>())
|
|
{
|
|
GameObject particleGo = particleSystem.gameObject;
|
|
if (particleGo.activeInHierarchy)
|
|
{
|
|
particleGo.SetActive(false);
|
|
_temporarilyDisabledGameObjects.Add(particleGo);
|
|
}
|
|
}
|
|
|
|
// Move the scene root to a lower position just to be safe.
|
|
_currentSceneRoot.Transform.LocalPosition = Vector3.down * 100f;
|
|
}
|
|
|
|
private void ShowCurrentScene()
|
|
{
|
|
_currentGameScene?.SetLightingActive(true);
|
|
// Move the scene root back to the original position.
|
|
_currentSceneRoot.Transform.LocalPosition = Vector3.zero;
|
|
|
|
foreach (GameObject disabledGo in _temporarilyDisabledGameObjects)
|
|
{
|
|
if (disabledGo != null) disabledGo.SetActive(true);
|
|
}
|
|
|
|
foreach (Renderer renderer in _temporarilyDisabledRenderers)
|
|
{
|
|
if (renderer != null) renderer.enabled = true;
|
|
}
|
|
|
|
foreach (ActorMovementController movementController in
|
|
_currentSceneRoot.GetComponentsInChildren<ActorMovementController>())
|
|
{
|
|
movementController.ResumeMovement();
|
|
}
|
|
|
|
_temporarilyDisabledRenderers.Clear();
|
|
_temporarilyDisabledGameObjects.Clear();
|
|
}
|
|
|
|
private void DisposeCurrentScene()
|
|
{
|
|
_temporarilyDisabledRenderers.Clear();
|
|
_temporarilyDisabledGameObjects.Clear();
|
|
|
|
if (_currentGameScene != null)
|
|
{
|
|
Pal3.Instance.Execute(new SceneLeavingCurrentSceneNotification());
|
|
_currentGameScene.Destroy();
|
|
_currentGameScene = null;
|
|
}
|
|
|
|
DisposeCombatScene();
|
|
|
|
if (_currentSceneRoot != null)
|
|
{
|
|
_currentSceneRoot.Destroy();
|
|
_currentSceneRoot = null;
|
|
}
|
|
}
|
|
|
|
public void Execute(SceneLoadCommand command) => LoadScene(command.SceneCityName, command.SceneName);
|
|
|
|
public void Execute(ResetGameStateCommand command) => DisposeCurrentScene();
|
|
|
|
public void Execute(SceneObjectDoNotLoadFromSaveStateCommand command)
|
|
{
|
|
_sceneObjectIdsToNotLoadFromSaveState.Add(command.ObjectId);
|
|
}
|
|
}
|
|
} |