/// <summary> /// Load specified ISavable object from a binarydata as string already saved in PlayerPrefs /// </summary> /// <param name="savable">ISavable to load</param> /// <param name="keyString">key of binarydata in PlayerPrefs</param> public static void LoadBinaryFromPlayerPrefs(ISavable savable, string keyString, string password = null) { if (savable == null) { throw new ArgumentNullException("Invalid ISavable object."); } if (string.IsNullOrEmpty(keyString)) { throw new ArgumentException("Invalid key."); } string binaryAsStringData = null; if (password != null) { binaryAsStringData = SecurePlayerPrefs.GetString(keyString, password); } else { binaryAsStringData = UnityEngine.PlayerPrefs.GetString(keyString); } byte[] binaryData = ConvertStringToBytes(binaryAsStringData); MemoryStream stream = new MemoryStream(binaryData, false); PCBinaryLoadStream loadStream = new PCBinaryLoadStream(stream); LoadFromStream(savable, loadStream); loadStream.Close(); }
/// <summary> /// Load specified ISavable object from a stream /// </summary> /// <param name="savable">ISavable to load</param> /// <param name="stream">Stream to load</param> public static void LoadFromStream(ISavable savable, PCBinaryLoadStream stream) { if (savable == null) { throw new ArgumentNullException("Invalid ISavable object."); } savable.Load(stream); }
/// <summary> /// Load specified ISavable object from binary file /// </summary> /// <param name="savable">ISavable to load</param> /// <param name="fileName"> full path of file</param> public static void LoadFromBinaryFile(ISavable savable, string fileName, string password = null) { if (savable == null) { throw new ArgumentNullException("Invalid ISavable object."); } if (!File.Exists(fileName)) { throw new FileNotFoundException("Invalie fileName."); } if (password != null) { System.IO.FileStream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.TextReader reader = new System.IO.StreamReader(fileStream); string content = reader.ReadToEnd(); string decryptedContent; SecurePlayerPrefs.TryDecrypt(content, password, out decryptedContent); byte[] binaryData = ConvertStringToBytes(decryptedContent); MemoryStream stream = new MemoryStream(binaryData, false); PCBinaryLoadStream loadStream = new PCBinaryLoadStream(stream); LoadFromStream(savable, loadStream); loadStream.Close(); } else { PCBinaryLoadStream stream = new PCBinaryLoadStream(fileName); try { LoadFromStream(savable, stream); } catch (Exception) { throw; } finally { stream.Close(); } } }