void CreateNewPlayerCoreData() { ensureDirectoryExists(rootPath); string path = rootPath + "/" + userID + ".json"; PlayerCore newData = new PlayerCore(); newData.userID = userID; string jsonSaveFile; if (hashingEnabled) { HashedPlayerCore hashedData = new HashedPlayerCore(); hashedData.playerData = newData; string resultHash = DataUtility.Md5Sum(JsonUtility.ToJson(hashedData.playerData, true)); hashedData.hash = resultHash; jsonSaveFile = JsonUtility.ToJson(hashedData, true); } else { jsonSaveFile = JsonUtility.ToJson(newData, true); } File.WriteAllText(path, jsonSaveFile); }
// hash related methods bool isHashedDataValid(HashedPlayerCore playerCore) { string resultHash = DataUtility.Md5Sum(JsonUtility.ToJson(hashedCoreData.playerData, true)); if (debugMode) { Debug.Log("Resultant hash is: " + resultHash); } if (resultHash == hashedCoreData.hash) { if (debugMode) { Debug.Log("Hashes match! Data is valid"); } return(true); } if (debugMode) { Debug.Log("Hashes do NOT match. Data is invalid."); } return(false); }
public void SavePlayerCoreData(string userID, bool autoReload = true) { ensureDirectoryExists(rootPath); string path = rootPath + "/" + userID + ".json"; string coreDatajson = JsonUtility.ToJson(coreData, true); string resultJson; if (hashingEnabled) { string hash = DataUtility.Md5Sum(coreDatajson); HashedPlayerCore hashedCore = new HashedPlayerCore(); hashedCore.hash = hash; hashedCore.playerData = coreData; resultJson = JsonUtility.ToJson(hashedCore, true); } else { resultJson = JsonUtility.ToJson(coreData, true); } File.WriteAllText(path, resultJson); if (debugMode) { Debug.Log("Saved Core Player Data to: " + path); print("SavedPlayerCoreData called. Results: " + resultJson); } if (autoReload) { JsonUtility.FromJsonOverwrite(resultJson, coreData); } }
public void LoadPlayerCoreData(string userID) { string path = Application.persistentDataPath + "/" + dataDirectoryName + "/" + userID + ".json"; string json; try { json = File.ReadAllText(path); // Checks if the hashes match if (hashingEnabled) { hashedCoreData = JsonUtility.FromJson <HashedPlayerCore>(json); if (isHashedDataValid(hashedCoreData)) { coreData = hashedCoreData.playerData; } else { CreateNewPlayerCoreData(); return; } } else { coreData = JsonUtility.FromJson <PlayerCore>(json); } if (debugMode) { Debug.Log("Player " + coreData.userID + " has been successfully loaded! Json printed below: "); Debug.Log("-------------------------------------------------------------"); Debug.Log(json); } if (PlayerDataLoaded != null) { PlayerDataLoaded(); } } catch (IOException e) { print(e); print("No save file located!"); } }