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.

105 lines
4.5KB

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