// System using System; // Unity using UnityEngine; // GUPS - AntiCheat using GUPS.AntiCheat.Detector; namespace GUPS.AntiCheat.Protected.Time { /// /// Represents a set of protected time-related properties and methods, safeguarded against cheating. Use this ProtectedTime class instead of UnityEngine.Time /// or System.DateTime to access time-related values while protecting against cheating. /// /// /// /// The class provides access to various time-related properties while protecting against cheating. It incorporates detection /// mechanisms to ensure the integrity of time-related values and prevents manipulation by external tools or hacks. /// /// /// The class includes properties for accessing delta time, unscaled delta time, time scale, time, unscaled time, time since level load, realtime since /// startup, and Coordinated Universal Time (UTC). These properties are backed by detectors from the to detect and mitigate /// cheating attempts. /// /// public sealed class ProtectedTime { /// /// The detector for the game time cheating. /// private static GameTimeCheatingDetector gameTimeCheatingDetector; /// /// The detector for the device time cheating. /// private static DeviceTimeCheatingDetector deviceTimeCheatingDetector; /// /// The protected time in seconds it took to complete the last frame (Read Only). /// public static float deltaTime { get { // Get the detector from the AntiCheatMonitor. if(gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.deltaTime; } // ...otherwise return the protected value. return gameTimeCheatingDetector.DeltaTime; } } /// /// The protected timeScale-independent interval in seconds from the last frame to the current one (Read Only). /// public static float unscaledDeltaTime { get { // Get the detector from the AntiCheatMonitor. if (gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.unscaledDeltaTime; } // ...otherwise return the protected value. return gameTimeCheatingDetector.UnscaledDeltaTime; } } /// /// The protected scale at which the time is passing. This can be used for slow motion effects. /// public static float timeScale { get { return UnityEngine.Time.timeScale; } set { UnityEngine.Time.timeScale = value; } } /// /// The protected time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game. /// public static float time { get { // Get the detector from the AntiCheatMonitor. if (gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.time; } // ...otherwise return the protected value. return gameTimeCheatingDetector.Time; } } /// /// The protected timeScale-independent time for this frame (Read Only). This is the time in seconds since the start of the game. /// public static float unscaledTime { get { // Get the detector from the AntiCheatMonitor. if (gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.unscaledTime; } // ...otherwise return the protected value. return gameTimeCheatingDetector.UnscaledTime; } } /// /// The protected time this frame has started (Read Only). This is the time in seconds since the last level has been loaded. /// public static float timeSinceLevelLoad { get { // Get the detector from the AntiCheatMonitor. if (gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.timeSinceLevelLoad; } // ...otherwise return the protected value. return gameTimeCheatingDetector.TimeSinceLevelLoad; } } /// /// The protected real time in seconds since the game started (Read Only). /// public static float realtimeSinceStartup { get { // Get the detector from the AntiCheatMonitor. if (gameTimeCheatingDetector == null) { gameTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (gameTimeCheatingDetector == null) { return UnityEngine.Time.realtimeSinceStartup; } // ...otherwise return the protected value. return gameTimeCheatingDetector.RealtimeSinceStartup; } } /// /// The protected Coordinated Universal Time (UTC) DateTime (Read Only). The calculated utc time, which may differs from the original DateTime.UtcNow /// because it is calculated to be secure and trustable as possible. /// public static DateTime UtcNow { get { // Get the detector from the AntiCheatMonitor. if (deviceTimeCheatingDetector == null) { deviceTimeCheatingDetector = AntiCheatMonitor.Instance.GetDetector(); } // If the detector is still null, return the default... if (deviceTimeCheatingDetector == null) { return DateTime.UtcNow; } // ...otherwise return the protected value. return deviceTimeCheatingDetector.CurrentUtcTime; } } } }