Class ObscuredFilePrefs
ObscuredPrefs analogue but uses File IO instead of PlayerPrefs as a backend, has more flexibility and can work from a background thread.
public static class ObscuredFilePrefs
- Inheritance
-
objectObscuredFilePrefs
Remarks
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.
using CodeStage.AntiCheat.Storage;
// Initialize with default settings
ObscuredFilePrefs.Init();
// Listen for cheating detection
ObscuredFilePrefs.NotGenuineDataDetected += OnDataCheat;
ObscuredFilePrefs.DataFromAnotherDeviceDetected += OnLockCheat;
// Write preference
ObscuredFilePrefs.Set("coins", 100);
ObscuredFilePrefs.Set("playerName", "John");
ObscuredFilePrefs.Set("highScore", 1500.5f);
// Read preference
var coins = ObscuredFilePrefs.Get("coins", 0);
var playerName = ObscuredFilePrefs.Get("playerName", "Unknown");
var highScore = ObscuredFilePrefs.Get("highScore", 0f);
// Alternatively, specify data type and omit defaultValue argument
var coinsTyped = ObscuredFilePrefs.Get<int>("coins");
// Get all pref keys to iterate when needed
var keys = ObscuredFilePrefs.GetKeys();
foreach (var key in keys)
{
Debug.Log($"Key: {key}");
}
// Save changes to disk
ObscuredFilePrefs.Save();
private void OnDataCheat()
{
Debug.LogWarning("Data tampering detected!");
}
private void OnLockCheat()
{
Debug.LogWarning("Data from another device detected!");
}
Fields
DefaultFileName
Filename used by default, if other name or path was not specified in constructor.
public const string DefaultFileName = "actkfileprefs"
Field Value
- string
Properties
CurrentSettings
Allows checking current settings.
public static IObscuredFileSettings CurrentSettings { get; }
Property Value
Remarks
Use Init(bool) to set the initial settings.
FilePath
Returns path to the prefs file. It's always not empty and valid even if prefs was not saved to the physical file yet.
public static string FilePath { get; }
Property Value
- string
IsBusy
Returns true if prefs file is busy with long-running process such as loading or saving.
public static bool IsBusy { get; }
Property Value
- bool
IsExists
Returns true if prefs file physically exists on disk. File may not exist until Save() is called.
public static bool IsExists { get; }
Property Value
- bool
IsInited
Allows checking if Init(bool) was called previously.
public static bool IsInited { get; }
Property Value
- bool
IsLoaded
Allows checking if prefs cache was loaded \ initialized. It can be true while IsExists is false if new prefs was not saved yet.
public static bool IsLoaded { get; }
Property Value
- bool
IsSaved
Returns true if prefs file has unsaved changes.
public static bool IsSaved { get; }
Property Value
- bool
LastFileReadResult
Contains reference to the last underlying ObscuredFile read operation result.
public static ObscuredFileReadResult LastFileReadResult { get; }
Property Value
Remarks
Filled on prefs file read.
May be invalid if no read operations were executed.
Check ObscuredFileReadResult.IsValid property to figure this out.
LastFileWriteResult
Contains reference to the last underlying ObscuredFile write operation result.
public static ObscuredFileWriteResult LastFileWriteResult { get; }
Property Value
Remarks
Filled on prefs file saving.
May be invalid if no write operations were executed yet.
Check ObscuredFileWriteResult.IsValid property to figure this out.
Methods
DeleteAll()
⚠️ Warning: Use with caution! Removes all keys and values from the prefs.
public static void DeleteAll()
DeleteKey(string)
Removes key and its corresponding value from the ObscuredFilePrefs.
public static void DeleteKey(string key)
Parameters
keystring
Remarks
Calls LoadPrefs() internally.
Get(string, ObscuredBigInteger)
public static ObscuredBigInteger Get(string key, ObscuredBigInteger defaultValue = default)
Parameters
keystringdefaultValueObscuredBigInteger
Returns
Get(string, ObscuredBool)
public static ObscuredBool Get(string key, ObscuredBool defaultValue = default)
Parameters
keystringdefaultValueObscuredBool
Returns
Get(string, ObscuredByte)
public static ObscuredByte Get(string key, ObscuredByte defaultValue = default)
Parameters
keystringdefaultValueObscuredByte
Returns
Get(string, ObscuredChar)
public static ObscuredChar Get(string key, ObscuredChar defaultValue = default)
Parameters
keystringdefaultValueObscuredChar
Returns
Get(string, ObscuredDateTime)
public static ObscuredDateTime Get(string key, ObscuredDateTime defaultValue = default)
Parameters
keystringdefaultValueObscuredDateTime
Returns
Get(string, ObscuredDateTimeOffset)
public static ObscuredDateTimeOffset Get(string key, ObscuredDateTimeOffset defaultValue = default)
Parameters
keystringdefaultValueObscuredDateTimeOffset
Returns
Get(string, ObscuredDecimal)
public static ObscuredDecimal Get(string key, ObscuredDecimal defaultValue = default)
Parameters
keystringdefaultValueObscuredDecimal
Returns
Get(string, ObscuredDouble)
public static ObscuredDouble Get(string key, ObscuredDouble defaultValue = default)
Parameters
keystringdefaultValueObscuredDouble
Returns
Get(string, ObscuredFloat)
public static ObscuredFloat Get(string key, ObscuredFloat defaultValue = default)
Parameters
keystringdefaultValueObscuredFloat
Returns
Get(string, ObscuredGuid)
public static ObscuredGuid Get(string key, ObscuredGuid defaultValue = default)
Parameters
keystringdefaultValueObscuredGuid
Returns
Get(string, ObscuredInt)
public static ObscuredInt Get(string key, ObscuredInt defaultValue = default)
Parameters
keystringdefaultValueObscuredInt
Returns
Get(string, ObscuredLong)
public static ObscuredLong Get(string key, ObscuredLong defaultValue = default)
Parameters
keystringdefaultValueObscuredLong
Returns
Get(string, ObscuredQuaternion)
public static ObscuredQuaternion Get(string key, ObscuredQuaternion defaultValue = default)
Parameters
keystringdefaultValueObscuredQuaternion
Returns
Get(string, ObscuredSByte)
public static ObscuredSByte Get(string key, ObscuredSByte defaultValue = default)
Parameters
keystringdefaultValueObscuredSByte
Returns
Get(string, ObscuredShort)
public static ObscuredShort Get(string key, ObscuredShort defaultValue = default)
Parameters
keystringdefaultValueObscuredShort
Returns
Get(string, ObscuredString)
public static ObscuredString Get(string key, ObscuredString defaultValue = null)
Parameters
keystringdefaultValueObscuredString
Returns
Get(string, ObscuredUInt)
public static ObscuredUInt Get(string key, ObscuredUInt defaultValue = default)
Parameters
keystringdefaultValueObscuredUInt
Returns
Get(string, ObscuredULong)
public static ObscuredULong Get(string key, ObscuredULong defaultValue = default)
Parameters
keystringdefaultValueObscuredULong
Returns
Get(string, ObscuredUShort)
public static ObscuredUShort Get(string key, ObscuredUShort defaultValue = default)
Parameters
keystringdefaultValueObscuredUShort
Returns
Get(string, ObscuredVector2)
public static ObscuredVector2 Get(string key, ObscuredVector2 defaultValue = default)
Parameters
keystringdefaultValueObscuredVector2
Returns
Get(string, ObscuredVector2Int)
public static ObscuredVector2Int Get(string key, ObscuredVector2Int defaultValue = default)
Parameters
keystringdefaultValueObscuredVector2Int
Returns
Get(string, ObscuredVector3)
public static ObscuredVector3 Get(string key, ObscuredVector3 defaultValue = default)
Parameters
keystringdefaultValueObscuredVector3
Returns
Get(string, ObscuredVector3Int)
public static ObscuredVector3Int Get(string key, ObscuredVector3Int defaultValue = default)
Parameters
keystringdefaultValueObscuredVector3Int
Returns
GetKeys()
Returns all existing prefs keys in current ObscuredFilePrefs.
public static ICollection<string> GetKeys()
Returns
- ICollection<string>
Remarks
Calls LoadPrefs() internally.
Get<T>(string, T)
Returns the value corresponding to key in the preference file if it exists.
If it doesn't exist, it will return defaultValue.
public static T Get<T>(string key, T defaultValue = default)
Parameters
keystringdefaultValueT
Returns
- T
Type Parameters
T
Remarks
// Read values with default fallback
var coins = ObscuredFilePrefs.Get("coins", 0);
var playerName = ObscuredFilePrefs.Get("playerName", "Unknown");
// Read with explicit type specification
var coinsTyped = ObscuredFilePrefs.Get<int>("coins");
var playerNameTyped = ObscuredFilePrefs.Get<string>("playerName");
HasKey(string)
Returns true if key exists in the ObscuredFilePrefs.
public static bool HasKey(string key)
Parameters
keystring
Returns
- bool
Remarks
Calls LoadPrefs() internally.
Init(IObscuredFileSettings, bool)
Initializes ObscuredFilePrefs with file name set to DefaultFileName and custom specific settings.
public static void Init(IObscuredFileSettings settings, bool loadPrefs)
Parameters
settingsIObscuredFileSettingsSpecific custom settings.
loadPrefsboolPass
trueto automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.
Init(bool)
Initializes ObscuredFilePrefs with file name set to DefaultFileName and default ObscuredFileSettings.
public static void Init(bool loadPrefs = false)
Parameters
loadPrefsboolPass
trueto automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.
Init(string, IObscuredFileSettings, bool)
Initializes ObscuredFilePrefs with specified file name or file path and custom specific settings.
public static void Init(string fileNameOrPath, IObscuredFileSettings settings, bool loadPrefs)
Parameters
fileNameOrPathstringFile path if using ObscuredFileLocation.Custom, otherwise represents file name to use with set ObscuredFileLocation kind.
settingsIObscuredFileSettingsSpecific custom settings.
loadPrefsboolPass
trueto automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.
Init(string, bool)
Initializes ObscuredFilePrefs with specified file name and default ObscuredFileSettings.
public static void Init(string fileName, bool loadPrefs)
Parameters
fileNamestringCustom file name to place at ObscuredFileLocation.PersistentData.
loadPrefsboolPass
trueto automatically call LoadPrefs(). This may block calling thread, consider using asynchronously for large files.
LoadPrefs()
Loads prefs from existing file if it wasn't loaded before.
public static void LoadPrefs()
Remarks
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.
Save()
Writes all modified prefs to underlying ObscuredFile on disk.
public static bool Save()
Returns
- bool
True if save was successful or wasn't needed and false if something went wrong. Check LastFileWriteResult for details if this method returns false.
Remarks
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.
Set(string, ObscuredBigInteger)
public static void Set(string key, ObscuredBigInteger value)
Parameters
keystringvalueObscuredBigInteger
Set(string, ObscuredBool)
public static void Set(string key, ObscuredBool value)
Parameters
keystringvalueObscuredBool
Set(string, ObscuredByte)
public static void Set(string key, ObscuredByte value)
Parameters
keystringvalueObscuredByte
Set(string, ObscuredChar)
public static void Set(string key, ObscuredChar value)
Parameters
keystringvalueObscuredChar
Set(string, ObscuredDateTime)
public static void Set(string key, ObscuredDateTime value)
Parameters
keystringvalueObscuredDateTime
Set(string, ObscuredDateTimeOffset)
public static void Set(string key, ObscuredDateTimeOffset value)
Parameters
keystringvalueObscuredDateTimeOffset
Set(string, ObscuredDecimal)
public static void Set(string key, ObscuredDecimal value)
Parameters
keystringvalueObscuredDecimal
Set(string, ObscuredDouble)
public static void Set(string key, ObscuredDouble value)
Parameters
keystringvalueObscuredDouble
Set(string, ObscuredFloat)
public static void Set(string key, ObscuredFloat value)
Parameters
keystringvalueObscuredFloat
Set(string, ObscuredGuid)
public static void Set(string key, ObscuredGuid value)
Parameters
keystringvalueObscuredGuid
Set(string, ObscuredInt)
public static void Set(string key, ObscuredInt value)
Parameters
keystringvalueObscuredInt
Set(string, ObscuredLong)
public static void Set(string key, ObscuredLong value)
Parameters
keystringvalueObscuredLong
Set(string, ObscuredQuaternion)
public static void Set(string key, ObscuredQuaternion value)
Parameters
keystringvalueObscuredQuaternion
Set(string, ObscuredSByte)
public static void Set(string key, ObscuredSByte value)
Parameters
keystringvalueObscuredSByte
Set(string, ObscuredShort)
public static void Set(string key, ObscuredShort value)
Parameters
keystringvalueObscuredShort
Set(string, ObscuredString)
public static void Set(string key, ObscuredString value)
Parameters
keystringvalueObscuredString
Set(string, ObscuredUInt)
public static void Set(string key, ObscuredUInt value)
Parameters
keystringvalueObscuredUInt
Set(string, ObscuredULong)
public static void Set(string key, ObscuredULong value)
Parameters
keystringvalueObscuredULong
Set(string, ObscuredUShort)
public static void Set(string key, ObscuredUShort value)
Parameters
keystringvalueObscuredUShort
Set(string, ObscuredVector2)
public static void Set(string key, ObscuredVector2 value)
Parameters
keystringvalueObscuredVector2
Set(string, ObscuredVector2Int)
public static void Set(string key, ObscuredVector2Int value)
Parameters
keystringvalueObscuredVector2Int
Set(string, ObscuredVector3)
public static void Set(string key, ObscuredVector3 value)
Parameters
keystringvalueObscuredVector3
Set(string, ObscuredVector3Int)
public static void Set(string key, ObscuredVector3Int value)
Parameters
keystringvalueObscuredVector3Int
Set<T>(string, T)
Sets the value of the preference identified by key.
public static void Set<T>(string key, T value)
Parameters
keystringvalueT
Type Parameters
T
UnInit()
Releases internal prefs cache, unsubscribes events and frees other used resources.
public static void UnInit()
Remarks
Please call Init(bool) again if you wish to re-use it.
UnloadPrefs(bool)
Unloads cached prefs from memory. Optionally saves current prefs to the file before unloading.
public static void UnloadPrefs(bool saveBeforeUnloading = true)
Parameters
saveBeforeUnloadingbool
Remarks
⚠️ Warning: Unsaved data will be lost!
Events
DataFromAnotherDeviceDetected
Fires when saved data from some other device detected.
public static event Action DataFromAnotherDeviceDetected
Event Type
- Action
Remarks
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.
NotGenuineDataDetected
Fires when saved data tampering detected. Will not fire when data is damaged and not readable.
public static event Action NotGenuineDataDetected
Event Type
- Action