Skip to content

Commit

Permalink
Patch AddressablesUtility.InstantiateAsync for mod prefabs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPurple6411 committed Dec 21, 2022
1 parent 68deae3 commit aee54c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions SMLHelper/Patchers/PrefabDatabasePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace SMLHelper.V2.Patchers
using UWE;
using InternalLogger = InternalLogger;
using SMLHelper.V2.Utility;
using static SMLHelper.V2.PatchUtils;
using static TechStringCache;
using UnityEngine.ResourceManagement.AsyncOperations;

internal static class PrefabDatabasePatcher
{
Expand Down Expand Up @@ -99,6 +102,29 @@ internal static bool GetPrefabAsync_Prefix(ref IPrefabRequest __result, string c
return __result == null;
}

[PatchUtils.Prefix]
[HarmonyPatch(typeof(AddressablesUtility), nameof(AddressablesUtility.InstantiateAsync), new Type[]
{
typeof(string), typeof(IOut<GameObject>), typeof(Transform), typeof(Vector3), typeof(Quaternion), typeof(bool)
})]
internal static bool InstantiateAsync_Prefix(ref IEnumerator __result,string key, IOut<GameObject> result, Transform parent, Vector3 position, Quaternion rotation, bool awake)
{
if(!ModPrefab.TryGetFromFileName(key, out var prefab))
return true;

__result = InstantiateAsync(prefab, result, parent, position, rotation, awake);
return false;
}

internal static IEnumerator InstantiateAsync(ModPrefab modPrefab, IOut<GameObject> result, Transform parent, Vector3 position, Quaternion rotation, bool awake)
{
TaskResult<GameObject> task = new TaskResult<GameObject>();
yield return modPrefab.GetGameObjectAsync(task);

var prefab = task.Get();
result.Set(GameObject.Instantiate(prefab, parent, position, rotation, awake));
yield break;
}

// transpiler for ProtobufSerializer.DeserializeObjectsAsync
private static IEnumerable<CodeInstruction> DeserializeObjectsAsync_Transpiler(IEnumerable<CodeInstruction> cins)
Expand Down
8 changes: 8 additions & 0 deletions SMLHelper/SMLHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
<HintPath>$(Dependencies)\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.Addressables">
<HintPath>..\Dependencies\Subnautica.stable\Unity.Addressables.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.ResourceManager">
<HintPath>..\Dependencies\Subnautica.stable\Unity.ResourceManager.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>$(Dependencies)\UnityEngine.dll</HintPath>
<Private>False</Private>
Expand Down

0 comments on commit aee54c2

Please sign in to comment.