Example #1
0
    public static bool SaveGame()
    {
        EventState.SaveData(); // TODO: think about if this belongs here or not

        // back up existing save
        try
        {
            File.Delete(backupSaveFilename);
            if (File.Exists(saveFilename))
            {
                File.Move(saveFilename, backupSaveFilename);
            }
        }
        catch (Exception e)
        {
            Debug.LogError("couldn't save game. couldn't backup the current save file.");
            Debug.LogError(e.ToString());

            return(false);
        }

        // make a new save
        try
        {
            FileStream      file = File.Open(saveFilename, FileMode.Create, FileAccess.Write);
            BinaryFormatter bf   = new BinaryFormatter();
            bf.Serialize(file, singleton);
            file.Close();
        }
        catch (Exception e)
        {
            Debug.LogError("couldn't write a new save file.");
            Debug.LogError(e.ToString());

            // try to restore the backup
            try
            {
                File.Move(backupSaveFilename, saveFilename);
            }
            catch (Exception restoreException)
            {
                Debug.LogError("failed to restore the backup save file. oh no.");
                Debug.LogError(restoreException.ToString());
            }

            return(false);
        }

        return(false);
    }