2024-07-12 13:33:45 +08:00
|
|
|
|
using HybridCLR;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using YooAsset;
|
|
|
|
|
|
|
|
|
|
namespace Unity.Loader
|
|
|
|
|
{
|
|
|
|
|
public class Loader
|
|
|
|
|
{
|
|
|
|
|
public List<string> aotDllNames = new List<string>()
|
|
|
|
|
{
|
2024-07-19 11:31:00 +08:00
|
|
|
|
LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_mscorlib_dll_bytes,
|
|
|
|
|
LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_Sirenix_Utilities_dll_bytes,
|
|
|
|
|
LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_UniTask_dll_bytes,
|
|
|
|
|
LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_YooAsset_dll_bytes,
|
|
|
|
|
LoaderAssetConst.Assets_DemoGame_GameRes_AotDlls_UnityEngine_CoreModule_dll_bytes,
|
2024-07-12 13:33:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2024-07-19 11:31:00 +08:00
|
|
|
|
string dllName = LoaderAssetConst.Assets_DemoGame_GameRes_Codes_Hotfix_dll_bytes;
|
2024-07-12 13:33:45 +08:00
|
|
|
|
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
|
|
|
|
|
#if !UNITY_EDITOR
|
|
|
|
|
string hotfixDll = dllName;
|
|
|
|
|
var loadAssetAsync = await ResourcesComponent.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 ResourcesComponent.Instance.LoadAssetAsync<TextAsset>(dll);
|
|
|
|
|
this.aotDlls.Add(dll, loadAssetAsync);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await UniTask.Yield();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnStart(GameObject go, EPlayMode playMode)
|
|
|
|
|
{
|
|
|
|
|
foreach (var kv in this.aotDlls) // 先加载aot内容,避免报错
|
|
|
|
|
{
|
|
|
|
|
TextAsset textAsset = kv.Value;
|
2024-07-15 16:30:56 +08:00
|
|
|
|
// Debug.Log($":{kv.Key}. text: {textAsset.text}");
|
2024-07-12 13:33:45 +08:00
|
|
|
|
var errorCode = RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
|
|
|
|
|
Debug.Log($"LoadMetadataForAOTAssembly:{kv.Key}. ret: {errorCode}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 11:31:00 +08:00
|
|
|
|
string dllName = LoaderAssetConst.Assets_DemoGame_GameRes_Codes_Hotfix_dll_bytes;
|
2024-07-12 13:33:45 +08:00
|
|
|
|
// Editor环境下,HotUpdate.dll.bytes已经被自动加载,不需要加载,重复加载反而会出问题。
|
|
|
|
|
#if !UNITY_EDITOR
|
|
|
|
|
var dllAsset = this.dlls[dllName];
|
|
|
|
|
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("ZC.GameEntry");
|
|
|
|
|
|
2024-07-15 16:30:56 +08:00
|
|
|
|
this.Create();
|
|
|
|
|
|
2024-07-12 13:33:45 +08:00
|
|
|
|
object[] pa = new object[] { go, playMode };
|
|
|
|
|
type.GetMethod("Init").Invoke(null, pa);
|
|
|
|
|
}
|
2024-07-15 16:30:56 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建实例,防止引用缺失
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Create()
|
|
|
|
|
{
|
|
|
|
|
// ??
|
|
|
|
|
SystemInfo info = new SystemInfo();
|
|
|
|
|
//
|
|
|
|
|
Compass compass = new Compass();
|
|
|
|
|
Input input = new Input();
|
|
|
|
|
//
|
|
|
|
|
Debug.Log($"info : {info.GetType()}");
|
|
|
|
|
}
|
2024-07-12 13:33:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|