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.

Client.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Reflection;
  3. using HarmonyLib;
  4. using RobocraftX.Services;
  5. using UnityEngine;
  6. using GamecraftModdingAPI.Utility;
  7. using RobocraftX.Common;
  8. namespace GamecraftModdingAPI.App
  9. {
  10. /// <summary>
  11. /// The Gamecraft 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. /// Gamecraft 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 Gamecraft 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 Gamecraft'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("GamecraftModdingAPI.App.Client:GenInstanceGetter")
  100. .MakeGenericMethod(errorHandler)
  101. .Invoke(null, new object[0]);
  102. EnqueueError = (Action<object, Error>) AccessTools.Method("GamecraftModdingAPI.App.Client:GenEnqueueError")
  103. .MakeGenericMethod(errorHandler, errorHandle)
  104. .Invoke(null, new object[0]);
  105. /*HandleErrorClosed = (Action<object>) AccessTools.Method("GamecraftModdingAPI.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. }