public class PlayerData
{
public int Level { get; set; } // Will be serialized
public int MaxHealth { get; set; } // Will be serialized
public string Username; // Won't be serialized
public static void Save( PlayerData data )
{
FileSystem.Data.WriteJson( "player.json", AntiTamper.Lock( data, 1254854 ) );
}
public static PlayerData Load()
{
return AntiTamper.Unlock(
FileSystem.Data.ReadJson<LockedData>( "player.json" ),
1254854,
new PlayerData()
);
}
}
// later...
try
{
data = PlayerData.Load();
} catch
{
Log.Info( "Naughty cheater!" );
data = new PlayerData();
}This library serializes your struct as usual, then performs a hash on the string and a magic number you've provided.
When you load the save from the disk, the hash is checked before the object is accepted.
Pick a unique number for your project, but
NEVER CHANGE IT! If you do, all old saves will be invalid.
Q: Are my saves actually safe from tampering?
A: Haha, no. There's only so much you can do when users have your game's code.
This library prevents the most basic tampering where you literally just edit values in the json.
To bypass it, someone would have to open your game code and find the magic number you picked, then do annoying hash calculations themselves.
Sample project:
https://nixx.is-fantabulo.us/antitamper.zip