Example #1
0
        public static Stream CreateStream(Stream stream, ES3Settings settings, ES3FileMode fileMode)
        {
            try
            {
                bool isWriteStream = (fileMode != ES3FileMode.Read);

                            #if !DISABLE_ENCRYPTION
                // Encryption
                if (settings.encryptionType != ES3.EncryptionType.None && stream.GetType() != typeof(UnbufferedCryptoStream))
                {
                    EncryptionAlgorithm alg = null;
                    if (settings.encryptionType == ES3.EncryptionType.AES)
                    {
                        alg = new AESEncryptionAlgorithm();
                    }
                    stream = new UnbufferedCryptoStream(stream, !isWriteStream, settings.encryptionPassword, settings.bufferSize, alg);
                }
                #endif

                // Compression
                if (settings.compressionType != ES3.CompressionType.None && stream.GetType() != typeof(GZipStream))
                {
                    if (settings.compressionType == ES3.CompressionType.Gzip)
                    {
                        stream = isWriteStream ? new GZipStream(stream, CompressionMode.Compress) : new GZipStream(stream, CompressionMode.Decompress);
                    }
                }

                return(stream);
            }
            catch (System.Exception e)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
                if (e.GetType() == typeof(System.Security.Cryptography.CryptographicException))
                {
                    throw new System.Security.Cryptography.CryptographicException("Could not decrypt file. Please ensure that you are using the same password used to encrypt the file.");
                }
                else
                {
                    throw e;
                }
            }
        }
Example #2
0
        public static Stream CreateStream(Stream stream, ES3Settings settings, ES3FileMode fileMode)
        {
            bool isWriteStream = (fileMode != ES3FileMode.Read);

                        #if !DISABLE_ENCRYPTION
            if (settings.encryptionType != ES3.EncryptionType.None && stream.GetType() != typeof(UnbufferedCryptoStream))
            {
                EncryptionAlgorithm alg = null;
                if (settings.encryptionType == ES3.EncryptionType.AES)
                {
                    alg = new AESEncryptionAlgorithm();
                }
                stream = new UnbufferedCryptoStream(stream, !isWriteStream, settings.encryptionPassword, settings.bufferSize, alg);
            }
                        #endif

            return(stream);
        }
Example #3
0
        public static Stream CreateStream(ES3Settings settings, ES3FileMode fileMode)
        {
            bool   isWriteStream = (fileMode != ES3FileMode.Read);
            Stream stream        = null;

            if (settings.location == ES3.Location.Memory)
            {
                // There's no point in creating an empty MemoryStream if we're only reading from it.
                if (!isWriteStream)
                {
                    return(null);
                }
                stream = new MemoryStream(settings.bufferSize);
            }
            else if (settings.location == ES3.Location.File)
            {
                if (!isWriteStream && !ES3IO.FileExists(settings.FullPath))
                {
                    return(null);
                }
                stream = new ES3FileStream(settings.FullPath, fileMode, settings.bufferSize, false);
            }
            else if (settings.location == ES3.Location.PlayerPrefs)
            {
                if (isWriteStream)
                {
                    stream = new ES3PlayerPrefsStream(settings.FullPath, settings.bufferSize, (fileMode == ES3FileMode.Append));
                }
                else
                {
                    if (!PlayerPrefs.HasKey(settings.FullPath))
                    {
                        return(null);
                    }
                    stream = new ES3PlayerPrefsStream(settings.FullPath);
                }
            }
            else if (settings.location == ES3.Location.Resources)
            {
                if (!isWriteStream)
                {
                    var resourcesStream = new ES3ResourcesStream(settings.FullPath);
                    if (resourcesStream.Exists)
                    {
                        stream = resourcesStream;
                    }
                    else
                    {
                        resourcesStream.Dispose();
                        return(null);
                    }
                }
                else if (UnityEngine.Application.isEditor)
                {
                    throw new System.NotSupportedException("Cannot write directly to Resources folder. Try writing to a directory outside of Resources, and then manually move the file there.");
                }
                else
                {
                    throw new System.NotSupportedException("Cannot write to Resources folder at runtime. Use a different save location at runtime instead.");
                }
            }

                        #if !DISABLE_ENCRYPTION
            if (settings.encryptionType != ES3.EncryptionType.None)
            {
                EncryptionAlgorithm alg = null;
                if (settings.encryptionType == ES3.EncryptionType.AES)
                {
                    alg = new AESEncryptionAlgorithm();
                }
                stream = new UnbufferedCryptoStream(stream, !isWriteStream, settings.encryptionPassword, settings.bufferSize, alg);
            }
                        #endif

            return(stream);
        }