1
This commit is contained in:
147
Src/Unity/Scripts/Mod/UnityModLoader.cs
Normal file
147
Src/Unity/Scripts/Mod/UnityModLoader.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Printing;
|
||||
using System.Text;
|
||||
using BITKit.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
// ReSharper disable SuspiciousTypeConversion.Global
|
||||
|
||||
namespace BITKit.Mod
|
||||
{
|
||||
|
||||
public class UnityModLoader : MonoBehaviour
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string,IMod> _mods=new();
|
||||
private static readonly ConcurrentDictionary<string,ResourcePackage> _packages=new();
|
||||
private void OnEnable()
|
||||
{
|
||||
ModService.OnModLoadAsync += OnModLoadAsync;
|
||||
ModService.OnModUnloadAsync += OnModUnloadAsync;
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
ModService.OnModLoadAsync -= OnModLoadAsync;
|
||||
ModService.OnModUnloadAsync -= OnModUnloadAsync;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_mods.Clear();
|
||||
_packages.Clear();
|
||||
}
|
||||
|
||||
private async UniTask OnModUnloadAsync(IMod mod)
|
||||
{
|
||||
switch (mod)
|
||||
{
|
||||
case IAssetMod:
|
||||
_packages.TryRemove(mod.PackageName,out var package);
|
||||
package.ClearPackageSandbox();
|
||||
await package.ClearAllCacheFilesAsync();
|
||||
await package.ClearUnusedCacheFilesAsync();
|
||||
YooAssets.DestroyPackage(mod.PackageName);
|
||||
YooAssetUtils.UnregisterPackage(mod.PackageName);
|
||||
YooAssetUtils.UnregisterResourcePackage(_packages[mod.PackageName]);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask OnModLoadAsync(IMod arg)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (arg)
|
||||
{
|
||||
case IAssetMod:
|
||||
var package = YooAssets.CreatePackage(arg.PackageName);
|
||||
|
||||
|
||||
|
||||
var defaultHostServer = new RemoteServices(
|
||||
arg.FolderPath, arg.FolderPath)
|
||||
{
|
||||
|
||||
};
|
||||
var queryServices = new LocalQueryServices(arg.PackageName);
|
||||
|
||||
var initParameters = new HostPlayModeParameters
|
||||
{
|
||||
BuildinQueryServices = queryServices,
|
||||
RemoteServices = defaultHostServer
|
||||
};
|
||||
var initOperation = package.InitializeAsync(initParameters);
|
||||
await initOperation;
|
||||
|
||||
if(initOperation.Status!=EOperationStatus.Succeed)
|
||||
{
|
||||
throw new Exception(initOperation.Error);
|
||||
}
|
||||
|
||||
var updateOperation = package.UpdatePackageVersionAsync(false);
|
||||
await updateOperation;
|
||||
|
||||
if (updateOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
throw new Exception(updateOperation.Error);
|
||||
}
|
||||
|
||||
|
||||
var updateManifestOperation = package.UpdatePackageManifestAsync(updateOperation.PackageVersion, false);
|
||||
await updateManifestOperation;
|
||||
if(updateManifestOperation.Status!=EOperationStatus.Succeed)
|
||||
{
|
||||
throw new Exception(updateManifestOperation.Error);
|
||||
}
|
||||
|
||||
var downloader = package.CreateResourceDownloader(8, 0);
|
||||
|
||||
downloader.BeginDownload();
|
||||
|
||||
await downloader;
|
||||
|
||||
if (downloader.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
throw new Exception(downloader.Error);
|
||||
}
|
||||
|
||||
|
||||
_packages.TryAdd(arg.PackageName, package);
|
||||
YooAssetUtils.RegisterResourcePackage(package);
|
||||
YooAssetUtils.RegisterPackage(arg.PackageName);
|
||||
|
||||
|
||||
// var logBuilder = new StringBuilder();
|
||||
// logBuilder.AppendLine("该包中的资源:");
|
||||
// foreach (var x in package.GetAssetInfos(arg.Tags))
|
||||
// {
|
||||
// logBuilder.AppendLine(x.Address);
|
||||
// }
|
||||
// logBuilder.AppendLine("默认包中的资源");
|
||||
// foreach (var x in YooAssets.GetAssetInfos(arg.Tags))
|
||||
// {
|
||||
// logBuilder.AppendLine(x.Address);
|
||||
// }
|
||||
// BIT4Log.Log<UnityModLoadAssetTester>(logBuilder.ToString());
|
||||
|
||||
BIT4Log.Log<UnityModLoader>($"<color=green>资源包{arg.PackageName}初始化成功</color>");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user