Replace ToManagedArray() and fix getting blocks from group

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

View file

@ -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();
}
}

View file

@ -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;
}
/// <summary>
/// Collects each block that is a part of this group.
/// 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>
/// Collects each block that is a part of this group. Also sets the position and rotation.
/// </summary>
/// <returns>An array of blocks</returns>
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>

View file

@ -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<uint>();
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<GridConnectionsEntityStruct>();
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<Block>();
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));
}

View file

@ -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<ulong> removedConnections = new NativeHashSet<ulong>();
private NativeDynamicArray selectedBlocksInGroup;
private NativeHashSet<ulong> removedConnections = new NativeHashSet<ulong>();
private static readonly Type PlaceBlueprintUtilityType =
AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlueprintUtility");
@ -44,22 +46,29 @@ namespace GamecraftModdingAPI.Blocks
public void Ready()
{
selectedBlocksInGroup = NativeDynamicArray.Alloc<EGID>(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<Block>();
object blockPos = null, blockRot = null;
getBlocksFromGroup.Invoke(null, new[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot});
for (uint i = 0; i < selectedBlocksInGroup.Count<EGID>(); i++)
list.Add(new Block(selectedBlocksInGroup.Get<EGID>(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<EGID>();
var ret = new Block[count];
for (uint i = 0; i < count; i++)
ret[i] = new Block(selectedBlocksInGroup.Get<EGID>(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];
}
}
}