Exemplo n.º 1
0
 protected virtual void ValidateReadSaveData(GameSaveData readData, string filePath)
 {
     // If the essential fields aren't filled, then there was a read error.
     if (string.IsNullOrEmpty(readData.SceneName) ||
         readData.Items.Count == 0)
     {
         throw new System.FormatException("Save data at " + filePath + " is not valid.");
     }
 }
 public virtual void SetFrom(GameSaveData other)
 {
     base.SetFrom(other as SaveData);
     this.description       = other.description;
     this.slotNumber        = other.slotNumber;
     this.lastWritten       = other.lastWritten;
     this.progressMarkerKey = other.progressMarkerKey;
     this.items.Clear();
     this.items.AddRange(other.items);
 }
        /// <summary>
        /// Writes the passed save data to the passed save directory, returning true if successful,
        /// and false otherwise. If successful, the saved file's location is put into the passed outputDir.
        /// </summary>
        public virtual bool WriteOneToDisk(GameSaveData saveData, string saveDir, out string outputDir)
        {
            var success = WriteOneToDisk(saveData, saveDir);

            outputDir = "";

            if (success)
            {
                var fileName = string.Format(fileNameFormat,
                                             savePrefix, saveData.SlotNumber,
                                             fileExtension);
                var filePath = string.Format(filePathFormat, saveDir, fileName);
                outputDir = filePath;
            }

            return(success);
        }
        /// <summary>
        /// Writes the passed save data to the passed save directory, returning true if successful, or
        /// false otherwise.
        /// </summary>
        public virtual bool WriteOneToDisk(GameSaveData saveData, string saveDir)
        {
            // Safety.
            if (!Directory.Exists(saveDir))
            {
                var messageFormat =
                    @"Could not write save to {0}; that directory does not exist.";
                var message = string.Format(messageFormat, saveDir);
                Debug.LogError(message);
                return(false);
            }

            var dataToWrite = JsonUtility.ToJson(saveData, true);

            // Write the file at the appropriate directory with the appropriate writing method.
            var fileName = string.Format(fileNameFormat,
                                         savePrefix, saveData.SlotNumber,
                                         fileExtension);
            var filePath = string.Format(filePathFormat, saveDir, fileName);

            if (!writeEncrypted)
            {
                File.WriteAllText(filePath, dataToWrite, actualEncoding);
            }

            else
            {
                // Note: The binary-writing is not yet secure.
                using (Stream fileStream = File.Open(filePath, FileMode.Create))
                {
                    using (BinaryWriter writer = new BinaryWriter(fileStream, actualEncoding))
                    {
                        writer.Write(dataToWrite);
                    }
                }
            }
            Signals.GameSaveWritten.Invoke(saveData, filePath, fileName);
            GameSaveWritten.Invoke(saveData, filePath, fileName);
            return(true);
        }
Exemplo n.º 5
0
 protected virtual void OnGameSaveWritten(GameSaveData saveData, string filePath, string fileName)
 {
     unwrittenSaves.Remove(saveData);
     WrittenSaves[fileName] = saveData;
 }
 public GameSaveData(GameSaveData other)
 {
     SetFrom(other);
     Signals.GameSaveCreated.Invoke(this);
 }
 public virtual void Load(GameSaveData saveData)
 {
     saveManager.LoadSave(saveData);
 }
 /// <summary>
 /// Saves new save data to disk and the slot the data was set to be assigned to.
 /// </summary>
 public virtual void SaveToSlot(GameSaveData saveData)
 {
     saveManager.AddSave(saveData, true);
 }
 public virtual void ClearSlot(GameSaveData saveData)
 {
     saveManager.EraseSave(saveData);
 }