public static string GetStringAndRelease(StringBuilder sb) { string str = sb.ToString(); KTStringBuilderCache.Release(sb); return(str); }
private static byte[] DevLuaLoader(string luaDir, string path) { var sb = KTStringBuilderCache.Acquire() .Append(luaDir) .Append('/') .Append(path) .Replace('.', '/') .Append(KTConfigs.kLuaExt); var fullpath = KTStringBuilderCache.GetStringAndRelease(sb); if (File.Exists(fullpath)) { return(File.ReadAllBytes(fullpath)); } else { Debug.LogErrorFormat("Lua 路径不对{0}", path + ":" + fullpath); } return(null); }
/// <summary> /// 拷贝文件 /// todo:有可能错误,待处理 /// </summary> /// <param name="infile">输入目录</param> /// <param name="outfile">输出目录</param> /// <returns></returns> private IEnumerator CopyFile(string infile, string outfile) { if (File.Exists(outfile)) { File.Delete(outfile); } var sb = KTStringBuilderCache.Acquire() .Append("正在解包文件:>") .Append(":") .Append(infile) .Append(":") .Append(outfile); Debug.Log(KTStringBuilderCache.GetStringAndRelease(sb)); if (Application.platform == RuntimePlatform.Android) { yield return(KTDownloadHelper.WWWDownloadRequest(infile, (www) => { File.WriteAllBytes(outfile, www.bytes); } , (www) => { Debug.LogError("拷贝释放" + Path.GetFileName(infile) + "文件错误"); })); } else { if (File.Exists(outfile)) { File.Delete(outfile); } File.Copy(infile, outfile, true); } yield return(new WaitForEndOfFrame());//等一帧 }
public void New() { //选择文件夹,只对Project右侧子面板的选择有效 UnityEngine.Object obj = Selection.activeObject; if (obj == null) { return; } var sb = KTStringBuilderCache.Acquire() .Append(AssetDatabase.GetAssetPath(obj)) .Append("/") .Append(scriptName) .Append(KTConfigs.kAssetExt); var fullpath = KTStringBuilderCache.GetStringAndRelease(sb); try { var type = assembly.GetTypes().First((t) => { return(t.Name.ToUpper() == scriptName.ToUpper()); }); if (type != null) { ScriptableObject scriptObj = ScriptableObject.CreateInstance(type); AssetDatabase.CreateAsset(scriptObj, fullpath); AssetDatabase.Refresh(); } else { Debug.LogWarning("需要创建的ScriptableObject类型错误"); } } catch (Exception e) { Debug.LogWarning("需要创建的ScriptableObject类型错误"); } }
/// <summary> /// 普通更新 /// </summary> /// <returns></returns> private IEnumerator UpdateResource() { this.m_updatPhase = UpdatePhase.NormalUpdate; InitUpdate(); //下载新file string dataPath = KTPathHelper.DataPath; var sb = KTStringBuilderCache.Acquire() .Append("file://") .Append(KTPathHelper.AppContentPath()) .Append("files.txt"); var sb2 = KTStringBuilderCache.Acquire() .Append(KTConfigs.kWebUrl) .Append("files.txt?v=") .Append(DateTime.Now.ToString("yyyymmddhhmmss")); string fileUrl = KTConfigs.kDebugMode ? KTStringBuilderCache.GetStringAndRelease(sb) : KTStringBuilderCache.GetStringAndRelease(sb2); this.m_updatPhase = UpdatePhase.DeltaDownloadNewFile; Debug.Log("LoadUpdate---->>>" + fileUrl); //下载file this.m_updatPhase = UpdatePhase.DownloadNewFile; yield return(KTDownloadHelper.WWWDownloadRequest(fileUrl, (www) => { www.bytes.CopyTo(newFileData, 0); string[] newFileLines = www.text.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.None); string[] oldFileLines = File.ReadAllLines(dataPath + "files.txt"); ExecuteDownloadTask(newFileLines, oldFileLines); if (m_fileToCreate.Count == 0) { Debug.Log("没有可更新资源"); if (hotUpdateComplete != null) { hotUpdateComplete(); } return; } if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } ExecuteDownloadTask(newFileLines, oldFileLines); //删掉多余的旧资源 for (int i = 0, j = m_fileToDelete.Count; i < j; i++) { string localfile = (dataPath + m_fileToDelete[i]).Trim(); if (File.Exists(localfile)) { File.Delete(localfile); } } //递归删除BuildRes下的所有空文件夹 DeleteDir(dataPath + "BuildRes"); //创建加载任务 for (int i = 0, j = m_fileToCreate.Count; i < j; i++) { string fileFullPath = (dataPath + m_fileToCreate[i]).Trim(); if (File.Exists(fileFullPath)) { File.Delete(fileFullPath); } string path = Path.GetDirectoryName(fileFullPath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } sb = KTStringBuilderCache.Acquire() .Append("file://") .Append(KTPathHelper.AppContentPath()) .Append(m_fileToCreate[i]); sb2 = KTStringBuilderCache.Acquire() .Append(KTConfigs.kWebUrl) .Append(m_fileToCreate[i]) .Append("?v=") .Append(DateTime.Now.ToString("yyyymmddhhmmss")); string srcUrl = KTConfigs.kDebugMode ? KTStringBuilderCache.GetStringAndRelease(sb) : KTStringBuilderCache.GetStringAndRelease(sb2); KTUpdateLoaderManager.It.Add(srcUrl, fileFullPath); //StartCoroutine(KTDownloadHelper.WWWDownloadRequest(srcUrl, (www2) => //{ // File.WriteAllBytes(fileFullPath, www2.bytes); //} //,(www2) => //{ // StartCoroutine(UpdateFail("下载新"+ m_fileToCreate[i] + "文件失败")); //})); //StartCoroutine(UpdateComplete()); } } , (www) => { StartCoroutine(UpdateFail("下载新file.txt文件失败")); })); this.m_updatPhase = UpdatePhase.DownloadSrc; yield return(KTUpdateLoaderManager.It.StartLoadAsync((data) => { StartCoroutine(UpdateComplete()); }, null, (msg) => { StartCoroutine(UpdateFail("下载新" + msg + "文件失败")); })); }
/// <summary> /// 增量更新 /// 下载files文件到Application.persistentDataPath下,对比本地files文件判断是否需要更新,如果有 /// 下载delta文件到persistentDataPath/Temp目录下 /// 组合delta文件和src.zip文件到persistentDataPath目录下,名称为src.zip /// 把新的src.zip文件拷贝到persistentDataPath/Temp目录下,覆盖掉原来的src.zip /// 解压新的Temp/src.zip到persistentDataPath目录下 /// 把下载的新files文件覆盖掉原来的files /// </summary> /// <returns></returns> private IEnumerator DeltaUpdateResource() { this.m_updatPhase = UpdatePhase.DeltaUpdate; InitUpdate(); //下载新file string dataPath = KTPathHelper.DataPath; var sb = KTStringBuilderCache.Acquire() .Append("file://") .Append(KTPathHelper.AppContentPath()) .Append("files.txt"); var sb2 = KTStringBuilderCache.Acquire() .Append(KTConfigs.kWebUrl) .Append("files.txt?v=") .Append(DateTime.Now.ToString("yyyymmddhhmmss")); string fileUrl = KTConfigs.kDebugMode ? KTStringBuilderCache.GetStringAndRelease(sb) : KTStringBuilderCache.GetStringAndRelease(sb2); this.m_updatPhase = UpdatePhase.DeltaDownloadNewFile; Debug.Log("LoadUpdate---->>>" + fileUrl); yield return(KTDownloadHelper.WWWDownloadRequest(fileUrl, (www) => { www.bytes.CopyTo(newFileData, 0); var newFileLines = www.text.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.None); var oldFileLines = File.ReadAllLines(dataPath + "files.txt"); ExecuteDownloadTask(newFileLines, oldFileLines); if (m_fileToCreate.Count == 0) { Debug.Log("没有可更新资源"); if (hotUpdateComplete != null) { hotUpdateComplete(); } return; } //创建加载任务 string deltaFullPath = dataPath + "Temp/" + KTConfigs.kDeltaName; if (File.Exists(deltaFullPath)) { File.Delete(deltaFullPath); } sb = KTStringBuilderCache.Acquire() .Append("file://") .Append(KTPathHelper.AppContentPath()) .Append(KTConfigs.kDeltaName); sb2 = KTStringBuilderCache.Acquire()//此处因为暂定只需要下载一个zip文件,所以不需要从m_fileToCreate拿到具体文件名,但当多zip分包时就必须遍历m_fileToCreate下载了 .Append(KTConfigs.kWebUrl) .Append(KTConfigs.kDeltaName) .Append("?v=") .Append(DateTime.Now.ToString("yyyymmddhhmmss")); string deltaUrl = KTConfigs.kDebugMode ? KTStringBuilderCache.GetStringAndRelease(sb) : KTStringBuilderCache.GetStringAndRelease(sb2); this.m_updatPhase = UpdatePhase.DownloadDelta; Debug.Log("LoadUpdate---->>>" + deltaUrl); StartCoroutine(KTDownloadHelper.WWWDownloadRequest(deltaUrl, (www2) => { File.WriteAllBytes(deltaFullPath, www2.bytes); StartCoroutine(PitchFile()); } , (www2) => { StartCoroutine(UpdateFail("下载新src.zip.delta文件失败")); })); } , (www) => { StartCoroutine(UpdateFail("下载新file.txt文件失败")); })); }