- Home /
How do I make it so players can't edit the file but the game can? File.CreateText
So my high score system is a little weird. I use the File.CreateText thing to do it, here is the script if you need it: (Java and its called ScoreSave)
import System.IO;
var fileName = "hs.data";
var ScoreAmount : int;
var HighScore : int;
function Start (){
HighScore = ScoreLoad.CompareScore;
ScoreAmount = GlobalScore.CurrentScore;
if (ScoreAmount >= HighScore){
var OurFile = File.CreateText(fileName);
OurFile.WriteLine (ScoreAmount);
OurFile.Close();
}
}
Also here is my script for loading the score if that is needed for explanation (java and its called ScoreLoad):
import System.IO;
var fileName = "HS.data";
var ScoreLoad : String;
var HighScoreDisplay : GameObject;
var line : String;
static var CompareScore : int;
function Start(){
var sr : StreamReader = new StreamReader(fileName);
line = sr.ReadLine();
while (line != null){
ScoreLoad = line;
line = sr.ReadLine();
}
sr.Close();
HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
CompareScore = int.Parse(ScoreLoad);
}
My question is how do I make it so in the final build, players wont just be able to go to the game files, go into the file called hs.data and change their high score to a high number aka cheat? How do I like encrypt it or lock it or make it permanently read only or something? Anything helps, thank you! Also it needs to be able to save the highscore when it gets a new one so would read only not work? Help please. Thanks.
Answer by Namey5 · May 24, 2017 at 10:13 AM
I see two ways of doing it. Either set the Read/Write access of the file to something secure, i.e.
File.SetAttributes (fileName, FileAttributes.Hidden);
File.SetAttributes (fileName, FileAttributes.ReadOnly);
Or, you could use a binary formatter to convert the text to binary; unreadable and unchangeable for anything but the system that encoded it.
import System.IO;
import System.Runtime.Serialization.Formatters.Binary; //I know, it's a bit long eh?
var fileName = "hs.data";
var ScoreAmount : int;
var HighScore : int;
function Start (){
HighScore = ScoreLoad.CompareScore;
ScoreAmount = GlobalScore.CurrentScore;
if (ScoreAmount >= HighScore){
var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, FileMode.OpenOrCreate);
bf.Serialize (OurFile, ScoreAmount);
OurFile.Close();
}
}
...
//Then for loading the file;
var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, FileMode.Open);
line = bf.Deserialize (OurFile).ToString();
OurFile.Close ();
There might be an error in the "bf.Deserialize()" line, simply because it's been a while since I did deserialization in JS, so if you use this method and have difficulties just leave a comment.
Both solutions are neither secure nor does an attacker need to know anything about the system that saved the information in order to read and modify the information. $$anonymous$$arking a file as hidden and read only can be reverted even without any advanced knowledge.
The binary serialization format is well documented and quite trivial to read / modify. You can even reconstruct the actual datastructure used (at least the members that got serialized).
Finally there is no way to store such information on the user's side and prevent modifications. It would be possible to encrypt the data or store additional checksums, however your code can be decompiled very easily so any encryption or checksum calculations can be bypassed.
Judging by the wording of the question, we probably aren't dealing with hackers here. This solution is more to stop people casually co$$anonymous$$g across the file and potentially modifying data.
The save one went good, however where do I put the load code? this is what I have now: import System.IO;
var fileName = "hs.data";
var ScoreLoad : String;
var HighScoreDisplay : GameObject;
var line : String;
static var CompareScore : int;
function Start(){
var sr : StreamReader = new StreamReader(fileName);
var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, File$$anonymous$$ode.Open);
{
line = bf.Deserialize (fileName).ToString();
OurFile.Close ();
}
sr.Close();
HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
CompareScore = int.Parse(ScoreLoad);
}
I think that's right? And im getting the error:
Assets/ScoreLoad.js(16,27): BCE0023: No appropriate version of 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize' for the argument list '(String)' was found.
$$anonymous$$y bad, made a typo in the deserialization statement. Answer has been updated. Now if it returns an error, I'm guessing it's to do with type casting. But from memory that isn't a thing in JS, so it should work. As for the placement, just replace all the old loading stuff with that. I may question as to the extra set of braces surrounding the loading code, however. They shouldn't need to be there.
btw for your save code I just copied and pasted it replacing the old one, thanks!
I need the loaded script to display it too? This is what I have now:
import System.IO;
import System.Runtime.Serialization.Formatters.Binary; //I know, it's a bit long eh?
var fileName = "hs.data";
var ScoreAmount : int;
var HighScore : int;
function Start (){
HighScore = ScoreLoad.CompareScore;
ScoreAmount = GlobalScore.CurrentScore;
if (ScoreAmount >= HighScore){
var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, File$$anonymous$$ode.OpenOrCreate);
bf.Serialize (OurFile, ScoreAmount);
OurFile.Close();
}
}
What exactly is the issue? Just replace the loading stuff from your old script with that above (which should now be fixed).
So for saving the score, I used the script you provided. For loading I did:
import System.IO;
var fileName = "hs.data";
var ScoreLoad : String;
var HighScoreDisplay : GameObject;
var line : String;
static var CompareScore : int;
var bf = new BinaryFormatter ();
var OurFile = File.Open (fileName, File$$anonymous$$ode.Open);
line = bf.Deserialize (OurFile).ToString();
OurFile.Close ();
HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
CompareScore = int.Parse(ScoreLoad);
Im getting a problem where I load in and it says high score is 0, then i go and get points and it remains 0 like it isnt loading it? (I posted the entire load script start to finish) (Im not an expert and sort of a noob and thank you so much for helping and i am stuck)
Answer by shadowpuppet · May 24, 2017 at 09:26 PM
not an answer, just an observation as I "struggled" with this very same thing. But in the end I said "who cares" . Games are meant to be fun and played as designed so if some snot nosed punk wants to cheat and pad his score, by all means have at it. Unless you're making a AAA title for massive online play or there is some tangible reward for getting a high sore like bit coins I say let them hack it. Let them have that rush of pseudo superiority