BS 0.1 基础构建完成 / 0.2 Visual 同为Unity UI控件部分
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System.IO;
|
||||
|
||||
namespace ES3Internal
|
||||
{
|
||||
public enum ES3FileMode {Read, Write, Append}
|
||||
|
||||
public class ES3FileStream : FileStream
|
||||
{
|
||||
private bool isDisposed = false;
|
||||
|
||||
public ES3FileStream( string path, ES3FileMode 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, 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;
|
||||
}
|
||||
|
||||
protected static FileMode GetFileMode(ES3FileMode fileMode)
|
||||
{
|
||||
if (fileMode == ES3FileMode.Read)
|
||||
return FileMode.Open;
|
||||
else if (fileMode == ES3FileMode.Write)
|
||||
return FileMode.Create;
|
||||
else
|
||||
return FileMode.Append;
|
||||
}
|
||||
|
||||
protected static FileAccess GetFileAccess(ES3FileMode fileMode)
|
||||
{
|
||||
if (fileMode == ES3FileMode.Read)
|
||||
return FileAccess.Read;
|
||||
else if (fileMode == ES3FileMode.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 == 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);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 202b4849a1146f84cbb875d7f17a56b6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,63 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ES3Internal
|
||||
{
|
||||
internal class ES3PlayerPrefsStream : MemoryStream
|
||||
{
|
||||
private string path;
|
||||
private bool append;
|
||||
private bool isWriteStream = false;
|
||||
private bool isDisposed = false;
|
||||
|
||||
// This constructor should be used for read streams only.
|
||||
public ES3PlayerPrefsStream(string path) : base(GetData(path,false))
|
||||
{
|
||||
this.path = path;
|
||||
this.append = false;
|
||||
}
|
||||
|
||||
// This constructor should be used for write streams only.
|
||||
public ES3PlayerPrefsStream(string path, int bufferSize, bool append=false) : base(bufferSize)
|
||||
{
|
||||
this.path = path;
|
||||
this.append = append;
|
||||
this.isWriteStream = true;
|
||||
}
|
||||
|
||||
private static byte[] GetData(string path, bool isWriteStream)
|
||||
{
|
||||
if(!PlayerPrefs.HasKey(path))
|
||||
throw new FileNotFoundException("File \""+path+"\" could not be found in PlayerPrefs");
|
||||
return System.Convert.FromBase64String(PlayerPrefs.GetString(path));
|
||||
}
|
||||
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
if(isDisposed)
|
||||
return;
|
||||
isDisposed = true;
|
||||
if(isWriteStream && this.Length > 0)
|
||||
{
|
||||
if (append)
|
||||
{
|
||||
// Convert data back to bytes before appending, as appending Base-64 strings directly can corrupt the data.
|
||||
var sourceBytes = System.Convert.FromBase64String(PlayerPrefs.GetString(path));
|
||||
var appendBytes = this.ToArray();
|
||||
var finalBytes = new byte[sourceBytes.Length + appendBytes.Length];
|
||||
System.Buffer.BlockCopy(sourceBytes, 0, finalBytes, 0, sourceBytes.Length);
|
||||
System.Buffer.BlockCopy(appendBytes, 0, finalBytes, sourceBytes.Length, appendBytes.Length);
|
||||
|
||||
PlayerPrefs.SetString(path, System.Convert.ToBase64String(finalBytes));
|
||||
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
else
|
||||
PlayerPrefs.SetString(path + ES3IO.temporaryFileSuffix, System.Convert.ToBase64String(this.ToArray()));
|
||||
// Save the timestamp to a separate key.
|
||||
PlayerPrefs.SetString("timestamp_" + path, System.DateTime.UtcNow.Ticks.ToString());
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b9abb6e182b38e44b4060906605701f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,32 @@
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ES3Internal
|
||||
{
|
||||
internal class ES3ResourcesStream : MemoryStream
|
||||
{
|
||||
// Check that data exists by checking stream is not empty.
|
||||
public bool Exists{ get{ return this.Length > 0; } }
|
||||
|
||||
// Used when creating
|
||||
public ES3ResourcesStream(string path) : base(GetData(path))
|
||||
{
|
||||
}
|
||||
|
||||
private static byte[] GetData(string path)
|
||||
{
|
||||
var textAsset = Resources.Load(path) as TextAsset;
|
||||
|
||||
// If data doesn't exist in Resources, return an empty byte array.
|
||||
if(textAsset == null)
|
||||
return new byte[0];
|
||||
|
||||
return textAsset.bytes;
|
||||
}
|
||||
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f791d53f385e2f94681f83648eb6a5d8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
122
Convention/[ES3]/Easy Save 3/Scripts/Streams/ES3Stream.cs
Normal file
122
Convention/[ES3]/Easy Save 3/Scripts/Streams/ES3Stream.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace ES3Internal
|
||||
{
|
||||
public static class ES3Stream
|
||||
{
|
||||
public static Stream CreateStream(ES3Settings settings, ES3FileMode fileMode)
|
||||
{
|
||||
bool isWriteStream = (fileMode != ES3FileMode.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 == 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95fcefc93e7d25546819082901d9607c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user