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

Static Public Member Functions

static void SetRawValue (string encryptedKey, string encryptedValue)
static bool GetRawValue (string key, out string encryptedKey, out string encryptedValue)
static bool HasKey (string key)
static void DeleteKey (string key)
static void DeleteCryptoKey ()
static void DeleteAll ()
static void Save ()
static void Set< T > (string key, T value)
static T Get< T > (string key, T defaultValue=default)
static bool MigrateFromACTkV1 (string key, string cryptoKey="e806f6")
static string EncryptKeyWithACTkV1Algorithm (string key, string cryptoKey="e806f6")

Static Public Attributes

static bool preservePlayerPrefs = false


static DeviceLockSettings DeviceLockSettings = new DeviceLockSettings() [get]


static Action NotGenuineDataDetected
static Action DataFromAnotherDeviceDetected

Detailed Description

This is an Obscured analogue of the PlayerPrefs class.

Saves data in encrypted state, optionally locking it to the current device.
Automatically encrypts PlayerPrefs on first read (auto migration), has tampering detection and more.
Check out ObscuredFilePrefs if you wish to save big data amounts.

Member Function Documentation

◆ SetRawValue()

static void SetRawValue ( string  encryptedKey,
string  encryptedValue 

Allows to set the raw encrypted key and value.

◆ GetRawValue()

static bool GetRawValue ( string  key,
out string  encryptedKey,
out string  encryptedValue 

Allows to get the raw encrypted key and value for the specified key.

True if key was found and false otherwise.

◆ HasKey()

static bool HasKey ( string  key)

Returns true if key exists in the ObscuredPrefs or in regular PlayerPrefs.

◆ DeleteKey()

static void DeleteKey ( string  key)

Removes key and its corresponding value from the ObscuredPrefs and regular PlayerPrefs.

◆ DeleteCryptoKey()

static void DeleteCryptoKey ( )

Removes saved crypto key. Use only when you wish to completely remove all obscured prefs!

WARNING: Any existing obscured prefs will be lost after this action.

◆ DeleteAll()

static void DeleteAll ( )

Removes all keys and values from the preferences, including anything saved with regular PlayerPrefs. Use with caution!

WARNING: Please use this method to remove all prefs instead of PlayerPrefs.DeleteAll() to properly clear internals and avoid any data loss when saving new obscured prefs after DeleteAll() call.

◆ Save()

static void Save ( )

Writes all modified preferences to disk.

By default, Unity writes preferences to disk on Application Quit.
In case when the game crashes or otherwise prematurely exits, you might want to write the preferences at sensible 'checkpoints' in your game.
This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual game play.

◆ Set< T >()

static void Set< T > ( string  key,

Sets the value of the preference identified by key.

WARNING: Not all types are supported, see StorageDataType for list of supported types.

◆ Get< T >()

static T Get< T > ( string  key,
defaultValue = default 

Returns the value corresponding to key in the preference file if it exists. If it doesn't exist, it will return defaultValue.

WARNING: Not all types are supported, see StorageDataType for list of supported types.

◆ MigrateFromACTkV1()

static bool MigrateFromACTkV1 ( string  key,
string  cryptoKey = "e806f6" 

Use to migrate ACTk v1.* prefs to the newer format.

keyPrefs key you wish to migrate.
cryptoKeyCustom crypto key you used for ObscuredPrefs, if any. Don't use this argument to utilize default key from ACTk v1.
True if migration was successful, false otherwise.

◆ EncryptKeyWithACTkV1Algorithm()

static string EncryptKeyWithACTkV1Algorithm ( string  key,
string  cryptoKey = "e806f6" 

Use to encrypt ACTkv1's value key for later use with SetRawValue to let it migrate.

keyPrefs key.
cryptoKeyCrypto key you used with ACTk v1, if any.
Prefs key, encrypted with old ACTk v1 encryption.

Member Data Documentation

◆ preservePlayerPrefs

bool preservePlayerPrefs = false

Allows saving original PlayerPrefs values while migrating to ObscuredPrefs.

In such case, original value still will be readable after switching from PlayerPrefs to ObscuredPrefs and it should be removed manually as it became unneeded.
Original PlayerPrefs value will be automatically removed after read by default.

Property Documentation

◆ DeviceLockSettings

Controls DeviceLock feature settings. Read more at DeviceLockSettings docs.

Event Documentation

◆ NotGenuineDataDetected

Action NotGenuineDataDetected

Allows reacting on saves alteration. May be helpful for banning potential cheaters.

Fires only once.

◆ DataFromAnotherDeviceDetected

Action DataFromAnotherDeviceDetected

Allows reacting on detection of possible saves from some other device.

May be helpful to ban potential cheaters, trying to use someone's purchased in-app goods for example.
May fire on same device in case cheater manipulates saved data in some special way.
Fires only once.

NOTE: May be called if same device ID was changed (pretty rare case though).