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.

84 lines
2.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. using System.Text.Formatting;
  7. using GamecraftModdingAPI.Blocks;
  8. using GamecraftModdingAPI.Engines;
  9. using GamecraftModdingAPI.Players;
  10. using HarmonyLib;
  11. using RobocraftX.GUI.Debug;
  12. using Svelto.ECS;
  13. using Svelto.ECS.Experimental;
  14. namespace GamecraftModdingAPI.Utility
  15. {
  16. public class DebugInterfaceEngine : IApiEngine
  17. {
  18. private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>();
  19. public void Ready()
  20. {
  21. SetInfo("lookedAt", LookedAt);
  22. }
  23. public EntitiesDB entitiesDB { get; set; }
  24. public void Dispose()
  25. {
  26. }
  27. public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter;
  28. public bool RemoveInfo(string id) => _extraInfo.Remove(id);
  29. private Player player;
  30. private string LookedAt()
  31. {
  32. if (player == null)
  33. player = new Player(PlayerType.Local);
  34. Block block = player.GetBlockLookedAt();
  35. if (block == null) return "Block: none";
  36. return "Block: " + block.Type + "\nColor: " + block.Color + "\n" + "At: " + block.Position;
  37. }
  38. public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine";
  39. public bool isRemovable { get; } = true;
  40. [HarmonyPatch]
  41. private class Patch
  42. {
  43. public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  44. {
  45. var list = new List<CodeInstruction>(instructions);
  46. try
  47. {
  48. //Before setting the text from the StringBuffer
  49. int index = list.FindLastIndex(inst => inst.opcode == OpCodes.Ldfld);
  50. var array = new CodeInstruction[]
  51. {
  52. new CodeInstruction(OpCodes.Ldloc_0), //StringBuffer
  53. new CodeInstruction(OpCodes.Call, ((Action<StringBuffer>)AddInfo).Method)
  54. };
  55. list.InsertRange(index, array);
  56. }
  57. catch (Exception e)
  58. {
  59. Console.WriteLine(e);
  60. }
  61. return list;
  62. }
  63. public static void AddInfo(StringBuffer sb)
  64. {
  65. foreach (var info in _extraInfo.Values)
  66. sb.Append(info() + "\n");
  67. }
  68. public static MethodInfo TargetMethod()
  69. {
  70. return AccessTools.Method("RobocraftX.GUI.Debug.DebugDisplayEngine:UpdateDisplay");
  71. }
  72. }
  73. }
  74. }