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.

108 lines
4.6KB

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