Universal - Game Time - Detector (Pro)
In Unity, frame-based calculations rely heavily on UnityEngine.Time.deltaTime. Unfortunately, this time calculation is vulnerable to manipulation by cheat or hacking tools that allow unwanted acceleration, deceleration or interruption of your game. AntiCheat has introduced a way to step up against such time manipulation.
Detector
The 'GameTimeCheatingDetector' is used to detect game time (UnityEngine.Time) cheating. It observes the 'GameTimeMonitor' and subscribes to time deviations, based on this it calculates the possibility of cheating and notifies observers of the detected cheating. Additionally, it starts doing counter measures by calculating the game time based on system ticks, if a cheating got detected. So even if cheated, the game time will be calculated correctly and applied in GUPS.AntiCheat.Protected.Time.ProtectedTime.
Observed Subject
The detector observes the 'GameTimeStatus' sent by the 'GameTimeMonitor'.
Detector Status
The detector accumulates the received game time deviations and calculates an average to decide wether the game was paused, speed up, slowed down or runs as planned. If a deviation and so cheating was detected, the detector notifies its observers with a 'CheatingDetectionStatus'. The status contains the following properties:
- PossibilityOfFalsePositive: A value indicating the possibility of a false positive when assessing threats for the implementing subject from 0.0 to 1.0.
- ThreatRating: The threat rating associated with the detected cheating, indicating the assessed level of a potential threat.
Supported Platforms
The detector is available on all platforms.
Requirements
There are no requirements.
How To Use
The usage is quite simple, attach a 'GameTimeMonitor' (if not already done) and the 'GameTimeCheatingDetector' to a child GameObject of the 'AntiCheat-Monitor' and define a reaction on cheating detection.
Add Monitor Component
To detect any kind of game time manipulation, an origin of game time deviation data is required. To do so use the 'GameTimeMonitor' and attach it to the same GameObject you will attach the 'GameTimeCheatingDetector' to. The detector will subscribe to the 'GameTimeStatus', containing the game time deviation, send by the monitor.
Add Detector Component
Manual
Add the 'GameTimeCheatingDetector' MonoBehavior from the 'GUPS.AntiCheat.Detector' namespace to your 'AntiCheat-Monitor' GameObject or better to a child GameObject, next to your 'GameTimeMonitor' MonoBehavior.
Add the 'GameTimeCheatingDetector' as Component.
Prefab
There is also a prefab, including the detector and the monitors, which you can directly attach as a GameObject to the 'AntiCheat-Monitor'.
Add the 'Game Time Cheating Detector' prefab to the 'AntiCheat-Monitor'.
Settings
After attaching the 'GameTimeCheatingDetector' MonoBehavior to a GameObject, you will see the following in the inspector:
The settings of the 'GameTimeCheatingDetector' Component.
- General Settings: Define here if the detector should be active.
- Threat Rating Settings: Define here the severity of the detected cheating.
- Observable Settings: Add here callbacks invoken when a cheating got detected.
Runtime
On the Awake callback the detector observes the 'GameTimeMonitor' and subscribes to time deviations, based on this it calculates the possibility of cheating and notifies observers of the detected cheating. It also provides a trustworthy game time, provided in the ProtectedTime class.
An abstract of the ProtectedTime class:
// Replace your usage of the UnityEngine.Time class with GUPS.AntiCheat.Protected.Time.ProtectedTime.
public static class ProtectedTime
{
// The time in seconds it took to complete the last frame (Read Only).
public static float deltaTime { get; }
// The real time in seconds since the game started (Read Only).
public static float realtimeSinceStartup { get; }
// The 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; }
// The scale at which the time is passing. This can be used for slow motion effects.
public static float timeScale { get; set; }
// The 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; }
// The timeScale-independent interval in seconds from the last frame to the current one (Read Only).
public static float unscaledDeltaTime { get; }
// The 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; }
}
React On Cheating
When the monitor (data provider) and detector (data validator) are set up, you sure want to react on detected cheating.
Punisher
In general, any cheat detected is forwarded to the 'AntiCheat-Monitor', which calculates an overall threat level. Based on the threat level, you can apply punishments by using Punisher components added to a child GameObject of the 'AntiCheat-Monitor'. There are some built-in punishers that you can find here as prefabs:
The location of the built-in Punisher prefabs.
Inspector
You can set a callback in the Unity Inspector view of the detector. This callback is invoken as soon as the specific cheating is detected.
A list of callbacks invoken when cheating got detected from the detector.
Code
If you would like to write a custom listener to the detector, you can attach an observer:
// Get the detector.
var detector = AntiCheatMonitor.Instance
.GetDetector<GameTimeCheatingDetector>();
// Subscribe as observer and get notified on inconsistency.
detector.Subscribe(myObserver);
Also the detector has an inherited property 'PossibleCheatingDetected' which is set to true once a cheating got detected.