Skip to content

Commit a9fab85

Browse files
author
DESKTOP-UBV38B7\codingworks
committed
准备为添加BundleResourceHelper资源解密函数
1 parent d4cd093 commit a9fab85

File tree

7 files changed

+60
-24
lines changed

7 files changed

+60
-24
lines changed

Assets/Game/Scripts/Base/GameMode.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public class GameMode : MonoBehaviour
4242
/// 资源本地路径
4343
/// </summary>
4444
public PathType LocalPathType = PathType.ReadOnly;
45-
/// <summary>
46-
/// ab资源默认包名称
47-
/// </summary>
48-
public string AssetBundleName = "AssetBundles/AssetBundles";
4945
/// <summary>
5046
/// 资源更新的路径
5147
/// </summary>
@@ -70,7 +66,6 @@ IEnumerator Start()
7066
Resource.ResUpdateType = ResUpdateType;
7167
Resource.ResUpdatePath = ResUpdatePath;
7268
Resource.LocalPathType = LocalPathType;
73-
Resource.RootAssetBundle = AssetBundleName;
7469
#endregion
7570

7671
#region WebRequest

Assets/Game/Scripts/Editor/GameModeEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ void DrawEditorModeGUI()
256256
}
257257

258258
EditorGUILayout.LabelField("Path", path);
259-
_gameMode.AssetBundleName =
260-
EditorGUILayout.TextField("AssetBundle Name", _gameMode.AssetBundleName);
261259
}
262260

263261
GUILayout.EndVertical();

Assets/Game/Scripts/Resource/EditorResourceHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace GameFramework.Taurus
2222
{
2323
public class EditorResourceHelper : IResourceHelper
2424
{
25-
public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles")
25+
public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles", bool isEncrypt=false)
2626
{
2727
}
2828

Assets/Game/Scripts/State/LoadResourceState.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
using System.Collections;
1111
using System.Collections.Generic;
12+
using System.IO;
13+
using UnityEngine;
1214

1315
namespace GameFramework.Taurus
1416
{
@@ -19,8 +21,15 @@ public class LoadResourceState : GameState
1921
public override void OnEnter(params object[] parameters)
2022
{
2123
base.OnEnter(parameters);
22-
//设置ab包的加载方式
23-
GameMode.Resource.SetResourceHelper(new BundleResourceHelper());
24+
25+
string localPath = Path.Combine(GameMode.Resource.LocalPath, "AssetVersion.txt");
26+
AssetBundleVersionInfo versionInfo = JsonUtility.FromJson<AssetBundleVersionInfo>(File.ReadAllText(localPath));
27+
28+
//设置ab包的加载方式
29+
GameMode.Resource.SetResourceHelper(new BundleResourceHelper());
30+
//加载ab包的mainfest文件
31+
GameMode.Resource.SetMainfestAssetBundle(versionInfo.ManifestAssetBundle, versionInfo.IsEncrypt);
32+
2433
}
2534

2635
public override void OnExit()

Assets/GameFramework/Resource/BundleResourceHelper .cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
using System;
1111
using System.Collections.Generic;
12+
using System.IO;
1213
using UnityEngine;
1314
using UnityEngine.SceneManagement;
1415
using Object = UnityEngine.Object;
@@ -28,7 +29,7 @@ public sealed class BundleResourceHelper : IResourceHelper
2829
/// 设置资源的路径,默认是为只读路径:Application.streamingAssetsPath;
2930
/// </summary>
3031
/// <param name="path"></param>
31-
public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles")
32+
public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles",bool isEncrypt=false)
3233
{
3334
switch (pathType)
3435
{
@@ -56,8 +57,14 @@ public void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBu
5657
int index = rootAssetBundle.LastIndexOf("/");
5758
if (index > 0 && index < (rootAssetBundle.Length - 1))
5859
directionPath += "/" + rootAssetBundle.Substring(0, index);
59-
//加载mainfest文件
60-
LoadPlatformMainfest(rootABPath, directionPath);
60+
//加载mainfest文件
61+
if (!isEncrypt)
62+
LoadPlatformMainfest(rootABPath, directionPath);
63+
else
64+
{
65+
EnciphererKey keyAsset = Resources.Load("Key") as EnciphererKey;
66+
LoadPlatformMainfest(rootABPath, directionPath, keyAsset);
67+
}
6168
}
6269

6370
/// <summary>
@@ -162,10 +169,10 @@ public void Clear()
162169
/// <summary>
163170
/// 加载mainfest
164171
/// </summary>
165-
private void LoadPlatformMainfest(string rootBundleaPath, string folderPath)
172+
private void LoadPlatformMainfest(string rootBundlePath, string folderPath)
166173
{
167174
//string assetBundlePath = _readPath + "/AssetBundles";
168-
AssetBundle mainfestAssetBundle = AssetBundle.LoadFromFile(rootBundleaPath);
175+
AssetBundle mainfestAssetBundle = AssetBundle.LoadFromFile(rootBundlePath);
169176
_mainfest = mainfestAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//
170177
string[] assetBundleNames = _mainfest.GetAllAssetBundles();
171178
foreach (var item in assetBundleNames)
@@ -182,7 +189,30 @@ private void LoadPlatformMainfest(string rootBundleaPath, string folderPath)
182189
}
183190
mainfestAssetBundle.Unload(false);
184191
}
185-
#endregion
186192

187-
}
193+
private void LoadPlatformMainfest(string rootBundlePath, string folerPath,EnciphererKey keyAsset)
194+
{
195+
//从内存中加载&解密
196+
//
197+
//DirectoryInfo dir = new DirectoryInfo(folerPath);
198+
//FileSystemInfo[] infos = dir.GetFileSystemInfos();
199+
//foreach (FileSystemInfo info in infos)
200+
//{
201+
// if (info is FileInfo)
202+
// {
203+
// if (info.Extension == "")
204+
// {
205+
// byte[] bs = File.ReadAllBytes(info.FullName);
206+
// byte[] cipbs = Encipherer.AESDecrypt(bs, keyAsset);
207+
// AssetBundle.LoadFromMemory
208+
// File.WriteAllBytes(info.FullName, cipbs);
209+
// Debug.Log("完成资源包 " + info.Name + " 的加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff"));
210+
// }
211+
// }
212+
//}
213+
}
214+
215+
#endregion
216+
217+
}
188218
}

Assets/GameFramework/Resource/IResourceHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IResourceHelper
1919
/// 设置资源的路径,默认是为只读路径:Application.streamingAssetsPath;
2020
/// </summary>
2121
/// <param name="path"></param>
22-
void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles");
22+
void SetResourcePath(PathType pathType, string rootAssetBundle = "AssetBundles/AssetBundles", bool isEncrypt = false);
2323

2424
/// <summary>
2525
/// 加载资源

Assets/GameFramework/Resource/ResourceManager.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,18 @@ public ResourceManager()
105105
public void SetResourceHelper(IResourceHelper resourceHelper)
106106
{
107107
_resourceHelper = resourceHelper;
108-
109-
//设置资源的路径,默认是为只读路径: Application.streamingAssetsPath;
110-
if (resourceHelper is BundleResourceHelper)
111-
_resourceHelper.SetResourcePath(LocalPathType, RootAssetBundle);
112108
}
113-
114109

115-
/// <summary>
110+
/// <summary>
111+
/// 在设置BundleResourceHelper 需要调用此函数加载AssetBundle的Mainfest文件
112+
/// </summary>
113+
/// <param name="mainfestName"></param>
114+
public void SetMainfestAssetBundle(string mainfestName, bool isEncrypt = false)
115+
{
116+
_resourceHelper?.SetResourcePath(LocalPathType, RootAssetBundle, isEncrypt);
117+
}
118+
119+
/// <summary>
116120
/// 加载资源
117121
/// </summary>
118122
/// <typeparam name="T"></typeparam>

0 commit comments

Comments
 (0)