ZK_Framework/Assets/Scripts/Mono/LoadDll.cs

117 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HybridCLR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cysharp.Threading.Tasks;
using Mono;
using UnityEngine;
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>()
{
};
// public List<string> dllNames = new List<string>()
// {
// "Hotfix.dll", "Hotfix.pdb"
// };
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 = AssetsConst.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()
{
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[AssetsConst.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[] { };
type.GetMethod("Init").Invoke(null, null);
// var instance = Activator.CreateInstance(type, pa);
// var game1 = instance as Game.Game;
//
// return game1;
}
// void Start()
// {
// this.game.Start();
// }
//
// private void Update()
// {
// this.game.Update();
// }
//
// private void OnDestroy()
// {
// this.game.OnDestroy();
// }
}