Fix ConcurrentModificationException and some attempts

This commit is contained in:
Norbi Peti 2020-10-22 02:34:59 +02:00
parent c06ed340a2
commit b0b496f22f
2 changed files with 122 additions and 52 deletions

View file

@ -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,7 +114,116 @@ namespace GamecraftModdingAPI.Blocks
/*var res = Addressables.LoadContentCatalogAsync("customCatalog.json");
while (!res.IsDone) yield return Yield.It;*/
}
public static MethodBase TargetMethod()
{ //General block registration
return AccessTools.Method("RobocraftX.Blocks.BlocksCompositionRoot:RegisterPartPrefabs");
}
}
[HarmonyPatch]
public static class RendererPatch
{
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<MeshRenderer>().Length);
if (materials != null)
gameObject.GetComponentsInChildren<MeshRenderer>()[0].sharedMaterials = materials;
ECSGPUIResourceManager.Instance.RegisterRuntimePrefabs(
new[] {new PrefabData {prefabId = prefabID, prefabName = "Assets/Prefabs/Cube.prefab"}},
new List<GameObject> {gameObject}).Complete();
GPUInstancerAPI.AddInstancesToPrefabPrototypeAtRuntime(ECSGPUIResourceManager.Instance.prefabManager,
gameObject.GetComponent<GPUInstancerPrefab>().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(
"Entity: " + ECSPhysicResourceManager.Instance.GetUECSPhysicEntityPrefab(prefabID));
Console.WriteLine("Prefab ID: " + PrefabsID.DBIDMAP[500]);
PhysicsCollider componentData = MGPatch.data.Item1.EntityManager.GetComponentData<PhysicsCollider>(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<BoxCollider>();
gameObject.AddComponent<Transform>();
Console.WriteLine("Registered prefab to physics");
}
else if (gameObject.name == "CTR_CommandBlock")
materials = gameObject.GetComponentsInChildren<MeshRenderer>()[0].sharedMaterials;
}
public static MethodBase TargetMethod()
{
return AccessTools.Method("RobocraftX.Common.ECSGPUIResourceManager:RegisterPrefab",
new[] {typeof(uint), typeof(GameObject)});
}
}
[HarmonyPatch]
public static class RMPatch
{
public static void Prefix(World physicsWorld,
GameObjectConversionSystem getExistingSystem,
FasterList<GameObject> gos,
List<PrefabData> 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<Entity> 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<PhysicsCollider>(primaryEntity));
while (entities.MoveNext())
{
Entity current = entities.Current;
Console.WriteLine("Entity " + current + " has collider: " +
entityManager.HasComponent<PhysicsCollider>(current));
}
}
}
public static MethodBase TargetMethod()
{
return AccessTools.Method("RobocraftX.Common.ECSResourceManagerUtility:RelinkEntities",
new[]
{
typeof(World),
typeof(GameObjectConversionSystem),
typeof(FasterList<GameObject>),
typeof(List<PrefabData>)
});
}
}
[HarmonyPatch]
public static class MGPatch
{
internal static (World, BlobAssetStore) data;
public static void Prefix(World physicsWorld, BlobAssetStore blobStore, IDataDB dataDB)
{
data = (physicsWorld, blobStore);
var blocks = dataDB.GetValues<CubeListData>();
blocks.Add("modded_ConsoleBlock", new CubeListData
{
cubeType = CubeType.Block,
@ -123,57 +240,6 @@ namespace GamecraftModdingAPI.Blocks
});
}
public static MethodBase TargetMethod()
{ //General block registration
return AccessTools.Method("RobocraftX.Blocks.BlocksCompositionRoot:RegisterPartPrefabs");
}
}
[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<MeshRenderer>().Length);
if (materials != null)
gameObject.GetComponentsInChildren<MeshRenderer>()[0].sharedMaterials = materials;
ECSGPUIResourceManager.Instance.RegisterRuntimePrefabs(
new[] {new PrefabData {prefabId = 500, prefabName = "Assets/Prefabs/Cube.prefab"}},
new List<GameObject> {gameObject}).Complete();
GPUInstancerAPI.AddInstancesToPrefabPrototypeAtRuntime(ECSGPUIResourceManager.Instance.prefabManager,
gameObject.GetComponent<GPUInstancerPrefab>().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<MeshRenderer>()[0].sharedMaterials;
}
public static MethodBase TargetMethod()
{
return AccessTools.Method("RobocraftX.Common.ECSGPUIResourceManager:RegisterPrefab",
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");
@ -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;

View file

@ -113,6 +113,10 @@
<HintPath>..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.GenericPhysicsBlocks.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.GenericPhysicsBlocks.dll</HintPath>
</Reference>
<Reference Include="Gamecraft.Blocks.LightBlock">
<HintPath>..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LightBlock.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LightBlock.dll</HintPath>
</Reference>
<Reference Include="Gamecraft.Blocks.LogicBlock">
<HintPath>..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LogicBlock.dll</HintPath>
<HintPath>..\..\ref\Gamecraft_Data\Managed\Gamecraft.Blocks.LogicBlock.dll</HintPath>