SRDebugService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. namespace SRDebugger.Services.Implementation
  2. {
  3. using System;
  4. using Internal;
  5. using SRF;
  6. using SRF.Service;
  7. using UnityEngine;
  8. using Object = UnityEngine.Object;
  9. using SRF.UI;
  10. using UnityEngine.UI;
  11. [Service(typeof (IDebugService))]
  12. public class SRDebugService : IDebugService
  13. {
  14. public IDockConsoleService DockConsole
  15. {
  16. get { return Service.DockConsole; }
  17. }
  18. public IConsoleFilterState ConsoleFilter
  19. {
  20. get
  21. {
  22. if (_consoleFilterState == null)
  23. {
  24. _consoleFilterState = SRServiceManager.GetService<IConsoleFilterState>();
  25. }
  26. return _consoleFilterState;
  27. }
  28. }
  29. public event VisibilityChangedDelegate PanelVisibilityChanged;
  30. public event PinnedUiCanvasCreated PinnedUiCanvasCreated;
  31. private readonly IDebugPanelService _debugPanelService;
  32. private readonly IDebugTriggerService _debugTrigger;
  33. private readonly ISystemInformationService _informationService;
  34. private readonly IOptionsService _optionsService;
  35. private readonly IPinnedUIService _pinnedUiService;
  36. private IConsoleFilterState _consoleFilterState;
  37. private EntryCode? _entryCode;
  38. private bool _hasAuthorised;
  39. private DefaultTabs? _queuedTab;
  40. private RectTransform _worldSpaceTransform;
  41. private DynamicOptionContainer _looseOptionContainer;
  42. public SRDebugService()
  43. {
  44. SRServiceManager.RegisterService<IDebugService>(this);
  45. // Load profiler
  46. SRServiceManager.GetService<IProfilerService>();
  47. // Setup trigger service
  48. _debugTrigger = SRServiceManager.GetService<IDebugTriggerService>();
  49. _informationService = SRServiceManager.GetService<ISystemInformationService>();
  50. _pinnedUiService = SRServiceManager.GetService<IPinnedUIService>();
  51. _pinnedUiService.OptionsCanvasCreated += transform =>
  52. {
  53. if (PinnedUiCanvasCreated == null) return;
  54. try
  55. {
  56. PinnedUiCanvasCreated(transform);
  57. }
  58. catch(Exception e)
  59. {
  60. Debug.LogException(e);
  61. }
  62. };
  63. _optionsService = SRServiceManager.GetService<IOptionsService>();
  64. // Create debug panel service (this does not actually load any UI resources until opened)
  65. _debugPanelService = SRServiceManager.GetService<IDebugPanelService>();
  66. // Subscribe to visibility changes to provide API-facing event for panel open/close
  67. _debugPanelService.VisibilityChanged += DebugPanelServiceOnVisibilityChanged;
  68. _debugTrigger.IsEnabled = Settings.EnableTrigger == Settings.TriggerEnableModes.Enabled ||
  69. Settings.EnableTrigger == Settings.TriggerEnableModes.MobileOnly && Application.isMobilePlatform ||
  70. Settings.EnableTrigger == Settings.TriggerEnableModes.DevelopmentBuildsOnly && Debug.isDebugBuild;
  71. _debugTrigger.Position = Settings.TriggerPosition;
  72. if (Settings.EnableKeyboardShortcuts)
  73. {
  74. SRServiceManager.GetService<KeyboardShortcutListenerService>();
  75. }
  76. if (Settings.Instance.RequireCode)
  77. {
  78. if (Settings.Instance.EntryCode.Count != 4)
  79. {
  80. Debug.LogError("[SRDebugger] RequireCode is enabled, but pin is not 4 digits");
  81. }
  82. else
  83. {
  84. _entryCode = new EntryCode(Settings.Instance.EntryCode[0], Settings.Instance.EntryCode[1],
  85. Settings.Instance.EntryCode[2], Settings.Instance.EntryCode[3]);
  86. }
  87. }
  88. // Ensure that root object cannot be destroyed on scene loads
  89. var srDebuggerParent = Hierarchy.Get("SRDebugger");
  90. Object.DontDestroyOnLoad(srDebuggerParent.gameObject);
  91. // Add any options containers that were created on init
  92. var internalRegistry = SRServiceManager.GetService<InternalOptionsRegistry>();
  93. internalRegistry.SetHandler(_optionsService.AddContainer);
  94. }
  95. public Settings Settings
  96. {
  97. get { return Settings.Instance; }
  98. }
  99. public bool IsDebugPanelVisible
  100. {
  101. get { return _debugPanelService.IsVisible; }
  102. }
  103. public bool IsTriggerEnabled
  104. {
  105. get { return _debugTrigger.IsEnabled; }
  106. set { _debugTrigger.IsEnabled = value; }
  107. }
  108. public bool IsTriggerErrorNotificationEnabled
  109. {
  110. get { return _debugTrigger.ShowErrorNotification; }
  111. set { _debugTrigger.ShowErrorNotification = value; }
  112. }
  113. public bool IsProfilerDocked
  114. {
  115. get { return Service.PinnedUI.IsProfilerPinned; }
  116. set { Service.PinnedUI.IsProfilerPinned = value; }
  117. }
  118. public void AddSystemInfo(InfoEntry entry, string category = "Default")
  119. {
  120. _informationService.Add(entry, category);
  121. }
  122. public void ShowDebugPanel(bool requireEntryCode = true)
  123. {
  124. if (requireEntryCode && _entryCode.HasValue && !_hasAuthorised)
  125. {
  126. PromptEntryCode();
  127. return;
  128. }
  129. _debugPanelService.IsVisible = true;
  130. }
  131. public void ShowDebugPanel(DefaultTabs tab, bool requireEntryCode = true)
  132. {
  133. if (requireEntryCode && _entryCode.HasValue && !_hasAuthorised)
  134. {
  135. _queuedTab = tab;
  136. PromptEntryCode();
  137. return;
  138. }
  139. _debugPanelService.IsVisible = true;
  140. _debugPanelService.OpenTab(tab);
  141. }
  142. public void HideDebugPanel()
  143. {
  144. _debugPanelService.IsVisible = false;
  145. }
  146. public void SetEntryCode(EntryCode newCode)
  147. {
  148. _hasAuthorised = false;
  149. _entryCode = newCode;
  150. }
  151. public void DisableEntryCode()
  152. {
  153. _entryCode = null;
  154. }
  155. public void DestroyDebugPanel()
  156. {
  157. _debugPanelService.IsVisible = false;
  158. _debugPanelService.Unload();
  159. }
  160. #region Options
  161. public void AddOptionContainer(object container)
  162. {
  163. _optionsService.AddContainer(container);
  164. }
  165. public void RemoveOptionContainer(object container)
  166. {
  167. _optionsService.RemoveContainer(container);
  168. }
  169. public void AddOption(OptionDefinition option)
  170. {
  171. if(_looseOptionContainer == null)
  172. {
  173. _looseOptionContainer = new DynamicOptionContainer();
  174. _optionsService.AddContainer(_looseOptionContainer);
  175. }
  176. _looseOptionContainer.AddOption(option);
  177. }
  178. public bool RemoveOption(OptionDefinition option)
  179. {
  180. if (_looseOptionContainer != null)
  181. {
  182. return _looseOptionContainer.RemoveOption(option);
  183. }
  184. return false;
  185. }
  186. public void PinAllOptions(string category)
  187. {
  188. foreach (var op in _optionsService.Options)
  189. {
  190. if (op.Category == category)
  191. {
  192. _pinnedUiService.Pin(op);
  193. }
  194. }
  195. }
  196. public void UnpinAllOptions(string category)
  197. {
  198. foreach (var op in _optionsService.Options)
  199. {
  200. if (op.Category == category)
  201. {
  202. _pinnedUiService.Unpin(op);
  203. }
  204. }
  205. }
  206. public void PinOption(string name)
  207. {
  208. foreach (var op in _optionsService.Options)
  209. {
  210. if (op.Name == name)
  211. {
  212. _pinnedUiService.Pin(op);
  213. }
  214. }
  215. }
  216. public void UnpinOption(string name)
  217. {
  218. foreach (var op in _optionsService.Options)
  219. {
  220. if (op.Name == name)
  221. {
  222. _pinnedUiService.Unpin(op);
  223. }
  224. }
  225. }
  226. public void ClearPinnedOptions()
  227. {
  228. _pinnedUiService.UnpinAll();
  229. }
  230. #endregion
  231. #region Bug Reporter
  232. public void ShowBugReportSheet(ActionCompleteCallback onComplete = null, bool takeScreenshot = true,
  233. string descriptionContent = null)
  234. {
  235. var popoverService = SRServiceManager.GetService<BugReportPopoverService>();
  236. if (popoverService.IsShowingPopover)
  237. {
  238. return;
  239. }
  240. popoverService.ShowBugReporter((succeed, message) =>
  241. {
  242. if (onComplete != null)
  243. {
  244. onComplete(succeed);
  245. }
  246. }, takeScreenshot, descriptionContent);
  247. }
  248. #endregion
  249. private void DebugPanelServiceOnVisibilityChanged(IDebugPanelService debugPanelService, bool b)
  250. {
  251. if (PanelVisibilityChanged == null)
  252. {
  253. return;
  254. }
  255. try
  256. {
  257. PanelVisibilityChanged(b);
  258. }
  259. catch (Exception e)
  260. {
  261. Debug.LogError("[SRDebugger] Event target threw exception (IDebugService.PanelVisiblityChanged)");
  262. Debug.LogException(e);
  263. }
  264. }
  265. private void PromptEntryCode()
  266. {
  267. SRServiceManager.GetService<IPinEntryService>()
  268. .ShowPinEntry(_entryCode.Value, SRDebugStrings.Current.PinEntryPrompt,
  269. entered =>
  270. {
  271. if (entered)
  272. {
  273. if (!Settings.Instance.RequireEntryCodeEveryTime)
  274. {
  275. _hasAuthorised = true;
  276. }
  277. if (_queuedTab.HasValue)
  278. {
  279. var t = _queuedTab.Value;
  280. _queuedTab = null;
  281. ShowDebugPanel(t, false);
  282. }
  283. else
  284. {
  285. ShowDebugPanel(false);
  286. }
  287. }
  288. _queuedTab = null;
  289. });
  290. }
  291. public RectTransform EnableWorldSpaceMode()
  292. {
  293. if (_worldSpaceTransform != null)
  294. {
  295. return _worldSpaceTransform;
  296. }
  297. if (Settings.Instance.UseDebugCamera)
  298. {
  299. throw new InvalidOperationException("UseDebugCamera cannot be enabled at the same time as EnableWorldSpaceMode.");
  300. }
  301. _debugPanelService.IsVisible = true;
  302. var root = ((DebugPanelServiceImpl) _debugPanelService).RootObject;
  303. root.Canvas.gameObject.RemoveComponentIfExists<SRRetinaScaler>();
  304. root.Canvas.gameObject.RemoveComponentIfExists<CanvasScaler>();
  305. root.Canvas.renderMode = RenderMode.WorldSpace;
  306. var rectTransform = root.Canvas.GetComponent<RectTransform>();
  307. rectTransform.sizeDelta = new Vector2(1024, 768);
  308. rectTransform.position = Vector3.zero;
  309. return _worldSpaceTransform = rectTransform;
  310. }
  311. public void SetBugReporterHandler(IBugReporterHandler bugReporterHandler)
  312. {
  313. SRServiceManager.GetService<IBugReportService>().SetHandler(bugReporterHandler);
  314. }
  315. }
  316. }