From c06ed340a255030be8480a7033c347a0c3f491bd Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 17 Sep 2020 23:08:26 +0200 Subject: [PATCH] Using the console block's material Progressed a lot --- GamecraftModdingAPI/Blocks/CustomBlock.cs | 46 +++++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/GamecraftModdingAPI/Blocks/CustomBlock.cs b/GamecraftModdingAPI/Blocks/CustomBlock.cs index 9801e40..bc67fe9 100644 --- a/GamecraftModdingAPI/Blocks/CustomBlock.cs +++ b/GamecraftModdingAPI/Blocks/CustomBlock.cs @@ -13,6 +13,7 @@ using RobocraftX.Common; using RobocraftX.Schedulers; using Svelto.Tasks; using Svelto.Tasks.ExtraLean; +using Unity.Entities; using UnityEngine; using UnityEngine.AddressableAssets; @@ -112,7 +113,7 @@ namespace GamecraftModdingAPI.Blocks cubeCategory = CubeCategory.ConsoleBlock, inventoryCategory = InventoryCategory.Logic, ID = 500, - Path = "Assets/Cube.prefab", //Index out of range exception: Asset failed to load (wrong path) + Path = "Assets/Prefabs/Cube.prefab", //Index out of range exception: Asset failed to load (wrong path) SpriteName = "CTR_CommandBlock", CubeNameKey = "strConsoleBlock", SelectableFaces = new[] {0, 1, 2, 3, 4, 5}, @@ -131,19 +132,51 @@ namespace GamecraftModdingAPI.Blocks [HarmonyPatch] public static class GOPatch { + private static Material[] materials; public static void Prefix(uint prefabID, GameObject gameObject) { Console.WriteLine("ID: " + prefabID + " - Name: " + gameObject.name); if (gameObject.name == "Cube") + { + //Console.WriteLine("Length: " + gameObject.GetComponentsInChildren().Length); + if (materials != null) + gameObject.GetComponentsInChildren()[0].sharedMaterials = materials; ECSGPUIResourceManager.Instance.RegisterRuntimePrefabs( - new[] {new PrefabData {prefabId = 500, prefabName = "Assets/Cube.prefab"}}, - new List {gameObject}, 1).Complete(); + new[] {new PrefabData {prefabId = 500, prefabName = "Assets/Prefabs/Cube.prefab"}}, + new List {gameObject}).Complete(); + GPUInstancerAPI.AddInstancesToPrefabPrototypeAtRuntime(ECSGPUIResourceManager.Instance.prefabManager, + gameObject.GetComponent().prefabPrototype, new[] {gameObject}); + Console.WriteLine("Registered prefab to instancer"); + + var register = AccessTools.Method("RobocraftX.Common.ECSPhysicResourceManager:RegisterPrefab", + new[] {typeof(uint), typeof(GameObject), typeof(World), typeof(BlobAssetStore)}); + register.Invoke(ECSPhysicResourceManager.Instance, + new object[] {prefabID, gameObject, MGPatch.data.Item1, MGPatch.data.Item2}); + Console.WriteLine("Registered prefab to physics"); + } + else if (gameObject.name == "CTR_CommandBlock") + materials = gameObject.GetComponentsInChildren()[0].sharedMaterials; } public static MethodBase TargetMethod() { return AccessTools.Method("RobocraftX.Common.ECSGPUIResourceManager:RegisterPrefab", - new[] {typeof(uint), typeof(GameObject), typeof(uint)}); + new[] {typeof(uint), typeof(GameObject)}); + } + } + + [HarmonyPatch] + public static class MGPatch + { + internal static (World, BlobAssetStore) data; + public static void Prefix(World physicsWorld, BlobAssetStore blobStore) + { + data = (physicsWorld, blobStore); + } + + public static MethodBase TargetMethod() + { + return AccessTools.Method("RobocraftX.CR.MainGame.MainGameCompositionRoot:Init"); } } @@ -154,6 +187,11 @@ namespace GamecraftModdingAPI.Blocks while (!res.IsDone) yield return Yield.It; Console.WriteLine("Loaded custom catalog: " + res.Result.LocatorId); Addressables.AddResourceLocator(res.Result); + /*Console.WriteLine("Loading Cube asset..."); + var loadTask = Addressables.LoadAssetAsync("Assets/Cube.prefab"); + while (!loadTask.IsDone) yield return Yield.It; + Console.WriteLine("Exception: "+loadTask.OperationException); + Console.WriteLine("Result: " + loadTask.Result.name);*/ } } } \ No newline at end of file