示例#1
0
        /// <summary>
        /// Load specified ISavable object to binary file
        /// </summary>
        /// <param name="savable">ISavable to save</param>
        /// <param name="fileName"> full path of destination file</param>
        public static void SaveToBinaryFile(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);
            }

            PCBinarySaveStream stream = new PCBinarySaveStream(tempFile);

            try
            {
                if (password != null)
                {
                    string str = SaveToBinaryString(savable);
                    str = SecurePlayerPrefs.Encrypt(str, password);
                    stream.Write(str);
                }
                else
                {
                    SaveToStream(savable, stream);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                stream.Close();
            }

            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }
            System.IO.File.Move(tempFile, fileName);
        }
示例#2
0
        /// <summary>
        /// Save specified ISavable object to binary string
        /// </summary>
        /// <param name="savable">ISavable to save</param>
        public static string SaveToBinaryString(ISavable savable)
        {
            if (savable == null)
            {
                throw new ArgumentNullException("Invalid ISavable object.");
            }
            MemoryStream       stream     = new MemoryStream();
            PCBinarySaveStream saveStream = new PCBinarySaveStream(stream);

            SaveToStream(savable, saveStream);
            stream.Flush();
            string saveData = ConvertBytesToString(stream.ToArray());

            saveStream.Close();
            return(saveData);
        }