- Home /
Binary save file doesn't seem to be in binary?
I recently wrote a pretty simple save system following a youtube tutorial, which is supposed to save the file in binary and make it hard to edit so players can't just edit their save data easily, however this is how the save file looks when opened in notepad:
ÿÿÿÿ FAssembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
PlayerData
playerName Evelyn
As you can see, it would be pretty easy to edit the name there, and it's obvious what the field is referencing. Did I do something wrong in my code to make it be serialized this way?
Here is my code:
Player.cs:
using UnityEngine;
using TMPro;
public class Player : MonoBehaviour
{
public string playerName;
public TextMeshProUGUI nameDisplay;
private void Awake()
{
nameDisplay.text = playerName;
}
public void SavePlayer()
{
SaveSystem.SavePlayer(this);
Debug.Log("Saved: " + playerName);
}
public void LoadPlayer ()
{
PlayerData data = SaveSystem.LoadPlayer();
Debug.Log("Loaded");
playerName = data.playerName;
nameDisplay.text = playerName;
}
}
PlayerData.cs:
[System.Serializable]
public class PlayerData
{
public string playerName;
public PlayerData (Player player)
{
playerName = player.playerName;
}
}
SaveSystem.cs:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SavePlayer (Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "player.cozy";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(player);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadPlayer()
{
string path = Application.persistentDataPath + "player.cozy";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}
Data is fine. The issue is that text is, well, binary : )
What you need to do is to obfuscate this data and do so non-destructively so you can read it. The simplest form of obfuscation I can think of is to just compress it ( System.IO.Compression
). You can also "salt" the byte[]
data manually (bit-shifting etc.).
Your answer
Follow this Question
Related Questions
The best way to save character progress used by 2 games? 2 Answers
Saving and Loading in Binary corrupted save help 1 Answer
Should I avoid using BinaryFormatter altogether? 3 Answers
How do I save a List in my project? 0 Answers
My balance in the game and my multiplier are not saving, i cant figure out why, any help? 0 Answers