private void LoadLeaderboard() { // Caches the filepath the JSON file will be saved to string filepath = Application.persistentDataPath + "/Leaderboard.json"; // Checks if a save file exists if (File.Exists(filepath)) { // String to store the data the streamReader recovers. string readData = ""; // Get the leaderboard JSON save file using (StreamReader streamReader = new StreamReader(filepath)) { // Declares a temporary line to store things in string line; // While the line is different to null (ReadLine is actually reading something) while ((line = streamReader.ReadLine()) != null) { // Add the line to readData readData += DataCrypto.DecryptText(line); } } // Reconstructs the leaderboard leaderboard = JsonUtility.FromJson <Leaderboard>(readData); } else // If no save file exists { // Creates a sample leaderboard entry list List <LeaderboardEntry> sampleEntryList = new List <LeaderboardEntry>() { new LeaderboardEntry(5, 6350, 0, "Beat Me!") }; // Creates a leaderboard using the sample entry list leaderboard = new Leaderboard(sampleEntryList); } }
// Saves the player metrics in a save file using a JSON format private void SavePlayerMetrics() { // Caches the filepath the JSON file will be saved to string filepath = Application.persistentDataPath + "/Leaderboard.json"; // Stores the game data into a LeaderboardEntry struct LeaderboardEntry playerMetrics = new LeaderboardEntry(currentGame.wavesSurvived, (int)currentGame.playerScore, currentGame.newGamePlus, playerName.text); // Checks if a save file exists if (File.Exists(filepath)) { // String to store the data the streamReader recovers. string readData = ""; // Get the leaderboard JSON save file using (StreamReader streamReader = new StreamReader(filepath)) { // Declares a temporary line to store things in string line; // While the line is different to null (ReadLine is actually reading something) while ((line = streamReader.ReadLine()) != null) { // Add the line to readData readData += DataCrypto.DecryptText(line); } } // Reconstructs the leaderboard Leaderboard saveFile = JsonUtility.FromJson <Leaderboard>(readData); // Adds a the LeaderboardEntry to the list saveFile.gameLeaderboard.Add(playerMetrics); // Writes the data to disk // Using keyword allows us to dispose of the StreamWriter once we're done using it. using (StreamWriter streamWriter = new StreamWriter(filepath)) { // Uses stream writer to write the JSON-converted save data. streamWriter.WriteLine(DataCrypto.EncryptText(JsonUtility.ToJson(saveFile))); } } else // If the file does not exist, create it { // Creates a new list of Leaderboard Entries List <LeaderboardEntry> newLeaderboard = new List <LeaderboardEntry>(); // Adds the player's current save data to the leaderboard newLeaderboard.Add(playerMetrics); // Creates leaderboard and passes it the list containing the current save data Leaderboard saveFile = new Leaderboard(newLeaderboard); // Writes the data to disk // Using keyword allows us to dispose of the StreamWriter once we're done using it. using (StreamWriter streamWriter = new StreamWriter(filepath)) { // Uses stream writer to write the JSON-converted save data. streamWriter.WriteLine(DataCrypto.EncryptText(JsonUtility.ToJson(saveFile))); } } }