mirror of https://github.com/tuyoogame/YooAsset
原生文件加密/解密
parent
07d34891ef
commit
c6377ce544
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
namespace YooAsset.Editor
|
||||||
|
{
|
||||||
|
public class TaskEncryption_RFBP : TaskEncryption, IBuildTask
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加密文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
public void Run(BuildContext context)
|
||||||
|
{
|
||||||
|
var buildParameters = context.GetContextObject<BuildParametersContext>();
|
||||||
|
var buildMapContext = context.GetContextObject<BuildMapContext>();
|
||||||
|
|
||||||
|
var buildMode = buildParameters.Parameters.BuildMode;
|
||||||
|
|
||||||
|
if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
|
||||||
|
{
|
||||||
|
EncryptingBundleFiles(buildParameters, buildMapContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b3e156139dcc25f4c9440ec3d6cb96d2
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -32,6 +32,7 @@ namespace YooAsset.Editor
|
||||||
new TaskPrepare_RFBP(),
|
new TaskPrepare_RFBP(),
|
||||||
new TaskGetBuildMap_RFBP(),
|
new TaskGetBuildMap_RFBP(),
|
||||||
new TaskBuilding_RFBP(),
|
new TaskBuilding_RFBP(),
|
||||||
|
new TaskEncryption_RFBP(),
|
||||||
new TaskUpdateBundleInfo_RFBP(),
|
new TaskUpdateBundleInfo_RFBP(),
|
||||||
new TaskCreateManifest_RFBP(),
|
new TaskCreateManifest_RFBP(),
|
||||||
new TaskCreateReport_RFBP(),
|
new TaskCreateReport_RFBP(),
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace YooAsset
|
namespace YooAsset
|
||||||
{
|
{
|
||||||
|
@ -267,7 +268,17 @@ namespace YooAsset
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
string filePath = GetBuildinFileLoadPath(bundle);
|
string filePath = GetBuildinFileLoadPath(bundle);
|
||||||
return FileUtility.ReadAllBytes(filePath);
|
var data = FileUtility.ReadAllBytes(filePath);
|
||||||
|
if (bundle.Encrypted)
|
||||||
|
{
|
||||||
|
if (DecryptionServices == null)
|
||||||
|
{
|
||||||
|
YooLogger.Error($"DecryptionServices is Null!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return DecryptionServices.ReadFileData(data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
public virtual string ReadFileText(PackageBundle bundle)
|
public virtual string ReadFileText(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
|
@ -278,7 +289,18 @@ namespace YooAsset
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
string filePath = GetBuildinFileLoadPath(bundle);
|
string filePath = GetBuildinFileLoadPath(bundle);
|
||||||
return FileUtility.ReadAllText(filePath);
|
var data = FileUtility.ReadAllBytes(filePath);
|
||||||
|
|
||||||
|
if (bundle.Encrypted)
|
||||||
|
{
|
||||||
|
if (DecryptionServices == null)
|
||||||
|
{
|
||||||
|
YooLogger.Error($"DecryptionServices is Null!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
data = DecryptionServices.ReadFileData(data);
|
||||||
|
}
|
||||||
|
return Encoding.UTF8.GetString(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region 内部方法
|
#region 内部方法
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace YooAsset
|
namespace YooAsset
|
||||||
{
|
{
|
||||||
|
@ -316,9 +317,19 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
if (Exists(bundle) == false)
|
if (Exists(bundle) == false)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
string filePath = GetCacheFileLoadPath(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
return FileUtility.ReadAllBytes(filePath);
|
var data = FileUtility.ReadAllBytes(filePath);
|
||||||
|
|
||||||
|
if (bundle.Encrypted)
|
||||||
|
{
|
||||||
|
if (DecryptionServices == null)
|
||||||
|
{
|
||||||
|
YooLogger.Error($"DecryptionServices is Null!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return DecryptionServices.ReadFileData(data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
public virtual string ReadFileText(PackageBundle bundle)
|
public virtual string ReadFileText(PackageBundle bundle)
|
||||||
{
|
{
|
||||||
|
@ -326,7 +337,18 @@ namespace YooAsset
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
string filePath = GetCacheFileLoadPath(bundle);
|
string filePath = GetCacheFileLoadPath(bundle);
|
||||||
return FileUtility.ReadAllText(filePath);
|
var data = FileUtility.ReadAllBytes(filePath);
|
||||||
|
|
||||||
|
if (bundle.Encrypted)
|
||||||
|
{
|
||||||
|
if (DecryptionServices == null)
|
||||||
|
{
|
||||||
|
YooLogger.Error($"DecryptionServices is Null!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
data = DecryptionServices.ReadFileData(data);
|
||||||
|
}
|
||||||
|
return Encoding.UTF8.GetString(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#region 内部方法
|
#region 内部方法
|
||||||
|
|
|
@ -124,13 +124,14 @@ namespace YooAsset
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="verifyLevel">缓存文件的校验等级</param>
|
/// <param name="verifyLevel">缓存文件的校验等级</param>
|
||||||
/// <param name="rootDirectory">内置文件的根路径</param>
|
/// <param name="rootDirectory">内置文件的根路径</param>
|
||||||
public static FileSystemParameters CreateDefaultBuildinRawFileSystemParameters(EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null)
|
public static FileSystemParameters CreateDefaultBuildinRawFileSystemParameters(IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null)
|
||||||
{
|
{
|
||||||
string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName;
|
string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName;
|
||||||
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
|
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
|
||||||
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel);
|
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel);
|
||||||
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true);
|
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true);
|
||||||
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true);
|
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true);
|
||||||
|
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices);
|
||||||
return fileSystemParams;
|
return fileSystemParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +157,7 @@ namespace YooAsset
|
||||||
/// <param name="remoteServices">远端资源地址查询服务类</param>
|
/// <param name="remoteServices">远端资源地址查询服务类</param>
|
||||||
/// <param name="verifyLevel">缓存文件的校验等级</param>
|
/// <param name="verifyLevel">缓存文件的校验等级</param>
|
||||||
/// <param name="rootDirectory">文件系统的根目录</param>
|
/// <param name="rootDirectory">文件系统的根目录</param>
|
||||||
public static FileSystemParameters CreateDefaultCacheRawFileSystemParameters(IRemoteServices remoteServices, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null)
|
public static FileSystemParameters CreateDefaultCacheRawFileSystemParameters(IRemoteServices remoteServices, IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null)
|
||||||
{
|
{
|
||||||
string fileSystemClass = typeof(DefaultCacheFileSystem).FullName;
|
string fileSystemClass = typeof(DefaultCacheFileSystem).FullName;
|
||||||
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
|
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
|
||||||
|
@ -164,6 +165,7 @@ namespace YooAsset
|
||||||
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel);
|
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel);
|
||||||
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true);
|
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true);
|
||||||
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true);
|
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true);
|
||||||
|
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices);
|
||||||
return fileSystemParams;
|
return fileSystemParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,5 +34,12 @@ namespace YooAsset
|
||||||
/// 注意:加载流对象在资源包对象释放的时候会自动释放
|
/// 注意:加载流对象在资源包对象释放的时候会自动释放
|
||||||
/// </summary>
|
/// </summary>
|
||||||
AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream);
|
AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解密字节数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="encryptData"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public byte[] ReadFileData(byte[] encryptData);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue