diff --git a/GamecraftModdingAPI/App/GameGameEngine.cs b/GamecraftModdingAPI/App/GameGameEngine.cs
index dea3821..85fb672 100644
--- a/GamecraftModdingAPI/App/GameGameEngine.cs
+++ b/GamecraftModdingAPI/App/GameGameEngine.cs
@@ -102,16 +102,27 @@ namespace GamecraftModdingAPI.App
if (filter == BlockIDs.Invalid)
{
foreach (var (blocks, _) in allBlocks)
- foreach (var block in blocks.ToBuffer().buffer.ToManagedArray())
- blockEGIDs.Add(block.ID);
+ {
+ var buffer = blocks.ToBuffer().buffer;
+ for (int i = 0; i < buffer.capacity; i++)
+ blockEGIDs.Add(buffer[i].ID);
+ }
+
return blockEGIDs.ToArray();
}
else
{
foreach (var (blocks, _) in allBlocks)
- foreach (var block in blocks.ToBuffer().buffer.ToManagedArray())
- if (block.DBID == (ulong) filter)
- blockEGIDs.Add(block.ID);
+ {
+ var array = blocks.ToBuffer().buffer;
+ for (var index = 0; index < array.capacity; index++)
+ {
+ var block = array[index];
+ if (block.DBID == (ulong) filter)
+ blockEGIDs.Add(block.ID);
+ }
+ }
+
return blockEGIDs.ToArray();
}
}
diff --git a/GamecraftModdingAPI/BlockGroup.cs b/GamecraftModdingAPI/BlockGroup.cs
index 2cd0f42..8d9e761 100644
--- a/GamecraftModdingAPI/BlockGroup.cs
+++ b/GamecraftModdingAPI/BlockGroup.cs
@@ -1,4 +1,7 @@
using Gamecraft.Blocks.BlockGroups;
+using Unity.Mathematics;
+using UnityEngine;
+
using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility;
@@ -11,23 +14,36 @@ namespace GamecraftModdingAPI
{
internal static BlueprintEngine _engine = new BlueprintEngine();
public int Id { get; }
- private Block _sourceBlock;
+ private readonly Block sourceBlock;
internal BlockGroup(int id, Block block)
{
if (id == BlockGroupUtility.GROUP_UNASSIGNED)
throw new BlockException("Cannot create a block group for blocks without a group!");
Id = id;
- _sourceBlock = block;
+ sourceBlock = block;
}
+
+ ///
+ /// The position of the block group. Calculated when GetBlocks() is used.
+ ///
+ public float3 Position { get; private set; }
+
+ ///
+ /// The rotation of the block group. Calculated when GetBlocks() is used.
+ ///
+ public float3 Rotation { get; private set; }
///
- /// Collects each block that is a part of this group.
+ /// Collects each block that is a part of this group. Also sets the position and rotation.
///
/// An array of blocks
public Block[] GetBlocks()
{
- return _engine.GetBlocksFromGroup(_sourceBlock.Id);
+ var ret = _engine.GetBlocksFromGroup(sourceBlock.Id, out var pos, out var rot);
+ Position = pos;
+ Rotation = ((Quaternion) rot).eulerAngles;
+ return ret;
}
///
diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs
index 5566525..9ce6d62 100644
--- a/GamecraftModdingAPI/Blocks/BlockEngine.cs
+++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs
@@ -12,9 +12,9 @@ using RobocraftX.Scene.Simulation;
using Svelto.DataStructures;
using Svelto.ECS;
using Svelto.ECS.Hybrid;
+using Unity.Mathematics;
using GamecraftModdingAPI.Engines;
-using Unity.Mathematics;
namespace GamecraftModdingAPI.Blocks
{
@@ -224,8 +224,10 @@ namespace GamecraftModdingAPI.Blocks
var bodies = new HashSet();
foreach (var (coll, _) in groups)
{
- foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
+ var array = coll.ToBuffer().buffer;
+ for (var index = 0; index < array.capacity; index++)
{
+ var conn = array[index];
if (conn.clusterId == cid)
bodies.Add(conn.machineRigidBodyId);
}
@@ -251,8 +253,11 @@ namespace GamecraftModdingAPI.Blocks
var groups = entitiesDB.QueryEntities();
foreach (var (coll, _) in groups)
{
- foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
- { //Static blocks don't have a cluster ID but the cluster destruction manager should have one
+ var array = coll.ToBuffer().buffer;
+ for (var index = 0; index < array.capacity; index++)
+ {
+ var conn = array[index];
+ //Static blocks don't have a cluster ID but the cluster destruction manager should have one
if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
return new Cluster(conn.clusterId);
}
@@ -267,8 +272,10 @@ namespace GamecraftModdingAPI.Blocks
var set = new HashSet();
foreach (var (coll, _) in groups)
{
- foreach (var conn in coll.ToBuffer().buffer.ToManagedArray())
+ var array = coll.ToBuffer().buffer;
+ for (var index = 0; index < array.capacity; index++)
{
+ var conn = array[index];
if (conn.machineRigidBodyId == sbid)
set.Add(new Block(conn.ID));
}
diff --git a/GamecraftModdingAPI/Blocks/BlueprintEngine.cs b/GamecraftModdingAPI/Blocks/BlueprintEngine.cs
index f2f0bef..4104ae6 100644
--- a/GamecraftModdingAPI/Blocks/BlueprintEngine.cs
+++ b/GamecraftModdingAPI/Blocks/BlueprintEngine.cs
@@ -17,6 +17,7 @@ using Svelto.ECS.Serialization;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
+using Allocator = Svelto.Common.Allocator;
namespace GamecraftModdingAPI.Blocks
{
@@ -24,8 +25,9 @@ namespace GamecraftModdingAPI.Blocks
{
private readonly MethodInfo getBlocksFromGroup =
AccessTools.Method("RobocraftX.CR.MachineEditing.PlaceBlockUtility:GetBlocksSharingBlockgroup");
- private readonly NativeDynamicArray selectedBlocksInGroup = new NativeDynamicArray();
- private readonly NativeHashSet removedConnections = new NativeHashSet();
+
+ private NativeDynamicArray selectedBlocksInGroup;
+ private NativeHashSet removedConnections = new NativeHashSet();
private static readonly Type PlaceBlueprintUtilityType =
AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlueprintUtility");
@@ -44,22 +46,29 @@ namespace GamecraftModdingAPI.Blocks
public void Ready()
{
+ selectedBlocksInGroup = NativeDynamicArray.Alloc(Allocator.Persistent);
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
+ selectedBlocksInGroup.Dispose();
}
- public Block[] GetBlocksFromGroup(EGID blockID)
+ public Block[] GetBlocksFromGroup(EGID blockID, out float3 pos, out quaternion rot)
{
- var list = new FasterList();
- object blockPos = null, blockRot = null;
- getBlocksFromGroup.Invoke(null, new[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot});
- for (uint i = 0; i < selectedBlocksInGroup.Count(); i++)
- list.Add(new Block(selectedBlocksInGroup.Get(i)));
+ var blockPos = default(float3);
+ var blockRot = default(quaternion);
+ var parameters = new object[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot};
+ getBlocksFromGroup.Invoke(null, parameters);
+ pos = (float3) parameters[3];
+ rot = (quaternion) parameters[4];
+ int count = selectedBlocksInGroup.Count();
+ var ret = new Block[count];
+ for (uint i = 0; i < count; i++)
+ ret[i] = new Block(selectedBlocksInGroup.Get(i));
selectedBlocksInGroup.FastClear();
- return list.ToArray();
+ return ret;
}
public void RemoveBlockGroup(int id)
@@ -195,7 +204,7 @@ namespace GamecraftModdingAPI.Blocks
public static MethodBase TargetMethod()
{
- return AccessTools.Constructor(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.RemoveBlockEngine"));
+ return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.RemoveBlockEngine"))[0];
}
}
@@ -213,7 +222,7 @@ namespace GamecraftModdingAPI.Blocks
public static MethodBase TargetMethod()
{
- return AccessTools.Constructor(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.SelectBlockEngine"));
+ return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.SelectBlockEngine"))[0];
}
}
}