Anti-Cheat Toolkit  2023.2.6
Multi-purpose anti-cheat solution for Unity Engine
TimeCheatingDetector Class Reference

Inherits ACTkDetectorBase< TimeCheatingDetector >.

Classes

struct  OnlineTimeResult
 

Public Types

enum class  CheckResult {
  Unknown = 0 , CheckPassed = 5 , WrongTimeDetected = 10 , CheatDetected = 15 ,
  Error = 100
}
 
enum class  ErrorKind {
  NoError = 0 , IncorrectUri = 3 , OnlineTimeError = 5 , NotStarted = 10 ,
  AlreadyCheckingForCheat = 15 , Unknown = 100
}
 
enum class  RequestMethod { Head , Get }
 

Public Member Functions

delegate void OnlineTimeCallback (OnlineTimeResult result)
 
delegate void TimeCheatingDetectorEventHandler (CheckResult result, ErrorKind error)
 
void TriggerDetection ()
 
bool ForceCheck ()
 
IEnumerator ForceCheckEnumerator ()
 
async Task< CheckResultForceCheckTask ()
 
bool IsReadyForForceCheck ()
 

Static Public Member Functions

static TimeCheatingDetector AddToSceneOrGetExisting ()
 
static TimeCheatingDetector StartDetection (TimeCheatingDetectorEventHandler cheatCheckedEventHandler=null)
 
static TimeCheatingDetector StartDetection (float intervalMinutes, TimeCheatingDetectorEventHandler cheatCheckedEventHandler=null)
 
static void StopDetection ()
 
static void Dispose ()
 
static IEnumerator GetOnlineTimeCoroutine (string url, OnlineTimeCallback callback, RequestMethod method=RequestMethod.Head)
 
static IEnumerator GetOnlineTimeCoroutine (Uri uri, OnlineTimeCallback callback, RequestMethod method=RequestMethod.Head)
 
static Task< OnlineTimeResultGetOnlineTimeTask (string url, RequestMethod method=RequestMethod.Head)
 
static Task< OnlineTimeResultGetOnlineTimeTask (string url, System.Threading.CancellationToken cancellationToken, RequestMethod method=RequestMethod.Head)
 
static Task< OnlineTimeResultGetOnlineTimeTask (Uri uri, RequestMethod method=RequestMethod.Head)
 
static async Task< OnlineTimeResultGetOnlineTimeTask (Uri uri, System.Threading.CancellationToken cancellationToken, RequestMethod method=RequestMethod.Head)
 

Public Attributes

RequestMethod requestMethod = RequestMethod.Head
 
int timeoutSeconds = 10
 
float interval = 5
 
int realCheatThreshold = 65
 
int wrongTimeThreshold = 65
 
bool ignoreSetCorrectTime = true
 
bool autoStart
 
bool autoDispose
 

Properties

string RequestUrl [get, set]
 
ErrorKind LastError [get]
 
CheckResult LastResult [get]
 
OnlineTimeResult LastOnlineTimeResult [get]
 
bool IsCheckingForCheat [get]
 
bool IsCheatDetected [get, protected set]
 
bool IsStarted [get, protected set]
 
bool IsRunning [get, protected set]
 

Events

TimeCheatingDetectorEventHandler CheatChecked
 
Action CheatDetected
 

Detailed Description

Allows to detect time cheating using time from any properly configured server (almost all servers around the web).

Requires Internet connection and appropriate 'android.permission.INTERNET' permission when used on Android platform.
Automatically switches to the current domain on WebGL (if RequestUrl leads to any external resource) to avoid CORS limitation.
Make sure your server returns correct Date response header value in case of problems.

Doesn't detects cheating if there is no Internet connection, should work even with weak connection though.
Just add it to any GameObject as usual or through the "GameObject > Create Other > Code Stage > Anti-Cheat Toolkit" menu to get started.
You can use detector completely from inspector without writing any code except the actual reaction on cheating.

Avoid using detectors from code at the Awake phase.

Member Enumeration Documentation

◆ CheckResult

enum CheckResult
strong

Describes possible detector check results.

Enumerator
Unknown 

Cheat detection was not started yet or is in progress.

CheckPassed 

Cheat check successfully passed and nor cheating nor wrong time was detected.

WrongTimeDetected 

Direct time cheating was not detected but difference between local and online UTC clocks exceeds the wrongTimeThreshold.

CheatDetected 

Subsequent measurements difference of local and online time difference exceeded the realCheatThreshold.

Error 

There was error while making cheat check, please check LastError for details.

◆ ErrorKind

enum ErrorKind
strong

Describes possible detector errors.

Enumerator
NoError 

Indicates there were no any error registered.

IncorrectUri 

Url set for the online time receiving is not a correct URI.

OnlineTimeError 

Error while receiving online time (check logs for error details).

NotStarted 

Detector was not started yet. It should be started before performing any cheat checks.

AlreadyCheckingForCheat 

Detector already checks for the cheat. Please make sure IsCheckingForCheat == false before trying to force another cheat check.

Unknown 

Something strange happened, please check logs for details.

◆ RequestMethod

enum RequestMethod
strong

Method of the request to the server. Please consider Head by default and fall back to Get in case of problems.

Some servers do not like HEAD requests and sometimes treat it as a malicious bot activity and may temporary block the caller.
For such servers use GET method as a more compatible yet slower and loading all page data.

Enumerator
Head 

Preferable method as it requests only headers thus it runs faster and has minimal possible traffic.

Get 

More compatible method which loads whole content at the given URL.

Member Function Documentation

◆ OnlineTimeCallback()

delegate void OnlineTimeCallback ( OnlineTimeResult  result)

Delegate with result of online time receive attempt.

Parameters
resultOnlineTimeResult instance with details about operation result.

◆ TimeCheatingDetectorEventHandler()

delegate void TimeCheatingDetectorEventHandler ( CheckResult  result,
ErrorKind  error 
)

Delegate with cheat check result.

Parameters
resultResult of the cheat check.
errorKind of occured error, if any.

◆ AddToSceneOrGetExisting()

static TimeCheatingDetector AddToSceneOrGetExisting ( )
inlinestatic

Creates new instance of the detector at scene if it doesn't exists. Make sure to call NOT from Awake phase.

Returns
New or existing instance of the detector.

◆ StartDetection() [1/2]

static TimeCheatingDetector StartDetection ( TimeCheatingDetectorEventHandler  cheatCheckedEventHandler = null)
inlinestatic

Starts detection with specified callback.

If you have detector in scene make sure it has empty Detection Event.
Creates a new detector instance if it doesn't exists in scene.

Parameters
cheatCheckedEventHandlerMethod to call after each cheat check. Pass null if you wish to use event, set in detector inspector.

◆ StartDetection() [2/2]

static TimeCheatingDetector StartDetection ( float  intervalMinutes,
TimeCheatingDetectorEventHandler  cheatCheckedEventHandler = null 
)
inlinestatic

Starts detection with specified callback using passed interval.

If you pass cheatCheckedCallback than make sure you have no filled Detection Event on detector instance in scene to avoid duplicate event calls.
Creates a new detector instance if it doesn't exists in scene.

Parameters
intervalMinutesTime in minutes between checks. Overrides interval property.
cheatCheckedEventHandlerMethod to call after each cheat check. Pass null if you wish to use event, set in detector inspector.

◆ StopDetection()

static void StopDetection ( )
inlinestatic

- Stops detector. Detector's component remains in the scene. Use Dispose() to completely remove detector.

◆ Dispose()

static void Dispose ( )
inlinestatic

Stops and completely disposes detector component.

On dispose Detector follows 2 rules:

  • if Game Object's name is "Anti-Cheat Toolkit Detectors": it will be automatically destroyed if no other Detectors left attached regardless of any other components or children;
  • if Game Object's name is NOT "Anti-Cheat Toolkit Detectors": it will be automatically destroyed only if it has neither other components nor children attached;

◆ GetOnlineTimeCoroutine() [1/2]

static IEnumerator GetOnlineTimeCoroutine ( string  url,
OnlineTimeCallback  callback,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously in coroutine.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
urlAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
callbackDelegate to call and pass OnlineTimeResult to.
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.

◆ GetOnlineTimeCoroutine() [2/2]

static IEnumerator GetOnlineTimeCoroutine ( Uri  uri,
OnlineTimeCallback  callback,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously in coroutine.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
uriAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
callbackDelegate to call and pass OnlineTimeResult to.
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.

◆ GetOnlineTimeTask() [1/4]

static Task<OnlineTimeResult> GetOnlineTimeTask ( string  url,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
urlAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.
Returns
OnlineTimeResult with UTC seconds or error.

◆ GetOnlineTimeTask() [2/4]

static Task<OnlineTimeResult> GetOnlineTimeTask ( string  url,
System.Threading.CancellationToken  cancellationToken,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
urlAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
cancellationTokenCancellationToken to cancel the operation.
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.
Returns
OnlineTimeResult with UTC seconds or error.

◆ GetOnlineTimeTask() [3/4]

static Task<OnlineTimeResult> GetOnlineTimeTask ( Uri  uri,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
uriAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.
Returns
OnlineTimeResult with UTC seconds or error.

◆ GetOnlineTimeTask() [4/4]

static async Task<OnlineTimeResult> GetOnlineTimeTask ( Uri  uri,
System.Threading.CancellationToken  cancellationToken,
RequestMethod  method = RequestMethod.Head 
)
inlinestatic

Receives UTC seconds from url. Runs asynchronously.

Automatically switches to the current domain when running in WebGL to avoid CORS limitation.

Parameters
uriAbsolute url to receive time from. Make sure this server has proper Date values in the response headers (almost all popular web sites are suitable).
cancellationTokenCancellationToken to cancel the operation.
methodMethod to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.
Returns
OnlineTimeResult with UTC seconds or error.

◆ TriggerDetection()

void TriggerDetection ( )
inline

Manually triggers cheating detection and invokes assigned events.

◆ ForceCheck()

bool ForceCheck ( )
inline

Allows to manually execute cheating check. Restarts interval.

Listen for detector events to know about check result.

Returns
True if check was started.

◆ ForceCheckEnumerator()

IEnumerator ForceCheckEnumerator ( )
inline

Allows to manually execute cheating check and wait for the completion within coroutine. Restarts interval.

Use inside of the coroutine and check LastResult property after yielding this method. Detector events will be invoked too. Example:

StartCoroutine(MakeForcedCheck());
private IEnumerator MakeForcedCheck()
{
yield return TimeCheatingDetector.Instance.ForceCheckEnumerator();
// check TimeCheatingDetector.Instance.LastResult
// ...
}

◆ ForceCheckTask()

async Task<CheckResult> ForceCheckTask ( )
inline

Allows to manually execute cheating check and wait for the completion within async method. Restarts interval.

Await this method in async method to wait for the check completion and to get the check result. Detector events will be invoked too. Disabled for WebGL platform.

MakeForcedCheckAsync();
private async void MakeForcedCheckAsync()
{
var result = await TimeCheatingDetector.Instance.ForceCheckTask();
// check result
// ...
}

◆ IsReadyForForceCheck()

bool IsReadyForForceCheck ( )
inline

Call this to make sure detector is ready for the ForceCheck() call.

Returns
True if you can call the ForceCheck and False otherwise.

Member Data Documentation

◆ requestMethod

Method to use for url request. Use Head method if possible and fall back to get if server does not reply or block head requests.

◆ timeoutSeconds

int timeoutSeconds = 10

Online time request timeout in seconds. Request will be automatically aborted if server will not response in specified time.

◆ interval

float interval = 5

Time (in minutes) between detector checks. Set to 0 to disable automatic time checks and use ForceCheck(), ForceCheckEnumerator() or ForceCheckTask() to manually run a check.

◆ realCheatThreshold

int realCheatThreshold = 65

Maximum allowed difference between subsequent measurements, in minutes.

◆ wrongTimeThreshold

int wrongTimeThreshold = 65

Maximum allowed difference between local and online time, in minutes.

If online and local time difference exceed this threshold, detector will raise an event with

◆ ignoreSetCorrectTime

bool ignoreSetCorrectTime = true

Ignore case when time changes to be in sync with online correct time.

Wrong time threshold is taken into account.

◆ autoStart

bool autoStart
inherited

Allows to start detector automatically. Otherwise, you'll need to call StartDetection() method to start it.

Useful in conjunction with proper Detection Event configuration in the inspector. Allows to use detector without writing any code except the actual reaction on cheating.

◆ autoDispose

bool autoDispose
inherited

Detector component will be automatically disposed after firing callback if enabled. Otherwise, it will just stop internal processes.

On dispose Detector follows 2 rules:

  • if Game Object's name is "Anti-Cheat Toolkit": it will be automatically destroyed if no other Detectors left attached regardless of any other components or children;
  • if Game Object's name is NOT "Anti-Cheat Toolkit": it will be automatically destroyed only if it has neither other components nor children attached;

Property Documentation

◆ RequestUrl

string RequestUrl
getset

Absolute URL which will return correct Date in response header to the HEAD request (nearly any popular web server out there including google.com, microsoft.com etc.).

◆ LastError

ErrorKind LastError
get

Last detector error kind. Will be ErrorKind.NoError if there were no errors.

◆ LastResult

CheckResult LastResult
get

Last check result. Check LastError if this has value of CheckResult.Error.

◆ LastOnlineTimeResult

OnlineTimeResult LastOnlineTimeResult
get

Complete response data for the last time request.

◆ IsCheckingForCheat

bool IsCheckingForCheat
get

Allows to check if cheating check is currently in process.

◆ IsCheatDetected

bool IsCheatDetected
getprotected setinherited

Indicates if cheat was detected by this detector.

◆ IsStarted

bool IsStarted
getprotected setinherited

Allows to check if detector is started (stays true even when it's paused).

◆ IsRunning

bool IsRunning
getprotected setinherited

Allows to check if detection is currently running and not paused.

Event Documentation

◆ CheatChecked

Gets called after each cheat check and provides results of the check.

◆ CheatDetected

Action CheatDetected
inherited

Subscribe to this event to get notified when cheat will be detected.