Ejemplo n.º 1
0
        /// <summary>
        /// Load specified ISavable object from xml file
        /// </summary>
        /// <param name="savable">ISavable to load</param>
        /// <param name="fileName"> full path of file</param>
        public static void LoadFromXmlFile(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.");
            }

            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 xmlContent = reader.ReadToEnd();

            if (password != null)
            {
                string decryptedXmlContent;
                if (SecurePlayerPrefs.TryDecrypt(xmlContent, password, out decryptedXmlContent))
                {
                    xmlContent = decryptedXmlContent;
                }
                else
                {
                    xmlContent = string.Empty;
                }
            }
            reader.Close();
            fileStream.Close();

            LoadFromXmlContent(savable, xmlContent);
        }
Ejemplo n.º 2
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();
                }
            }
        }