Exemple #1
0
        public (PlayerData data, DataState state) Load()
        {
            try
            {
                if (!File.Exists(dataPath) || !File.Exists(hashPath))
                {
                    return(null, DataState.notfound);
                }

                FileStream dataStream = new FileStream(dataPath, FileMode.Open);
                PlayerData data       = (PlayerData)binaryFromatter.Deserialize(dataStream);
                string     computeHash;
                using (ACH1 hasher = new ACH1(ACH1.InitType.stream))
                    computeHash = BitConverter.ToString(hasher.ComputeHash(dataStream));
                dataStream.Close();

                string readHash;
                using (BinaryReader reader = new BinaryReader(File.Open(hashPath, FileMode.Open)))
                    readHash = BitConverter.ToString(reader.ReadBytes(20));

                if (readHash == computeHash)
                {
                    return(data, DataState.ok);
                }
                else
                {
                    Debug.LogError("Checksum invalid (is the checksum or save corrupted?) @GameplaySaveManager.cs PlayerData Load()");
                    return(null, DataState.corrupt);
                }
            } catch (FileNotFoundException ex)
            {
                Debug.LogError("Player data or checksum not found. @GameplaySaveManager.cs PlayerData Load()");
                Debug.LogError(ex.ToString());
                return(null, DataState.notfound);
            }

            catch (Exception ex)
            {
                Debug.LogError("Unable to load player data. @GameplaySaveManager.cs PlayerData Load()");
                Debug.LogError(ex.ToString());
                return(null, DataState.corrupt);
            }
        }
Exemple #2
0
        public void Save(PlayerData data, string dataPath, string hashPath)
        {
            try
            {
                if (File.Exists(dataPath))
                {
                    File.Delete(dataPath);
                }
                if (File.Exists(hashPath))
                {
                    File.Delete(hashPath);
                }

                FileStream dataStream = new FileStream(dataPath, FileMode.Create);
                binaryFromatter.Serialize(dataStream, data);
                using (ACH1 hasher = new ACH1(ACH1.InitType.stream))
                    using (BinaryWriter writer = new BinaryWriter(File.Open(hashPath, FileMode.Create)))
                        writer.Write(hasher.ComputeHash(dataStream));

                dataStream.Close();
                Debug.Log("Successfully saved @GameplaySaveManager.cs Save()");
            } catch (Exception ex)
            {
                if (!secondAttempt)
                {
                    Debug.LogError("Critical error! Could not save player data, making second attempt...");
                    secondAttempt = true;
                    Save(data, dataPath, hashPath);
                    return;
                }
                else
                {
                    Debug.LogError("Critical error! Could not save player data. @GameplaySaveManager Save()");
                    // error popup lol
                    Debug.LogError(ex.ToString());
                }
            }
        }