This commit is contained in:
CortexCore
2024-03-11 02:16:25 +08:00
parent 605ccbcf8d
commit 6ef7c5f005
16 changed files with 832 additions and 45 deletions

View File

@@ -184,6 +184,9 @@ namespace BITKit.Mod
OnLocked?.Invoke(value);
}
}
public static event Action<ModPackage> OnPackageLoad;
public static event Action<ModPackage> OnPackageLoaded;
public static event Action<IMod> OnModLoad;
public static event Action<IMod> OnModLoaded;
@@ -409,6 +412,14 @@ namespace BITKit.Mod
if(package.EntryPoint is null) throw new InvalidOperationException("空入口,无法识别类型");
path = Path.Combine(Path.GetDirectoryName(path)!, package.EntryPoint);
if(File.Exists(path) is false) throw new InvalidOperationException($"未找到入口文件:{path}");
OnPackageLoad?.Invoke(package);
foreach (var name in package.Dlls)
{
}
var fileInfo = new FileInfo(path);
switch (fileInfo.Extension)
{
@@ -429,6 +440,7 @@ namespace BITKit.Mod
#endif
}
OnPackageLoaded?.Invoke(package);
}
public static void Load(IMod mod)
{

View File

@@ -1,3 +1,6 @@
using System;
using Unity.Mathematics;
namespace BITKit
{
public enum TransformMode : int
@@ -7,4 +10,13 @@ namespace BITKit
Rotate,
Scale,
}
public interface ITransform:IDisposable
{
float3 LocalPosition { get; set; }
float3 Position { get; set; }
quaternion LocalRotation { get; set; }
quaternion Rotation { get; set; }
float3 LocalScale { get; set; }
float4x4 Matrix { get; set; }
}
}

View File

@@ -25,31 +25,18 @@ namespace BITKit.Mod
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 UNITY_EDITOR
var 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
if (System.IO.File.Exists(dll) is false)
if (SearchDll(dllName,out var dll) is false)
{
BIT4Log.Warning<UnityModService>($"未找到:{dll}");
continue;
}
BITSharp.ReferencedAssemblies.Add(@$"""{dll}""");
}
@@ -67,6 +54,9 @@ namespace BITKit.Mod
destroyCancellationToken.Register(ModService.Dispose);
if (!loadLocalPackageOnStart) return;
ModService.OnPackageLoad+=OnPackageLoad;
var packages = await ModService.SearchPackages();
if (destroyCancellationToken.IsCancellationRequested) return;
@@ -75,6 +65,51 @@ namespace BITKit.Mod
await ModService.LoadFromPackage(package.PackagePath);
if (destroyCancellationToken.IsCancellationRequested) return;
}
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);
}
}
}

View File

@@ -0,0 +1,58 @@
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
namespace BITKit
{
public static class TransformExtensions
{
public static UnityTransform ToUnityTransform(this Transform transform)
{
return new UnityTransform(transform);
}
}
public readonly struct UnityTransform:ITransform
{
private readonly Transform _transform;
public UnityTransform(Transform transform)
{
_transform = transform;
}
public void Dispose()
{
if (_transform)
Object.Destroy(_transform.gameObject);
}
public float3 LocalPosition
{
get => _transform.localPosition;
set => _transform.localPosition = value;
}
public float3 Position
{
get => _transform.position;
set => _transform.position = value;
}
public quaternion LocalRotation
{
get => _transform.localRotation;
set => _transform.localRotation = value;
}
public quaternion Rotation
{
get => _transform.rotation;
set => _transform.rotation = value;
}
public float3 LocalScale
{
get => _transform.localScale;
set => _transform.localScale = value;
}
public float4x4 Matrix
{
get =>new float4x4(_transform);
set => _transform.SetPositionAndRotation(value.c3.xyz,quaternion.LookRotation(value.c2.xyz,value.c1.xyz));
}
}
}