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.

85 lines
2.8KB

  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;
  7. using System.Text.Formatting;
  8. using GamecraftModdingAPI.Blocks;
  9. using GamecraftModdingAPI.Engines;
  10. using GamecraftModdingAPI.Players;
  11. using HarmonyLib;
  12. using RobocraftX.GUI.Debug;
  13. using Svelto.ECS;
  14. using Svelto.ECS.Experimental;
  15. namespace GamecraftModdingAPI.Utility
  16. {
  17. public class DebugInterfaceEngine : IApiEngine
  18. {
  19. private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>();
  20. public void Ready()
  21. {
  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. public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine";
  30. public bool isRemovable { get; } = true;
  31. [HarmonyPatch]
  32. private class Patch
  33. {
  34. public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  35. {
  36. var list = new List<CodeInstruction>(instructions);
  37. try
  38. {
  39. //Before setting the text from the StringBuffer
  40. int index = list.FindLastIndex(inst => inst.opcode == OpCodes.Ldfld);
  41. var array = new CodeInstruction[]
  42. {
  43. new CodeInstruction(OpCodes.Ldloc_0), //StringBuffer
  44. new CodeInstruction(OpCodes.Call, ((Action<StringBuilder>)AddInfo).Method)
  45. };
  46. list.InsertRange(index - 1, array); //-1: ldloc.1 ("local") before ldfld
  47. }
  48. catch (Exception e)
  49. {
  50. Logging.LogWarning("Failed to inject AddInfo method for the debug display!\n" + e);
  51. }
  52. return list;
  53. }
  54. public static void AddInfo(StringBuilder sb)
  55. {
  56. foreach (var info in _extraInfo)
  57. {
  58. try
  59. {
  60. string text = info.Value().Trim();
  61. if (text.Length != 0)
  62. sb.Append(text + "\n");
  63. }
  64. catch (Exception e)
  65. {
  66. Logging.LogWarning("Unable to get info for " + info.Key + "\n" + e);
  67. }
  68. }
  69. }
  70. public static MethodInfo TargetMethod()
  71. {
  72. return AccessTools.Method("RobocraftX.GUI.Debug.DebugDisplayEngine:UpdateDisplay");
  73. }
  74. }
  75. }
  76. }