iFactory.Cutting.Unity/Assets/BITKit/Unity/Scripts/Mod/UnityModService.cs

117 lines
3.0 KiB
C#
Raw Normal View History

2024-01-23 02:56:26 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-03-04 18:45:21 +08:00
using System.IO;
2024-03-05 15:27:29 +08:00
using System.Linq;
2024-03-04 18:45:21 +08:00
#if UNITY_EDITOR
using UnityEditor;
#endif
2024-01-23 02:56:26 +08:00
using UnityEngine;
namespace BITKit.Mod
{
public class UnityModService : MonoBehaviour
{
[SerializeReference,SubclassSelector] private IReference[] referencedAssemblies;
2024-03-04 18:45:21 +08:00
[SerializeField] private bool loadLocalPackageOnStart;
private async void Start()
2024-01-23 02:56:26 +08:00
{
2024-03-05 15:27:29 +08:00
if (Application.isEditor is false)
{
BIT4Log.Log<UnityModService>($"UnityPlayer所在位置:{Application.dataPath}");
BIT4Log.Log<UnityModService>($"{nameof(System.Linq)}位于{typeof(Enumerable).Assembly.Location}");
}
2024-03-11 02:16:25 +08:00
2024-01-23 02:56:26 +08:00
foreach (var x in referencedAssemblies)
{
var dllName = x.Value.Contains(".dll") ? x.Value : $"{x.Value}.dll";
2024-03-11 02:16:25 +08:00
if (SearchDll(dllName,out var dll) is false)
2024-03-04 18:45:21 +08:00
{
BIT4Log.Warning<UnityModService>($"未找到:{dll}");
2024-03-11 02:16:25 +08:00
continue;
2024-03-04 18:45:21 +08:00
}
2024-03-11 02:16:25 +08:00
2024-03-04 18:45:21 +08:00
BITSharp.ReferencedAssemblies.Add(@$"""{dll}""");
}
try
{
ModService.Initialize();
}
catch (Exception e)
{
BIT4Log.Warning<UnityModService>("初始化失败");
BIT4Log.LogException(e);
return;
2024-01-23 02:56:26 +08:00
}
2024-03-04 18:45:21 +08:00
2024-01-23 02:56:26 +08:00
destroyCancellationToken.Register(ModService.Dispose);
2024-03-04 18:45:21 +08:00
if (!loadLocalPackageOnStart) return;
2024-03-11 02:16:25 +08:00
ModService.OnPackageLoad+=OnPackageLoad;
2024-03-04 18:45:21 +08:00
var packages = await ModService.SearchPackages();
if (destroyCancellationToken.IsCancellationRequested) return;
foreach (var package in packages)
{
await ModService.LoadFromPackage(package.PackagePath);
if (destroyCancellationToken.IsCancellationRequested) return;
}
2024-03-11 02:16:25 +08:00
destroyCancellationToken.Register(() =>
{
ModService.OnPackageLoad-=OnPackageLoad;
});
}
private void OnPackageLoad(ModPackage obj)
{
var loadedDlls = referencedAssemblies.Cast();
var reportBuilder = new System.Text.StringBuilder();
//对比已加载的dll和当前引用的dll
foreach (var x in obj.Dlls.Except(loadedDlls))
{
if (SearchDll(x, out var dll) is false)
{
BIT4Log.Warning<UnityModService>($"未找到:{dll}");
continue;
}
BITSharp.ReferencedAssemblies.Add(@$"""{dll}""");
reportBuilder.AppendLine($"加载:{dll}");
}
BIT4Log.Log<UnityModService>(reportBuilder.ToString());
}
private bool SearchDll(string dllName,out string dll,params string[] moreFolder)
{
#if UNITY_EDITOR
dll = System.IO.Path.Combine(Environment.CurrentDirectory, "Library", "ScriptAssemblies", dllName);
var folder = EditorApplication.applicationPath;
folder = Path.GetDirectoryName(folder);
if(File.Exists(dll) is false)
{
dll = Path.Combine(folder,"Data", "MonoBleedingEdge", "lib","mono","unityjit-win32",dllName);
}
if (File.Exists(dll) is false)
{
dll = Path.Combine(folder,"Data", "MonoBleedingEdge", "lib","mono","unityjit-win32","Facades",dllName);
}
#else
var dll = System.IO.Path.Combine(Environment.CurrentDirectory,$"{Application.productName}_Data", "Managed", dllName);
#endif
return File.Exists(dll);
2024-01-23 02:56:26 +08:00
}
}
}