123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- /* ---------------------------------------
- * Author: Martin Pane (martintayx@gmail.com) (@tayx94)
- * Collaborators: Lars Aalbertsen (@Rockylars)
- * Project: Graphy - Ultimate Stats Monitor
- * Date: 23-Dec-17
- * Studio: Tayx
- *
- * This project is released under the MIT license.
- * Attribution is not required, but it is always welcomed!
- * -------------------------------------*/
- using UnityEngine;
- using UnityEngine.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Tayx.Graphy.Audio;
- using Tayx.Graphy.Fps;
- using Tayx.Graphy.Ram;
- using Tayx.Graphy.Utils;
- namespace Tayx.Graphy
- {
- public class GraphyDebugger : G_Singleton<GraphyDebugger>
- {
- /* ----- TODO: ----------------------------
- * Add summaries to the variables.
- * Add summaries to the functions.
- * Ask why we're not using System.Serializable instead for the helper class.
- * Simplify the initializers of the DebugPackets, but check wether we should as some wont work with certain lists.
- * --------------------------------------*/
- protected GraphyDebugger () { }
- #region Enums -> Public
- public enum DebugVariable
- {
- Fps,
- Fps_Min,
- Fps_Max,
- Fps_Avg,
- Ram_Allocated,
- Ram_Reserved,
- Ram_Mono,
- Audio_DB
- }
- public enum DebugComparer
- {
- Less_than,
- Equals_or_less_than,
- Equals,
- Equals_or_greater_than,
- Greater_than
- }
- public enum ConditionEvaluation
- {
- All_conditions_must_be_met,
- Only_one_condition_has_to_be_met,
- }
- public enum MessageType
- {
- Log,
- Warning,
- Error
- }
- #endregion
- #region Structs -> Public
- [Serializable]
- public struct DebugCondition
- {
- [Tooltip("Variable to compare against")]
- public DebugVariable Variable;
- [Tooltip("Comparer operator to use")]
- public DebugComparer Comparer;
- [Tooltip("Value to compare against the chosen variable")]
- public float Value;
- }
- #endregion
- #region Helper Classes
- [Serializable]
- public class DebugPacket
- {
- [Tooltip("If false, it won't be checked")]
- public bool Active = true;
- [Tooltip("Optional Id. It's used to get or remove DebugPackets in runtime")]
- public int Id;
- [Tooltip("If true, once the actions are executed, this DebugPacket will delete itself")]
- public bool ExecuteOnce = true;
- [Tooltip("Time to wait before checking if conditions are met (use this to avoid low fps drops triggering the conditions when loading the game)")]
- public float InitSleepTime = 2;
- [Tooltip("Time to wait before checking if conditions are met again (once they have already been met and if ExecuteOnce is false)")]
- public float ExecuteSleepTime = 2;
- public ConditionEvaluation ConditionEvaluation = ConditionEvaluation.All_conditions_must_be_met;
- [Tooltip("List of conditions that will be checked each frame")]
- public List<DebugCondition> DebugConditions = new List<DebugCondition>();
- // Actions on conditions met
- public MessageType MessageType;
- [Multiline]
- public string Message = string.Empty;
- public bool TakeScreenshot = false;
- public string ScreenshotFileName = "Graphy_Screenshot";
- [Tooltip("If true, it pauses the editor")]
- public bool DebugBreak = false;
- public UnityEvent UnityEvents;
- public List<System.Action> Callbacks = new List<System.Action>();
- private bool canBeChecked = false;
- private bool executed = false;
- private float timePassed = 0;
-
- public bool Check { get { return canBeChecked; } }
- public void Update()
- {
- if (!canBeChecked)
- {
- timePassed += Time.deltaTime;
- if ( (executed && timePassed >= ExecuteSleepTime)
- || (!executed && timePassed >= InitSleepTime))
- {
- canBeChecked = true;
- timePassed = 0;
- }
- }
- }
- public void Executed()
- {
- canBeChecked = false;
- executed = true;
- }
- }
- #endregion
- #region Variables -> Serialized Private
- [SerializeField] private List<DebugPacket> m_debugPackets = new List<DebugPacket>();
- #endregion
- #region Variables -> Private
- private G_FpsMonitor m_fpsMonitor = null;
- private G_RamMonitor m_ramMonitor = null;
- private G_AudioMonitor m_audioMonitor = null;
- #endregion
- #region Methods -> Unity Callbacks
- private void Start()
- {
- m_fpsMonitor = GetComponentInChildren<G_FpsMonitor>();
- m_ramMonitor = GetComponentInChildren<G_RamMonitor>();
- m_audioMonitor = GetComponentInChildren<G_AudioMonitor>();
- }
- private void Update()
- {
- CheckDebugPackets();
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Add a new DebugPacket.
- /// </summary>
- public void AddNewDebugPacket(DebugPacket newDebugPacket)
- {
- if (m_debugPackets != null)
- {
- m_debugPackets.Add(newDebugPacket);
- }
- }
- /// <summary>
- /// Add a new DebugPacket.
- /// </summary>
- public void AddNewDebugPacket
- (
- int newId,
- DebugCondition newDebugCondition,
- MessageType newMessageType,
- string newMessage,
- bool newDebugBreak,
- System.Action newCallback
- )
- {
- DebugPacket newDebugPacket = new DebugPacket();
- newDebugPacket.Id = newId;
- newDebugPacket.DebugConditions.Add(newDebugCondition);
- newDebugPacket.MessageType = newMessageType;
- newDebugPacket.Message = newMessage;
- newDebugPacket.DebugBreak = newDebugBreak;
- newDebugPacket.Callbacks.Add(newCallback);
- AddNewDebugPacket(newDebugPacket);
- }
- /// <summary>
- /// Add a new DebugPacket.
- /// </summary>
- public void AddNewDebugPacket
- (
- int newId,
- List<DebugCondition> newDebugConditions,
- MessageType newMessageType,
- string newMessage,
- bool newDebugBreak,
- System.Action newCallback
- )
- {
- DebugPacket newDebugPacket = new DebugPacket();
- newDebugPacket.Id = newId;
- newDebugPacket.DebugConditions = newDebugConditions;
- newDebugPacket.MessageType = newMessageType;
- newDebugPacket.Message = newMessage;
- newDebugPacket.DebugBreak = newDebugBreak;
- newDebugPacket.Callbacks.Add(newCallback);
- AddNewDebugPacket(newDebugPacket);
- }
- /// <summary>
- /// Add a new DebugPacket.
- /// </summary>
- public void AddNewDebugPacket
- (
- int newId,
- DebugCondition newDebugCondition,
- MessageType newMessageType,
- string newMessage,
- bool newDebugBreak,
- List<System.Action> newCallbacks
- )
- {
- DebugPacket newDebugPacket = new DebugPacket();
- newDebugPacket.Id = newId;
- newDebugPacket.DebugConditions.Add(newDebugCondition);
- newDebugPacket.MessageType = newMessageType;
- newDebugPacket.Message = newMessage;
- newDebugPacket.DebugBreak = newDebugBreak;
- newDebugPacket.Callbacks = newCallbacks;
- AddNewDebugPacket(newDebugPacket);
- }
- /// <summary>
- /// Add a new DebugPacket.
- /// </summary>
- public void AddNewDebugPacket
- (
- int newId,
- List<DebugCondition> newDebugConditions,
- MessageType newMessageType,
- string newMessage,
- bool newDebugBreak,
- List<System.Action> newCallbacks
- )
- {
- DebugPacket newDebugPacket = new DebugPacket();
- newDebugPacket.Id = newId;
- newDebugPacket.DebugConditions = newDebugConditions;
- newDebugPacket.MessageType = newMessageType;
- newDebugPacket.Message = newMessage;
- newDebugPacket.DebugBreak = newDebugBreak;
- newDebugPacket.Callbacks = newCallbacks;
- AddNewDebugPacket(newDebugPacket);
- }
- /// <summary>
- /// Returns the first Packet with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="packetId"></param>
- /// <returns></returns>
- public DebugPacket GetFirstDebugPacketWithId(int packetId)
- {
- return m_debugPackets.First(x => x.Id == packetId);
- }
- /// <summary>
- /// Returns a list with all the Packets with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="packetId"></param>
- /// <returns></returns>
- public List<DebugPacket> GetAllDebugPacketsWithId(int packetId)
- {
- return m_debugPackets.FindAll(x => x.Id == packetId);
- }
- /// <summary>
- /// Removes the first Packet with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="packetId"></param>
- /// <returns></returns>
- public void RemoveFirstDebugPacketWithId(int packetId)
- {
- if (m_debugPackets != null && GetFirstDebugPacketWithId(packetId) != null)
- {
- m_debugPackets.Remove(GetFirstDebugPacketWithId(packetId));
- }
- }
- /// <summary>
- /// Removes all the Packets with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="packetId"></param>
- /// <returns></returns>
- public void RemoveAllDebugPacketsWithId(int packetId)
- {
- if (m_debugPackets != null)
- {
- m_debugPackets.RemoveAll(x => x.Id == packetId);
- }
- }
- /// <summary>
- /// Add an Action callback to the first Packet with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="callback"></param>
- /// <param name="id"></param>
- public void AddCallbackToFirstDebugPacketWithId(System.Action callback, int id)
- {
- if (GetFirstDebugPacketWithId(id) != null)
- {
- GetFirstDebugPacketWithId(id).Callbacks.Add(callback);
- }
- }
- /// <summary>
- /// Add an Action callback to all the Packets with the specified ID in the DebugPacket list.
- /// </summary>
- /// <param name="callback"></param>
- /// <param name="id"></param>
- public void AddCallbackToAllDebugPacketWithId(System.Action callback, int id)
- {
- if (GetAllDebugPacketsWithId(id) != null)
- {
- foreach (var debugPacket in GetAllDebugPacketsWithId(id))
- {
- if (callback != null)
- {
- debugPacket.Callbacks.Add(callback);
- }
- }
- }
- }
- #endregion
- #region Methods -> Private
- /// <summary>
- /// Checks all the Debug Packets to see if they have to be executed.
- /// </summary>
- private void CheckDebugPackets()
- {
- if (m_debugPackets == null)
- {
- return;
- }
- for (var i = 0; i < m_debugPackets.Count; i++)
- {
- DebugPacket packet = m_debugPackets[i];
- if (packet != null && packet.Active)
- {
- packet.Update();
- if (packet.Check)
- {
- switch (packet.ConditionEvaluation)
- {
- case ConditionEvaluation.All_conditions_must_be_met:
- int count = 0;
- foreach (var packetDebugCondition in packet.DebugConditions)
- {
- if (CheckIfConditionIsMet(packetDebugCondition))
- {
- count++;
- }
- }
- if (count >= packet.DebugConditions.Count)
- {
- ExecuteOperationsInDebugPacket(packet);
- if (packet.ExecuteOnce)
- {
- m_debugPackets[i] = null;
- }
- }
- break;
- case ConditionEvaluation.Only_one_condition_has_to_be_met:
- foreach (var packetDebugCondition in packet.DebugConditions)
- {
- if (CheckIfConditionIsMet(packetDebugCondition))
- {
- ExecuteOperationsInDebugPacket(packet);
- if (packet.ExecuteOnce)
- {
- m_debugPackets[i] = null;
- }
- break;
- }
- }
- break;
- }
- }
- }
- }
- m_debugPackets.RemoveAll((packet) => packet == null);
- }
- /// <summary>
- /// Returns true if a condition is met.
- /// </summary>
- /// <param name="debugCondition"></param>
- /// <returns></returns>
- private bool CheckIfConditionIsMet(DebugCondition debugCondition)
- {
- switch (debugCondition.Comparer)
- {
- case DebugComparer.Less_than:
- return GetRequestedValueFromDebugVariable(debugCondition.Variable) < debugCondition.Value;
- case DebugComparer.Equals_or_less_than:
- return GetRequestedValueFromDebugVariable(debugCondition.Variable) <= debugCondition.Value;
- case DebugComparer.Equals:
- return Mathf.Approximately(GetRequestedValueFromDebugVariable(debugCondition.Variable), debugCondition.Value);
- case DebugComparer.Equals_or_greater_than:
- return GetRequestedValueFromDebugVariable(debugCondition.Variable) >= debugCondition.Value;
- case DebugComparer.Greater_than:
- return GetRequestedValueFromDebugVariable(debugCondition.Variable) > debugCondition.Value;
- default:
- return false;
- }
- }
- /// <summary>
- /// Obtains the requested value from the specified variable.
- /// </summary>
- /// <param name="debugVariable"></param>
- /// <returns></returns>
- private float GetRequestedValueFromDebugVariable(DebugVariable debugVariable)
- {
- switch (debugVariable)
- {
- case DebugVariable.Fps:
- return m_fpsMonitor != null ? m_fpsMonitor.CurrentFPS : 0;
- case DebugVariable.Fps_Min:
- return m_fpsMonitor != null ? m_fpsMonitor.MinFPS : 0;
- case DebugVariable.Fps_Max:
- return m_fpsMonitor != null ? m_fpsMonitor.MaxFPS : 0;
- case DebugVariable.Fps_Avg:
- return m_fpsMonitor != null ? m_fpsMonitor.AverageFPS : 0;
- case DebugVariable.Ram_Allocated:
- return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
- case DebugVariable.Ram_Reserved:
- return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
- case DebugVariable.Ram_Mono:
- return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
- case DebugVariable.Audio_DB:
- return m_audioMonitor != null ? m_audioMonitor.MaxDB : 0;
- default:
- return 0;
- }
- }
- /// <summary>
- /// Executes the operations in the DebugPacket specified.
- /// </summary>
- /// <param name="debugPacket"></param>
- private void ExecuteOperationsInDebugPacket(DebugPacket debugPacket)
- {
- if (debugPacket != null)
- {
- if (debugPacket.DebugBreak)
- {
- Debug.Break();
- }
- if (debugPacket.Message != "")
- {
- string message = "[Graphy] (" + System.DateTime.Now + "): " + debugPacket.Message;
- switch (debugPacket.MessageType)
- {
- case MessageType.Log:
- Debug.Log(message);
- break;
- case MessageType.Warning:
- Debug.LogWarning(message);
- break;
- case MessageType.Error:
- Debug.LogError(message);
- break;
- }
- }
- if (debugPacket.TakeScreenshot)
- {
- string path = debugPacket.ScreenshotFileName + "_" + System.DateTime.Now + ".png";
- path = path.Replace("/", "-").Replace(" ", "_").Replace(":", "-");
- #if UNITY_2017_1_OR_NEWER
- ScreenCapture.CaptureScreenshot(path);
- #else
- Application.CaptureScreenshot(path);
- #endif
- }
- debugPacket.UnityEvents.Invoke();
- foreach (var callback in debugPacket.Callbacks)
- {
- if (callback != null) callback();
- }
- debugPacket.Executed();
- }
- }
- #endregion
- }
- }
|