Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. using System.Collections.Generic;
  7.  
  8. namespace NovelScript
  9. {
  10.     public class SettingManager : VariableManager
  11.     {
  12.  
  13.         public new static readonly SettingManager Instance = new SettingManager();
  14.  
  15.         protected string saveFilePath = string.Format("{0}/settingsData.Ubinary", Application.persistentDataPath);
  16.         private BinaryFormatter formatter = new BinaryFormatter();
  17.         private DESCryptoServiceProvider DesSerivice = new DESCryptoServiceProvider();
  18.  
  19.         /*public new string Set(string key, string value)
  20.         {   //TODO : fileWrite
  21.             return base.Set(key, value);
  22.         }*/
  23.         public bool Save()
  24.         {
  25.             using (FileStream file = File.Create(saveFilePath))
  26.             using (Stream cryptoStream = new CryptoStream(file, DesSerivice.CreateEncryptor(), CryptoStreamMode.Write))
  27.             {
  28.  
  29.                 formatter.Serialize(cryptoStream, base.__storage);
  30.                 return true;
  31.             }
  32.             return false;
  33.             //base.__storage = loadedData;
  34.         }
  35.         public bool Load()
  36.         {
  37.             if (File.Exists(saveFilePath))
  38.             {
  39.                 using (Stream innerStream = File.Open(saveFilePath, FileMode.Open))
  40.                 // 2. create a CryptoStream in read mode
  41.                 using (Stream cryptoStream = new CryptoStream(innerStream, DesSerivice.CreateDecryptor(), CryptoStreamMode.Read))
  42.                 {
  43.                     // 3. read from the cryptoStream
  44.                     base.__storage = (Dictionary<string, Variable>)formatter.Deserialize(cryptoStream);
  45.                     return true;
  46.                 }
  47.             }
  48.             return false;
  49.         }
  50.         public new string Set(string key,string value)
  51.         {
  52.             this.Save();
  53.             return base.Set(key, value);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement