public IEnumerator LoadAllDependenciesAsync(string fname, int ttl) { if (manifest == null) { LogUtil.Error("manifest == null"); yield break; } string dpname = fname.Replace("\\", "/"); // Get dependecies from the AssetBundleManifest object.. string[] dependencies = manifest.GetAllDependencies(dpname); var length = dependencies.Length; if (length == 0) { yield break; } // Record and load all dependencies. var count = (float)length; for (int i = 0; i < length; i++) { var depend = RemapVariantName(dependencies[i]); LogUtil.Trace(fname + ":dependancy:" + depend); var iter = BundleManager.LoadAsync(fname, depend, ttl); while (iter.MoveNext()) { yield return(null); } } }
// NOTE: this can not load custom encrypted asset bundles public static WWW CreateBundleFromWWWAsync(string uri, int version, uint crc = 0) { uri = fixWWWUri(uri); LogUtil.Trace("BundleEncoder: LoadFromCacheOrDownload " + uri); return(WWW.LoadFromCacheOrDownload(uri, version, crc)); }
public static void GetTTLSettings(string path, ref int ttl) { if (ttlSettings.TryGetValue(path, out ttl)) { LogUtil.Trace("GetTTLSettings:" + path + "=" + ttl.ToString()); return; } if (noTTLSettings.ContainsKey(path)) { return; } foreach (var pair in ttlPatternSettings) { var key = pair.Key; if (path.Contains(key)) { ttl = pair.Value; SetTTLSettings(path, ttl); LogUtil.Trace("GetTTLSettingsPattern:" + path + "=" + ttl.ToString()); return; } } LogUtil.Trace("[GetTTLSettings]" + path + " has no ttl settings"); noTTLSettings[path] = true; }
/* ========================== Load From WWW ========================== */ // NOTE: this can not load custom encrypted asset bundles public static AssetBundle CreateBundleFromWWW(string uri, int version, uint crc = 0) { uri = fixWWWUri(uri); LogUtil.Trace("BundleEncoder: LoadFromCacheOrDownload (sync) " + uri); var www = WWW.LoadFromCacheOrDownload(uri, version, crc); // busy wait while (!www.isDone) { // LogUtil.Debug("BundleEncoder: LoadFromCacheOrDownload (sync) busy waiting..."); if (www.error != null && www.error != "") { LogUtil.Error("BundleEncoder: CreateBundleFromWWW not done and failed: " + www.error); return(null); } } ; // LogUtil.Debug("BundleEncoder: LoadFromCacheOrDownload (sync) done."); if (www.error == null || www.error == "") { return(www.assetBundle); } else { LogUtil.Error("BundleEncoder: CreateBundleFromWWW failed: " + www.error); return(null); } }
// Load an AssetBundle, without loading its dependencies public static AssetBundleRef Load(string uri, int ttl) { string keyName = GetBundleKey(uri); AssetBundleRef abRef; GetTTLSettings(uri, ref ttl); if (TryGetRefreshedBundleRef(keyName, ttl, out abRef)) { loadingBundles.Remove(keyName); return(abRef); } else { LogUtil.Trace("BundleManager: Load " + uri); AssetBundle bundle = null; if (loadWithWWW) { bundle = BundleEncoder.CreateBundleFromWWW(uri, wwwVersion, wwwCrc); } else { bundle = BundleEncoder.CreateBundleFromFile(uri); } if (bundle != null) { abRef = new AssetBundleRef(uri, ttl); abRef.AssetBundle = bundle; dictAssetBundleRefs.Add(keyName, abRef); listAssetBundleUris.Add(uri); loadingBundles.Remove(keyName); AddLoadHistory(uri, true); return(abRef); } else { if (TryGetRefreshedBundleRef(keyName, ttl, out abRef)) { loadingBundles.Remove(keyName); return(abRef); } LogUtil.Error("BundleManager: creating bundle from uri failed! " + uri); } } return(null); }
public static void UnloadDeadBundles(bool unloadAllLoadedObjects, int iterCount) { tempUriList.Clear(); int i = 0; while (i < iterCount && curUnloadIndex < listAssetBundleUris.Count) { var keyName = GetBundleKey(listAssetBundleUris[curUnloadIndex]); AssetBundleRef abRef; if (dictAssetBundleRefs.TryGetValue(keyName, out abRef)) { if (abRef.Dead) { tempUriList.Add(abRef.uri); } } else { LogUtil.Error("BundleManager: iterate invalid keyName " + keyName); } ++i; ++curUnloadIndex; if (curUnloadIndex >= listAssetBundleUris.Count) { curUnloadIndex = 0; } } if (curUnloadIndex >= listAssetBundleUris.Count) { curUnloadIndex = 0; } for (i = 0; i < tempUriList.Count; ++i) { var uri = tempUriList[i]; LogUtil.Trace("BundleManager: Unload dead bundle " + uri); Unload(uri, unloadAllLoadedObjects); } tempUriList.Clear(); }
public void UnloadAsset(string assetPath) { if (assetRequests == null) { return; } AssetBundleRequest req = null; if (assetRequests.TryGetValue(assetPath, out req)) { if (req.isDone) { LogUtil.Trace("AssetBundleRef UnloadAsset:" + assetPath); assetRequests.Remove(assetPath); Resources.UnloadAsset(req.asset); } } }
public static Encoding GetEncodingOfData(byte[] data) { // headers: // [0x55 0x6e] unity standard bundle headers // [0x00 0x00] for non-encrypt // [0x00 0x01] for rc4 encryption // [0x00 0x02] for aes_256_cbc encryption // [0x00 0x03] for chacha20 encryption if (data.Length < 2) { LogUtil.Error("GetEncodingOfData data length is " + data.Length); return(Encoding.Unknown); } LogUtil.Trace("GetEncodingOfData 1: " + data[0] + " 2: " + data[1]); if (data[0] == 0x55 && data[1] == 0x6e) { return(Encoding.Unity); } if (data[0] == 0x00 && data[1] == 0x00) { return(Encoding.Plain); } else if (data[0] == 0x00 && data[1] == 0x01) { return(Encoding.Rc4); } else if (data[0] == 0x00 && data[1] == 0x02) { return(Encoding.AES_256_CBC); } else if (data[0] == 0x00 && data[1] == 0x03) { return(Encoding.Chacha20); } else { return(Encoding.Unknown); } }
public void LoadAllDependencies(string fname, int ttl) { if (manifest == null) { LogUtil.Error("manifest==null"); return; } #if PROFILE_FILE Profiler.BeginSample("BundleDependencies.LoadAllDependencies"); #endif string dpname = fname.Replace("\\", "/"); // int idx = dpname.LastIndexOf("/") + 1; // dpname = dpname.Substring(idx, dpname.Length - idx); // Get dependecies from the AssetBundleManifest object.. string[] dependencies = manifest.GetAllDependencies(dpname); var length = dependencies.Length; if (length == 0) { return; } // Record and load all dependencies. for (int i = 0; i < length; i++) { var depend = RemapVariantName(dependencies[i]); LogUtil.Trace(fname + ":dependancy:" + depend); var bundle = BundleManager.Load(depend, ttl); if (bundle != null) { bundle.AddParentBundle(fname); } } #if PROFILE_FILE Profiler.EndSample(); #endif }
// Unload an AssetBundle public static void Unload(string uri, bool unloadAllLoadedObjects) { string keyName = GetBundleKey(uri); AssetBundleRef abRef; if (dictAssetBundleRefs.TryGetValue(keyName, out abRef)) { LogUtil.Trace("BundleManager: Unload " + uri); if (!abRef.CanUnload()) { LogUtil.Trace("loading asset async, cannot be unloaded " + uri); return; } abRef.assetBundle.Unload(unloadAllLoadedObjects); abRef.assetBundle = null; abRef.CleanUp(); dictAssetBundleRefs.Remove(keyName); listAssetBundleUris.Remove(uri); AddLoadHistory(uri, false); } }
/* ========================== Load From File ========================== */ public static AssetBundle CreateBundleFromFile(string path) { #if PROFILE_FILE Profiler.BeginSample("BundleEncoder.CreateBundleFromFile"); #endif AssetBundle bundle = null; #if LOAD_FROM_FILE try { Encoding enc = GetEncodingOfFile(path); LogUtil.Trace("CreateBundleFromFile: " + path + " enc=" + enc); if (enc == Encoding.Unity) { string fullpath = FileUtils.PrepareBundleFile(path); bundle = AssetBundle.LoadFromFile(fullpath); #if PROFILE_FILE Profiler.EndSample(); #endif return(bundle); } LogUtil.Trace("CreateBundleFromFile: fallback to LoadFromMemory for this encoding"); } catch (Exception e) { // just fallback to LoadFromMemory LogUtil.Error("CreateBundleFromFile: fallback to LoadFromMemory because of Error " + e); } #endif // LOAD_FROM_FILE byte[] data = DecodeBundleFile(path); bundle = AssetBundle.LoadFromMemory(data); #if PROFILE_FILE Profiler.EndSample(); #endif return(bundle); }
public static void UnloadDyingBundles(double period, bool unloadAllLoadedObjects) { tempUriList.Clear(); var now = Time.realtimeSinceStartup; foreach (KeyValuePair <string, AssetBundleRef> pair in dictAssetBundleRefs) { var abRef = pair.Value; if (abRef.EndTime - now < period) { tempUriList.Add(abRef.uri); } } for (var i = 0; i < tempUriList.Count; ++i) { var uri = tempUriList[i]; LogUtil.Trace("BundleManager: Unload dying bundle " + uri + " period=" + period); Unload(uri, unloadAllLoadedObjects); } tempUriList.Clear(); }
internal static IEnumerator LoadAsync(string parentBundlePath, string uri, int ttl) { string keyName = GetBundleKey(uri); GetTTLSettings(uri, ref ttl); AssetBundleRef abRef; while (loadingBundles.Contains(keyName)) { LogUtil.Trace("BundleManager: is already Loading " + keyName); yield return(null); } if (TryGetRefreshedBundleRef(keyName, ttl, out abRef)) { loadingBundles.Remove(keyName); abRef.AddParentBundle(parentBundlePath); yield break; } else { loadingBundles.Add(keyName); LogUtil.Trace("BundleManager: LoadAsyncWithNoCallback " + uri); AssetBundle bundle = null; if (loadWithWWW) { var www = BundleEncoder.CreateBundleFromWWWAsync(uri, wwwVersion, wwwCrc); while (!www.isDone) { yield return(null); } if (www.error == null || www.error == "") { bundle = www.assetBundle; } else { LogUtil.Error("BundleManager: creating bundle from WWW failed: " + www.error); } } else { var req = BundleEncoder.CreateBundleFromFileAsync(uri); while (!req.isDone && !TryGetRefreshedBundleRef(keyName, ttl, out abRef)) { yield return(null); } if (req.isDone) { bundle = req.assetBundle; } } if (bundle != null) { abRef = new AssetBundleRef(uri, ttl); abRef.AssetBundle = bundle; dictAssetBundleRefs.Add(keyName, abRef); listAssetBundleUris.Add(uri); AddLoadHistory(uri, true); loadingBundles.Remove(keyName); abRef.AddParentBundle(parentBundlePath); } else { if (TryGetRefreshedBundleRef(keyName, ttl, out abRef)) { loadingBundles.Remove(keyName); abRef.AddParentBundle(parentBundlePath); } else { LogUtil.Error("BundleManager: creating bundle from uri failed! " + uri); } } loadingBundles.Remove(keyName); } }