/// <summary> /// Checks if the save file exists. /// If not, creates it. /// </summary> public static void DoCheckFileAndCreate() { if (!File.Exists(savepath)) { FileStream fileStream = File.Create(savepath); BinaryFormatter binaryFormatter = new BinaryFormatter(); SaveBlobV2 blob = new SaveBlobV2(); blob.InitEmpty(); binaryFormatter.Serialize(fileStream, blob); fileStream.Close(); } }
/// <summary> /// checks contents of the save file, /// updates the save file format if old version detected, /// returns save data after operations /// </summary> /// <returns></returns> 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(); // object saveObject = binaryFormatter.Deserialize(fileStream); try { SaveBlobV2 blob = (SaveBlobV2)binaryFormatter.Deserialize(fileStream); fileStream.Close(); if (blob.version != Constants.SAVE_VER) { // * the save file has wrong version 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) { // * the file does not contain a saveblobV2 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) { // * the file does not coontain a saveblob(V1) either returnBlob = new SaveBlobV2(); returnBlob.InitEmpty(); } } fileStream.Close(); fileStream = File.Create(savepath); binaryFormatter.Serialize(fileStream, returnBlob); fileStream.Close(); return(returnBlob); }