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.

159 lines
5.8KB

  1. using System;
  2. using System.Reflection;
  3. using HarmonyLib;
  4. using RobocraftX.Services;
  5. using UnityEngine;
  6. using RobocraftX.Common;
  7. using TechbloxModdingAPI.Utility;
  8. namespace TechbloxModdingAPI.App
  9. {
  10. /// <summary>
  11. /// The Techblox application that is running this code right now.
  12. /// </summary>
  13. public class Client
  14. {
  15. // extensible engine
  16. protected static AppEngine appEngine = new AppEngine();
  17. protected static Func<object> ErrorHandlerInstanceGetter;
  18. protected static Action<object, Error> EnqueueError;
  19. protected static Action<object> HandleErrorClosed;
  20. /// <summary>
  21. /// An event that fires whenever the main menu is loaded.
  22. /// </summary>
  23. public static event EventHandler<MenuEventArgs> EnterMenu
  24. {
  25. add => appEngine.EnterMenu += value;
  26. remove => appEngine.EnterMenu -= value;
  27. }
  28. /// <summary>
  29. /// An event that fire whenever the main menu is exited.
  30. /// </summary>
  31. public static event EventHandler<MenuEventArgs> ExitMenu
  32. {
  33. add => appEngine.ExitMenu += value;
  34. remove => appEngine.ExitMenu -= value;
  35. }
  36. /// <summary>
  37. /// Techblox build version string.
  38. /// Usually this is in the form YYYY.mm.DD.HH.MM.SS
  39. /// </summary>
  40. /// <value>The version.</value>
  41. public string Version
  42. {
  43. get => Application.version;
  44. }
  45. /// <summary>
  46. /// Unity version string.
  47. /// </summary>
  48. /// <value>The unity version.</value>
  49. public string UnityVersion
  50. {
  51. get => Application.unityVersion;
  52. }
  53. /// <summary>
  54. /// Game saves currently visible in the menu.
  55. /// These take a second to completely populate after the EnterMenu event fires.
  56. /// </summary>
  57. /// <value>My games.</value>
  58. public Game[] MyGames
  59. {
  60. get
  61. {
  62. if (!appEngine.IsInMenu) return new Game[0];
  63. return appEngine.GetMyGames();
  64. }
  65. }
  66. /// <summary>
  67. /// Whether Techblox is in the Main Menu
  68. /// </summary>
  69. /// <value><c>true</c> if in menu; <c>false</c> when loading or in a game.</value>
  70. public bool InMenu
  71. {
  72. get => appEngine.IsInMenu;
  73. }
  74. /// <summary>
  75. /// Open a popup which prompts the user to click a button.
  76. /// This reuses Techblox's error dialog popup
  77. /// </summary>
  78. /// <param name="popup">The popup to display. Use an instance of SingleChoicePrompt or DualChoicePrompt.</param>
  79. public void PromptUser(Error popup)
  80. {
  81. // if the stuff wasn't mostly set to internal, this would be written as:
  82. // RobocraftX.Services.ErrorHandler.Instance.EqueueError(error);
  83. object errorHandlerInstance = ErrorHandlerInstanceGetter();
  84. EnqueueError(errorHandlerInstance, popup);
  85. }
  86. // TODO
  87. /*public void CloseCurrentPrompt()
  88. {
  89. // RobocraftX.Services.ErrorHandler.Instance.HandlePopupClosed();
  90. // FIXME: this is a call that is also called when closing, not the actual closing action itself (so it doesn't work)
  91. object errorHandlerInstance = ErrorHandlerInstanceGetter();
  92. HandleErrorClosed(errorHandlerInstance);
  93. }*/
  94. internal static void Init()
  95. {
  96. // this would have been so much simpler if this didn't involve a bunch of internal fields & classes
  97. Type errorHandler = AccessTools.TypeByName("RobocraftX.Services.ErrorHandler");
  98. Type errorHandle = AccessTools.TypeByName("RobocraftX.Services.ErrorHandle");
  99. ErrorHandlerInstanceGetter = (Func<object>) AccessTools.Method("TechbloxModdingAPI.App.Client:GenInstanceGetter")
  100. .MakeGenericMethod(errorHandler)
  101. .Invoke(null, new object[0]);
  102. EnqueueError = (Action<object, Error>) AccessTools.Method("TechbloxModdingAPI.App.Client:GenEnqueueError")
  103. .MakeGenericMethod(errorHandler, errorHandle)
  104. .Invoke(null, new object[0]);
  105. /*HandleErrorClosed = (Action<object>) AccessTools.Method("TechbloxModdingAPI.App.Client:GenHandlePopupClosed")
  106. .MakeGenericMethod(errorHandler)
  107. .Invoke(null, new object[0]);*/
  108. // register engines
  109. MenuEngineManager.AddMenuEngine(appEngine);
  110. }
  111. // Creating delegates once is faster than reflection every time
  112. // Admittedly, this way is more difficult to code and less readable
  113. private static Func<object> GenInstanceGetter<T>()
  114. {
  115. Type errorHandler = AccessTools.TypeByName("RobocraftX.Services.ErrorHandler");
  116. MethodInfo instance = AccessTools.PropertyGetter(errorHandler, "Instance");
  117. Func<T> getterSimple = (Func<T>) Delegate.CreateDelegate(typeof(Func<T>), null, instance);
  118. Func<object> getterCasted = () => (object) getterSimple();
  119. return getterCasted;
  120. }
  121. private static Action<object, Error> GenEnqueueError<T, TRes>()
  122. {
  123. Type errorHandler = AccessTools.TypeByName("RobocraftX.Services.ErrorHandler");
  124. MethodInfo enqueueError = AccessTools.Method(errorHandler, "EnqueueError");
  125. Func<T, Error, TRes> enqueueSimple =
  126. (Func<T, Error, TRes>) Delegate.CreateDelegate(typeof(Func<T, Error, TRes>), enqueueError);
  127. Action<object, Error> enqueueCasted =
  128. (object instance, Error error) => { enqueueSimple((T) instance, error); };
  129. return enqueueCasted;
  130. }
  131. private static Action<object> GenHandlePopupClosed<T>()
  132. {
  133. Type errorHandler = AccessTools.TypeByName("RobocraftX.Services.ErrorHandler");
  134. MethodInfo handlePopupClosed = AccessTools.Method(errorHandler, "HandlePopupClosed");
  135. Action<T> handleSimple =
  136. (Action<T>) Delegate.CreateDelegate(typeof(Action<T>), handlePopupClosed);
  137. Action<object> handleCasted = (object instance) => handleSimple((T) instance);
  138. return handleCasted;
  139. }
  140. }
  141. }