From 4684b315763887a2b5d5d0241402e0f9bcd9134b Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Tue, 19 Jul 2022 15:14:28 +0300 Subject: [PATCH] feat: add save system class --- Assets/_Scripts/Systems/SaveSystem.meta | 8 ++ .../_Scripts/Systems/SaveSystem/PlayerData.cs | 8 ++ .../Systems/SaveSystem/PlayerData.cs.meta | 11 ++ .../_Scripts/Systems/SaveSystem/SaveSystem.cs | 125 ++++++++++++++++++ .../Systems/SaveSystem/SaveSystem.cs.meta | 11 ++ 5 files changed, 163 insertions(+) create mode 100644 Assets/_Scripts/Systems/SaveSystem.meta create mode 100644 Assets/_Scripts/Systems/SaveSystem/PlayerData.cs create mode 100644 Assets/_Scripts/Systems/SaveSystem/PlayerData.cs.meta create mode 100644 Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs create mode 100644 Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs.meta diff --git a/Assets/_Scripts/Systems/SaveSystem.meta b/Assets/_Scripts/Systems/SaveSystem.meta new file mode 100644 index 0000000..cdf3d39 --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4f939cb7b60259b3ebee72c45d71f2d2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs b/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs new file mode 100644 index 0000000..66f3e5b --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs @@ -0,0 +1,8 @@ +[System.Serializable] +public class PlayerData +{ + public string Username { get; set; } + public string Password { get; set; } + + public int HighScore { get; set; } +} diff --git a/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs.meta b/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs.meta new file mode 100644 index 0000000..51bd77b --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/PlayerData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 89eb7b496b46c2935a7ffba56bc751dd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs b/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs new file mode 100644 index 0000000..6f6265e --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs @@ -0,0 +1,125 @@ +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; +using System.Threading.Tasks; +using UnityEngine; +using Newtonsoft.Json; + +public static class SaveSystem +{ + private static readonly string Path = $"{Application.persistentDataPath}/saves"; + + public static void SaveToBinary(string fileName, T data) + { + if (!Directory.Exists(Path)) + { + Directory.CreateDirectory(Path); + } + + using var stream = new FileStream($"{Path}/{fileName}", FileMode.Create); + byte[] buffer = ObjectToByteArray(data); + stream.Write(buffer, 0, buffer.Length); + } + + public static T LoadFromBinary(string fileName) + { + if (!Directory.Exists($"{Path}") || !File.Exists($"{Path}/{fileName}")) + { + return default; + } + + using var stream = new FileStream($"{Path}/{fileName}", FileMode.Open); + var buffer = new byte[stream.Length]; + stream.Read(buffer, 0, buffer.Length); + return ByteArrayToObject(buffer); + } + + public static async Task SaveToBinaryAsync(string fileName, T data) + { + if (!Directory.Exists(Path)) + { + Directory.CreateDirectory(Path); + } + + using var stream = new FileStream($"{Path}/{fileName}", FileMode.Create); + byte[] buffer = ObjectToByteArray(data); + await stream.WriteAsync(buffer, 0, buffer.Length); + } + + public static async Task LoadFromBinaryAsync(string fileName) + { + if (!Directory.Exists($"{Path}") || !File.Exists($"{Path}/{fileName}")) + { + return default; + } + + using var stream = new FileStream($"{Path}/{fileName}", FileMode.Open); + var buffer = new byte[stream.Length]; + await stream.ReadAsync(buffer, 0, buffer.Length); + return ByteArrayToObject(buffer); + } + + public static void SaveToJson(string fileName, T data) + { + if (!Directory.Exists(Path)) + { + Directory.CreateDirectory(Path); + } + + using var outputFile = new StreamWriter($"{Path}/{fileName}"); + var json = JsonConvert.SerializeObject(data); + outputFile.Write(json); + } + + public static async Task SaveToJsonAsync(string fileName, T data) + { + if (!Directory.Exists(Path)) + { + Directory.CreateDirectory(Path); + } + + using var outputFile = new StreamWriter($"{Path}/{fileName}"); + var json = JsonConvert.SerializeObject(data); + await outputFile.WriteAsync(json); + } + + public static T LoadFromJson(string fileName) + { + if (!Directory.Exists($"{Path}") || !File.Exists($"{Path}/{fileName}")) + { + return default; + } + + using var inputFile = new StreamReader($"{Path}/{fileName}"); + var json = inputFile.ReadToEnd(); + return JsonConvert.DeserializeObject(json); + } + + public static async Task LoadFromJsonAsync(string fileName) + { + if (!Directory.Exists($"{Path}") || !File.Exists($"{Path}/{fileName}")) + { + return default; + } + + using var inputFile = new StreamReader($"{Path}/{fileName}"); + var json = await inputFile.ReadToEndAsync(); + return JsonConvert.DeserializeObject(json); + } + + private static byte[] ObjectToByteArray(System.Object obj) + { + var binaryFormatter = new BinaryFormatter(); + using var stream = new MemoryStream(); + binaryFormatter.Serialize(stream, obj); + return stream.ToArray(); + } + + private static T ByteArrayToObject(byte[] arr) + { + var binaryFormatter = new BinaryFormatter(); + using var stream = new MemoryStream(); + stream.Write(arr, 0, arr.Length); + stream.Position = 0; + return (T) binaryFormatter.Deserialize(stream); + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs.meta b/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs.meta new file mode 100644 index 0000000..b138713 --- /dev/null +++ b/Assets/_Scripts/Systems/SaveSystem/SaveSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e6f13b469d7bf838695d0abb771b6a4e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: