SRDebugService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Diagnostics;
  3. using SRDebugger.Internal;
  4. using SRDebugger.UI;
  5. using SRF;
  6. using SRF.Service;
  7. using SRF.UI;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace SRDebugger.Services.Implementation
  11. {
  12. [Service(typeof(IDebugService))]
  13. public class SRDebugService : IDebugService
  14. {
  15. public SRDebugService()
  16. {
  17. SRServiceManager.RegisterService<IDebugService>(this);
  18. SRServiceManager.GetService<IProfilerService>();
  19. this._debugTrigger = SRServiceManager.GetService<IDebugTriggerService>();
  20. this._informationService = SRServiceManager.GetService<ISystemInformationService>();
  21. this._pinnedUiService = SRServiceManager.GetService<IPinnedUIService>();
  22. this._optionsService = SRServiceManager.GetService<IOptionsService>();
  23. this._debugPanelService = SRServiceManager.GetService<IDebugPanelService>();
  24. this._debugPanelService.VisibilityChanged += this.DebugPanelServiceOnVisibilityChanged;
  25. this._debugTrigger.IsEnabled = (this.Settings.EnableTrigger == Settings.TriggerEnableModes.Enabled || (this.Settings.EnableTrigger == Settings.TriggerEnableModes.MobileOnly && Application.isMobilePlatform) || (this.Settings.EnableTrigger == Settings.TriggerEnableModes.DevelopmentBuildsOnly && UnityEngine.Debug.isDebugBuild));
  26. this._debugTrigger.Position = this.Settings.TriggerPosition;
  27. if (this.Settings.EnableKeyboardShortcuts)
  28. {
  29. SRServiceManager.GetService<KeyboardShortcutListenerService>();
  30. }
  31. this._entryCodeEnabled = (Settings.Instance.RequireCode && Settings.Instance.EntryCode.Count == 4);
  32. if (Settings.Instance.RequireCode && !this._entryCodeEnabled)
  33. {
  34. UnityEngine.Debug.LogError("[SRDebugger] RequireCode is enabled, but pin is not 4 digits");
  35. }
  36. Transform transform = Hierarchy.Get("SRDebugger");
  37. UnityEngine.Object.DontDestroyOnLoad(transform.gameObject);
  38. }
  39. public Settings Settings
  40. {
  41. get
  42. {
  43. return Settings.Instance;
  44. }
  45. }
  46. public bool IsDebugPanelVisible
  47. {
  48. get
  49. {
  50. return this._debugPanelService.IsVisible;
  51. }
  52. }
  53. public bool IsTriggerEnabled
  54. {
  55. get
  56. {
  57. return this._debugTrigger.IsEnabled;
  58. }
  59. set
  60. {
  61. this._debugTrigger.IsEnabled = value;
  62. }
  63. }
  64. public bool IsProfilerDocked
  65. {
  66. get
  67. {
  68. return Service.PinnedUI.IsProfilerPinned;
  69. }
  70. set
  71. {
  72. Service.PinnedUI.IsProfilerPinned = value;
  73. }
  74. }
  75. public void AddSystemInfo(InfoEntry entry, string category = "Default")
  76. {
  77. this._informationService.Add(entry, category);
  78. }
  79. public void ShowDebugPanel(bool requireEntryCode = true)
  80. {
  81. if (requireEntryCode && this._entryCodeEnabled && !this._hasAuthorised)
  82. {
  83. this.PromptEntryCode();
  84. return;
  85. }
  86. this._debugPanelService.IsVisible = true;
  87. }
  88. public void ShowDebugPanel(DefaultTabs tab, bool requireEntryCode = true)
  89. {
  90. if (requireEntryCode && this._entryCodeEnabled && !this._hasAuthorised)
  91. {
  92. this._queuedTab = new DefaultTabs?(tab);
  93. this.PromptEntryCode();
  94. return;
  95. }
  96. this._debugPanelService.IsVisible = true;
  97. this._debugPanelService.OpenTab(tab);
  98. }
  99. public void HideDebugPanel()
  100. {
  101. this._debugPanelService.IsVisible = false;
  102. }
  103. public void DestroyDebugPanel()
  104. {
  105. this._debugPanelService.IsVisible = false;
  106. this._debugPanelService.Unload();
  107. }
  108. public void AddOptionContainer(object container)
  109. {
  110. this._optionsService.AddContainer(container);
  111. }
  112. public void RemoveOptionContainer(object container)
  113. {
  114. this._optionsService.RemoveContainer(container);
  115. }
  116. public void PinAllOptions(string category)
  117. {
  118. foreach (OptionDefinition optionDefinition in this._optionsService.Options)
  119. {
  120. if (optionDefinition.Category == category)
  121. {
  122. this._pinnedUiService.Pin(optionDefinition, -1);
  123. }
  124. }
  125. }
  126. public void UnpinAllOptions(string category)
  127. {
  128. foreach (OptionDefinition optionDefinition in this._optionsService.Options)
  129. {
  130. if (optionDefinition.Category == category)
  131. {
  132. this._pinnedUiService.Unpin(optionDefinition);
  133. }
  134. }
  135. }
  136. public void PinOption(string name)
  137. {
  138. foreach (OptionDefinition optionDefinition in this._optionsService.Options)
  139. {
  140. if (optionDefinition.Name == name)
  141. {
  142. this._pinnedUiService.Pin(optionDefinition, -1);
  143. }
  144. }
  145. }
  146. public void UnpinOption(string name)
  147. {
  148. foreach (OptionDefinition optionDefinition in this._optionsService.Options)
  149. {
  150. if (optionDefinition.Name == name)
  151. {
  152. this._pinnedUiService.Unpin(optionDefinition);
  153. }
  154. }
  155. }
  156. public void ClearPinnedOptions()
  157. {
  158. this._pinnedUiService.UnpinAll();
  159. }
  160. public void ShowBugReportSheet(ActionCompleteCallback onComplete = null, bool takeScreenshot = true, string descriptionContent = null)
  161. {
  162. BugReportPopoverService service = SRServiceManager.GetService<BugReportPopoverService>();
  163. if (service.IsShowingPopover)
  164. {
  165. return;
  166. }
  167. service.ShowBugReporter(delegate(bool succeed, string message)
  168. {
  169. if (onComplete != null)
  170. {
  171. onComplete(succeed);
  172. }
  173. }, takeScreenshot, descriptionContent);
  174. }
  175. public IDockConsoleService DockConsole
  176. {
  177. get
  178. {
  179. return Service.DockConsole;
  180. }
  181. }
  182. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  183. public event VisibilityChangedDelegate PanelVisibilityChanged;
  184. private void DebugPanelServiceOnVisibilityChanged(IDebugPanelService debugPanelService, bool b)
  185. {
  186. if (this.PanelVisibilityChanged == null)
  187. {
  188. return;
  189. }
  190. try
  191. {
  192. this.PanelVisibilityChanged(b);
  193. }
  194. catch (Exception exception)
  195. {
  196. UnityEngine.Debug.LogError("[SRDebugger] Event target threw exception (IDebugService.PanelVisiblityChanged)");
  197. UnityEngine.Debug.LogException(exception);
  198. }
  199. }
  200. private void PromptEntryCode()
  201. {
  202. SRServiceManager.GetService<IPinEntryService>().ShowPinEntry(Settings.Instance.EntryCode, SRDebugStrings.Current.PinEntryPrompt, delegate(bool entered)
  203. {
  204. if (entered)
  205. {
  206. if (!Settings.Instance.RequireEntryCodeEveryTime)
  207. {
  208. this._hasAuthorised = true;
  209. }
  210. if (this._queuedTab != null)
  211. {
  212. DefaultTabs value = this._queuedTab.Value;
  213. this._queuedTab = null;
  214. this.ShowDebugPanel(value, false);
  215. }
  216. else
  217. {
  218. this.ShowDebugPanel(false);
  219. }
  220. }
  221. this._queuedTab = null;
  222. }, true);
  223. }
  224. public RectTransform EnableWorldSpaceMode()
  225. {
  226. if (this._worldSpaceTransform != null)
  227. {
  228. return this._worldSpaceTransform;
  229. }
  230. if (Settings.Instance.UseDebugCamera)
  231. {
  232. throw new InvalidOperationException("UseDebugCamera cannot be enabled at the same time as EnableWorldSpaceMode.");
  233. }
  234. this._debugPanelService.IsVisible = true;
  235. DebugPanelRoot rootObject = ((DebugPanelServiceImpl)this._debugPanelService).RootObject;
  236. rootObject.Canvas.gameObject.RemoveComponentIfExists<SRRetinaScaler>();
  237. rootObject.Canvas.gameObject.RemoveComponentIfExists<CanvasScaler>();
  238. rootObject.Canvas.renderMode = RenderMode.WorldSpace;
  239. RectTransform component = rootObject.Canvas.GetComponent<RectTransform>();
  240. component.sizeDelta = new Vector2(1024f, 768f);
  241. component.position = Vector3.zero;
  242. return this._worldSpaceTransform = component;
  243. }
  244. private readonly IDebugPanelService _debugPanelService;
  245. private readonly IDebugTriggerService _debugTrigger;
  246. private readonly ISystemInformationService _informationService;
  247. private readonly IOptionsService _optionsService;
  248. private readonly IPinnedUIService _pinnedUiService;
  249. private bool _entryCodeEnabled;
  250. private bool _hasAuthorised;
  251. private DefaultTabs? _queuedTab;
  252. private RectTransform _worldSpaceTransform;
  253. }
  254. }