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

Static Public Member Functions

static void Init (bool loadPrefs=false)
 
static void Init (string fileName, bool loadPrefs)
 
static void Init (IObscuredFileSettings settings, bool loadPrefs)
 
static void Init (string fileNameOrPath, IObscuredFileSettings settings, bool loadPrefs)
 
static void UnInit ()
 
static void LoadPrefs ()
 
static void UnloadPrefs (bool saveBeforeUnloading=true)
 
static bool HasKey (string key)
 
static ICollection< string > GetKeys ()
 
static void DeleteKey (string key)
 
static void DeleteAll ()
 
static bool Save ()
 
static void Set< T > (string key, T value)
 
static T Get< T > (string key, T defaultValue=default)
 

Static Public Attributes

const string DefaultFileName = "actkfileprefs"
 

Properties

static IObscuredFileSettings CurrentSettings [get]
 
static bool IsInited [get]
 
static bool IsLoaded [get]
 
static bool IsExists [get]
 
static bool IsSaved [get]
 
static bool IsBusy [get]
 
static string FilePath [get]
 
static ObscuredFileReadResult LastFileReadResult [get]
 
static ObscuredFileWriteResult LastFileWriteResult [get]
 

Events

static Action NotGenuineDataDetected
 
static Action DataFromAnotherDeviceDetected
 

Detailed Description

ObscuredPrefs analogue but uses File IO instead of PlayerPrefs as a backend, has more flexibility and can work from a background thread.

Don't forget to call Save() when you wish to save prefs file. You can call it from background thread. It also will save automatically on non-abnormal application quit (relies on Application.wantsToQuit API).
Please call DeviceIdHolder.ForceLockToDeviceInit before accessing this class from background thread if you are using Device Lock feature without custom DeviceID.

Member Function Documentation

◆ Init() [1/4]

static void Init ( bool  loadPrefs = false)
inlinestatic

Initializes ObscuredFilePrefs with file name set to DefaultFileName and default ObscuredFileSettings.

Parameters
loadPrefsPass true to automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.

◆ Init() [2/4]

static void Init ( string  fileName,
bool  loadPrefs 
)
inlinestatic

Initializes ObscuredFilePrefs with specified file name and default ObscuredFileSettings.

Parameters
fileNameCustom file name to place at ObscuredFileLocation.PersistentData.
loadPrefsPass true to automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.

◆ Init() [3/4]

static void Init ( IObscuredFileSettings  settings,
bool  loadPrefs 
)
inlinestatic

Initializes ObscuredFilePrefs with file name set to DefaultFileName and custom specific settings.

Parameters
settingsSpecific custom settings.
loadPrefsPass true to automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.

◆ Init() [4/4]

static void Init ( string  fileNameOrPath,
IObscuredFileSettings  settings,
bool  loadPrefs 
)
inlinestatic

Initializes ObscuredFilePrefs with specified file name or file path and custom specific settings.

Parameters
fileNameOrPathFile path if using ObscuredFileLocation.Custom, otherwise represents file name to use with set ObscuredFileLocation kind.
settingsSpecific custom settings.
loadPrefsPass true to automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.

◆ UnInit()

static void UnInit ( )
inlinestatic

Releases internal prefs cache, unsubscribes events and frees other used resources.

Please call Init() again if you wish to re-use it.

◆ LoadPrefs()

static void LoadPrefs ( )
inlinestatic

Loads prefs from existing file if it wasn't loaded before.

This function will read from disk potentially causing a hiccup especially when you have lots of data in your prefs, therefore it is not recommended to call it synchronously during actual game play. Instead, call it from separate thread asynchronously or at loading screens and other stall moments of your app.

◆ UnloadPrefs()

static void UnloadPrefs ( bool  saveBeforeUnloading = true)
inlinestatic

Unloads cached prefs from memory. Optionally saves current prefs to the file before unloading.

WARNING: Unsaved data will be lost!

◆ HasKey()

static bool HasKey ( string  key)
inlinestatic

Returns true if key exists in the ObscuredFilePrefs.

Calls LoadPrefs() internally.

◆ GetKeys()

static ICollection<string> GetKeys ( )
inlinestatic

Returns all existing prefs keys in current ObscuredFilePrefs.

Calls LoadPrefs() internally.

◆ DeleteKey()

static void DeleteKey ( string  key)
inlinestatic

Removes key and its corresponding value from the ObscuredFilePrefs.

Calls LoadPrefs() internally.

◆ DeleteAll()

static void DeleteAll ( )
inlinestatic

Use with caution! Removes all keys and values from the prefs.

◆ Save()

static bool Save ( )
inlinestatic

Writes all modified prefs to underlying ObscuredFile on disk.

By default, prefs are saved to disk on Application Quit (relies on Application.wantsToQuit API).
In case when the app crashes or otherwise prematurely exits, you might want to write the prefs at sensible 'checkpoints' in your app.
This function will write to disk potentially causing a hiccup especially when you have lots of data in your prefs, therefore it is not recommended to call it synchronously during actual game play. Instead, call it from separate thread asynchronously or at loading screens and other stall moments of your app.

Returns
True if save was successful or wasn't needed and false if something went wrong. Check LastFileWriteResult for details if this method returns false.

◆ Set< T >()

static void Set< T > ( string  key,
value 
)
inlinestatic

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 
)
inlinestatic

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.

Member Data Documentation

◆ DefaultFileName

const string DefaultFileName = "actkfileprefs"
static

Filename used by default, if other name or path was not specified in constructor.

Property Documentation

◆ CurrentSettings

IObscuredFileSettings CurrentSettings
staticget

Allows checking current settings.

Use Init() to set the initial settings.

See also
Init()

◆ IsInited

bool IsInited
staticget

Allows checking if Init() was called previously.

◆ IsLoaded

bool IsLoaded
staticget

Allows checking if prefs cache was loaded \ initialized. It can be true while IsExists is false if new prefs was not saved yet.

◆ IsExists

bool IsExists
staticget

Returns true if prefs file physically exists on disk. File may not exist until Save() is called.

◆ IsSaved

bool IsSaved
staticget

Returns true if prefs file has unsaved changes.

◆ IsBusy

bool IsBusy
staticget

Returns true if prefs file is busy with long-running process such as loading or saving.

◆ FilePath

string FilePath
staticget

Returns path to the prefs file. It's always not empty and valid even if prefs was not saved to the physical file yet.

◆ LastFileReadResult

ObscuredFileReadResult LastFileReadResult
staticget

Contains reference to the last underlying ObscuredFile read operation result.

Filled on prefs file read.
May be invalid if no read operations were executed. Check ObscuredFileReadResult.IsValid property to figure this out.

◆ LastFileWriteResult

ObscuredFileWriteResult LastFileWriteResult
staticget

Contains reference to the last underlying ObscuredFile write operation result.

Filled on prefs file saving.
May be invalid if no write operations were executed yet. Check ObscuredFileWriteResult.IsValid property to figure this out.

Event Documentation

◆ NotGenuineDataDetected

Action NotGenuineDataDetected
static

Fires when saved data tampering detected. Will not fire when data is damaged and not readable.

◆ DataFromAnotherDeviceDetected

Action DataFromAnotherDeviceDetected
static

Fires when saved data from some other device detected.

May be helpful to ban potential cheaters, trying to use someone's purchased in-app goods for example.
NOTE: Will fire if same device ID has changed (pretty rare case though). Read more at DeviceLockLevel.