GraphyDebugger.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* ---------------------------------------
  2. * Author: Martin Pane (martintayx@gmail.com) (@tayx94)
  3. * Collaborators: Lars Aalbertsen (@Rockylars)
  4. * Project: Graphy - Ultimate Stats Monitor
  5. * Date: 23-Dec-17
  6. * Studio: Tayx
  7. *
  8. * This project is released under the MIT license.
  9. * Attribution is not required, but it is always welcomed!
  10. * -------------------------------------*/
  11. using UnityEngine;
  12. using UnityEngine.Events;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using Tayx.Graphy.Audio;
  17. using Tayx.Graphy.Fps;
  18. using Tayx.Graphy.Ram;
  19. using Tayx.Graphy.Utils;
  20. namespace Tayx.Graphy
  21. {
  22. public class GraphyDebugger : G_Singleton<GraphyDebugger>
  23. {
  24. /* ----- TODO: ----------------------------
  25. * Add summaries to the variables.
  26. * Add summaries to the functions.
  27. * Ask why we're not using System.Serializable instead for the helper class.
  28. * Simplify the initializers of the DebugPackets, but check wether we should as some wont work with certain lists.
  29. * --------------------------------------*/
  30. protected GraphyDebugger () { }
  31. #region Enums -> Public
  32. public enum DebugVariable
  33. {
  34. Fps,
  35. Fps_Min,
  36. Fps_Max,
  37. Fps_Avg,
  38. Ram_Allocated,
  39. Ram_Reserved,
  40. Ram_Mono,
  41. Audio_DB
  42. }
  43. public enum DebugComparer
  44. {
  45. Less_than,
  46. Equals_or_less_than,
  47. Equals,
  48. Equals_or_greater_than,
  49. Greater_than
  50. }
  51. public enum ConditionEvaluation
  52. {
  53. All_conditions_must_be_met,
  54. Only_one_condition_has_to_be_met,
  55. }
  56. public enum MessageType
  57. {
  58. Log,
  59. Warning,
  60. Error
  61. }
  62. #endregion
  63. #region Structs -> Public
  64. [Serializable]
  65. public struct DebugCondition
  66. {
  67. [Tooltip("Variable to compare against")]
  68. public DebugVariable Variable;
  69. [Tooltip("Comparer operator to use")]
  70. public DebugComparer Comparer;
  71. [Tooltip("Value to compare against the chosen variable")]
  72. public float Value;
  73. }
  74. #endregion
  75. #region Helper Classes
  76. [Serializable]
  77. public class DebugPacket
  78. {
  79. [Tooltip("If false, it won't be checked")]
  80. public bool Active = true;
  81. [Tooltip("Optional Id. It's used to get or remove DebugPackets in runtime")]
  82. public int Id;
  83. [Tooltip("If true, once the actions are executed, this DebugPacket will delete itself")]
  84. public bool ExecuteOnce = true;
  85. [Tooltip("Time to wait before checking if conditions are met (use this to avoid low fps drops triggering the conditions when loading the game)")]
  86. public float InitSleepTime = 2;
  87. [Tooltip("Time to wait before checking if conditions are met again (once they have already been met and if ExecuteOnce is false)")]
  88. public float ExecuteSleepTime = 2;
  89. public ConditionEvaluation ConditionEvaluation = ConditionEvaluation.All_conditions_must_be_met;
  90. [Tooltip("List of conditions that will be checked each frame")]
  91. public List<DebugCondition> DebugConditions = new List<DebugCondition>();
  92. // Actions on conditions met
  93. public MessageType MessageType;
  94. [Multiline]
  95. public string Message = string.Empty;
  96. public bool TakeScreenshot = false;
  97. public string ScreenshotFileName = "Graphy_Screenshot";
  98. [Tooltip("If true, it pauses the editor")]
  99. public bool DebugBreak = false;
  100. public UnityEvent UnityEvents;
  101. public List<System.Action> Callbacks = new List<System.Action>();
  102. private bool canBeChecked = false;
  103. private bool executed = false;
  104. private float timePassed = 0;
  105. public bool Check { get { return canBeChecked; } }
  106. public void Update()
  107. {
  108. if (!canBeChecked)
  109. {
  110. timePassed += Time.deltaTime;
  111. if ( (executed && timePassed >= ExecuteSleepTime)
  112. || (!executed && timePassed >= InitSleepTime))
  113. {
  114. canBeChecked = true;
  115. timePassed = 0;
  116. }
  117. }
  118. }
  119. public void Executed()
  120. {
  121. canBeChecked = false;
  122. executed = true;
  123. }
  124. }
  125. #endregion
  126. #region Variables -> Serialized Private
  127. [SerializeField] private List<DebugPacket> m_debugPackets = new List<DebugPacket>();
  128. #endregion
  129. #region Variables -> Private
  130. private G_FpsMonitor m_fpsMonitor = null;
  131. private G_RamMonitor m_ramMonitor = null;
  132. private G_AudioMonitor m_audioMonitor = null;
  133. #endregion
  134. #region Methods -> Unity Callbacks
  135. private void Start()
  136. {
  137. m_fpsMonitor = GetComponentInChildren<G_FpsMonitor>();
  138. m_ramMonitor = GetComponentInChildren<G_RamMonitor>();
  139. m_audioMonitor = GetComponentInChildren<G_AudioMonitor>();
  140. }
  141. private void Update()
  142. {
  143. CheckDebugPackets();
  144. }
  145. #endregion
  146. #region Public Methods
  147. /// <summary>
  148. /// Add a new DebugPacket.
  149. /// </summary>
  150. public void AddNewDebugPacket(DebugPacket newDebugPacket)
  151. {
  152. if (m_debugPackets != null)
  153. {
  154. m_debugPackets.Add(newDebugPacket);
  155. }
  156. }
  157. /// <summary>
  158. /// Add a new DebugPacket.
  159. /// </summary>
  160. public void AddNewDebugPacket
  161. (
  162. int newId,
  163. DebugCondition newDebugCondition,
  164. MessageType newMessageType,
  165. string newMessage,
  166. bool newDebugBreak,
  167. System.Action newCallback
  168. )
  169. {
  170. DebugPacket newDebugPacket = new DebugPacket();
  171. newDebugPacket.Id = newId;
  172. newDebugPacket.DebugConditions.Add(newDebugCondition);
  173. newDebugPacket.MessageType = newMessageType;
  174. newDebugPacket.Message = newMessage;
  175. newDebugPacket.DebugBreak = newDebugBreak;
  176. newDebugPacket.Callbacks.Add(newCallback);
  177. AddNewDebugPacket(newDebugPacket);
  178. }
  179. /// <summary>
  180. /// Add a new DebugPacket.
  181. /// </summary>
  182. public void AddNewDebugPacket
  183. (
  184. int newId,
  185. List<DebugCondition> newDebugConditions,
  186. MessageType newMessageType,
  187. string newMessage,
  188. bool newDebugBreak,
  189. System.Action newCallback
  190. )
  191. {
  192. DebugPacket newDebugPacket = new DebugPacket();
  193. newDebugPacket.Id = newId;
  194. newDebugPacket.DebugConditions = newDebugConditions;
  195. newDebugPacket.MessageType = newMessageType;
  196. newDebugPacket.Message = newMessage;
  197. newDebugPacket.DebugBreak = newDebugBreak;
  198. newDebugPacket.Callbacks.Add(newCallback);
  199. AddNewDebugPacket(newDebugPacket);
  200. }
  201. /// <summary>
  202. /// Add a new DebugPacket.
  203. /// </summary>
  204. public void AddNewDebugPacket
  205. (
  206. int newId,
  207. DebugCondition newDebugCondition,
  208. MessageType newMessageType,
  209. string newMessage,
  210. bool newDebugBreak,
  211. List<System.Action> newCallbacks
  212. )
  213. {
  214. DebugPacket newDebugPacket = new DebugPacket();
  215. newDebugPacket.Id = newId;
  216. newDebugPacket.DebugConditions.Add(newDebugCondition);
  217. newDebugPacket.MessageType = newMessageType;
  218. newDebugPacket.Message = newMessage;
  219. newDebugPacket.DebugBreak = newDebugBreak;
  220. newDebugPacket.Callbacks = newCallbacks;
  221. AddNewDebugPacket(newDebugPacket);
  222. }
  223. /// <summary>
  224. /// Add a new DebugPacket.
  225. /// </summary>
  226. public void AddNewDebugPacket
  227. (
  228. int newId,
  229. List<DebugCondition> newDebugConditions,
  230. MessageType newMessageType,
  231. string newMessage,
  232. bool newDebugBreak,
  233. List<System.Action> newCallbacks
  234. )
  235. {
  236. DebugPacket newDebugPacket = new DebugPacket();
  237. newDebugPacket.Id = newId;
  238. newDebugPacket.DebugConditions = newDebugConditions;
  239. newDebugPacket.MessageType = newMessageType;
  240. newDebugPacket.Message = newMessage;
  241. newDebugPacket.DebugBreak = newDebugBreak;
  242. newDebugPacket.Callbacks = newCallbacks;
  243. AddNewDebugPacket(newDebugPacket);
  244. }
  245. /// <summary>
  246. /// Returns the first Packet with the specified ID in the DebugPacket list.
  247. /// </summary>
  248. /// <param name="packetId"></param>
  249. /// <returns></returns>
  250. public DebugPacket GetFirstDebugPacketWithId(int packetId)
  251. {
  252. return m_debugPackets.First(x => x.Id == packetId);
  253. }
  254. /// <summary>
  255. /// Returns a list with all the Packets with the specified ID in the DebugPacket list.
  256. /// </summary>
  257. /// <param name="packetId"></param>
  258. /// <returns></returns>
  259. public List<DebugPacket> GetAllDebugPacketsWithId(int packetId)
  260. {
  261. return m_debugPackets.FindAll(x => x.Id == packetId);
  262. }
  263. /// <summary>
  264. /// Removes the first Packet with the specified ID in the DebugPacket list.
  265. /// </summary>
  266. /// <param name="packetId"></param>
  267. /// <returns></returns>
  268. public void RemoveFirstDebugPacketWithId(int packetId)
  269. {
  270. if (m_debugPackets != null && GetFirstDebugPacketWithId(packetId) != null)
  271. {
  272. m_debugPackets.Remove(GetFirstDebugPacketWithId(packetId));
  273. }
  274. }
  275. /// <summary>
  276. /// Removes all the Packets with the specified ID in the DebugPacket list.
  277. /// </summary>
  278. /// <param name="packetId"></param>
  279. /// <returns></returns>
  280. public void RemoveAllDebugPacketsWithId(int packetId)
  281. {
  282. if (m_debugPackets != null)
  283. {
  284. m_debugPackets.RemoveAll(x => x.Id == packetId);
  285. }
  286. }
  287. /// <summary>
  288. /// Add an Action callback to the first Packet with the specified ID in the DebugPacket list.
  289. /// </summary>
  290. /// <param name="callback"></param>
  291. /// <param name="id"></param>
  292. public void AddCallbackToFirstDebugPacketWithId(System.Action callback, int id)
  293. {
  294. if (GetFirstDebugPacketWithId(id) != null)
  295. {
  296. GetFirstDebugPacketWithId(id).Callbacks.Add(callback);
  297. }
  298. }
  299. /// <summary>
  300. /// Add an Action callback to all the Packets with the specified ID in the DebugPacket list.
  301. /// </summary>
  302. /// <param name="callback"></param>
  303. /// <param name="id"></param>
  304. public void AddCallbackToAllDebugPacketWithId(System.Action callback, int id)
  305. {
  306. if (GetAllDebugPacketsWithId(id) != null)
  307. {
  308. foreach (var debugPacket in GetAllDebugPacketsWithId(id))
  309. {
  310. if (callback != null)
  311. {
  312. debugPacket.Callbacks.Add(callback);
  313. }
  314. }
  315. }
  316. }
  317. #endregion
  318. #region Methods -> Private
  319. /// <summary>
  320. /// Checks all the Debug Packets to see if they have to be executed.
  321. /// </summary>
  322. private void CheckDebugPackets()
  323. {
  324. if (m_debugPackets == null)
  325. {
  326. return;
  327. }
  328. for (var i = 0; i < m_debugPackets.Count; i++)
  329. {
  330. DebugPacket packet = m_debugPackets[i];
  331. if (packet != null && packet.Active)
  332. {
  333. packet.Update();
  334. if (packet.Check)
  335. {
  336. switch (packet.ConditionEvaluation)
  337. {
  338. case ConditionEvaluation.All_conditions_must_be_met:
  339. int count = 0;
  340. foreach (var packetDebugCondition in packet.DebugConditions)
  341. {
  342. if (CheckIfConditionIsMet(packetDebugCondition))
  343. {
  344. count++;
  345. }
  346. }
  347. if (count >= packet.DebugConditions.Count)
  348. {
  349. ExecuteOperationsInDebugPacket(packet);
  350. if (packet.ExecuteOnce)
  351. {
  352. m_debugPackets[i] = null;
  353. }
  354. }
  355. break;
  356. case ConditionEvaluation.Only_one_condition_has_to_be_met:
  357. foreach (var packetDebugCondition in packet.DebugConditions)
  358. {
  359. if (CheckIfConditionIsMet(packetDebugCondition))
  360. {
  361. ExecuteOperationsInDebugPacket(packet);
  362. if (packet.ExecuteOnce)
  363. {
  364. m_debugPackets[i] = null;
  365. }
  366. break;
  367. }
  368. }
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. m_debugPackets.RemoveAll((packet) => packet == null);
  375. }
  376. /// <summary>
  377. /// Returns true if a condition is met.
  378. /// </summary>
  379. /// <param name="debugCondition"></param>
  380. /// <returns></returns>
  381. private bool CheckIfConditionIsMet(DebugCondition debugCondition)
  382. {
  383. switch (debugCondition.Comparer)
  384. {
  385. case DebugComparer.Less_than:
  386. return GetRequestedValueFromDebugVariable(debugCondition.Variable) < debugCondition.Value;
  387. case DebugComparer.Equals_or_less_than:
  388. return GetRequestedValueFromDebugVariable(debugCondition.Variable) <= debugCondition.Value;
  389. case DebugComparer.Equals:
  390. return Mathf.Approximately(GetRequestedValueFromDebugVariable(debugCondition.Variable), debugCondition.Value);
  391. case DebugComparer.Equals_or_greater_than:
  392. return GetRequestedValueFromDebugVariable(debugCondition.Variable) >= debugCondition.Value;
  393. case DebugComparer.Greater_than:
  394. return GetRequestedValueFromDebugVariable(debugCondition.Variable) > debugCondition.Value;
  395. default:
  396. return false;
  397. }
  398. }
  399. /// <summary>
  400. /// Obtains the requested value from the specified variable.
  401. /// </summary>
  402. /// <param name="debugVariable"></param>
  403. /// <returns></returns>
  404. private float GetRequestedValueFromDebugVariable(DebugVariable debugVariable)
  405. {
  406. switch (debugVariable)
  407. {
  408. case DebugVariable.Fps:
  409. return m_fpsMonitor != null ? m_fpsMonitor.CurrentFPS : 0;
  410. case DebugVariable.Fps_Min:
  411. return m_fpsMonitor != null ? m_fpsMonitor.MinFPS : 0;
  412. case DebugVariable.Fps_Max:
  413. return m_fpsMonitor != null ? m_fpsMonitor.MaxFPS : 0;
  414. case DebugVariable.Fps_Avg:
  415. return m_fpsMonitor != null ? m_fpsMonitor.AverageFPS : 0;
  416. case DebugVariable.Ram_Allocated:
  417. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  418. case DebugVariable.Ram_Reserved:
  419. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  420. case DebugVariable.Ram_Mono:
  421. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  422. case DebugVariable.Audio_DB:
  423. return m_audioMonitor != null ? m_audioMonitor.MaxDB : 0;
  424. default:
  425. return 0;
  426. }
  427. }
  428. /// <summary>
  429. /// Executes the operations in the DebugPacket specified.
  430. /// </summary>
  431. /// <param name="debugPacket"></param>
  432. private void ExecuteOperationsInDebugPacket(DebugPacket debugPacket)
  433. {
  434. if (debugPacket != null)
  435. {
  436. if (debugPacket.DebugBreak)
  437. {
  438. Debug.Break();
  439. }
  440. if (debugPacket.Message != "")
  441. {
  442. string message = "[Graphy] (" + System.DateTime.Now + "): " + debugPacket.Message;
  443. switch (debugPacket.MessageType)
  444. {
  445. case MessageType.Log:
  446. Debug.Log(message);
  447. break;
  448. case MessageType.Warning:
  449. Debug.LogWarning(message);
  450. break;
  451. case MessageType.Error:
  452. Debug.LogError(message);
  453. break;
  454. }
  455. }
  456. if (debugPacket.TakeScreenshot)
  457. {
  458. string path = debugPacket.ScreenshotFileName + "_" + System.DateTime.Now + ".png";
  459. path = path.Replace("/", "-").Replace(" ", "_").Replace(":", "-");
  460. #if UNITY_2017_1_OR_NEWER
  461. ScreenCapture.CaptureScreenshot(path);
  462. #else
  463. Application.CaptureScreenshot(path);
  464. #endif
  465. }
  466. debugPacket.UnityEvents.Invoke();
  467. foreach (var callback in debugPacket.Callbacks)
  468. {
  469. if (callback != null) callback();
  470. }
  471. debugPacket.Executed();
  472. }
  473. }
  474. #endregion
  475. }
  476. }