Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
[feat][editor]:增加差异包提取工具
[fix][runtime]:修复偏移加密的bug
  • Loading branch information
CatImmortal committed Mar 2, 2023
1 parent ff4eacd commit cb7ef56
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ReturnCode Run()
{
if (configParam.TargetPlatform == BuildTarget.WebGL)
{
//WebGL平台不加密 因为无法偏移加密 而异或加密内存开销太高会爆WebGL平台的内存
//WebGL平台不允许加密 因为无法偏移加密 而异或加密内存开销太高会爆WebGL平台的内存
return ReturnCode.SuccessNotRun;
}

Expand Down
138 changes: 138 additions & 0 deletions Assets/CatAsset/Editor/Window/DifferenceBundleWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

using System;
using System.Collections.Generic;
using System.IO;
using CatAsset.Runtime;
using UnityEditor;
using UnityEngine;

namespace CatAsset.Editor
{
/// <summary>
/// 差异包提取窗口
/// </summary>
public class DifferenceBundleWindow : EditorWindow
{
private string oldBundlesFolder;
private string newBundlesFolder;
private string outputFolder;

[MenuItem("CatAsset/打开差异包提取窗口", priority = 2)]
private static void OpenWindow()
{
var window = GetWindow<DifferenceBundleWindow>(false, "差异包提取窗口");
window.minSize = new Vector2(800, 600);
window.Show();
}

private void OnGUI()
{

DrawSelectFolder(ref oldBundlesFolder,"旧版本的资源目录:");
DrawSelectFolder(ref newBundlesFolder,"新版本的资源目录:");
DrawSelectFolder(ref outputFolder,"差异包输出目录:");

if (GUILayout.Button("提取差异包",GUILayout.Width(200)))
{
OutputDifferenceBundles();
}

}

/// <summary>
/// 绘制选择目录的界面
/// </summary>
private void DrawSelectFolder(ref string targetFolder,string tips)
{
using (new GUILayout.HorizontalScope())
{
GUILayout.Label(tips, GUILayout.Width(150));
targetFolder = GUILayout.TextField(targetFolder,GUILayout.Width(500));
if (GUILayout.Button("选择目录", GUILayout.Width(100)))
{
string folder = EditorUtility.OpenFolderPanel($"选择{tips}", targetFolder,string.Empty);
if (!string.IsNullOrEmpty(folder))
{
targetFolder = folder;
}
}
}
}

/// <summary>
/// 提取差异包
/// </summary>
private void OutputDifferenceBundles()
{
string oldManifestPath = Path.Combine(oldBundlesFolder, CatAssetManifest.ManifestBinaryFileName);
string newManifestPath = Path.Combine(newBundlesFolder, CatAssetManifest.ManifestBinaryFileName);

if (!File.Exists(oldManifestPath))
{
Debug.LogError($"{oldManifestPath}不存在");
return;
}

if (!File.Exists(newManifestPath))
{
Debug.LogError($"{newManifestPath}不存在");
return;
}

byte[] bytes = File.ReadAllBytes(oldManifestPath);
CatAssetManifest oldManifest = CatAssetManifest.DeserializeFromBinary(bytes);

bytes = File.ReadAllBytes(newManifestPath);
CatAssetManifest newManifest = CatAssetManifest.DeserializeFromBinary(bytes);

//旧资源包集合
HashSet<BundleManifestInfo> oldBundleSet = new HashSet<BundleManifestInfo>(oldManifest.Bundles);

//差异资源包
List<BundleManifestInfo> diffBundles = new List<BundleManifestInfo>();

foreach (BundleManifestInfo bundleManifestInfo in newManifest.Bundles)
{
if (!oldBundleSet.Contains(bundleManifestInfo))
{
diffBundles.Add(bundleManifestInfo);
Debug.Log("差异包:" + bundleManifestInfo);
}
}

//将差异包输出到指定目录下
FileInfo fi;
string targetPath;
EditorUtil.CreateEmptyDirectory(outputFolder);
foreach (BundleManifestInfo diffBundle in diffBundles)
{
fi = new FileInfo(Path.Combine(newBundlesFolder, diffBundle.RelativePath));
targetPath = Path.Combine(outputFolder, diffBundle.RelativePath);

//冗余资源包没有diffBundle.Directory
if (!string.IsNullOrEmpty(diffBundle.Directory))
{
string targetDirectory = Path.Combine(outputFolder, diffBundle.Directory);
if (!Directory.Exists(targetDirectory))
{
//输出目录不存在则创建
Directory.CreateDirectory(targetDirectory);
}
}

fi.CopyTo(targetPath);
}

//复制资源清单
fi = new FileInfo(Path.Combine(newBundlesFolder, CatAssetManifest.ManifestBinaryFileName));
targetPath = Path.Combine(outputFolder, CatAssetManifest.ManifestBinaryFileName);
fi.CopyTo(targetPath);

fi = new FileInfo(Path.Combine(newBundlesFolder, CatAssetManifest.ManifestJsonFileName));
targetPath = Path.Combine(outputFolder, CatAssetManifest.ManifestJsonFileName);
fi.CopyTo(targetPath);

Debug.Log($"已将差异包复制到{outputFolder}下");
}
}
}
3 changes: 3 additions & 0 deletions Assets/CatAsset/Editor/Window/DifferenceBundleWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/CatAsset/Editor/Window/DifferenceBundleWindow.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions Assets/CatAsset/Runtime/Misc/EncryptUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static class EncryptUtil
/// 加密字节长度
/// </summary>
public const int EncryptBytesLength = 64;

/// <summary>
/// 偏移加密的头部字节值
/// </summary>
private const byte encryptOffsetHead = 64;

/// <summary>
/// 异或加密Key
Expand Down Expand Up @@ -58,18 +63,18 @@ public static void EncryptOffset(string filePath)
//写入额外的头部数据
for (int i = 0; i < EncryptBytesLength; i++)
{
cachedBytes[i] = (byte)UnityEngine.Random.Range(byte.MinValue, byte.MaxValue);
cachedBytes[i] = encryptOffsetHead;
}

//写入原始数据
Array.Copy(bytes,0,cachedBytes,EncryptBytesLength,bytes.Length);

using (FileStream fs = File.OpenWrite(filePath))
{
fs.Write(cachedBytes, 0, cachedBytes.Length);
fs.Position = 0;
fs.Write(cachedBytes, 0, newLength);
}

Array.Clear(cachedBytes,0,cachedBytes.Length);
Array.Clear(cachedBytes,0,newLength);
ReleaseCachedBytes(cachedBytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,12 @@ protected virtual void LoadAsync()
break;

case BundleEncryptOptions.XOr:
//异或加密的资源包
if (BundleRuntimeInfo.BundleState == BundleRuntimeInfo.State.InReadWrite ||
Application.platform != RuntimePlatform.Android)
{
//存在于读写区 或 非安卓平台 可以进行IO操作 使用Stream进行解密
//这里不考虑WebGL平台,因为WebGL平台不允许加密
BundleRuntimeInfo.Stream = new DecryptXOrStream(BundleRuntimeInfo.LoadPath, FileMode.Open, FileAccess.Read);
request = AssetBundle.LoadFromStreamAsync(BundleRuntimeInfo.Stream,0,1024*1024);
}
Expand All @@ -371,8 +373,6 @@ protected virtual void LoadAsync()
CatAssetManager.AddWebRequestTask(BundleRuntimeInfo.LoadPath, BundleRuntimeInfo.LoadPath,
onBundleBinaryLoadedCallback, TaskPriority.Middle);
}


break;

default:
Expand Down

0 comments on commit cb7ef56

Please sign in to comment.