/************************************ * 函数说明: 通过版本文件加载资源包 * 返 回 值: void * 注意事项: ************************************/ IEnumerator LoadFile(VersionBundle bundle, Action onComplate, Action onError) { string path = GameConfig.HOST_RES() + "ab/" + bundle.id + "?v=" + bundle.versionValue; WWW www = new WWW(path); yield return(www); if (null == www.error) { byte[] bytes = www.bytes; Debug.Log("write path: " + PathUtils.MakeFilePath(bundle.id, PathUtils.PathType.MobileDiskWrite)); FileInfo fileInfo = new FileInfo(PathUtils.MakeFilePath(bundle.id, PathUtils.PathType.MobileDiskWrite)); Stream stream = fileInfo.Create(); stream.Write(bytes, 0, bytes.Length); stream.Close(); stream.Dispose(); if (null != onComplate) { onComplate(); } } else { Debug.LogWarning(" LoadBundle Error : " + bundle.id + " : " + path + " ----- " + www.error); if (null != onError) { onError(); } } }
public void SubReference(string szPkgName, ref List <string> removeABs) { if (!szPkgName.EndsWith(gBundleFileName)) { szPkgName += gBundleFileName; } ResourceMgrData resoureData; if (gDicPkgResource.TryGetValue(szPkgName, out resoureData)) { if (--resoureData.referenceCount <= 0) { removeABs.Add(szPkgName); } VersionBundle bundle = VersionHelper.instance.GetBundle(szPkgName); if (bundle != null && bundle.dependency != null) { for (int i = 0; i < bundle.dependency.Length; i++) { SubReference(bundle.dependency[i], ref removeABs); } } } }
private static void ReadVersions() { string versionTxtPath = FileManager.VERSION_PATH + FileManager.DELIMITER + "version.txt"; if (Directory.Exists(FileManager.VERSION_PATH) && File.Exists(versionTxtPath)) { VersionBundle json = GetVersions(); nextBuild = lastBuild = json.lastBuild; latestBuilds = json.latestBuilds; } else { lastBuild = nextBuild = new Version() { code = "0.0.0", type = Version.Type.Internal, patchNotes = "" }; latestBuilds = new Dictionary <Version.Type, Version>() { { Version.Type.Internal, new Version() { code = "0.0.0", type = Version.Type.Internal } } }; Debug.Log(latestBuilds); BuildVersions(); } }
private static void BuildVersions() { if (!Directory.Exists(FileManager.VERSION_PATH)) { Directory.CreateDirectory(FileManager.VERSION_PATH); } VersionBundle jsonObj = GetVersions(); jsonObj.lastBuild = nextBuild; if (jsonObj.latestBuilds == null) { jsonObj.latestBuilds = new Dictionary <Version.Type, Version>(); } jsonObj.AddToLatestBuilds(jsonObj.lastBuild); if (jsonObj.previousBuilds == null) { jsonObj.previousBuilds = new List <Version>(); } jsonObj.previousBuilds.Add(nextBuild); string json = JsonUtility.ToJson(jsonObj); string versionTxtPath = FileManager.VERSION_PATH + FileManager.DELIMITER + "version.txt"; File.WriteAllText(versionTxtPath, json); }
string[] GetDependencies(string assetBundleName) { VersionBundle bundle = VersionHelper.instance.GetBundle(assetBundleName); if (bundle == null || bundle.dependency == null || bundle.dependency.Length < 1) { return(null); } return(bundle.dependency); }
/// <summary> /// 添加bundle到指定字典中 /// </summary> /// <param name="dic"></param> /// <param name="bundle"></param> void AddBundleToDic(Dictionary <string, VersionBundle> dic, VersionBundle bundle) { if (!dic.ContainsKey(bundle.id)) { dic.Add(bundle.id, bundle); } else { dic[bundle.id] = bundle; } }
public static bool SetCRC(VersionBundle bundle, string dirPath) { string path = dirPath + bundle.id + MANIFEST_FILE_SUF; uint crc = ReadCRC(path); if (crc > 0) { bundle.crc = crc; return(true); } return(false); }
public static List <VersionBundle> ReadManifestFile(string path) { List <VersionBundle> bundles = new List <VersionBundle>(); if (File.Exists(path)) { StreamReader sr = new StreamReader(path); string content = sr.ReadToEnd(); sr.Close(); string[] lines = content.Split(new string[] { "\n" }, StringSplitOptions.None); VersionBundle bundle = null; List <string> dependencies = new List <string>(); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("Info_")) { if (null != bundle) { bundle.dependency = dependencies.ToArray(); bundles.Add(bundle); bundle = new VersionBundle(); } else { bundle = new VersionBundle(); } } else if (lines[i].Contains("Name")) { string[] name = lines[i].Split(new string[] { ":" }, StringSplitOptions.None); bundle.id = name[1].Trim(); } else if (lines[i].Contains("Dependencies")) { dependencies.Clear(); } else if (lines[i].Contains("Dependency")) { string[] dependency = lines[i].Split(new string[] { ":" }, StringSplitOptions.None); dependencies.Add(dependency[1].Trim()); } } if (null != bundle) { bundle.dependency = dependencies.ToArray(); bundles.Add(bundle); } } return(bundles); }
private static VersionBundle GetVersions() { string versionTxtPath = FileManager.VERSION_PATH + FileManager.DELIMITER + "version.txt"; if (Directory.Exists(FileManager.VERSION_PATH) && File.Exists(versionTxtPath)) { string json = File.ReadAllText(versionTxtPath); VersionBundle jsonObj = JsonUtility.FromJson <VersionBundle>(json); return(jsonObj); } return(default);
// Where we actuall call WWW to download the assetBundle. protected bool LoadAssetBundleInternal(string assetBundleName, int level = 0) { // Already loaded. LoadedAssetBundle bundle = null; m_LoadedAssetBundles.TryGetValue(assetBundleName, out bundle); if (bundle != null) { bundle.m_ReferencedCount++; return(true); } // @TODO: Do we need to consider the referenced count of WWWs? // In the demo, we never have duplicate WWWs as we wait LoadAssetAsync()/LoadLevelAsync() to be finished before calling another LoadAssetAsync()/LoadLevelAsync(). // But in the real case, users can call LoadAssetAsync()/LoadLevelAsync() several times then wait them to be finished which might have duplicate WWWs. if (m_DownloadingBundles.Contains(assetBundleName)) { return(true); } VersionBundle vb = null; VersionHelper.instance.GetBundleHttp(assetBundleName, out vb); if (vb != null) { string id = vb.versionValue + "_" + vb.id; if (ResHelper.Instance().downloadedFiles.ContainsKey(id)) { string url = PathUtils.MakeFilePath(id, PathUtils.PathType.MobileDiskWrite); AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(url); m_InProgressOperations.Add(new AssetBundleDownloadFromFile(assetBundleName, abcr, url)); } else { string url = GameConfig.HOST_RES() + vb.id + "?v=" + vb.versionValue; m_InProgressOperations.Add(new AssetBundleDownloadFromWebOperation(assetBundleName, url, vb.versionValue, vb.crc, level)); } } else { PathUtils.PathType eType = PathUtils.PathType.None; string url = PathUtils.GetReadablePath(assetBundleName, ref eType, false); AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(url); m_InProgressOperations.Add(new AssetBundleDownloadFromFile(assetBundleName, abcr, url)); } m_DownloadingBundles.Add(assetBundleName); return(false); }
public VersionBundle GetBundle(string id) { VersionBundle vb = null; if (m_bundles.TryGetValue(id, out vb)) { return(vb); } if (m_httpBundles.TryGetValue(id, out vb)) { return(vb); } return(vb); }
public VersionBundle Clone() { VersionBundle vb = new VersionBundle(); vb.id = this.id; vb.version = this.version; vb.crc = this.crc; if (null != this.dependency) { vb.dependency = new string[this.dependency.Length]; for (int i = 0; i < this.dependency.Length; i++) { vb.dependency[i] = this.dependency[i]; } } return(vb); }
/// <summary> /// 缓存bundle包并确定需要更新的资源包 /// !!!: crc > 0 表示为http资源,使用WWW.LoadFromCacheOrDownload加载 /// !!!: crc <= 0 并且本地版本号低于服务器版本号的需要更新,暂时为子bundle更新,后续可能更改为zip包更新解压方式 /// </summary> void M_CacheBundles() { VersionBundle vb = null; m_bundles.Clear(); m_needUpdateBundles.Clear(); if (null != m_localConfig && null != m_localConfig.bundles) { for (int i = 0; i < m_localConfig.bundles.Length; i++) { vb = m_localConfig.bundles[i]; if (m_bundles.ContainsKey(vb.id)) { Debug.LogError(m_localConfig.bundles[i].id); } m_bundles.Add(vb.id, vb); } } if (null != m_loadedConfig && null != m_loadedConfig.bundles) { for (int i = 0; i < m_loadedConfig.bundles.Length; i++) { vb = m_loadedConfig.bundles[i]; if (m_bundles.ContainsKey(vb.id)) { if (m_bundles[vb.id].versionValue < vb.versionValue) { m_bundles[vb.id] = vb; AddBundleToDic(m_needUpdateBundles, vb); } } else { m_bundles.Add(vb.id, vb); AddBundleToDic(m_needUpdateBundles, vb); } } } }
public void AddReference(string szPkgName) { if (!szPkgName.EndsWith(gBundleFileName)) { szPkgName += gBundleFileName; } ResourceMgrData resoureData; if (gDicPkgResource.TryGetValue(szPkgName, out resoureData)) { resoureData.referenceCount++; VersionBundle bundle = VersionHelper.instance.GetBundle(szPkgName); if (bundle != null && bundle.dependency != null) { for (int i = 0; i < bundle.dependency.Length; i++) { AddReference(bundle.dependency[i]); } } } }
void M_CacheHttpBundles() { m_httpBundles.Clear(); string path = "file://" + PathUtils.MakeFilePath(GameConfig.LOCAL_HTTP_CONFIG_FILE, PathUtils.PathType.MobileDiskStreamAssert); if (Application.platform == RuntimePlatform.Android) { path = "jar:" + path; } try { Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read); if (stream != null && stream.Length > 0) { byte[] data = new byte[stream.Length]; stream.Read(data, 0, data.Length); stream.Close(); string content = System.Text.Encoding.Default.GetString(data); m_localHttpConfig = JsonFx.Json.JsonReader.Deserialize <VersionBundleConfig>(content); } else { m_localHttpConfig = null; } } catch (Exception e) { m_loadedConfig = null; } VersionBundle vb = null; Dictionary <string, VersionBundle> temp = new Dictionary <string, VersionBundle>(); if (null != m_localHttpConfig && null != m_localHttpConfig) { for (int i = 0; i < m_localHttpConfig.bundles.Length; i++) { vb = m_localHttpConfig.bundles[i]; temp.Add(vb.id, vb); } } if (null != m_httpConfig && null != m_httpConfig.bundles) { for (int i = 0; i < m_httpConfig.bundles.Length; i++) { vb = m_httpConfig.bundles[i]; if (temp.ContainsKey(vb.id)) { if (temp[vb.id].versionValue < vb.versionValue) { m_httpBundles.Add(vb.id, vb); } } else { m_httpBundles.Add(vb.id, vb); } } } }
public void GetLocalBundle(string id, out VersionBundle vb) { m_bundles.TryGetValue(id, out vb); }
public void GetBundleHttp(string id, out VersionBundle vb) { m_httpBundles.TryGetValue(id, out vb); }
/************************************ * 函数说明: 通过版本文件加载资源包 * 返 回 值: void * 注意事项: ************************************/ public void LoadBundle(VersionBundle bundle, Action onComplate, Action onError) { GameWorld.instance.StartCoroutine(LoadFile(bundle, onComplate, onError)); }
/************************************ * 函数说明: 内部加载资源 * 返 回 值: System.Collections.IEnumerator * 参数说明: resRequest * 注意事项: ************************************/ private IEnumerator LoadResources(string szPkgName, System.Action <string, AssetBundle> callback) { do { /** @brief: 检查参数 */ if (string.IsNullOrEmpty(szPkgName)) { //JZLog.LogError("AsyncLoadRes : Error szPkgName == null"); yield break; } ResourceMgrData resourceData = null; /** @brief: 检查是否加载过此包 或者正在加载此包 */ if (gDicPkgResource.ContainsKey(szPkgName) == false) { resourceData = new ResourceMgrData() { eState = ResourceMgrData.State.None, assetBundle = null }; gDicPkgResource[szPkgName] = resourceData; } else { resourceData = gDicPkgResource[szPkgName]; if (resourceData.eState == ResourceMgrData.State.LoadFinish) { if (null != callback) { callback(szPkgName, resourceData.assetBundle); callback = null; } yield break; } } /** @brief: 避免一帧时间过长 */ /** @brief: Non matching Profiler.EndSample (BeginSample and EndSample count must match) */ /** @brief: One is catching an exception from another object, the other is long frame times. If it's either of those two, it can only be fixed by the Unity team. */ // yield return new WaitForEndOfFrame(); if (gDicPkgResource.ContainsKey(szPkgName) == false) { //JZLog.LogError("LoadResources : Warning Pkg == " + szPkgName + " Have no this Pkg , MayBe Remove it!"); yield break; } if (resourceData.eState == ResourceMgrData.State.None) { resourceData.eState = ResourceMgrData.State.Loading; } else { break; } VersionBundle bundle = VersionHelper.instance.GetBundle(szPkgName); if (null != bundle && null != bundle.dependency && bundle.dependency.Length > 0) { for (int i = 0; i < bundle.dependency.Length; i++) { string depName = bundle.dependency[i]; if (gDicPkgResource.ContainsKey(depName) == false || gDicPkgResource[depName].assetBundle == null) { yield return(GameWorld.instance.StartCoroutine(LoadResources(depName, null))); } } } AssetBundle asset = null; if (VersionHelper.instance.HasBundleHttp(szPkgName)) { VersionBundle httpAB = VersionHelper.instance.GetBundle(szPkgName); string szUrl = GameConfig.HOST_RES() + httpAB.id + "?v=" + httpAB.versionValue; WWW www = WWW.LoadFromCacheOrDownload(szUrl, httpAB.versionValue, httpAB.crc); yield return(www); if (null != www && null == www.error) { asset = www.assetBundle; www.Dispose(); www = null; } else { JZLog.LogError("LoadResources : Load from www Error: " + szUrl + "\n" + www.error); } } else { PathUtils.PathType eType = PathUtils.PathType.None; string szUrl = PathUtils.GetReadablePath(szPkgName, ref eType, false); AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(szUrl); yield return(abcr); if (null != abcr.assetBundle) { asset = abcr.assetBundle; } else { JZLog.LogError("LoadResources : Load from file Error: " + szUrl); } } if (null != asset) { if (resourceData.eState == ResourceMgrData.State.Loading) { resourceData.assetBundle = asset; #if UNITY_EDITOR if ("shader_greymaterial.ab".Equals(szPkgName)) { Material mat = gDicPkgResource[szPkgName].assetBundle.LoadAsset <Material>("GreyMaterial"); mat.shader = Shader.Find(mat.shader.name); } #endif } } else { resourceData.assetBundle = null; } /** @brief: 判断是否提前释放 */ if (resourceData.eState == ResourceMgrData.State.Loading) { resourceData.eState = ResourceMgrData.State.LoadFinish; } else { if (IsInvalid(resourceData.assetBundle) == false) { resourceData.assetBundle.Unload(true); resourceData.assetBundle = null; } } } while (false); ResourceMgrData resourceMgrData = gDicPkgResource[szPkgName]; /** @brief: 等待下载完成进行检测 */ while (resourceMgrData.eState == ResourceMgrData.State.Loading) { yield return(new WaitForEndOfFrame()); } /** @brief: 提前释放 */ if (resourceMgrData.eState == ResourceMgrData.State.None) { callback = null; ////JZLog.LogWarning("LoadResources : You have Destroyed this bundle " + szPkgName + " , Ignore ..."); yield break; } InitHideFlag(resourceMgrData.assetBundle); if (callback != null) { callback.Invoke(szPkgName, resourceMgrData.assetBundle); callback = null; } }