浏览代码

Removed BinaryFormatter and Refactor.

Removed the BinaryFormatter because it's unsafe to use, so we're currently serializing and deserializing to json.
Refactored the code for simplicity and readability.
/UI
Bronson Zgeb 3 年前
当前提交
5d81bec6
共有 7 个文件被更改,包括 106 次插入84 次删除
  1. 20
      UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
  2. 93
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  3. 9
      UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs
  4. 39
      UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs
  5. 3
      UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs.meta
  6. 23
      UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs
  7. 3
      UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs.meta

20
UOP1_Project/Assets/Scripts/SaveSystem/Save.cs


/// <summary>
/// <b>DO NOT DELETE THIS FILE !!!!!!</b><br/>
/// This class contains all the variables that will be serialized and saved to a binary file.<br/>
using UnityEngine;
/// <summary>
/// This class contains all the variables that will be serialized and saved to a file.<br/>
// This is test data, written accodring to TestScript.cs class
// This is test data, written according to TestScript.cs class
public string ToJson()
{
return JsonUtility.ToJson(this);
}
public void LoadFromJson(string json)
{
JsonUtility.FromJsonOverwrite(json, this);
}
}

93
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public interface ISaveable
{
/// <summary>
/// <para>The function which needs to be subscribed to the <see cref="SaveSystem.AddToRegistryCallback"/></para>
/// Include it in a place which only gets executed once to avoid data duplication.<br/>
/// Like in OnEnable() function of Monobehaviour or ScriptableObject
/// </summary>
void AddToSaveRegistry(HashSet<ISaveable> registry);
/// <summary>
/// Pure virtual function for saving data to a save file.<br/>
/// This will comprise the serialization logic.
/// </summary>
void Serialize(Save saveFile);
/// <summary>
/// Pure virtual function for loading data from a save file.<br/>
/// This will comprise the deserialziation logic.
/// </summary>
void Deserialize(Save saveFile);
}
using System.Collections.Generic;
using UnityEngine;
[SerializeField]
string _fileNameExtension;
const string SAVE_FILENAME = "save.chop";
private void Start()
{

private void LoadGame()
{
FileStream file;
if(!File.Exists(Application.persistentDataPath + "/save" + _fileNameExtension))
if (FileManager.LoadFromFile(SAVE_FILENAME, out var json))
Debug.LogError("No Save Data found");
return;
Save saveData = new Save();
saveData.LoadFromJson(json);
} else
{
file = File.Open(Application.persistentDataPath + "/save" + _fileNameExtension, FileMode.Open);
foreach (ISaveable saveable in _saveRegistry)
{
saveable.Deserialize(saveData);
}
BinaryFormatter formatter = new BinaryFormatter();
Save saveClass = (Save)formatter.Deserialize(file);
file.Close();
foreach(ISaveable saveable in _saveRegistry)
{
saveable.Deserialize(saveClass);
}
}
private void SaveAndQuit()
{
SaveGame();
Application.Quit();
FileStream file;
if(!File.Exists(Application.persistentDataPath + "/save" + _fileNameExtension))
{
file = File.Create(Application.persistentDataPath + "/save" + _fileNameExtension);
} else
// A class with name "Save" must exist in the project. It will contain the appropriate save file structure.
Save saveData = new Save();
foreach (ISaveable saveable in _saveRegistry)
file = File.Open(Application.persistentDataPath + "/save" + _fileNameExtension, FileMode.Open);
saveable.Serialize(saveData);
// A class with name "Save" must exist in the project. It will contain the appropriate save file structure.
Save saveClass = new Save();
foreach(ISaveable saveable in _saveRegistry)
if (FileManager.WriteToFile(SAVE_FILENAME, saveData.ToJson()))
saveable.Serialize(saveClass);
Debug.Log("Save successful");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(file, saveClass);
file.Close();
SaveAndQuit();
SaveGame();
}

9
UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs


using System.Collections.Generic;
using System.IO;
/// Unfortunately, inspite of all my efforts, I wasn't able to remove all dependencies.
/// Unfortunately, in spite of all my efforts, I wasn't able to remove all dependencies.
int _testInteger = default;
float _testFloat = default;
bool _testBool = default;
int _testInteger;
float _testFloat;
bool _testBool;
private void OnEnable()
{

39
UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs


using System;
using System.IO;
using UnityEngine;
public static class FileManager
{
public static bool WriteToFile(string fileName, string fileContents)
{
var fullPath = Path.Combine(Application.persistentDataPath, fileName);
try
{
File.WriteAllText(fullPath, fileContents);
return true;
}
catch (Exception e)
{
Debug.LogError($"Failed to write to {fullPath} with exception {e}");
return false;
}
}
public static bool LoadFromFile(string fileName, out string result)
{
var fullPath = Path.Combine(Application.persistentDataPath, fileName);
try
{
result = File.ReadAllText(fullPath);
return true;
}
catch (Exception e)
{
Debug.LogError($"Failed to read from {fullPath} with exception {e}");
result = "";
return false;
}
}
}

3
UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs.meta


fileFormatVersion: 2
guid: ec47a33ed5b5434f8bf6500b09136709
timeCreated: 1613414864

23
UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs


using System.Collections.Generic;
public interface ISaveable
{
/// <summary>
/// <para>The function which needs to be subscribed to the <see cref="SaveSystem.AddToRegistryCallback"/></para>
/// Include it in a place which only gets executed once to avoid data duplication.<br/>
/// Like in OnEnable() function of Monobehaviour or ScriptableObject
/// </summary>
void AddToSaveRegistry(HashSet<ISaveable> registry);
/// <summary>
/// Pure virtual function for saving data to a save file.<br/>
/// This will comprise the serialization logic.
/// </summary>
void Serialize(Save saveFile);
/// <summary>
/// Pure virtual function for loading data from a save file.<br/>
/// This will comprise the deserialziation logic.
/// </summary>
void Deserialize(Save saveFile);
}

3
UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs.meta


fileFormatVersion: 2
guid: ea156e29696b4339a873e00d168dd164
timeCreated: 1613415034
正在加载...
取消
保存