Skip to content

Instantly share code, notes, and snippets.

@kurokuru
Created October 30, 2019 08:57
Show Gist options
  • Save kurokuru/4152b55dea777ea7cecd1c74195ef175 to your computer and use it in GitHub Desktop.
Save kurokuru/4152b55dea777ea7cecd1c74195ef175 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.Util;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
// Token: 0x02000019 RID: 25
internal class CustomAssetBundleResource : IAssetBundleResource
{
// Token: 0x060000BE RID: 190 RVA: 0x000049B4 File Offset: 0x00002BB4
private UnityWebRequest CreateWebRequest(IResourceLocation loc)
{
bool flag = this.m_Options == null;
UnityWebRequest result;
if (flag)
{
result = UnityWebRequestAssetBundle.GetAssetBundle(loc.InternalId);
}
else
{
UnityWebRequest webRequest = (!string.IsNullOrEmpty(this.m_Options.Hash)) ? UnityWebRequestAssetBundle.GetAssetBundle(loc.InternalId, Hash128.Parse(this.m_Options.Hash), this.m_Options.Crc) : UnityWebRequestAssetBundle.GetAssetBundle(loc.InternalId, this.m_Options.Crc);
bool flag2 = this.m_Options.Timeout > 0;
if (flag2)
{
webRequest.timeout = this.m_Options.Timeout;
}
bool flag3 = this.m_Options.RedirectLimit > 0;
if (flag3)
{
webRequest.redirectLimit = this.m_Options.RedirectLimit;
}
webRequest.chunkedTransfer = this.m_Options.ChunkedTransfer;
bool flag4 = this.m_ProvideHandle.ResourceManager.CertificateHandlerInstance != null;
if (flag4)
{
webRequest.certificateHandler = this.m_ProvideHandle.ResourceManager.CertificateHandlerInstance;
webRequest.disposeCertificateHandlerOnDispose = false;
}
result = webRequest;
}
return result;
}
// Token: 0x060000BF RID: 191 RVA: 0x00004ACC File Offset: 0x00002CCC
private float PercentComplete()
{
return (this.m_RequestOperation != null) ? this.m_RequestOperation.progress : 0f;
}
// Token: 0x060000C0 RID: 192 RVA: 0x00004AF8 File Offset: 0x00002CF8
public AssetBundle GetAssetBundle()
{
bool flag = this.m_AssetBundle == null && this.m_downloadHandler != null;
if (flag)
{
this.m_AssetBundle = this.m_downloadHandler.assetBundle;
this.m_downloadHandler.Dispose();
this.m_downloadHandler = null;
}
return this.m_AssetBundle;
}
// Token: 0x060000C1 RID: 193 RVA: 0x00004B54 File Offset: 0x00002D54
internal void Start(ProvideHandle provideHandle)
{
this.m_Retries = 0;
this.m_AssetBundle = null;
this.m_downloadHandler = null;
this.m_ProvideHandle = provideHandle;
this.m_Options = (this.m_ProvideHandle.Location.Data as AssetBundleRequestOptions);
this.m_RequestOperation = null;
this.BeginOperation();
provideHandle.SetProgressCallback(new Func<float>(this.PercentComplete));
}
// Token: 0x060000C2 RID: 194 RVA: 0x00004BBC File Offset: 0x00002DBC
private void BeginOperation()
{
string path = this.m_ProvideHandle.Location.InternalId;
bool flag = File.Exists(path);
if (flag)
{
this.m_RequestOperation = AssetBundle.LoadFromFileAsync(path, (this.m_Options == null) ? 0u : this.m_Options.Crc);
this.m_RequestOperation.completed += this.LocalRequestOperationCompleted;
}
else
{
bool flag2 = CustomResourceManagerConfig.ShouldPathUseWebRequest(path);
if (flag2)
{
UnityWebRequest req = this.CreateWebRequest(this.m_ProvideHandle.Location);
req.disposeDownloadHandlerOnDispose = false;
this.m_RequestOperation = req.SendWebRequest();
this.m_RequestOperation.completed += this.WebRequestOperationCompleted;
}
else
{
this.m_RequestOperation = null;
this.m_ProvideHandle.Complete<CustomAssetBundleResource>(null, false, new Exception(string.Format("Invalid path in AssetBundleProvider: '{0}'.", path)));
}
}
}
// Token: 0x060000C3 RID: 195 RVA: 0x00004C96 File Offset: 0x00002E96
private void LocalRequestOperationCompleted(AsyncOperation op)
{
this.m_AssetBundle = (op as AssetBundleCreateRequest).assetBundle;
this.m_ProvideHandle.Complete<CustomAssetBundleResource>(this, this.m_AssetBundle != null, null);
}
// Token: 0x060000C4 RID: 196 RVA: 0x00004CC4 File Offset: 0x00002EC4
private void WebRequestOperationCompleted(AsyncOperation op)
{
UnityWebRequestAsyncOperation remoteReq = op as UnityWebRequestAsyncOperation;
UnityWebRequest webReq = remoteReq.webRequest;
bool flag = string.IsNullOrEmpty(webReq.error);
if (flag)
{
this.m_downloadHandler = (webReq.downloadHandler as DownloadHandlerAssetBundle);
this.m_ProvideHandle.Complete<CustomAssetBundleResource>(this, true, null);
}
else
{
this.m_downloadHandler = (webReq.downloadHandler as DownloadHandlerAssetBundle);
this.m_downloadHandler.Dispose();
this.m_downloadHandler = null;
int retries = this.m_Retries;
this.m_Retries = retries + 1;
bool flag2 = retries < this.m_Options.RetryCount;
if (flag2)
{
Debug.LogFormat("Web request {0} failed with error '{1}', retrying ({2}/{3})...", new object[]
{
webReq.url,
webReq.error,
this.m_Retries,
this.m_Options.RetryCount
});
this.BeginOperation();
}
else
{
Exception exception = new Exception(string.Format("RemoteAssetBundleProvider unable to load from url {0}, result='{1}'.", webReq.url, webReq.error));
this.m_ProvideHandle.Complete<CustomAssetBundleResource>(null, false, exception);
}
}
webReq.Dispose();
}
// Token: 0x060000C5 RID: 197 RVA: 0x00004DE8 File Offset: 0x00002FE8
public void Unload()
{
bool flag = this.m_AssetBundle != null;
if (flag)
{
this.m_AssetBundle.Unload(true);
this.m_AssetBundle = null;
}
bool flag2 = this.m_downloadHandler != null;
if (flag2)
{
this.m_downloadHandler.Dispose();
this.m_downloadHandler = null;
}
this.m_RequestOperation = null;
}
// Token: 0x04000046 RID: 70
private AssetBundle m_AssetBundle;
// Token: 0x04000047 RID: 71
private DownloadHandlerAssetBundle m_downloadHandler;
// Token: 0x04000048 RID: 72
private AsyncOperation m_RequestOperation;
// Token: 0x04000049 RID: 73
private ProvideHandle m_ProvideHandle;
// Token: 0x0400004A RID: 74
private AssetBundleRequestOptions m_Options;
// Token: 0x0400004B RID: 75
private int m_Retries;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment