Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Threading;
- using UnityEngine;
- namespace Assets
- {
- using Assets.Midnight_Engine.Scripts.Files;
- public class Test123
- {
- [Serializable]
- private class Hello
- {
- public string Value { get; set; }
- public int Value2 { get; set; }
- public override string ToString()
- {
- return this.Value + " " + this.Value2;
- }
- }
- public static void Test()
- {
- LogHelper.Log(typeof(Test123), " Starting...");
- object readO;
- object o = "123";
- SimpleSave.SaveObject(o, "stringtest.dat");
- Thread.Sleep(300);
- SimpleLoad.LoadObject(out readO, "stringtest.dat");
- LogHelper.Log(typeof(Test123), "string = " + readO);
- Hello readH;
- Hello h = new Hello { Value = "hello world", Value2 = 200 };
- SimpleSave.SaveObject(h, "classTest");
- Thread.Sleep(300);
- SimpleLoad.LoadObjectOfType(out readH, "classTest");
- LogHelper.Log(typeof(Test123), "class = " + readH);
- }
- }
- }
- namespace Assets.Midnight_Engine.Scripts.Files
- {
- internal static class Shared
- {
- internal const string DEFAULT_SAVE_FILE_NAME = "SavedData.dat";
- internal static string CheckFilePath(string filePath)
- {
- if (!filePath.ToUpper().EndsWith(".DAT"))
- filePath += ".dat";
- if (filePath.StartsWith("//"))
- filePath = filePath.Remove(0, 1);
- filePath = Application.persistentDataPath + "/" + filePath;
- return filePath;
- }
- }
- public static class SimpleSave
- {
- /// <summary>
- /// Make sure the object contains [Serializeable]
- /// </summary>
- /// <param name="o">Make sure the object contains [Serializeable]</param>
- public static void SaveObject<T>(T o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME)
- {
- filePath = Shared.CheckFilePath(filePath);
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fs = File.Create(filePath);
- bf.Serialize(fs, o);
- fs.Close();
- LogHelper.Log(typeof(SimpleSave), "Saved file " + filePath);
- }
- }
- public static class SimpleLoad
- {
- /// <summary>
- /// Make sure the object contains [Serializeable] and has an empty constructor
- /// </summary>
- /// <param name="o">Make sure the object contains [Serializeable] and has an empty constructor</param>
- public static void LoadObjectOfType<T>(out T o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME) where T : new()
- {
- filePath = Shared.CheckFilePath(filePath);
- if (!File.Exists(filePath))
- {
- LogHelper.WriteErrorMessage(typeof(SimpleLoad), "LoadObject", "File does not exist: " + filePath);
- o = new T();
- return;
- }
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fs = File.Open(filePath, FileMode.Open);
- o = (T)bf.Deserialize(fs);
- fs.Close();
- }
- /// <summary>
- /// Make sure the object contains [Serializeable]
- /// </summary>
- /// <param name="o">Make sure the object contains [Serializeable]</param>
- public static void LoadObject(out object o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME)
- {
- filePath = Shared.CheckFilePath(filePath);
- if (!File.Exists(filePath))
- {
- LogHelper.WriteErrorMessage(typeof(SimpleLoad), "LoadObject", "File does not exist: " + filePath);
- o = null;
- return;
- }
- BinaryFormatter bf = new BinaryFormatter();
- FileStream fs = File.Open(filePath, FileMode.Open);
- o = bf.Deserialize(fs);
- fs.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement