Пример #1
0
        /// <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();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Save specified ISavable object to a xml string in PlayerPrefs
        /// </summary>
        /// <param name="savable">ISavable to save</param>
        /// <param name="keyString">key of xmldata in PlayerPrefs</param>
        public static void SaveXmlToPlayerPrefs(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 xmlContent = SaveToXmlContent(savable);

            if (password != null)
            {
                SecurePlayerPrefs.SetString(keyString, xmlContent, password);
            }
            else
            {
                UnityEngine.PlayerPrefs.SetString(keyString, xmlContent);
            }
            UnityEngine.PlayerPrefs.Save();
        }
Пример #3
0
        /// <summary>
        /// Load specified ISavable object from a xml string already saved in PlayerPrefs
        /// </summary>
        /// <param name="savable">ISavable to load</param>
        /// <param name="keyString">key of xmldata in PlayerPrefs</param>
        public static void LoadXmlFromPlayerPrefs(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 xmlContent = null;

            if (password != null)
            {
                xmlContent = SecurePlayerPrefs.GetString(keyString, password);
            }
            else
            {
                xmlContent = UnityEngine.PlayerPrefs.GetString(keyString);
            }
            LoadFromXmlContent(savable, xmlContent);
        }
Пример #4
0
        /// <summary>
        /// Load specified ISavable object to xml file
        /// </summary>
        /// <param name="savable">ISavable to save</param>
        /// <param name="fileName"> full path of destination file</param>
        public static void SaveToXmlFile(ISavable savable, string fileName, string password = null)
        {
            if (savable == null)
            {
                throw new ArgumentNullException("Invalid ISavable object.");
            }

            string dir  = System.IO.Path.GetDirectoryName(fileName);
            string name = System.IO.Path.GetFileName(fileName);

            int    tempPostfix = 0;
            string tempFile    = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix);

            while (System.IO.File.Exists(tempFile))
            {
                tempPostfix++;
                tempFile = System.IO.Path.Combine(dir, name + "_Tmp" + tempPostfix);
            }

            string xmlContent = SaveToXmlContent(savable);

            if (password != null)
            {
                xmlContent = SecurePlayerPrefs.Encrypt(xmlContent, password);
            }

            System.IO.FileStream fileStream = new System.IO.FileStream(tempFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            System.IO.TextWriter writer     = new System.IO.StreamWriter(fileStream);
            writer.Write(xmlContent);
            writer.Close();
            fileStream.Close();

            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }
            System.IO.File.Move(tempFile, fileName);
        }