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.

DebugInterfaceEngine.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  22. public EntitiesDB entitiesDB { get; set; }
  23. public void Dispose()
  24. {
  25. }
  26. public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter;
  27. public bool RemoveInfo(string id) => _extraInfo.Remove(id);
  28. public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine";
  29. public bool isRemovable { get; } = true;
  30. [HarmonyPatch]
  31. private class Patch
  32. {
  33. public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  34. {
  35. var list = new List<CodeInstruction>(instructions);
  36. try
  37. {
  38. //Before setting the text from the StringBuffer
  39. int index = list.FindLastIndex(inst => inst.opcode == OpCodes.Ldfld);
  40. var array = new CodeInstruction[]
  41. {
  42. new CodeInstruction(OpCodes.Ldloc_0), //StringBuffer
  43. new CodeInstruction(OpCodes.Call, ((Action<StringBuffer>)AddInfo).Method)
  44. };
  45. list.InsertRange(index, array);
  46. }
  47. catch (Exception e)
  48. {
  49. Logging.LogWarning("Failed to inject AddInfo method for the debug display!\n" + e);
  50. }
  51. return list;
  52. }
  53. public static void AddInfo(StringBuffer sb)
  54. {
  55. foreach (var info in _extraInfo)
  56. {
  57. try
  58. {
  59. sb.Append(info.Value() + "\n");
  60. }
  61. catch (Exception e)
  62. {
  63. Logging.LogWarning("Unable to get info for " + info.Key + "\n" + e);
  64. }
  65. }
  66. }
  67. public static MethodInfo TargetMethod()
  68. {
  69. return AccessTools.Method("RobocraftX.GUI.Debug.DebugDisplayEngine:UpdateDisplay");
  70. }
  71. }
  72. }
  73. }