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.

106 lines
4.5KB

  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.placedBlockTweaksMustCopy = true;
  46. if (entitiesDB.Exists<DBEntityStruct>(pickedBlock.pickedBlockEntityID)
  47. && entitiesDB.Exists<DBEntityStruct>(pickedBlock.placedBlockEntityID))
  48. {
  49. copyFromBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
  50. uint playerID = Player.LocalPlayer.Id;
  51. var parameters = new object[] {playerID, pickedBlock};
  52. copyWireFromBlock.Invoke(Patch.copyWireEngine, parameters);
  53. pickedBlock = (PickedBlockExtraDataStruct) parameters[1]; //ref arg
  54. copyToBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
  55. ExclusiveGroupStruct group = WiresExclusiveGroups.WIRES_COPY_GROUP + playerID;
  56. copyWireToBlock.Invoke(Patch.createWireEngine, new object[] {group, pickedBlock.ID});
  57. pickedBlock.placedBlockTweaksMustCopy = false;
  58. }
  59. pickedBlock = oldStruct; //Make sure to not interfere with the game - Although that might not be the case with the wire copying
  60. }
  61. }
  62. }
  63. [HarmonyPatch]
  64. private static class Patch
  65. {
  66. public static object copyEngine;
  67. public static object copyWireEngine;
  68. public static object createWireEngine;
  69. public static void Postfix(object __instance)
  70. {
  71. if (__instance.GetType() == copyEngineType)
  72. copyEngine = __instance;
  73. else if (__instance.GetType() == copyWireEngineType)
  74. copyWireEngine = __instance;
  75. else if (__instance.GetType() == createWireEngineType)
  76. createWireEngine = __instance;
  77. }
  78. public static IEnumerable<MethodBase> TargetMethods()
  79. {
  80. return new[]
  81. {
  82. AccessTools.GetDeclaredConstructors(copyEngineType)[0],
  83. AccessTools.GetDeclaredConstructors(copyWireEngineType)[0],
  84. AccessTools.GetDeclaredConstructors(createWireEngineType)[0]
  85. };
  86. }
  87. }
  88. public string Name { get; } = "GamecraftModdingAPIBlockCloneGameEngine";
  89. public bool isRemovable { get; } = false;
  90. }
  91. }