69 lines
2.6 KiB
C#
69 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
namespace ShrinkModFramework
|
|
{
|
|
/// <summary>默认模组组件根;目录变更只提交完整的新组合,失败时由 Host 恢复旧组合。</summary>
|
|
public static class ShrinkModCordisRuntime
|
|
{
|
|
private static ShrinkModContextHost _host;
|
|
|
|
public static bool IsInitialized => _host != null;
|
|
public static ShrinkModContextHost Host => _host ?? throw new InvalidOperationException(
|
|
"ShrinkModCordisRuntime has not been initialized.");
|
|
public static IReadOnlyDictionary<string, ShrinkModHandle> Mods =>
|
|
_host?.Mods ?? EmptyMods;
|
|
|
|
private static readonly IReadOnlyDictionary<string, ShrinkModHandle> EmptyMods =
|
|
new Dictionary<string, ShrinkModHandle>();
|
|
|
|
public static async UniTask<IReadOnlyDictionary<string, ShrinkModHandle>> ApplyDiscoveredAsync(
|
|
ShrinkModFrameworkSettings settings = null)
|
|
{
|
|
settings ??= ShrinkModFrameworkSettings.Instance;
|
|
var verboseLogging = settings == null || settings.verboseLogging;
|
|
if (_host == null)
|
|
{
|
|
_host = new ShrinkModContextHost(
|
|
settings == null || settings.enableHarmonyPatching,
|
|
verboseLogging);
|
|
}
|
|
|
|
var revisionState = ShrinkExternalModAssemblyLoader.CaptureState();
|
|
ShrinkModNetworkManager.Configure(settings == null || settings.enableNetworkSync, verboseLogging);
|
|
try
|
|
{
|
|
var sources = ShrinkModComponentDiscovery.Discover(settings, verboseLogging);
|
|
await _host.ApplyAsync(sources);
|
|
}
|
|
catch
|
|
{
|
|
// DLL revision discovery is part of the same composition transaction:
|
|
// a failing replacement must not make the failed assembly current.
|
|
ShrinkExternalModAssemblyLoader.RestoreState(revisionState);
|
|
throw;
|
|
}
|
|
|
|
if (verboseLogging)
|
|
UnityEngine.Debug.Log($"[ShrinkModFramework] Cordis 模组组合已提交,共 {_host.Mods.Count} 个。");
|
|
return _host.Mods;
|
|
}
|
|
|
|
public static IReadOnlyShrinkModRegistry<T> GetOrCreateRegistry<T>(string name) =>
|
|
Host.GetOrCreateRegistry<T>(name);
|
|
|
|
internal static void ResetForDomainReload()
|
|
{
|
|
_host = null;
|
|
}
|
|
|
|
internal static void ResetForTesting()
|
|
{
|
|
if (_host != null)
|
|
_host.ShutdownAsync().GetAwaiter().GetResult();
|
|
_host = null;
|
|
}
|
|
}
|
|
}
|