Files
Pal3.Unity/Assets/Scripts/Pal3.Game/GameResourceInitializer.cs
Jiaqi Liu 8ef2c946db Migrate PAL3/PAL3A rendering to URP Forward+ with independent Toon lighting (#90)
* Migrate original rendering and frosted UI to URP Forward+

Port the original materials, fog and screen passes to URP. Capture the finished scene for frosted UI and keep the renderer assets ready for clustered lighting.

* Add standalone Toon lighting and shared model rendering support

Add independent Toon shaders, scene and character lighting, contact shadows and fire controls. Share POL materials, preserve MV3 seams and texture swaps across both styles, and support scene-specific Toon water.

* Skip redundant CVD mesh updates

Reuse unchanged poses and UVs, and recompute Toon normals only when geometry or the linear transform changes.

* Manage game Bloom and screen effect state by camera

Expose a persisted Bloom switch, restore the active style default on reset, and release camera and volume state when the manager is disposed.

* Restore opt-in story VFX loading

Use the existing local VFX resource path for synchronous and asynchronous loading. Keep the preference off by default and document compatible optional assets.

* Save Unity 6.6 and URP project settings

Persist the current graphics, quality, player and shader graph settings plus the URP material upgrade marker.

* Fix Unity 6.6 startup compiler warnings

Use Unity assembly discovery and object lookup APIs, keep PAL3 command reflection independent of UnityEngine, and preserve the runtime GDB dictionaries in JSON exports.

* Use the new Input System as the only input backend

Select Input System Package as the sole backend. Remove the extra scroll-sensitivity reductions in the console and file browser, preserving their previous Both-mode scrolling behavior.

* Preserve two-sided model normals across animation

Recognize coincident MV3 vertices by their complete animation trajectories and skeletal vertices by their bind positions and bone. Separate opposing faces only where a source normal group is shared, preserving UV seams and adjacent single-sided faces.

* Isolate optional VFX lifetimes and private asset builds

Centralize optional prefab loading, persistent attachments, active-effect cleanup and cache invalidation. Runtime toggles hide active effects and restore surviving attachments. Block private VFX and legacy Toon assets in public builds, with an explicit local private-build override.

Release portal attachments on destruction, reject dead parents and prune orphaned persistent attachments.

* Manage camera post-processing, anti-aliasing and menu glass

Give each game camera its own VolumeStack without mutating shared profiles, and restore inherited camera and effect state on disposal. Map the old PPv2 Bloom intensity through its exponential compositing coefficient; this does not establish visual equivalence across pipelines. Share saved and live MSAA handling with ResourceViewer, and make the main menu acrylic background whiter with less scene color showing through.

Restore pipeline settings even after partial initialization or service cleanup failure. Protect the persistent URP asset from ordinary Play Mode saves and restore the two runtime fields across Play exit and script reloads without reverting unrelated edits.

* Tune Toon lighting, water and bounded point-light shadows

Strengthen indoor and outdoor lighting contrast, integrate SSAO and baked shading while retaining character detail, and light transparent water with the shared Toon shader. Expose independent switches and strengths through the console. Support up to four shadowed fires with bounded allocation-free selection, preserve saved preferences, adapt the shadow atlas, and default desktop devices to four shadowed fires.

Report invalid console strengths and shadow budgets without saving rejected values.

* Fix DXT3 alpha decoding that produces foliage stripes

Expand the high alpha nibble into both halves of the output byte. Fully opaque DXT3 texels now decode to 255 in both pixel positions, preventing alternating coverage in the cutout and blended rendering passes. Add editor regressions for opaque RGB/alpha, all sixteen alpha levels in pixel order, and partial blocks with buffer-tail protection.

* Document Unity CI after disabling the Codacy integration
2026-11-12 02:28:17 +08:00

348 lines
14 KiB
C#

// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2025, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Pal3.Game
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using Constants;
using Core.Command;
using Core.Contract.Constants;
using Core.DataReader;
using Core.DataReader.Cpk;
using Core.FileSystem;
using Core.Utilities;
using Data;
using Engine.Core.Abstraction;
using Engine.Core.Implementation;
using Engine.Extensions;
using Engine.Logging;
using Engine.Services;
using Engine.Utilities;
using Rendering.Material;
using Scene;
using Settings;
using SimpleFileBrowser;
using TMPro;
using UnityEngine;
using UnityEngine.Assemblies;
using Transform = UnityEngine.Transform;
/// <summary>
/// The Game Resource initializer
/// Initialize file system and all required services.
/// </summary>
public sealed class GameResourceInitializer : GameEntityScript
{
[SerializeField] private GameObject startingComponent;
[SerializeField] private TextMeshProUGUI loadingText;
private IEnumerator Start()
{
loadingText.text = "正在加载游戏数据...";
yield return null; // Wait for next frame to make sure the text is updated
yield return InitResourceAsync();
}
private IEnumerator InitResourceAsync()
{
EngineLogger.Log("Initializing game resources...");
// Create and init Crc32 hash
Crc32Hash crcHash = new ();
crcHash.Init();
ServiceLocator.Instance.Register<Crc32Hash>(crcHash);
// Init settings store
ITransactionalKeyValueStore settingsStore = new PlayerPrefsStore();
// Init settings
GameSettings gameSettings = new (settingsStore);
gameSettings.InitOrLoadSettings();
ServiceLocator.Instance.Register<GameSettings>(gameSettings);
// Init codepage
int codepage = gameSettings.Language == Language.SimplifiedChinese ? 936 : 950;
// Init file system
Queue<string> gameDataFolderSearchLocations = new(gameSettings.GetGameDataFolderSearchLocations());
ICpkFileSystem cpkFileSystem = null;
if (gameDataFolderSearchLocations.Count == 0)
{
EngineLogger.LogError("No game data folder search locations found");
yield break;
}
while (gameDataFolderSearchLocations.Count > 0)
{
string gameDataFolderPath = gameDataFolderSearchLocations.Dequeue();
Exception exception = null;
yield return InitFileSystemAsync(gameDataFolderPath,
crcHash, codepage, (fileSystem, ex) =>
{
cpkFileSystem = fileSystem;
exception = ex;
});
if (cpkFileSystem != null) // Init file system successfully
{
ServiceLocator.Instance.Register<ICpkFileSystem>(cpkFileSystem);
// Save game data folder path when file system initialized successfully,
// since it's possible that user changed the game data folder path
// during the file system initialization
gameSettings.GameDataFolderPath = gameDataFolderPath;
gameSettings.SaveSettings();
break; // Stop searching when file system is initialized successfully
}
// If file system is not initialized successfully, retry with next search
// location when there is any
if (gameDataFolderSearchLocations.Count > 0) continue;
string userPickedGameDataFolderPath = null;
yield return WaitForUserToPickGameDataFolderAsync(path => userPickedGameDataFolderPath = path);
if (!string.IsNullOrEmpty(userPickedGameDataFolderPath))
{
// Enqueue the user picked game data folder path to the search
// locations and retry
gameDataFolderSearchLocations.Enqueue(userPickedGameDataFolderPath);
loadingText.text = "正在重新加载游戏数据...";
yield return null; // Wait for next frame to make sure the text is updated
}
else
{
string errorMessage = exception switch
{
null => "游戏数据加载失败,未能找到原游戏数据文件",
DirectoryNotFoundException => $"游戏数据加载失败,原始游戏数据根目录不存在: {exception.Message}",
FileNotFoundException => $"游戏数据加载失败,游戏原CPK数据文件不存在: {exception.Message}",
_ => $"游戏数据加载失败,错误信息:{exception}"
};
loadingText.text = errorMessage;
yield break; // Stop initialization if failed to init file system
}
}
// Create and register IFileReader<T> instances using reflection
Assembly[] loadedAssemblies = CurrentAssemblies.GetLoadedAssemblies().ToArray();
foreach (Assembly assembly in loadedAssemblies)
{
// Get all types that implement any version of IFileReader<>
IEnumerable<Type> fileReaderTypes = assembly.GetTypes().Where(type =>
type.GetInterfaces().Any(_ =>_.IsGenericType &&_.GetGenericTypeDefinition() == typeof(IFileReader<>)));
foreach (Type readerType in fileReaderTypes)
{
Type fileType = readerType.GetInterfaces()
.First(_ => _.GetGenericTypeDefinition() == typeof(IFileReader<>)).GetGenericArguments()[0];
ServiceLocator.Instance.GetType()
.GetMethod(nameof(ServiceLocator.Instance.Register))!
.MakeGenericMethod(typeof(IFileReader<>).MakeGenericType(fileType))
.Invoke(ServiceLocator.Instance, new[] { Activator.CreateInstance(readerType) });
}
}
loadingText.text = "正在初始化游戏资源...";
yield return null; // Wait for next frame to make sure the text is updated
// Init TextureFactory
ITextureFactory textureFactory = new UnityTextureFactory();
ServiceLocator.Instance.Register<ITextureFactory>(textureFactory);
// Init TextureLoaderFactory
TextureLoaderFactory textureLoaderFactory = new (textureFactory);
ServiceLocator.Instance.Register<ITextureLoaderFactory>(textureLoaderFactory);
// Init MaterialFactory
IMaterialFactory materialFactory = new UnityMaterialFactory();
ServiceLocator.Instance.Register<IMaterialFactory>(materialFactory);
// Select the material family for the lifetime of this session.
IMaterialManager materialManager = new GameMaterialManager(materialFactory, gameSettings.ToonRendering);
// Init command related services
SceCommandTypeResolver sceCommandTypeResolver = new SceCommandTypeResolver();
sceCommandTypeResolver.Init();
ServiceLocator.Instance.Register<ISceCommandTypeResolver>(sceCommandTypeResolver);
SceCommandParser sceCommandParser = new SceCommandParser(sceCommandTypeResolver);
ServiceLocator.Instance.Register<ISceCommandParser>(sceCommandParser);
SceCommandPreprocessor sceCommandPreprocessor = new SceCommandPreprocessor();
sceCommandPreprocessor.Init(loadedAssemblies);
ServiceLocator.Instance.Register<ISceCommandPreprocessor>(sceCommandPreprocessor);
// Init scene object factory
SceneObjectFactory sceneObjectFactory = new SceneObjectFactory();
sceneObjectFactory.Init();
ServiceLocator.Instance.Register<ISceneObjectFactory>(sceneObjectFactory);
// Init Game resource provider
GameResourceProvider resourceProvider = new GameResourceProvider(cpkFileSystem,
textureLoaderFactory,
textureFactory,
materialManager,
gameSettings);
ServiceLocator.Instance.Register(resourceProvider);
EngineLogger.Log($"Game resources initialized");
loadingText.text = $"正在启动 {startingComponent.name}...";
yield return null; // Wait for next frame to make sure the text is updated
// Instantiate starting component
GameObject startingGameObject = Instantiate(startingComponent, null);
startingGameObject.name = startingComponent.name;
FinalizeInit();
}
private IEnumerator WaitForUserToPickGameDataFolderAsync(Action<string> callback)
{
string userPickedGameDataFolderPath = null;
yield return FileBrowser.WaitForLoadDialog(
FileBrowser.PickMode.Folders,
allowMultiSelection: false,
initialPath: null,
initialFilename: null,
title: $"请选择<<{GameConstants.AppNameCNFull}>>原始游戏文件夹根目录",
loadButtonText: "选择");
if (FileBrowser.Success &&
FileBrowser.Result.Length == 1)
{
#if !UNITY_EDITOR && UNITY_ANDROID
// On Android 10+, the result is a content URI of the selected folder in
// SAF (Storage Access Framework) format, so we need to copy it to the
// persistent data path in order to use it with System.IO APIs
if (FileBrowserHelpers.ShouldUseSAF)
{
string persistentDataPath = Application.persistentDataPath +
Path.DirectorySeparatorChar +
GameConstants.AppName +
Path.DirectorySeparatorChar;
// Delete the old folder if it exists
if (Directory.Exists(persistentDataPath))
{
Directory.Delete(persistentDataPath, true);
}
loadingText.text = "正在拷贝游戏数据至游戏可访问目录(预计需要几分钟)...";
yield return null; // Wait for next frame to make sure the text is updated
FileBrowserHelpers.CopyDirectory(FileBrowser.Result[0], persistentDataPath);
userPickedGameDataFolderPath = persistentDataPath;
}
else
#endif
{
userPickedGameDataFolderPath = FileBrowser.Result[0];
}
}
callback.Invoke(userPickedGameDataFolderPath);
}
private void FinalizeInit()
{
foreach (Transform child in transform)
{
child.gameObject.Destroy();
}
// Since everything except for ServiceLocator will be destroyed,
// we can just name the current game object as ServiceLocator
gameObject.name = nameof(ServiceLocator);
this.Destroy();
}
private IEnumerator InitFileSystemAsync(string gameDataFolderPath,
Crc32Hash crcHash,
int codepage,
Action<ICpkFileSystem, Exception> callback)
{
ICpkFileSystem cpkFileSystem = null;
string path = gameDataFolderPath;
Exception exception = null;
Thread fileSystemInitThread = new(() =>
{
try
{
Stopwatch timer = Stopwatch.StartNew();
cpkFileSystem = InitializeCpkFileSystem(path, crcHash, codepage);
EngineLogger.Log($"All CPK files mounted successfully in {timer.ElapsedMilliseconds} ms");
}
catch (Exception ex)
{
exception = ex;
}
finally
{
GC.Collect();
}
})
{
IsBackground = true,
Priority = System.Threading.ThreadPriority.Highest
};
fileSystemInitThread.Start();
while (fileSystemInitThread.IsAlive)
{
yield return null;
}
callback?.Invoke(cpkFileSystem, exception);
}
private ICpkFileSystem InitializeCpkFileSystem(string gameRootPath, Crc32Hash crcHash, int codepage)
{
ICpkFileSystem cpkFileSystem = new CpkFileSystem(gameRootPath, crcHash, codepage);
List<string> filesToMount = new ()
{
FileConstants.BaseDataCpkFileRelativePath,
FileConstants.MusicCpkFileRelativePath
};
foreach (string sceneCpkFileName in FileConstants.SceneCpkFileNames)
{
filesToMount.Add(FileConstants.GetSceneCpkFileRelativePath(sceneCpkFileName));
}
#if PAL3A
filesToMount.Add(FileConstants.ScnCpkFileRelativePath);
filesToMount.Add(FileConstants.SceCpkFileRelativePath);
#endif
foreach (string cpkFileRelativePath in filesToMount)
{
EngineLogger.Log($"Mounting CPK file: <{cpkFileRelativePath}>");
cpkFileSystem.Mount(cpkFileRelativePath, codepage);
}
return cpkFileSystem;
}
}
}