Esempio n. 1
0
 // Gets a temporary path if necessary.
 protected static string GetPath(string path, ES3FileMode fileMode)
 {
     // Attempt to create the directory incase it does not exist.
     ES3IO.CreateDirectory(ES3IO.GetDirectoryName(path));
     if (fileMode != ES3FileMode.Write || fileMode == ES3FileMode.Append)
     {
         return(path);
     }
     return((fileMode == ES3FileMode.Write) ? path + ES3.temporaryFileSuffix : path);
 }
Esempio n. 2
0
        // Gets a temporary path if necessary.
        protected static string GetPath(string path, ES3FileMode fileMode)
        {
            // Attempt to create the directory incase it does not exist.
            string directoryPath = ES3IO.GetDirectoryPath(path);

            if (directoryPath != UnityEngine.Application.persistentDataPath)
            {
                ES3IO.CreateDirectory(directoryPath);
            }
            if (fileMode != ES3FileMode.Write || fileMode == ES3FileMode.Append)
            {
                return(path);
            }
            return((fileMode == ES3FileMode.Write) ? path + ES3.temporaryFileSuffix : path);
        }
Esempio n. 3
0
        // Gets a temporary path if necessary.
        protected static string GetPath(string path, ES3FileMode fileMode)
        {
            string directoryPath = ES3IO.GetDirectoryPath(path);

            // Attempt to create the directory incase it does not exist if we are storing data.
            if (fileMode != ES3FileMode.Read && directoryPath != ES3IO.persistentDataPath)
            {
                ES3IO.CreateDirectory(directoryPath);
            }
            if (fileMode != ES3FileMode.Write || fileMode == ES3FileMode.Append)
            {
                return(path);
            }
            return((fileMode == ES3FileMode.Write) ? path + ES3IO.temporaryFileSuffix : path);
        }
Esempio n. 4
0
        protected override void Dispose(bool disposing)
        {
            // Ensure we only perform disposable once.
            if (isDisposed)
            {
                return;
            }
            isDisposed = true;

            base.Dispose(disposing);

            // If this is a file writer, we need to replace the temp file.
            if (fileMode == ES3FileMode.Write && fileMode != ES3FileMode.Append)
            {
                // Delete the old file before overwriting it.
                ES3IO.DeleteFile(path);
                // Rename temporary file to new file.
                ES3IO.MoveFile(path + ES3.temporaryFileSuffix, path);
            }
        }
        public static Stream CreateStream(ES3Settings settings, ES3FileMode fileMode)
        {
            bool   isWriteStream = (fileMode != ES3FileMode.Read);
            Stream stream        = null;

            try
            {
                if (settings.location == ES3.Location.InternalMS)
                {
                    // 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.");
                    }
                }

                return(CreateStream(stream, settings, fileMode));
            }
            catch (System.Exception e)
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
                throw e;
            }
        }
Esempio n. 6
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);
        }