From fad3b5cbf4abb7aa7a714751de57b1cb242fc8bc Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 14 Nov 2020 22:43:45 +0100 Subject: [PATCH] Fix picking block groups... --- GamecraftModdingAPI/Block.cs | 5 +- GamecraftModdingAPI/BlockGroup.cs | 12 ++- GamecraftModdingAPI/Blocks/BlueprintEngine.cs | 55 ++++++++----- GamecraftModdingAPI/Blocks/PlacementEngine.cs | 2 +- .../GamecraftModdingAPI.csproj | 2 +- .../Tests/GamecraftModdingAPIPluginTest.cs | 82 ------------------- doxygen.conf | 2 +- 7 files changed, 48 insertions(+), 112 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 0fecf40..d0ff657 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -365,7 +365,7 @@ namespace GamecraftModdingAPI private BlockGroup blockGroup; /// - /// Returns the block group this block is a part of. Block groups can be placed using blueprints. + /// Returns the block group this block is a part of. Block groups can also be placed using blueprints. /// Returns null if not part of a group.
/// Setting the group after the block has been initialized will not update everything properly, /// so you can only set this property on blocks newly placed by your code.
@@ -385,9 +385,6 @@ namespace GamecraftModdingAPI { if (Exists) { - /*var copy = Copy(); - copy.BlockGroup = value; //It won't run this on the new instance as it won't 'exist' yet - Remove();*/ Logging.LogWarning("Attempted to set group of existing block. This is not supported." + " Copy the block and set the group of the resulting block."); return; diff --git a/GamecraftModdingAPI/BlockGroup.cs b/GamecraftModdingAPI/BlockGroup.cs index 6c600b2..c1add9a 100644 --- a/GamecraftModdingAPI/BlockGroup.cs +++ b/GamecraftModdingAPI/BlockGroup.cs @@ -55,7 +55,7 @@ namespace GamecraftModdingAPI } /// - /// The position of the block group (center). Recalculated if blocks have been added/removed since the last query. + /// The position of the block group (center). Can only be used after initialization is complete. /// public float3 Position { @@ -78,7 +78,7 @@ namespace GamecraftModdingAPI } /// - /// The rotation of the block group. Recalculated if blocks have been added/removed since the last query. + /// The rotation of the block group. Can only be used after initialization is complete. /// public float3 Rotation { @@ -121,7 +121,7 @@ namespace GamecraftModdingAPI /// A new block group containing the given block public static BlockGroup Create(Block block) { - var bg = new BlockGroup(_engine.CreateBlockGroup(default, default), block); + var bg = new BlockGroup(_engine.CreateBlockGroup(block.Position, Quaternion.Euler(block.Rotation)), block); block.BlockGroup = bg; return bg; } @@ -166,7 +166,11 @@ namespace GamecraftModdingAPI item.BlockGroup = this; //Calls AddInternal } - internal void AddInternal(Block item) => blocks.Add(item); + internal void AddInternal(Block item) + { + blocks.Add(item); + _engine.AddBlockToGroup(item.Id, Id); + } /// /// Removes all blocks from this group. diff --git a/GamecraftModdingAPI/Blocks/BlueprintEngine.cs b/GamecraftModdingAPI/Blocks/BlueprintEngine.cs index ff22a4c..ddfb051 100644 --- a/GamecraftModdingAPI/Blocks/BlueprintEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlueprintEngine.cs @@ -29,7 +29,8 @@ namespace GamecraftModdingAPI.Blocks private NativeDynamicArray selectedBlocksInGroup; private NativeHashSet removedConnections = new NativeHashSet(); - + private int addingToBlockGroup = -1; + private static readonly Type PlaceBlueprintUtilityType = AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlueprintUtility"); private static readonly FieldInfo LocalBlockMap = @@ -50,13 +51,14 @@ namespace GamecraftModdingAPI.Blocks private static FasterList globalBlockMap; private static object SerializeGhostBlueprintInstance; private static GhostChildEntityFactory BuildGhostBlueprintFactory; - + public void Ready() { selectedBlocksInGroup = NativeDynamicArray.Alloc(Allocator.Persistent); } public EntitiesDB entitiesDB { get; set; } + public void Dispose() { selectedBlocksInGroup.Dispose(); @@ -96,6 +98,20 @@ namespace GamecraftModdingAPI.Blocks return nextFilterId; } + public void AddBlockToGroup(EGID blockID, int groupID) + { + if (globalBlockMap == null) + globalBlockMap = FullGameFields._deserialisedBlockMap; + if (groupID != addingToBlockGroup) + { + Logging.MetaDebugLog("Changing current block group from " + addingToBlockGroup + " to " + groupID); + addingToBlockGroup = groupID; + globalBlockMap.Clear(); + } + + globalBlockMap.Add(blockID); + } + public void SelectBlueprint(uint resourceID) { if (resourceID == uint.MaxValue) @@ -109,7 +125,7 @@ namespace GamecraftModdingAPI.Blocks uint index = clipboardManager.AllocateSerializationData(); return index; } - + public void ReplaceBlueprint(uint playerID, uint blueprintID, ICollection selected, float3 pos, quaternion rot) { var blockIDs = new EGID[selected.Count]; @@ -131,14 +147,14 @@ namespace GamecraftModdingAPI.Blocks //float3 bottomOffset = PlaceBlockUtility.GetBottomOffset(collider); //var rootPosition = math.mul(groupTransform.blockGroupGridRotation, bottomOffset) + groupTransform.blockGroupGridPosition; //var rootRotation = groupTransform.blockGroupGridRotation; - + clipboardManager.SetGhostSerialized(blueprintID, false); SelectionSerializationUtility.CopySelectionToClipboard(playerID, entitiesDB, serializationData.blueprintData, entitySerialization, entityFactory, blockIDs, (uint) blockIDs.Length, pos, rot, -1); BuildGhostBlueprint(selected, pos, rot, playerID); SerializeGhostBlueprint.Invoke(SerializeGhostBlueprintInstance, new object[] {playerID, blueprintID}); - + } private void BuildGhostBlueprint(ICollection blocks, float3 pos, quaternion rot, uint playerID) @@ -173,11 +189,12 @@ namespace GamecraftModdingAPI.Blocks } } int nextFilterId1 = BlockGroupUtility.NextFilterId; - entityFactory.BuildEntity(new EGID((uint) nextFilterId1, BlockGroupExclusiveGroups.BlockGroupEntityGroup)).Init(new BlockGroupTransformEntityComponent - { - blockGroupGridPosition = selectionPosition.position, - blockGroupGridRotation = selectionRotation.rotation - }); + entityFactory.BuildEntity(new EGID((uint) nextFilterId1, + BlockGroupExclusiveGroups.BlockGroupEntityGroup)).Init(new BlockGroupTransformEntityComponent + { + blockGroupGridPosition = selectionPosition.position, + blockGroupGridRotation = selectionRotation.rotation + }); var frot = Quaternion.Euler(rot); var grid = new GridRotationStruct {position = pos, rotation = frot}; var poss = new PositionEntityStruct {position = pos}; @@ -284,17 +301,17 @@ namespace GamecraftModdingAPI.Blocks } [HarmonyPatch] - private static class BuildGhostBlueprintPatch + private static class BuildGhostBlueprintPatch + { + public static void Postfix(GhostChildEntityFactory ghostChildEntityFactory) { - public static void Postfix(GhostChildEntityFactory ghostChildEntityFactory) - { - BuildGhostBlueprintFactory = ghostChildEntityFactory; - } + BuildGhostBlueprintFactory = ghostChildEntityFactory; + } - public static MethodBase TargetMethod() - {RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine - return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine"))[0]; - } + public static MethodBase TargetMethod() + { + return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine"))[0]; + } } public IEntityFactory Factory { get; set; } diff --git a/GamecraftModdingAPI/Blocks/PlacementEngine.cs b/GamecraftModdingAPI/Blocks/PlacementEngine.cs index fab9c62..b0d1365 100644 --- a/GamecraftModdingAPI/Blocks/PlacementEngine.cs +++ b/GamecraftModdingAPI/Blocks/PlacementEngine.cs @@ -107,7 +107,7 @@ namespace GamecraftModdingAPI.Blocks belongsTo = 32U, collidesWith = 239532U });*/ - + PrimaryRotationUtility.InitialisePrimaryDirection(rotation.rotation, ref structInitializer); EGID playerEGID = new EGID(playerId, CharacterExclusiveGroups.OnFootGroup); ref PickedBlockExtraDataStruct pickedBlock = ref entitiesDB.QueryEntity(playerEGID); diff --git a/GamecraftModdingAPI/GamecraftModdingAPI.csproj b/GamecraftModdingAPI/GamecraftModdingAPI.csproj index 88002d0..3dabe64 100644 --- a/GamecraftModdingAPI/GamecraftModdingAPI.csproj +++ b/GamecraftModdingAPI/GamecraftModdingAPI.csproj @@ -2,7 +2,7 @@ net472 true - 1.8.0 + 1.7.0 Exmods GNU General Public Licence 3+ https://git.exmods.org/modtainers/GamecraftModdingAPI diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index fb806ea..81a814c 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -410,88 +410,6 @@ namespace GamecraftModdingAPI.Tests return ((Action) MinimumSpecsCheck.CheckRequirementsMet).Method; } } - - [HarmonyPatch] - public class BugHuntPatch - { - public static MethodInfo method = - SymbolExtensions.GetMethodInfo(str => Console.WriteLine(str)); - - public static IEnumerable Transpiler(IEnumerable instructions) - { - int i = 0; - foreach (var instruction in instructions) - { - i++; - yield return instruction; //Return the instruction first - //stloc, dup, callvirt - if (instruction.opcode.Name.ToLower().StartsWith("stloc") - || instruction.opcode == OpCodes.Dup - || instruction.opcode == OpCodes.Callvirt) - { - yield return new CodeInstruction(OpCodes.Ldstr, - "Just ran the " + i + ". instruction ending with " + instruction.opcode.Name); - yield return new CodeInstruction(OpCodes.Call, method); - } - } - } - - public static MethodInfo TargetMethod() - { - return AccessTools.Method("RobocraftX.CR.MachineEditing.BoxSelect.CopySelectionEngine:GenerateThumbnail"); - } - } - - [HarmonyPatch] - public class BugHuntPatch2 - { - public static void Prefix(int width, float fieldOfView, Vector3 cameraDirection, Vector3 lightDirection) - { - Console.WriteLine("TakeThumbnail invoked with parameters: " + width + ", " + fieldOfView + ", " + - cameraDirection + ", " + lightDirection); - - GPUInstancerManager manager = GPUInstancerAPI.GetActiveManagers().Find(m => m is GPUInstancerPrefabManager); - Bounds instancesBounds = manager.ComputeInstancesBounds(2); - Console.WriteLine("Bounds: " + instancesBounds); - Console.WriteLine("Size: " + instancesBounds.size); - Console.WriteLine("Size.x < 0: " + (instancesBounds.size.x < 0)); - } - - public static void Postfix(Texture2D __result) - { - Console.WriteLine("TakeThumbnail returned: " + (__result == null ? null : __result.name)); - } - - private delegate Texture2D TakeThumbnailDel(int width, float fieldOfView, Vector3 cameraDirection, - Vector3 lightDirection); - - public static MethodInfo TargetMethod() - { - return ((TakeThumbnailDel) ThumbnailUtility.TakeThumbnail).Method; - } - } - - [HarmonyPatch] - public class BugHuntPatch3 - { - public static void Prefix(int width, int filterLayerMask, GPUInstancerManager manager, - Vector3 cameraPosition, Quaternion cameraRotation, float cameraFov, Vector3 lightDirection, - int cullingLayer) - { - Console.WriteLine("Inner TakeThumbnail invoked with parameters: " + width + ", " + filterLayerMask + - ", " + (manager != null ? manager.name : null) + ", " + cameraPosition + ", " + - cameraRotation + ", " + cameraFov + ", " + lightDirection + ", " + cullingLayer); - } - - private delegate Texture2D TakeThumbnailDel(int width, int filterLayerMask, GPUInstancerManager manager, - Vector3 cameraPosition, Quaternion cameraRotation, float cameraFov, Vector3 lightDirection, - int cullingLayer); - - public static MethodInfo TargetMethod() - { - return ((TakeThumbnailDel) ThumbnailUtility.TakeThumbnail).Method; - } - } } #endif } diff --git a/doxygen.conf b/doxygen.conf index 4b1eaed..9958d11 100644 --- a/doxygen.conf +++ b/doxygen.conf @@ -38,7 +38,7 @@ PROJECT_NAME = "GamecraftModdingAPI" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = "v1.8.0" +PROJECT_NUMBER = "v1.7.0" # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a