From b0b496f22f40d0ff4a841cb77de2062c4072b58c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 22 Oct 2020 02:34:59 +0200 Subject: [PATCH] Fix ConcurrentModificationException and some attempts --- GamecraftModdingAPI/Blocks/CustomBlock.cs | 108 ++++++++++++++---- .../GamecraftModdingAPI.csproj | 4 + 2 files changed, 91 insertions(+), 21 deletions(-) diff --git a/GamecraftModdingAPI/Blocks/CustomBlock.cs b/GamecraftModdingAPI/Blocks/CustomBlock.cs index bc67fe9..b960101 100644 --- a/GamecraftModdingAPI/Blocks/CustomBlock.cs +++ b/GamecraftModdingAPI/Blocks/CustomBlock.cs @@ -9,13 +9,21 @@ using DataLoader; using GamecraftModdingAPI.Utility; using GPUInstancer; using HarmonyLib; +using RobocraftX.Blocks.Ghost; using RobocraftX.Common; using RobocraftX.Schedulers; +using Svelto.DataStructures; +using Svelto.ECS.Extensions.Unity; using Svelto.Tasks; using Svelto.Tasks.ExtraLean; using Unity.Entities; +using Unity.Entities.Conversion; +using Unity.Physics; using UnityEngine; using UnityEngine.AddressableAssets; +using BoxCollider = UnityEngine.BoxCollider; +using Collider = UnityEngine.Collider; +using Material = UnityEngine.Material; namespace GamecraftModdingAPI.Blocks { @@ -106,21 +114,6 @@ namespace GamecraftModdingAPI.Blocks /*var res = Addressables.LoadContentCatalogAsync("customCatalog.json"); while (!res.IsDone) yield return Yield.It;*/ - - blocks.Add("modded_ConsoleBlock", new CubeListData - { - cubeType = CubeType.Block, - cubeCategory = CubeCategory.ConsoleBlock, - inventoryCategory = InventoryCategory.Logic, - ID = 500, - 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}, - GridScale = new[] {1, 1, 1}, - Mass = 1, - JointBreakAngle = 1 - }); } public static MethodBase TargetMethod() @@ -130,7 +123,7 @@ namespace GamecraftModdingAPI.Blocks } [HarmonyPatch] - public static class GOPatch + public static class RendererPatch { private static Material[] materials; public static void Prefix(uint prefabID, GameObject gameObject) @@ -142,16 +135,30 @@ namespace GamecraftModdingAPI.Blocks if (materials != null) gameObject.GetComponentsInChildren()[0].sharedMaterials = materials; ECSGPUIResourceManager.Instance.RegisterRuntimePrefabs( - new[] {new PrefabData {prefabId = 500, prefabName = "Assets/Prefabs/Cube.prefab"}}, + new[] {new PrefabData {prefabId = prefabID, 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", + /*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}); + new object[] {prefabID, gameObject, MGPatch.data.Item1, MGPatch.data.Item2});*/ + /*Console.WriteLine( + "Entity: " + ECSPhysicResourceManager.Instance.GetUECSPhysicEntityPrefab(prefabID)); + Console.WriteLine("Prefab ID: " + PrefabsID.DBIDMAP[500]); + PhysicsCollider componentData = MGPatch.data.Item1.EntityManager.GetComponentData(ECSPhysicResourceManager.Instance.GetUECSPhysicEntityPrefab(prefabID)); + Console.WriteLine("Collider valid: " + componentData.IsValid); + unsafe + { + Console.WriteLine("Collider type: " + componentData.ColliderPtr->Type); + CollisionFilter filter = componentData.Value.Value.Filter; + Console.WriteLine("Filter not empty: " + !filter.IsEmpty); + }*/ + //MGPatch.data.Item1.EntityManager.GetComponentData<>() + gameObject.AddComponent(); + gameObject.AddComponent(); Console.WriteLine("Registered prefab to physics"); } else if (gameObject.name == "CTR_CommandBlock") @@ -165,13 +172,72 @@ namespace GamecraftModdingAPI.Blocks } } + [HarmonyPatch] + public static class RMPatch + { + public static void Prefix(World physicsWorld, + GameObjectConversionSystem getExistingSystem, + FasterList gos, + List prefabData) + { + for (var index = 0; index < prefabData.Count; index++) + { + var data = prefabData[index]; + if (!data.prefabName.EndsWith("Cube.prefab")) continue; + //getExistingSystem.DeclareLinkedEntityGroup(gos[index]); + /*Entity entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(gos[index], + GameObjectConversionSettings.FromWorld(physicsWorld, MGPatch.data.Item2));*/ + Console.WriteLine("Transform: " + gos[index].transform.childCount); + Entity primaryEntity = getExistingSystem.GetPrimaryEntity(gos[index]); + MultiListEnumerator entities = getExistingSystem.GetEntities(gos[index]); + Console.WriteLine("ID: " + data.prefabId + " - Name: " + data.prefabName); + Console.WriteLine("Primary entity: " + primaryEntity); + EntityManager entityManager = physicsWorld.EntityManager; + Console.WriteLine("Has collider: " + entityManager.HasComponent(primaryEntity)); + while (entities.MoveNext()) + { + Entity current = entities.Current; + Console.WriteLine("Entity " + current + " has collider: " + + entityManager.HasComponent(current)); + } + } + } + + public static MethodBase TargetMethod() + { + return AccessTools.Method("RobocraftX.Common.ECSResourceManagerUtility:RelinkEntities", + new[] + { + typeof(World), + typeof(GameObjectConversionSystem), + typeof(FasterList), + typeof(List) + }); + } + } + [HarmonyPatch] public static class MGPatch { internal static (World, BlobAssetStore) data; - public static void Prefix(World physicsWorld, BlobAssetStore blobStore) + public static void Prefix(World physicsWorld, BlobAssetStore blobStore, IDataDB dataDB) { data = (physicsWorld, blobStore); + var blocks = dataDB.GetValues(); + blocks.Add("modded_ConsoleBlock", new CubeListData + { + cubeType = CubeType.Block, + cubeCategory = CubeCategory.ConsoleBlock, + inventoryCategory = InventoryCategory.Logic, + ID = 500, + 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}, + GridScale = new[] {1, 1, 1}, + Mass = 1, + JointBreakAngle = 1 + }); } public static MethodBase TargetMethod() @@ -181,7 +247,7 @@ namespace GamecraftModdingAPI.Blocks } public static IEnumerator Prep() - { + { //TODO: Don't let the game load until this finishes Console.WriteLine("Loading custom catalog..."); var res = Addressables.LoadContentCatalogAsync("customCatalog.json"); while (!res.IsDone) yield return Yield.It; diff --git a/GamecraftModdingAPI/GamecraftModdingAPI.csproj b/GamecraftModdingAPI/GamecraftModdingAPI.csproj index 8be57a7..ee28a68 100644 --- a/GamecraftModdingAPI/GamecraftModdingAPI.csproj +++ b/GamecraftModdingAPI/GamecraftModdingAPI.csproj @@ -113,6 +113,10 @@ ..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.GenericPhysicsBlocks.dll ..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.GenericPhysicsBlocks.dll + + ..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LightBlock.dll + ..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LightBlock.dll + ..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LogicBlock.dll ..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LogicBlock.dll