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.

563 lines
19 KiB

  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<Action> Callbacks = new List<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. 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. 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<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<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(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(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 && m_debugPackets.Count > 0)
  325. {
  326. foreach (DebugPacket packet in m_debugPackets)
  327. {
  328. if (packet != null && packet.Active)
  329. {
  330. packet.Update();
  331. if (packet.Check)
  332. {
  333. switch (packet.ConditionEvaluation)
  334. {
  335. case ConditionEvaluation.All_conditions_must_be_met:
  336. int count = 0;
  337. foreach (var packetDebugCondition in packet.DebugConditions)
  338. {
  339. if (CheckIfConditionIsMet(packetDebugCondition))
  340. {
  341. count++;
  342. }
  343. }
  344. if (count >= packet.DebugConditions.Count)
  345. {
  346. ExecuteOperationsInDebugPacket(packet);
  347. if (packet.ExecuteOnce)
  348. {
  349. m_debugPackets[m_debugPackets.IndexOf(packet)] = null;
  350. }
  351. }
  352. break;
  353. case ConditionEvaluation.Only_one_condition_has_to_be_met:
  354. foreach (var packetDebugCondition in packet.DebugConditions)
  355. {
  356. if (CheckIfConditionIsMet(packetDebugCondition))
  357. {
  358. ExecuteOperationsInDebugPacket(packet);
  359. if (packet.ExecuteOnce)
  360. {
  361. m_debugPackets[m_debugPackets.IndexOf(packet)] = null;
  362. }
  363. break;
  364. }
  365. }
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. if (m_debugPackets != null)
  373. {
  374. m_debugPackets.RemoveAll((packet) => packet == null);
  375. }
  376. }
  377. /// <summary>
  378. /// Returns true if a condition is met.
  379. /// </summary>
  380. /// <param name="debugCondition"></param>
  381. /// <returns></returns>
  382. private bool CheckIfConditionIsMet(DebugCondition debugCondition)
  383. {
  384. switch (debugCondition.Comparer)
  385. {
  386. case DebugComparer.Less_than:
  387. return GetRequestedValueFromDebugVariable(debugCondition.Variable) < debugCondition.Value;
  388. case DebugComparer.Equals_or_less_than:
  389. return GetRequestedValueFromDebugVariable(debugCondition.Variable) <= debugCondition.Value;
  390. case DebugComparer.Equals:
  391. return Mathf.Approximately(GetRequestedValueFromDebugVariable(debugCondition.Variable), debugCondition.Value);
  392. case DebugComparer.Equals_or_greater_than:
  393. return GetRequestedValueFromDebugVariable(debugCondition.Variable) >= debugCondition.Value;
  394. case DebugComparer.Greater_than:
  395. return GetRequestedValueFromDebugVariable(debugCondition.Variable) > debugCondition.Value;
  396. default:
  397. return false;
  398. }
  399. }
  400. /// <summary>
  401. /// Obtains the requested value from the specified variable.
  402. /// </summary>
  403. /// <param name="debugVariable"></param>
  404. /// <returns></returns>
  405. private float GetRequestedValueFromDebugVariable(DebugVariable debugVariable)
  406. {
  407. switch (debugVariable)
  408. {
  409. case DebugVariable.Fps:
  410. return m_fpsMonitor != null ? m_fpsMonitor.CurrentFPS : 0;
  411. case DebugVariable.Fps_Min:
  412. return m_fpsMonitor != null ? m_fpsMonitor.MinFPS : 0;
  413. case DebugVariable.Fps_Max:
  414. return m_fpsMonitor != null ? m_fpsMonitor.MaxFPS : 0;
  415. case DebugVariable.Fps_Avg:
  416. return m_fpsMonitor != null ? m_fpsMonitor.AverageFPS : 0;
  417. case DebugVariable.Ram_Allocated:
  418. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  419. case DebugVariable.Ram_Reserved:
  420. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  421. case DebugVariable.Ram_Mono:
  422. return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
  423. case DebugVariable.Audio_DB:
  424. return m_audioMonitor != null ? m_audioMonitor.MaxDB : 0;
  425. default:
  426. return 0;
  427. }
  428. }
  429. /// <summary>
  430. /// Executes the operations in the DebugPacket specified.
  431. /// </summary>
  432. /// <param name="debugPacket"></param>
  433. private void ExecuteOperationsInDebugPacket(DebugPacket debugPacket)
  434. {
  435. if (debugPacket != null)
  436. {
  437. if (debugPacket.DebugBreak)
  438. {
  439. Debug.Break();
  440. }
  441. if (debugPacket.Message != "")
  442. {
  443. string message = "[Graphy] (" + System.DateTime.Now + "): " + debugPacket.Message;
  444. switch (debugPacket.MessageType)
  445. {
  446. case MessageType.Log:
  447. Debug.Log(message);
  448. break;
  449. case MessageType.Warning:
  450. Debug.LogWarning(message);
  451. break;
  452. case MessageType.Error:
  453. Debug.LogError(message);
  454. break;
  455. }
  456. }
  457. if (debugPacket.TakeScreenshot)
  458. {
  459. string path = debugPacket.ScreenshotFileName + "_" + System.DateTime.Now + ".png";
  460. path = path.Replace("/", "-").Replace(" ", "_").Replace(":", "-");
  461. #if UNITY_2017_1_OR_NEWER
  462. ScreenCapture.CaptureScreenshot(path);
  463. #else
  464. Application.CaptureScreenshot(path);
  465. #endif
  466. }
  467. debugPacket.UnityEvents.Invoke();
  468. foreach (var callback in debugPacket.Callbacks)
  469. {
  470. if (callback != null) callback();
  471. }
  472. debugPacket.Executed();
  473. }
  474. }
  475. #endregion
  476. }
  477. }