Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Data/FileSystemCacheManager.cs
Jiaqi (0x7c13) Liu e870cf487a 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.
2026-11-12 13:37:27 +08:00

81 lines
3.0 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.Data
{
using System;
using System.Diagnostics;
using Command;
using Command.Extensions;
using Core.Command;
using Core.DataReader.Cpk;
using Core.FileSystem;
using Core.Utilities;
using Engine.Logging;
/// <summary>
/// Cache/dispose Cpk archive into memory based on scenarios.
/// </summary>
public sealed class FileSystemCacheManager : IDisposable,
ICommandExecutor<ScenePreLoadingNotification>
{
private readonly ICpkFileSystem _fileSystem;
private string _currentCityName;
private string _currentSceneName;
public FileSystemCacheManager(ICpkFileSystem fileSystem)
{
_fileSystem = Requires.IsNotNull(fileSystem, nameof(fileSystem));
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
public void Dispose()
{
_fileSystem.DisposeAllInMemoryArchives();
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public void Execute(ScenePreLoadingNotification notification)
{
string newCityName = notification.NewSceneInfo.CityName.ToLower();
string newSceneName = notification.NewSceneInfo.SceneName.ToLower();
// No need to do anything if the scene city is the same
if (newCityName.Equals(_currentCityName, StringComparison.OrdinalIgnoreCase)) return;
// Dispose current scene cpk from memory
if (!string.IsNullOrEmpty(_currentCityName))
{
string currentSceneFolderPath = $"{_currentCityName}{CpkConstants.FileExtension}" +
$"{CpkConstants.DirectorySeparatorChar}{_currentSceneName}";
if (_fileSystem.FileExists(currentSceneFolderPath, out string archiveName))
{
_fileSystem.DisposeInMemoryArchive(archiveName);
}
EngineLogger.Log($"Disposed in-memory archive: <{archiveName}>");
}
// Load new scene cpk into memory
{
string newSceneFolderPath = $"{newCityName}{CpkConstants.FileExtension}" +
$"{CpkConstants.DirectorySeparatorChar}{newSceneName}";
Stopwatch timer = Stopwatch.StartNew();
if (_fileSystem.FileExists(newSceneFolderPath, out string archiveName))
{
_fileSystem.LoadArchiveIntoMemory(archiveName);
}
EngineLogger.Log($"Loaded archive <{archiveName}> into memory in {timer.ElapsedMilliseconds} ms");
}
_currentCityName = newCityName;
_currentSceneName = newSceneName;
}
}
}