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.

130 lines
5.6KB

  1. using System.Reflection;
  2. using DataLoader;
  3. using Gamecraft.Blocks.BlockGroups;
  4. using Gamecraft.Wires;
  5. using HarmonyLib;
  6. using RobocraftX.Blocks;
  7. using RobocraftX.Character;
  8. using RobocraftX.Common;
  9. using RobocraftX.CR.MachineEditing.BoxSelect;
  10. using RobocraftX.Rendering;
  11. using RobocraftX.Rendering.GPUI;
  12. using Svelto.ECS;
  13. using Svelto.ECS.EntityStructs;
  14. using Unity.Mathematics;
  15. using TechbloxModdingAPI.Engines;
  16. using TechbloxModdingAPI.Utility;
  17. namespace TechbloxModdingAPI.Blocks.Engines
  18. {
  19. /// <summary>
  20. /// Engine which executes block placement actions
  21. /// </summary>
  22. public class PlacementEngine : IApiEngine
  23. {
  24. public bool IsInGame;
  25. public void Dispose()
  26. {
  27. IsInGame = false;
  28. }
  29. public void Ready()
  30. {
  31. IsInGame = true;
  32. }
  33. public EntitiesDB entitiesDB { get; set; }
  34. private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine
  35. private static IEntityFactory _entityFactory;
  36. public EntityInitializer PlaceBlock(BlockIDs block, float3 position, Player player, bool autoWire)
  37. { //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
  38. return BuildBlock((ushort) block, position, autoWire, (player ?? Player.LocalPlayer).Id);
  39. }
  40. private EntityInitializer BuildBlock(ushort block, float3 position, bool autoWire, uint playerId)
  41. {
  42. if (_blockEntityFactory == null)
  43. throw new BlockException("The factory is null.");
  44. if(!FullGameFields._dataDb.ContainsKey<CubeListData>(block))
  45. throw new BlockException("Block with ID " + block + " not found!");
  46. //RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine
  47. DBEntityStruct dbEntity = new DBEntityStruct {DBID = block};
  48. EntityInitializer structInitializer = _blockEntityFactory.Build(CommonExclusiveGroups.blockIDGeneratorClient.Next(), block); //The ghost block index is only used for triggers
  49. uint prefabAssetID = structInitializer.Has<PrefabAssetIDComponent>()
  50. ? structInitializer.Get<PrefabAssetIDComponent>().prefabAssetID
  51. : throw new BlockException("Prefab asset ID not found!"); //Set by the game
  52. uint prefabId = PrefabsID.GetOrAddPrefabID((ushort) prefabAssetID, (byte) BlockMaterial.SteelBodywork, 1, false);
  53. structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));
  54. structInitializer.Init(dbEntity);
  55. structInitializer.Init(new PositionEntityStruct {position = position});
  56. structInitializer.Init(new RotationEntityStruct {rotation = quaternion.identity});
  57. structInitializer.Init(new ScalingEntityStruct {scale = new float3(1, 1, 1)});
  58. structInitializer.Init(new GridRotationStruct
  59. {
  60. position = position,
  61. rotation = quaternion.identity
  62. });
  63. structInitializer.Init(new UniformBlockScaleEntityStruct {scaleFactor = 1});
  64. structInitializer.Get<CubeMaterialStruct>().materialId = (byte) BlockMaterial.SteelBodywork;
  65. var bssesopt = entitiesDB.QueryEntityOptional<BoxSelectStateEntityStruct>(new EGID(playerId,
  66. BoxSelectExclusiveGroups.BoxSelectVolumeExclusiveGroup));
  67. if (!bssesopt)
  68. throw new BlockException("Invalid player ID specified for block placement");
  69. structInitializer.Init(new BlockPlacementInfoStruct
  70. {
  71. loadedFromDisk = false,
  72. placedByBuildingDrone = bssesopt.Get().buildingDroneReference,
  73. triggerAutoWiring = autoWire && structInitializer.Has<BlockPortsStruct>()
  74. });
  75. int nextFilterId = BlockGroupUtility.NextFilterId;
  76. structInitializer.Init(new BlockGroupEntityComponent
  77. {
  78. currentBlockGroup = nextFilterId
  79. });
  80. _entityFactory.BuildEntity<BlockGroupEntityDescriptor>((uint) nextFilterId,
  81. BlockGroupExclusiveGroups.BlockGroupEntityGroup)
  82. .Init(new BlockGroupTransformEntityComponent
  83. {
  84. blockGroupGridRotation = quaternion.identity,
  85. blockGroupGridPosition = position
  86. });
  87. foreach (var group in CharacterExclusiveGroups.AllCharacters)
  88. {
  89. EGID playerEGID = new EGID(playerId, group);
  90. if (!entitiesDB.TryQueryEntitiesAndIndex<PickedBlockExtraDataStruct>(playerEGID, out uint index,
  91. out var array)) continue;
  92. ref PickedBlockExtraDataStruct pickedBlock = ref array[index];
  93. pickedBlock.placedBlockEntityID = structInitializer.EGID;
  94. pickedBlock.placedBlockWasAPickedBlock = false;
  95. }
  96. return structInitializer;
  97. }
  98. public string Name => "TechbloxModdingAPIPlacementGameEngine";
  99. public bool isRemovable => false;
  100. [HarmonyPatch]
  101. class FactoryObtainerPatch
  102. {
  103. static void Postfix(BlockEntityFactory blockEntityFactory, IEntityFactory entityFactory)
  104. {
  105. _blockEntityFactory = blockEntityFactory;
  106. _entityFactory = entityFactory;
  107. Logging.MetaDebugLog("Block entity factory injected.");
  108. }
  109. static MethodBase TargetMethod(Harmony instance)
  110. {
  111. return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine").GetConstructors()[0];
  112. }
  113. }
  114. }
  115. }