97 lines
3.6 KiB
C#
97 lines
3.6 KiB
C#
using HybridCLR;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using Cysharp.Threading.Tasks;
|
||
using Mono;
|
||
using UnityEngine;
|
||
using YooAsset;
|
||
using ZGame;
|
||
|
||
public class LoadDll
|
||
{
|
||
private void Awake()
|
||
{
|
||
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
|
||
//#if !UNITY_EDITOR
|
||
// Assembly hotUpdateAss = Assembly.Load(File.ReadAllBytes($"{Application.streamingAssetsPath}/Hotfix.dll.bytes"));
|
||
//#else
|
||
// // Editor下无需加载,直接查找获得HotUpdate程序集
|
||
// Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix");
|
||
//#endif
|
||
|
||
// game = hotUpdateAss.GetType("Hello");
|
||
// var instance = hotUpdateAss.CreateInstance("Game.Game");
|
||
// game = (Game.Game)instance;
|
||
// game.Awake();
|
||
// this.game = new Game.Game(gameObject, EPlayMode.HostPlayMode, GameInitType.None);
|
||
// this.game.GetMethod("Awake").Invoke(null, null);
|
||
}
|
||
|
||
|
||
public List<string> aotDllNames = new List<string>()
|
||
{
|
||
@"Assets/Res/AotDlls/mscorlib.dll.bytes",
|
||
@"Assets/Res/AotDlls/UniTask.dll.bytes",
|
||
@"Assets/Res/AotDlls/UnityEngine.CoreModule.dll.bytes"
|
||
};
|
||
|
||
private Dictionary<string, TextAsset> dlls = new Dictionary<string, TextAsset>();
|
||
private Dictionary<string, TextAsset> aotDlls = new Dictionary<string, TextAsset>();
|
||
|
||
public async UniTask DownloadAsync()
|
||
{
|
||
if (true) // Dll
|
||
{
|
||
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
|
||
#if !UNITY_EDITOR
|
||
string hotfixDll = @"Assets/Res/Codes/Hotfix.dll.bytes";
|
||
var loadAssetAsync = await ResourceComponent.Instance.LoadAssetAsync<TextAsset>(hotfixDll);
|
||
this.dlls.Add(hotfixDll, loadAssetAsync);
|
||
#else
|
||
// Editor下无需加载,直接查找获得HotUpdate程序集
|
||
// Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix");
|
||
|
||
string hotfixDll = "Hotfix";
|
||
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == hotfixDll);
|
||
#endif
|
||
}
|
||
|
||
if (true) // AOTDll
|
||
{
|
||
#if !UNITY_EDITOR
|
||
foreach (var dll in this.aotDllNames)
|
||
{
|
||
var loadAssetAsync = await ResourceComponent.Instance.LoadAssetAsync<TextAsset>(dll);
|
||
this.aotDlls.Add(dll, loadAssetAsync);
|
||
}
|
||
#endif
|
||
}
|
||
|
||
await UniTask.Yield();
|
||
}
|
||
|
||
public void OnStart(GameObject go, EPlayMode playMode, int index, Action callback)
|
||
{
|
||
foreach (var kv in this.aotDlls) // 先加载aot内容,避免报错
|
||
{
|
||
TextAsset textAsset = kv.Value;
|
||
RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
|
||
}
|
||
|
||
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
|
||
#if !UNITY_EDITOR
|
||
var dllAsset = this.dlls[@"Assets/Res/Codes/Hotfix.dll.bytes"];
|
||
Assembly hotUpdateAss = Assembly.Load(dllAsset.bytes);
|
||
#else
|
||
// Editor下无需加载,直接查找获得HotUpdate程序集
|
||
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Hotfix");
|
||
#endif
|
||
|
||
var type = hotUpdateAss.GetType("Game.GameEntry");
|
||
|
||
object[] pa = new object[] { go, playMode, index, callback };
|
||
type.GetMethod("Init").Invoke(null, pa);
|
||
}
|
||
} |