Skip to content

Commit ddf3950

Browse files
committed
fixed the issue where loading a non-existent DLL causes a Reference error
1 parent 647e838 commit ddf3950

1 file changed

Lines changed: 33 additions & 24 deletions

File tree

Assets/OxGFrame/Hotfixer/Scripts/Runtime/Hotfix/HotfixFsm/HotfixFsmStates.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,24 @@ private async UniTask _LoadAOTAssemblies()
351351
foreach (var dllName in aotMetaAssemblyFiles)
352352
{
353353
var dll = await AssetLoaders.LoadAssetAsync<TextAsset>(HotfixManager.GetInstance().packageName, dllName);
354-
// Get bytes 只能在 main thread 進行 (Unity TextAsset 的坑)
355-
byte[] binary = dll.bytes;
354+
if (dll != null)
355+
{
356+
// Get bytes 只能在 main thread 進行 (Unity TextAsset 的坑)
357+
byte[] binary = dll.bytes;
356358
#if !UNITY_WEBGL
357-
// 切換至其他線程
358-
await UniTask.SwitchToThreadPool();
359+
// 切換至其他線程
360+
await UniTask.SwitchToThreadPool();
359361
#endif
360-
// 加載 assembly 對應的 dll, 會自動為它 hook, 一旦 aot 泛型函數的 native 函數不存在, 用解釋器版本代碼
361-
LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(binary, mode);
362+
// 加載 assembly 對應的 dll, 會自動為它 hook, 一旦 aot 泛型函數的 native 函數不存在, 用解釋器版本代碼
363+
LoadImageErrorCode err = RuntimeApi.LoadMetadataForAOTAssembly(binary, mode);
362364
#if !UNITY_WEBGL
363-
// 切回至主線程
364-
await UniTask.SwitchToMainThread();
365+
// 切回至主線程
366+
await UniTask.SwitchToMainThread();
365367
#endif
366-
// Unload after load
367-
await AssetLoaders.UnloadAsset(dllName);
368-
Logging.Print<Logger>($"<color=#32fff5>Load <color=#ffde4c>AOT Assembly</color>: <color=#e2b3ff>{dllName}</color>, mode: {mode}, ret: {err}</color>");
368+
// Unload after load
369+
await AssetLoaders.UnloadAsset(dllName);
370+
Logging.Print<Logger>($"<color=#32fff5>Load <color=#ffde4c>AOT Assembly</color>: <color=#e2b3ff>{dllName}</color>, mode: {mode}, ret: {err}</color>");
371+
}
369372
}
370373
}
371374
}
@@ -417,7 +420,7 @@ private async UniTask _LoadHotfixAssemblies()
417420
{
418421
foreach (var dllName in hotfixAssemblyFiles)
419422
{
420-
Assembly hotfixAsm;
423+
Assembly hotfixAsm = null;
421424
if (Application.isEditor ||
422425
BundleConfig.playMode == BundleConfig.PlayMode.EditorSimulateMode ||
423426
HotfixManager.GetInstance().IsDisabled())
@@ -432,24 +435,30 @@ private async UniTask _LoadHotfixAssemblies()
432435
else
433436
{
434437
var dll = await AssetLoaders.LoadAssetAsync<TextAsset>(HotfixManager.GetInstance().packageName, dllName);
435-
// Get bytes 只能在 main thread 進行 (Unity TextAsset 的坑)
436-
byte[] binary = dll.bytes;
438+
if (dll != null)
439+
{
440+
// Get bytes 只能在 main thread 進行 (Unity TextAsset 的坑)
441+
byte[] binary = dll.bytes;
437442
#if !UNITY_WEBGL
438-
// 切換至其他線程
439-
await UniTask.SwitchToThreadPool();
443+
// 切換至其他線程
444+
await UniTask.SwitchToThreadPool();
440445
#endif
441-
// 加載熱更 dlls
442-
hotfixAsm = Assembly.Load(binary);
446+
// 加載熱更 dlls
447+
hotfixAsm = Assembly.Load(binary);
443448
#if !UNITY_WEBGL
444-
// 切回至主線程
445-
await UniTask.SwitchToMainThread();
449+
// 切回至主線程
450+
await UniTask.SwitchToMainThread();
446451
#endif
447-
// Unload after load
448-
await AssetLoaders.UnloadAsset(dllName);
452+
// Unload after load
453+
await AssetLoaders.UnloadAsset(dllName);
454+
}
449455
}
450456

451-
HotfixManager.GetInstance().AddHotfixAssembly(dllName, hotfixAsm);
452-
Logging.Print<Logger>($"<color=#32fff5>Load <color=#ffde4c>Hotfix Assembly</color>: <color=#e2b3ff>{dllName}</color></color>");
457+
if (hotfixAsm != null)
458+
{
459+
HotfixManager.GetInstance().AddHotfixAssembly(dllName, hotfixAsm);
460+
Logging.Print<Logger>($"<color=#32fff5>Load <color=#ffde4c>Hotfix Assembly</color>: <color=#e2b3ff>{dllName}</color></color>");
461+
}
453462
}
454463
}
455464
}

0 commit comments

Comments
 (0)