Assets/Scripts/Management/Serialization/SaveManager.cs
/// <summary>
/// checks contents of the save file,
/// updates the save file format if old version detected,
/// returns save data after operations
/// </summary>
/// <returns></returns>
checks contents of the save file,
updates the save file format if old version detected,
returns save data after operations
checks contents of the save file updates the save file format if old version detected returns save data after operations
public static SaveBlobV2 DoCheckVersionAndRectify()
        {
            if (!File.Exists(savepath))
            {
                throw new FileNotFoundException("save file is supposed to exist");
            }
            SaveBlobV2 returnBlob;
            FileStream fileStream = File.Open(savepath, FileMode.Open);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            try
            {
                SaveBlobV2 blob = (SaveBlobV2)binaryFormatter.Deserialize(fileStream);
                fileStream.Close();
                if (blob.version != Constants.SAVE_VER)
                {
                    returnBlob = new SaveBlobV2();
                    returnBlob.InitEmpty();
                    for (int _ = 0; _ < Math.Min(blob.NormalTimes.Length, Constants.CHECKPOINT_COUNT); _++)
                    {
                        returnBlob.NormalTimes[_] = blob.NormalTimes[_];
                        returnBlob.NormalCollectibles[_] = blob.NormalCollectibles[_];
                    }
                    for (int _ = 0; _ < Math.Min(blob.MadnessTimes.Length, Constants.MADNESS_LEVEL_COUNT); _++)
                    {
                        returnBlob.MadnessTimes[_] = blob.MadnessTimes[_];
                        returnBlob.MadnessCollectibles[_] = blob.MadnessCollectibles[_];
                    }
                }
                else
                {
                    returnBlob = blob;
                }
            }
            catch (SerializationException)
            {
                try
                {
                    SaveBlob blob = (SaveBlob)binaryFormatter.Deserialize(fileStream);
                    fileStream.Close();
                    returnBlob = new SaveBlobV2();
                    returnBlob.InitEmpty();
                    for (int _ = 0; _ < Math.Min(blob.SaveTimes.Length, Constants.CHECKPOINT_COUNT); _++)
                    {
                        returnBlob.NormalTimes[_] = blob.SaveTimes[_];
                        returnBlob.NormalCollectibles[_] = blob.SaveCollectibles[_];
                    }
                }
                catch (SerializationException)
                {
                    returnBlob = new SaveBlobV2();
                    returnBlob.InitEmpty();
                }
            }
            fileStream.Close();
            fileStream = File.Create(savepath);
            binaryFormatter.Serialize(fileStream, returnBlob);
            fileStream.Close();
            return returnBlob;
        }
public static SaveBlobV2 DoCheckVersionAndRectify ( ) { if ( ! File . Exists ( savepath ) ) { throw new FileNotFoundException ( " " ) ; } SaveBlobV2 returnBlob ; FileStream fileStream = File . Open ( savepath , FileMode . Open ) ; BinaryFormatter binaryFormatter = new BinaryFormatter ( ) ; try { SaveBlobV2 blob = ( SaveBlobV2 ) binaryFormatter . Deserialize ( fileStream ) ; fileStream . Close ( ) ; if ( blob . version != Constants . SAVE_VER ) { returnBlob = new SaveBlobV2 ( ) ; returnBlob . InitEmpty ( ) ; for ( int _ = 0 ; _ < Math . Min ( blob . NormalTimes . Length , Constants . CHECKPOINT_COUNT ) ; _ ++ ) { returnBlob . NormalTimes [ _ ] = blob . NormalTimes [ _ ] ; returnBlob . NormalCollectibles [ _ ] = blob . NormalCollectibles [ _ ] ; } for ( int _ = 0 ; _ < Math . Min ( blob . MadnessTimes . Length , Constants . MADNESS_LEVEL_COUNT ) ; _ ++ ) { returnBlob . MadnessTimes [ _ ] = blob . MadnessTimes [ _ ] ; returnBlob . MadnessCollectibles [ _ ] = blob . MadnessCollectibles [ _ ] ; } } else { returnBlob = blob ; } } catch ( SerializationException ) { try { SaveBlob blob = ( SaveBlob ) binaryFormatter . Deserialize ( fileStream ) ; fileStream . Close ( ) ; returnBlob = new SaveBlobV2 ( ) ; returnBlob . InitEmpty ( ) ; for ( int _ = 0 ; _ < Math . Min ( blob . SaveTimes . Length , Constants . CHECKPOINT_COUNT ) ; _ ++ ) { returnBlob . NormalTimes [ _ ] = blob . SaveTimes [ _ ] ; returnBlob . NormalCollectibles [ _ ] = blob . SaveCollectibles [ _ ] ; } } catch ( SerializationException ) { returnBlob = new SaveBlobV2 ( ) ; returnBlob . InitEmpty ( ) ; } } fileStream . Close ( ) ; fileStream = File . Create ( savepath ) ; binaryFormatter . Serialize ( fileStream , returnBlob ) ; fileStream . Close ( ) ; return returnBlob ; }
checks contents of the save file,
updates the save file format if old version detected,
returns save data after operations
checks contents of the save file updates the save file format if old version detected returns save data after operations
// object saveObject = binaryFormatter.Deserialize(fileStream); // * the save file has wrong version // * the file does not contain a saveblobV2 // * the file does not coontain a saveblob(V1) either
None