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.

101 lines
4.4KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Gamecraft.Wires;
  5. using HarmonyLib;
  6. using RobocraftX.Blocks;
  7. using RobocraftX.Character;
  8. using Svelto.DataStructures;
  9. using Svelto.ECS;
  10. using TechbloxModdingAPI.Common.Engines;
  11. namespace TechbloxModdingAPI.Blocks.Engines
  12. {
  13. public class BlockCloneEngine : IApiEngine
  14. {
  15. private static Type copyEngineType =
  16. AccessTools.TypeByName("Gamecraft.GUI.Tweaks.Engines.CopyTweaksOnPickEngine");
  17. private static Type copyWireEngineType =
  18. AccessTools.TypeByName("Gamecraft.Wires.WireConnectionCopyOnPickEngine");
  19. private static Type createWireEngineType =
  20. AccessTools.TypeByName("RobocraftX.GUI.Wires.WireConnectionCreateOnPlaceEngine");
  21. private MethodBase copyFromBlock = AccessTools.Method(copyEngineType, "CopyTweaksFromBlock");
  22. private MethodBase copyToBlock = AccessTools.Method(copyEngineType, "ApplyTweaksToPlacedBlock");
  23. private MethodBase copyWireFromBlock = AccessTools.Method(copyWireEngineType, "CopyWireInputsAndOutputs");
  24. private MethodBase copyWireToBlock = AccessTools.Method(createWireEngineType, "PlaceWiresOnPlaceNewCube");
  25. public void Ready()
  26. {
  27. }
  28. public EntitiesDB entitiesDB { get; set; }
  29. public void Dispose()
  30. {
  31. }
  32. public void CopyBlockStats(EGID sourceID, EGID targetID)
  33. {
  34. var allCharacters = (LocalFasterReadOnlyList<ExclusiveGroupStruct>) CharacterExclusiveGroups.AllCharacters;
  35. foreach (var ((pickedBlockColl, count), _) in entitiesDB.QueryEntities<PickedBlockExtraDataStruct>(allCharacters))
  36. {
  37. for (int i = 0; i < count; ++i)
  38. {
  39. ref PickedBlockExtraDataStruct pickedBlock = ref pickedBlockColl[i];
  40. var oldStruct = pickedBlock;
  41. pickedBlock.pickedBlockEntityID = sourceID;
  42. pickedBlock.placedBlockEntityID = targetID;
  43. pickedBlock.placedBlockTweaksMustCopy = true;
  44. if (entitiesDB.Exists<BlockTagEntityStruct>(pickedBlock.pickedBlockEntityID)
  45. && entitiesDB.Exists<BlockTagEntityStruct>(pickedBlock.placedBlockEntityID))
  46. {
  47. copyFromBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
  48. uint playerID = Player.LocalPlayer.Id;
  49. var parameters = new object[] {playerID, pickedBlock};
  50. copyWireFromBlock.Invoke(Patch.copyWireEngine, parameters);
  51. pickedBlock = (PickedBlockExtraDataStruct) parameters[1]; //ref arg
  52. copyToBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
  53. ExclusiveGroupStruct group = BuildModeWiresGroups.WIRES_COPY_GROUP + playerID;
  54. copyWireToBlock.Invoke(Patch.createWireEngine, new object[] {group, pickedBlock.ID});
  55. pickedBlock.placedBlockTweaksMustCopy = false;
  56. }
  57. pickedBlock = oldStruct; //Make sure to not interfere with the game - Although that might not be the case with the wire copying
  58. }
  59. }
  60. }
  61. [HarmonyPatch]
  62. private static class Patch
  63. {
  64. public static object copyEngine;
  65. public static object copyWireEngine;
  66. public static object createWireEngine;
  67. public static void Postfix(object __instance)
  68. {
  69. if (__instance.GetType() == copyEngineType)
  70. copyEngine = __instance;
  71. else if (__instance.GetType() == copyWireEngineType)
  72. copyWireEngine = __instance;
  73. else if (__instance.GetType() == createWireEngineType)
  74. createWireEngine = __instance;
  75. }
  76. public static IEnumerable<MethodBase> TargetMethods()
  77. {
  78. return new[]
  79. {
  80. AccessTools.GetDeclaredConstructors(copyEngineType)[0],
  81. AccessTools.GetDeclaredConstructors(copyWireEngineType)[0],
  82. AccessTools.GetDeclaredConstructors(createWireEngineType)[0]
  83. };
  84. }
  85. }
  86. }
  87. }