EP 0.1.1 Symbolization
This commit is contained in:
68
Convention/[Runtime]/EasySave/Streams/EasySaveFileStream.cs
Normal file
68
Convention/[Runtime]/EasySave/Streams/EasySaveFileStream.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Convention.EasySave.Internal
|
||||
{
|
||||
public enum EasySaveFileMode {Read, Write, Append}
|
||||
|
||||
public class EasySaveFileStream : FileStream
|
||||
{
|
||||
private bool isDisposed = false;
|
||||
|
||||
public EasySaveFileStream( string path, EasySaveFileMode fileMode, int bufferSize, bool useAsync)
|
||||
: base( GetPath(path, fileMode), GetFileMode(fileMode), GetFileAccess(fileMode), FileShare.None, bufferSize, useAsync)
|
||||
{
|
||||
}
|
||||
|
||||
// Gets a temporary path if necessary.
|
||||
protected static string GetPath(string path, EasySaveFileMode fileMode)
|
||||
{
|
||||
string directoryPath = EasySaveIO.GetDirectoryPath(path);
|
||||
// Attempt to create the directory incase it does not exist if we are storing data.
|
||||
if (fileMode != EasySaveFileMode.Read && directoryPath != EasySaveIO.persistentDataPath)
|
||||
EasySaveIO.CreateDirectory(directoryPath);
|
||||
if(fileMode != EasySaveFileMode.Write || fileMode == EasySaveFileMode.Append)
|
||||
return path;
|
||||
return (fileMode == EasySaveFileMode.Write) ? path + EasySaveIO.temporaryFileSuffix : path;
|
||||
}
|
||||
|
||||
protected static FileMode GetFileMode(EasySaveFileMode fileMode)
|
||||
{
|
||||
if (fileMode == EasySaveFileMode.Read)
|
||||
return FileMode.Open;
|
||||
else if (fileMode == EasySaveFileMode.Write)
|
||||
return FileMode.Create;
|
||||
else
|
||||
return FileMode.Append;
|
||||
}
|
||||
|
||||
protected static FileAccess GetFileAccess(EasySaveFileMode fileMode)
|
||||
{
|
||||
if (fileMode == EasySaveFileMode.Read)
|
||||
return FileAccess.Read;
|
||||
else if (fileMode == EasySaveFileMode.Write)
|
||||
return FileAccess.Write;
|
||||
else
|
||||
return FileAccess.Write;
|
||||
}
|
||||
|
||||
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 == EasySaveFileMode.Write && fileMode != EasySaveFileMode.Append)
|
||||
{
|
||||
// Delete the old file before overwriting it.
|
||||
EasySaveIO.DeleteFile(path);
|
||||
// Rename temporary file to new file.
|
||||
EasySaveIO.MoveFile(path + EasySave.temporaryFileSuffix, path);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
92
Convention/[Runtime]/EasySave/Streams/EasySaveStream.cs
Normal file
92
Convention/[Runtime]/EasySave/Streams/EasySaveStream.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System;
|
||||
|
||||
namespace Convention.EasySave.Internal
|
||||
{
|
||||
public static class EasySaveStream
|
||||
{
|
||||
public static Stream CreateStream(EasySaveSettings settings, EasySaveFileMode fileMode)
|
||||
{
|
||||
bool isWriteStream = (fileMode != EasySaveFileMode.Read);
|
||||
Stream stream = null;
|
||||
|
||||
// Check that the path is in a valid format. This will throw an exception if not.
|
||||
new FileInfo(settings.FullPath);
|
||||
|
||||
try
|
||||
{
|
||||
if (settings.location == EasySave.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 == EasySave.Location.File)
|
||||
{
|
||||
if (!isWriteStream && !EasySaveIO.FileExists(settings.FullPath))
|
||||
return null;
|
||||
stream = new EasySaveFileStream(settings.FullPath, fileMode, settings.bufferSize, false);
|
||||
}
|
||||
|
||||
return CreateStream(stream, settings, fileMode);
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
if (stream != null)
|
||||
stream.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static Stream CreateStream(Stream stream, EasySaveSettings settings, EasySaveFileMode fileMode)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isWriteStream = (fileMode != EasySaveFileMode.Read);
|
||||
|
||||
#if !DISABLE_ENCRYPTION
|
||||
// Encryption
|
||||
if(settings.encryptionType != EasySave.EncryptionType.None && stream.GetType() != typeof(UnbufferedCryptoStream))
|
||||
{
|
||||
EncryptionAlgorithm alg = null;
|
||||
if(settings.encryptionType == EasySave.EncryptionType.AES)
|
||||
alg = new AESEncryptionAlgorithm();
|
||||
stream = new UnbufferedCryptoStream(stream, !isWriteStream, settings.encryptionPassword, settings.bufferSize, alg);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Compression
|
||||
if (settings.compressionType != EasySave.CompressionType.None && stream.GetType() != typeof(GZipStream))
|
||||
{
|
||||
if (settings.compressionType == EasySave.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;
|
||||
}
|
||||
}
|
||||
|
||||
public static void CopyTo(Stream source, Stream destination)
|
||||
{
|
||||
#if UNITY_2019_1_OR_NEWER
|
||||
source.CopyTo(destination);
|
||||
#else
|
||||
byte[] buffer = new byte[2048];
|
||||
int bytesRead;
|
||||
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
|
||||
destination.Write(buffer, 0, bytesRead);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user