Replace ToManagedArray() and fix getting blocks from group

This commit is contained in:
Norbi Peti 2020-11-10 23:08:27 +01:00
parent 1c4e2a0db2
commit 3dd61b5e4f
4 changed files with 68 additions and 25 deletions

View file

@ -102,16 +102,27 @@ namespace GamecraftModdingAPI.App
if (filter == BlockIDs.Invalid) if (filter == BlockIDs.Invalid)
{ {
foreach (var (blocks, _) in allBlocks) 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(); return blockEGIDs.ToArray();
} }
else else
{ {
foreach (var (blocks, _) in allBlocks) foreach (var (blocks, _) in allBlocks)
foreach (var block in blocks.ToBuffer().buffer.ToManagedArray()) {
if (block.DBID == (ulong) filter) var array = blocks.ToBuffer().buffer;
blockEGIDs.Add(block.ID); for (var index = 0; index < array.capacity; index++)
{
var block = array[index];
if (block.DBID == (ulong) filter)
blockEGIDs.Add(block.ID);
}
}
return blockEGIDs.ToArray(); return blockEGIDs.ToArray();
} }
} }

View file

@ -1,4 +1,7 @@
using Gamecraft.Blocks.BlockGroups; using Gamecraft.Blocks.BlockGroups;
using Unity.Mathematics;
using UnityEngine;
using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
@ -11,23 +14,36 @@ namespace GamecraftModdingAPI
{ {
internal static BlueprintEngine _engine = new BlueprintEngine(); internal static BlueprintEngine _engine = new BlueprintEngine();
public int Id { get; } public int Id { get; }
private Block _sourceBlock; private readonly Block sourceBlock;
internal BlockGroup(int id, Block block) internal BlockGroup(int id, Block block)
{ {
if (id == BlockGroupUtility.GROUP_UNASSIGNED) if (id == BlockGroupUtility.GROUP_UNASSIGNED)
throw new BlockException("Cannot create a block group for blocks without a group!"); throw new BlockException("Cannot create a block group for blocks without a group!");
Id = id; Id = id;
_sourceBlock = block; sourceBlock = block;
} }
/// <summary>
/// The position of the block group. Calculated when GetBlocks() is used.
/// </summary>
public float3 Position { get; private set; }
/// <summary>
/// The rotation of the block group. Calculated when GetBlocks() is used.
/// </summary>
public float3 Rotation { get; private set; }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <returns>An array of blocks</returns> /// <returns>An array of blocks</returns>
public Block[] GetBlocks() 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;
} }
/// <summary> /// <summary>

View file

@ -12,9 +12,9 @@ using RobocraftX.Scene.Simulation;
using Svelto.DataStructures; using Svelto.DataStructures;
using Svelto.ECS; using Svelto.ECS;
using Svelto.ECS.Hybrid; using Svelto.ECS.Hybrid;
using Unity.Mathematics;
using GamecraftModdingAPI.Engines; using GamecraftModdingAPI.Engines;
using Unity.Mathematics;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -224,8 +224,10 @@ namespace GamecraftModdingAPI.Blocks
var bodies = new HashSet<uint>(); var bodies = new HashSet<uint>();
foreach (var (coll, _) in groups) 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) if (conn.clusterId == cid)
bodies.Add(conn.machineRigidBodyId); bodies.Add(conn.machineRigidBodyId);
} }
@ -251,8 +253,11 @@ namespace GamecraftModdingAPI.Blocks
var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>(); var groups = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
foreach (var (coll, _) in groups) foreach (var (coll, _) in groups)
{ {
foreach (var conn in coll.ToBuffer().buffer.ToManagedArray()) var array = coll.ToBuffer().buffer;
{ //Static blocks don't have a cluster ID but the cluster destruction manager should have one 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) if (conn.machineRigidBodyId == sbid && conn.clusterId != uint.MaxValue)
return new Cluster(conn.clusterId); return new Cluster(conn.clusterId);
} }
@ -267,8 +272,10 @@ namespace GamecraftModdingAPI.Blocks
var set = new HashSet<Block>(); var set = new HashSet<Block>();
foreach (var (coll, _) in groups) 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) if (conn.machineRigidBodyId == sbid)
set.Add(new Block(conn.ID)); set.Add(new Block(conn.ID));
} }

View file

@ -17,6 +17,7 @@ using Svelto.ECS.Serialization;
using Unity.Collections; using Unity.Collections;
using Unity.Mathematics; using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using Allocator = Svelto.Common.Allocator;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -24,8 +25,9 @@ namespace GamecraftModdingAPI.Blocks
{ {
private readonly MethodInfo getBlocksFromGroup = private readonly MethodInfo getBlocksFromGroup =
AccessTools.Method("RobocraftX.CR.MachineEditing.PlaceBlockUtility:GetBlocksSharingBlockgroup"); AccessTools.Method("RobocraftX.CR.MachineEditing.PlaceBlockUtility:GetBlocksSharingBlockgroup");
private readonly NativeDynamicArray selectedBlocksInGroup = new NativeDynamicArray();
private readonly NativeHashSet<ulong> removedConnections = new NativeHashSet<ulong>(); private NativeDynamicArray selectedBlocksInGroup;
private NativeHashSet<ulong> removedConnections = new NativeHashSet<ulong>();
private static readonly Type PlaceBlueprintUtilityType = private static readonly Type PlaceBlueprintUtilityType =
AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlueprintUtility"); AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlueprintUtility");
@ -44,22 +46,29 @@ namespace GamecraftModdingAPI.Blocks
public void Ready() public void Ready()
{ {
selectedBlocksInGroup = NativeDynamicArray.Alloc<EGID>(Allocator.Persistent);
} }
public EntitiesDB entitiesDB { get; set; } public EntitiesDB entitiesDB { get; set; }
public void Dispose() 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<Block>(); var blockPos = default(float3);
object blockPos = null, blockRot = null; var blockRot = default(quaternion);
getBlocksFromGroup.Invoke(null, new[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot}); var parameters = new object[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot};
for (uint i = 0; i < selectedBlocksInGroup.Count<EGID>(); i++) getBlocksFromGroup.Invoke(null, parameters);
list.Add(new Block(selectedBlocksInGroup.Get<EGID>(i))); pos = (float3) parameters[3];
rot = (quaternion) parameters[4];
int count = selectedBlocksInGroup.Count<EGID>();
var ret = new Block[count];
for (uint i = 0; i < count; i++)
ret[i] = new Block(selectedBlocksInGroup.Get<EGID>(i));
selectedBlocksInGroup.FastClear(); selectedBlocksInGroup.FastClear();
return list.ToArray(); return ret;
} }
public void RemoveBlockGroup(int id) public void RemoveBlockGroup(int id)
@ -195,7 +204,7 @@ namespace GamecraftModdingAPI.Blocks
public static MethodBase TargetMethod() 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() public static MethodBase TargetMethod()
{ {
return AccessTools.Constructor(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.SelectBlockEngine")); return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.SelectBlockEngine"))[0];
} }
} }
} }