加密:
在自定义的PostProcessBundles方法中,对复制到ServerData的bundle进行读取,然后加密后写入覆盖。
//加密 byte[] datas = File.ReadAllBytes(targetPath); File.WriteAllBytes(targetPath, AES.AESEncrypt(datas, KEY));
解密:
在自定义的AssetBundleProvider中的BeginOperation,所有的bundle的加载都先拿到byte数组先,不能直接用AssetBundle.LoadFromFile什么的了。
第三个网络下载的,后面会走第二个缓存bundle的逻辑。
private void BeginOperation()
{
string path = m_ProvideHandle.Location.InternalId;
var url = m_ProvideHandle.ResourceManager.TransformInternalId(m_ProvideHandle.Location);
string bundleName = Path.GetFileName(url);
// if a path starts with jar:file, it is an android embeded resource. The resource is a local file but cannot be accessed by
// FileStream(called in LoadWithDataProc) directly
// Need to use webrequest's async call to get the content.
if (AssetBundleManager.Instance.buildInData != null && AssetBundleManager.Instance.buildInData.BuildInBundleNames.Contains(bundleName))//本地资源
{
string streamPath = UnityEngine.AddressableAssets.Addressables.RuntimePath + "/" + PlatformMappingService.GetPlatformPathSubFolder() + "/" + bundleName;
Debug.Log("LoadOne:" + streamPath);
var crc = m_Options == null ? 0 : m_Options.Crc;
var req = CreateWebRequest(streamPath);
req.disposeDownloadHandlerOnDispose = false;
m_WebRequestQueueOperation = WebRequestQueue.QueueRequest(req);
if (m_WebRequestQueueOperation.IsDone)
{
m_RequestOperation = m_WebRequestQueueOperation.Result;
m_RequestOperation.completed += StreamWebRequestOperationCompleted;
}
else
{
m_WebRequestQueueOperation.OnComplete += asyncOp =>
{
m_RequestOperation = asyncOp;
m_RequestOperation.completed += StreamWebRequestOperationCompleted;
};
}
}
else if (AssetBundleManager.Instance.IsCache(bundleName))
{
string cachePath = Path.Combine(AssetBundleManager.Instance.GetBundleCachePath(), bundleName);
Debug.Log("LoadTwo:" + cachePath);
var crc = m_Options == null ? 0 : m_Options.Crc;
m_RequestOperation = AssetBundle.LoadFromMemoryAsync(AES.AESDecrypt(File.ReadAllBytes(cachePath), AES.GenBundleKey()));
m_RequestOperation.completed += LocalRequestOperationCompleted;
}
else if (ResourceManagerConfig.ShouldPathUseWebRequest(path))
{
Debug.Log("DownloadThree:" + url);
var req = CreateWebRequest(m_ProvideHandle.Location);
req.disposeDownloadHandlerOnDispose = false;
m_WebRequestQueueOperation = WebRequestQueue.QueueRequest(req);
if (m_WebRequestQueueOperation.IsDone)
{
m_RequestOperation = m_WebRequestQueueOperation.Result;
m_RequestOperation.completed += WebRequestOperationCompleted;
}
else
{
m_WebRequestQueueOperation.OnComplete += asyncOp =>
{
m_RequestOperation = asyncOp;
m_RequestOperation.completed += WebRequestOperationCompleted;
};
}
}
else
{
m_RequestOperation = null;
m_ProvideHandle.Complete<MyAssetBundleResource>(null, false, new Exception(string.Format("Invalid path in AssetBundleProvider: '{0}'.", path)));
}
}
private void StreamWebRequestOperationCompleted(AsyncOperation op)
{
UnityWebRequestAsyncOperation remoteReq = op as UnityWebRequestAsyncOperation;
var webReq = remoteReq.webRequest;
if (!UnityWebRequestUtilities.RequestHasErrors(webReq, out UnityWebRequestResult uwrResult))
{
m_downloadHandler = webReq.downloadHandler;
CompleteBundleLoad(AssetBundle.LoadFromMemory(AES.AESDecrypt(m_downloadHandler.data, KEY)));
}
webReq.Dispose();
}