This commit is contained in:
CortexCore
2025-02-24 23:02:43 +08:00
parent 41715e4413
commit 8261a458e2
105 changed files with 2934 additions and 696 deletions

View File

@@ -1,121 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BITKit.UX;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace BITKit.Mod
{
public class UnityModService : MonoBehaviour
{
[SerializeReference,SubclassSelector] private IReference[] referencedAssemblies;
private async void Start()
{
//DI.TryGet<IUXWaiting>(out var waiting);
IUXWaiting waiting = null;
var handle = waiting?.Get();
handle?.SetMessage("正在初始化Mod服务");
if (Application.isEditor is false)
{
BIT4Log.Log<UnityModService>($"UnityPlayer所在位置:{Application.dataPath}");
BIT4Log.Log<UnityModService>($"{nameof(System.Linq)}位于{typeof(Enumerable).Assembly.Location}");
}
foreach (var x in referencedAssemblies)
{
var dllName = x.Value.Contains(".dll") ? x.Value : $"{x.Value}.dll";
if (SearchDll(dllName,out var dll) is false)
{
BIT4Log.Warning<UnityModService>($"未找到:{dll}");
continue;
}
BITSharp.ReferencedAssemblies.Add(@$"""{dll}""");
}
try
{
ModService.Initialize();
}
catch (Exception e)
{
BIT4Log.Warning<UnityModService>("初始化失败");
BIT4Log.LogException(e);
return;
}
destroyCancellationToken.Register(ModService.Dispose);
ModService.OnPackageLoad+=OnPackageLoad;
var packages = await ModService.SearchPackages();
if (destroyCancellationToken.IsCancellationRequested) return;
foreach (var package in packages)
{
await ModService.LoadFromPackage(package.PackagePath);
if (destroyCancellationToken.IsCancellationRequested) return;
}
destroyCancellationToken.Register(() =>
{
ModService.OnPackageLoad-=OnPackageLoad;
});
waiting?.Release(handle);
}
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
dll = System.IO.Path.Combine(Environment.CurrentDirectory,$"{Application.productName}_Data", "Managed", dllName);
#endif
return File.Exists(dll);
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b030d336e53d2c646a7b382eb83897bf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,48 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace BITKit.Mod
{
public class UnityModServiceTester : MonoBehaviour
{
[Serializable]
public class TestLogGameTickMod:MyMod
{
public override void OnInitialized()
{
base.OnInitialized();
GameTickService.Add(OnTick);
}
private static void OnTick(float obj)
{
BIT4Log.Log<TestLogGameTickMod>($"On Test Mod Tick,delta:{obj}");
}
public override void OnDispose()
{
GameTickService.Remove(OnTick);
}
}
[SerializeReference, SubclassSelector] private IMod[] initialMods;
private void OnEnable()
{
foreach (var testMod in initialMods)
{
ModService.Install(testMod);
ModService.Load(testMod);
}
}
private void OnDisable()
{
foreach (var testMod in initialMods)
{
ModService.UnLoad(testMod);
ModService.UnInstall(testMod);
}
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: c9d7136f598a780459b866ee63545a5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: