Universal - Device Time - Detector (Pro)
Using the current date and time is essential for many applications. However, relying on the clock of the user's device creates trust issues, as users can manipulate it for various purposes such as extending trial periods or gaining advantages in games. AntiCheat has introduced a way to counteract such time manipulation.
Detector
The 'DeviceTimeCheatingDetector' is used to detect device or system time manipulation. It observes the 'DeviceTimeMonitor' and subscribes to time deviations; based on these it calculates the possibility of cheating and notifies observers of the detected cheating. It also provides a trustworthy DateTime.UtcNow, calculated either from the internet time or the device time, provided in the ProtectedTime class.
Observed Subject
The detector observes the 'DeviceTimeStatus' sent by the 'DeviceTimeMonitor'.
Detector Status
The detector receives device time deviations and decides whether the device time was paused, sped up, slowed down, or runs as planned. If a deviation, and so cheating, is 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 'DeviceTimeMonitor' (if not already done) and the 'DeviceTimeCheatingDetector' to a child GameObject of the 'AntiCheat-Monitor' and define a reaction on cheating detection.
Add Monitor Component
To detect any kind of device time manipulation, a source of device time deviation data is required. To do so, use the 'DeviceTimeMonitor' and attach it to the same GameObject you will attach the 'DeviceTimeCheatingDetector' to. The detector will subscribe to the 'DeviceTimeStatus', containing the device time deviation, sent by the monitor.
Add Detector Component
Manual
Add the 'DeviceTimeCheatingDetector' MonoBehavior from the 'GUPS.AntiCheat.Detector' namespace to your 'AntiCheat-Monitor' GameObject, or better, to a child GameObject, next to your 'DeviceTimeMonitor' MonoBehavior.

Add the 'DeviceTimeCheatingDetector' as a 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 'Device Time Cheating Detector' prefab to the 'AntiCheat-Monitor'.
Settings
After attaching the 'DeviceTimeCheatingDetector' MonoBehavior to a GameObject, you will see the following in the inspector:

The settings of the 'DeviceTimeCheatingDetector' Component.
- General Settings: Define here whether the detector should be active.
- Threat Rating Settings: Define here the severity of the detected cheating.
- Observable Settings: Add here callbacks invoked when cheating is detected.
- Device Time Settings: Apply here the settings used to provide a trustworthy DateTime.UtcNow.
Runtime
In the Awake callback, the detector observes the 'DeviceTimeMonitor' and subscribes to time deviations; based on these it calculates the possibility of cheating and notifies observers of the detected cheating. It also provides a trustworthy DateTime.UtcNow, calculated either from the internet time or the device time, provided in the ProtectedTime class.
An abstract of the ProtectedTime class:
// Instead of using DateTime.UtcNow, use the UtcNow from GUPS.AntiCheat.Protected.Time.ProtectedTime.
public static class ProtectedTime
{
// The protected Coordinated Universal Time (UTC) DateTime (Read Only). The calculated UTC time, which may differ from the original DateTime.UtcNow
// because it is calculated to be as secure and trustworthy as possible.
public static DateTime UtcNow { get; }
}
React On Cheating
When the monitor (data provider) and detector (data validator) are set up, you surely want to react to 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 invoked as soon as the specific cheating is detected.

A list of callbacks invoked when cheating is detected by the detector.
Code
If you would like to write a custom listener for the detector, you can attach an observer:
// Get the detector.
var detector = AntiCheatMonitor.Instance
.GetDetector<DeviceTimeCheatingDetector>();
// Subscribe as observer and get notified on inconsistency.
detector.Subscribe(myObserver);
The detector also has an inherited property 'PossibleCheatingDetected' which is set to true once cheating has been detected.