A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.8KB

  1. using System.Reflection;
  2. using Gamecraft.Blocks.BlockGroups;
  3. using Gamecraft.GUI.Blueprints;
  4. using GamecraftModdingAPI.Engines;
  5. using HarmonyLib;
  6. using RobocraftX.Blocks;
  7. using Svelto.DataStructures;
  8. using Svelto.ECS;
  9. using Svelto.ECS.DataStructures;
  10. using Unity.Collections;
  11. namespace GamecraftModdingAPI.Blocks
  12. {
  13. public class BlueprintEngine : IApiEngine
  14. {
  15. private readonly MethodInfo getBlocksFromGroup =
  16. AccessTools.Method("RobocraftX.CR.MachineEditing.PlaceBlockUtility:GetBlocksSharingBlockgroup");
  17. private readonly NativeDynamicArray selectedBlocksInGroup = new NativeDynamicArray();
  18. private readonly NativeHashSet<ulong> removedConnections = new NativeHashSet<ulong>();
  19. private static NativeEntityRemove nativeRemove;
  20. private static MachineGraphConnectionEntityFactory connectionFactory;
  21. public void Ready()
  22. {
  23. }
  24. public EntitiesDB entitiesDB { get; set; }
  25. public void Dispose()
  26. {
  27. }
  28. public Block[] GetBlocksFromGroup(EGID blockID)
  29. {
  30. var list = new FasterList<Block>();
  31. object blockPos = null, blockRot = null;
  32. getBlocksFromGroup.Invoke(null, new[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot});
  33. for (uint i = 0; i < selectedBlocksInGroup.Count<EGID>(); i++)
  34. list.Add(new Block(selectedBlocksInGroup.Get<EGID>(i)));
  35. selectedBlocksInGroup.FastClear();
  36. return list.ToArray();
  37. }
  38. public void RemoveBlockGroup(int id)
  39. {
  40. BlockGroupUtility.RemoveAllBlocksInBlockGroup(id, entitiesDB, removedConnections, nativeRemove,
  41. connectionFactory, default).Complete();
  42. }
  43. public void SelectBlueprint(uint resourceID)
  44. {
  45. BlueprintUtil.SelectBlueprint(null, new BlueprintInventoryItemEntityStruct
  46. {
  47. blueprintResourceId = resourceID,
  48. });
  49. }
  50. public string Name { get; } = "GamecraftModdingAPIBlueprintGameEngine";
  51. public bool isRemovable { get; }
  52. [HarmonyPatch]
  53. private static class RemoveEnginePatch
  54. {
  55. public static void Prefix(IEntityFunctions entityFunctions,
  56. MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory)
  57. {
  58. nativeRemove = entityFunctions.ToNativeRemove<BlockEntityDescriptor>("GCAPI" + nameof(BlueprintEngine));
  59. connectionFactory = machineGraphConnectionEntityFactory;
  60. }
  61. public static MethodBase TargetMethod()
  62. {
  63. return AccessTools.Constructor(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.RemoveBlockEngine"));
  64. }
  65. }
  66. }
  67. }