Advertisement
SHEePYTaGGeRNeP

Simple Save & Simple Load

Feb 24th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Threading;
  6. using UnityEngine;
  7.  
  8. namespace Assets
  9. {
  10.     using Assets.Midnight_Engine.Scripts.Files;
  11.     public class Test123
  12.     {
  13.         [Serializable]
  14.         private class Hello
  15.         {
  16.             public string Value { get; set; }
  17.             public int Value2 { get; set; }
  18.             public override string ToString()
  19.             {
  20.                 return this.Value + " " + this.Value2;
  21.             }
  22.         }
  23.  
  24.         public static void Test()
  25.         {
  26.             LogHelper.Log(typeof(Test123), " Starting...");
  27.             object readO;
  28.             object o = "123";
  29.             SimpleSave.SaveObject(o, "stringtest.dat");
  30.             Thread.Sleep(300);
  31.             SimpleLoad.LoadObject(out readO, "stringtest.dat");
  32.             LogHelper.Log(typeof(Test123), "string = " + readO);
  33.  
  34.             Hello readH;
  35.             Hello h = new Hello { Value = "hello world", Value2 = 200 };
  36.             SimpleSave.SaveObject(h, "classTest");
  37.             Thread.Sleep(300);
  38.             SimpleLoad.LoadObjectOfType(out readH, "classTest");
  39.             LogHelper.Log(typeof(Test123), "class = " + readH);
  40.         }
  41.     }
  42. }
  43.  
  44. namespace Assets.Midnight_Engine.Scripts.Files
  45. {
  46.     internal static class Shared
  47.     {
  48.         internal const string DEFAULT_SAVE_FILE_NAME = "SavedData.dat";
  49.         internal static string CheckFilePath(string filePath)
  50.         {
  51.             if (!filePath.ToUpper().EndsWith(".DAT"))
  52.                 filePath += ".dat";
  53.             if (filePath.StartsWith("//"))
  54.                 filePath = filePath.Remove(0, 1);
  55.             filePath = Application.persistentDataPath + "/" + filePath;
  56.             return filePath;
  57.         }
  58.     }
  59.     public static class SimpleSave
  60.     {
  61.         /// <summary>
  62.         /// Make sure the object contains [Serializeable]
  63.         /// </summary>
  64.         /// <param name="o">Make sure the object contains [Serializeable]</param>
  65.         public static void SaveObject<T>(T o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME)
  66.         {
  67.             filePath = Shared.CheckFilePath(filePath);
  68.             BinaryFormatter bf = new BinaryFormatter();
  69.             FileStream fs = File.Create(filePath);
  70.  
  71.             bf.Serialize(fs, o);
  72.             fs.Close();
  73.             LogHelper.Log(typeof(SimpleSave), "Saved file " + filePath);
  74.         }
  75.  
  76.     }
  77.  
  78.     public static class SimpleLoad
  79.     {
  80.         /// <summary>
  81.         /// Make sure the object contains [Serializeable] and has an empty constructor
  82.         /// </summary>
  83.         /// <param name="o">Make sure the object contains [Serializeable] and has an empty constructor</param>
  84.         public static void LoadObjectOfType<T>(out T o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME) where T : new()
  85.         {
  86.             filePath = Shared.CheckFilePath(filePath);
  87.             if (!File.Exists(filePath))
  88.             {
  89.                 LogHelper.WriteErrorMessage(typeof(SimpleLoad), "LoadObject", "File does not exist: " + filePath);
  90.                 o = new T();
  91.                 return;
  92.             }
  93.  
  94.             BinaryFormatter bf = new BinaryFormatter();
  95.             FileStream fs = File.Open(filePath, FileMode.Open);
  96.             o = (T)bf.Deserialize(fs);
  97.             fs.Close();
  98.         }
  99.         /// <summary>
  100.         /// Make sure the object contains [Serializeable]
  101.         /// </summary>
  102.         /// <param name="o">Make sure the object contains [Serializeable]</param>
  103.         public static void LoadObject(out object o, string filePath = "/" + Shared.DEFAULT_SAVE_FILE_NAME)
  104.         {
  105.             filePath = Shared.CheckFilePath(filePath);
  106.             if (!File.Exists(filePath))
  107.             {
  108.                 LogHelper.WriteErrorMessage(typeof(SimpleLoad), "LoadObject", "File does not exist: " + filePath);
  109.                 o = null;
  110.                 return;
  111.             }
  112.             BinaryFormatter bf = new BinaryFormatter();
  113.             FileStream fs = File.Open(filePath, FileMode.Open);
  114.             o = bf.Deserialize(fs);
  115.             fs.Close();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement