public static void _BuildFileVersionList() { #if UNITY_IOS string file_name = "FileVersionList_ios.json"; #else string file_name = "FileVersionList.json"; #endif try { File.Delete(file_name); } catch (Exception e) { } var files = Directory.GetFiles("AssetBundles", "*", System.IO.SearchOption.AllDirectories); FileVersionList list = new FileVersionList(); foreach (var p in files) { FileVersion v = new FileVersion(); v.file = p.Replace("\\", "/"); v.md5 = MD5Code.GetMD5HashFromFile(p); FileInfo info = new FileInfo(v.file); v.size = (int)info.Length; list.files.Add(v); } var json = UnityEngine.JsonUtility.ToJson(list); File.WriteAllText(file_name, json); }
public static void _BuildFSFirm() { try { Directory.Delete("Assets/StreamingAssets"); } catch (Exception e) { } try { Directory.CreateDirectory("Assets/StreamingAssets"); } catch (Exception e) { } var files = Directory.GetFiles("AssetBundles", "*", System.IO.SearchOption.AllDirectories); FileVersionList fsfirm_list = new FileVersionList(); foreach (var pp in files) { string p = pp.Replace("\\", "/"); if (Patches.Patcher.ForceCheckMD5(p)) { FileVersion v = new FileVersion(); v.file = p; v.size = (int)(new FileInfo(p)).Length; v.md5 = MD5Code.GetMD5HashFromFile(p); fsfirm_list.files.Add(v); //engine 文件不需要打进去 if (p.Contains("/engine/")) { continue; } string target = "Assets/StreamingAssets/" + p; int found = target.LastIndexOf("/"); if (found != target.Length) { if (!Directory.Exists(target.Substring(0, found))) { Directory.CreateDirectory(target.Substring(0, found)); } } File.Copy(p, target, true); } } try { File.Delete(DevConfig.FSFirmVersionListFileName); } catch (Exception e) { } File.WriteAllText("Assets/Resources/" + DevConfig.FSFirmVersionListFileName, JsonUtility.ToJson(fsfirm_list)); AssetDatabase.Refresh(); }
public static string UploadPatch(string local_file, string target) { string error_pre = "FTP Upload local_file=" + local_file + " error:"; string error = ""; string local_md5 = ""; try { local_md5 = MD5Code.GetMD5HashFromFile(local_file); if (string.IsNullOrEmpty(local_md5)) { return("local md5 error"); } var ins = File.ReadAllLines("ftp_config.txt"); string SERVER = ins[0]; string NAME = ins[1]; string PWD = ins[2]; FileInfo f = new FileInfo(local_file); var uri = new Uri(SERVER + "/" + target); FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(uri); reqFtp.UseBinary = true; reqFtp.Credentials = new NetworkCredential(NAME, PWD); reqFtp.KeepAlive = false; // reqFtp.EnableSsl = true; reqFtp.Method = WebRequestMethods.Ftp.UploadFile; reqFtp.ContentLength = f.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = f.OpenRead(); Stream strm = reqFtp.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); //反向下载 并且检查 文件MD5是否正确 // Get the object used to communicate with the server. WebClient request = new WebClient(); // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential(NAME, PWD); try { byte[] newFileData = request.DownloadData(uri.ToString()); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); string tmp_file_name = "tmp_ftp_md5_file.zip"; EditorUtils.Utils.TryDeleteFile(tmp_file_name); var x = File.Create(tmp_file_name); x.Write(newFileData, 0, newFileData.Length); x.Flush(); x.Close(); string remote_md5 = MD5Code.GetMD5HashFromFile(tmp_file_name); if (remote_md5 != local_md5) { return(error_pre + "remote md5 error re-try-upload-operation local=" + local_md5 + " remote=" + remote_md5); } } catch (WebException e) { return(error_pre + e.ToString()); } error = "ok"; } catch (Exception e) { error = error_pre + " error " + e.Message; } return(error); }
public static void _BuildZip_Android() { string path_pre_name = "VersionFiles/AssetBundles/Android/"; string path = EditorUtils.Utils.GetCurrentWorkingPath() + "/VersionFiles/AssetBundles/Android/AssetBundles_diff"; //需要构建的版本号 string v = File.ReadAllText("VersionFiles/VERSION.txt"); string out_zip_full_name = "android_" + v + ".zip"; // "VersionFiles/" + v + "/android_" + v + ".zip"; using (ZipFile zip = ZipFile.Create(out_zip_full_name)) { zip.BeginUpdate(); zip.AddDirectory("AssetBundles"); try { Directory.Delete("AssetBundles_diff", true); } catch (Exception e) { } //这样绕一下处理diff是因为 lua/lua这个文件 在 原路径下 打包zip会出错(ziplib的BUG) EditorUtils.Utils.CopyDir("VersionFiles/AssetBundles/Android/AssetBundles_diff", "AssetBundles_diff"); foreach (var d in Directory.GetDirectories("AssetBundles_diff")) { zip.AddDirectory(d.Replace("AssetBundles_diff", "AssetBundles")); } foreach (var p in Directory.GetFiles("AssetBundles_diff", "*", SearchOption.AllDirectories)) { zip.Add(p, p.Replace("AssetBundles_diff", "AssetBundles")); } zip.CommitUpdate(); Directory.Delete("AssetBundles_diff", true); } if (File.Exists("VersionFiles/" + v + "/android_" + v + ".zip")) { File.Delete("VersionFiles/" + v + "/android_" + v + ".zip"); } if (Directory.Exists("VersionFiles/" + v) == false) { Directory.CreateDirectory("VersionFiles/" + v); } File.Move(out_zip_full_name, "VersionFiles/" + v + "/android_" + v + ".zip"); File.Delete(out_zip_full_name); out_zip_full_name = "VersionFiles/" + v + "/android_" + v + ".zip"; string md5 = MD5Code.GetMD5HashFromFile(out_zip_full_name); int size = 0; using (var zip = File.OpenRead(out_zip_full_name)) { size = (int)zip.Length; } Patches.Version this_version = new Patches.Version(); Debug.Log("Patch Zip:will build version = " + v); this_version.Parse(v); this_version.MD5 = md5; this_version.FileSize = size; // Debug.LogError(LitJson.JsonMapper.ToJson(this_version)); { var ss = File.CreateText("VersionFiles/" + v + "/android_" + v + ".json"); ss.Write(LitJson.JsonMapper.ToJson(this_version)); ss.Flush(); ss.Close(); } //resfresh all { // Debug.LogError(pat); var patches = Directory.GetDirectories("VersionFiles", this_version.MainVersion + ".*.*", SearchOption.AllDirectories); List <Patches.Version> _patch_vs = new System.Collections.Generic.List <Patches.Version>(); foreach (var pp in patches) { var p = Path.GetFileName(pp); Patches.Version vv = new Patches.Version(); vv.Parse(p); _patch_vs.Add(vv); } _patch_vs.Sort(); VersionList _VL = new VersionList(); foreach (var p in _patch_vs) { string ver = p.MainVersion.ToString() + "." + p.SubVersion.ToString() + "." + p.PatchVersion.ToString() + "/android_" + p.MainVersion.ToString() + "." + p.SubVersion.ToString() + "." + p.PatchVersion.ToString() + ".json"; var this_v = File.ReadAllText("VersionFiles/" + ver); Debug.LogError(ver); _VL.versions.Add(LitJson.JsonMapper.ToObject <Patches.Version>(this_v)); } { var ss = File.CreateText("VersionFiles/FULL_VERSIONS.json"); ss.Write(LitJson.JsonMapper.ToJson(_VL)); ss.Flush(); ss.Close(); } } Debug.Log("Patch Zip:build android done "); }
public static void _BuildZip() { //需要构建的版本号 string v = File.ReadAllText("VersionFiles/VERSION.txt"); string out_zip_full_name = "android_" + v + ".zip"; // "VersionFiles/" + v + "/android_" + v + ".zip"; using (ZipFile zip = ZipFile.Create(out_zip_full_name)) { zip.BeginUpdate(); zip.AddDirectory("AssetBundles"); foreach (var d in Directory.GetDirectories("AssetBundles")) { zip.AddDirectory(d); } foreach (var p in Directory.GetFiles("AssetBundles", "*", SearchOption.AllDirectories)) { zip.Add(p); // Debug.LogError(p); } zip.CommitUpdate(); } if (File.Exists("VersionFiles/" + v + "/android_" + v + ".zip")) { File.Delete("VersionFiles/" + v + "/android_" + v + ".zip"); } if (Directory.Exists("VersionFiles/" + v) == false) { Directory.CreateDirectory("VersionFiles/" + v); } File.Move(out_zip_full_name, "VersionFiles/" + v + "/android_" + v + ".zip"); File.Delete(out_zip_full_name); out_zip_full_name = "VersionFiles/" + v + "/android_" + v + ".zip"; string md5 = MD5Code.GetMD5HashFromFile(out_zip_full_name); int size = 0; using (var zip = File.OpenRead(out_zip_full_name)) { size = (int)zip.Length; } Patches.Version this_version = new Patches.Version(); Debug.LogError(v); this_version.Parse(v); this_version.MD5 = md5; this_version.FileSize = size; Debug.LogError(LitJson.JsonMapper.ToJson(this_version)); { var ss = File.CreateText("VersionFiles/" + v + "/android_" + v + ".json"); ss.Write(LitJson.JsonMapper.ToJson(this_version)); ss.Flush(); ss.Close(); } //resfresh all { string pat = this_version.MainVersion.ToString() + "." + this_version.SubVersion.ToString() + "."; // Debug.LogError(pat); var patches = Directory.GetDirectories("VersionFiles", this_version.MainVersion + "." + this_version.SubVersion + ".*", SearchOption.AllDirectories); List <int> _patch_vs = new System.Collections.Generic.List <int>(); foreach (var pp in patches) { var p = Path.GetFileName(pp); Patches.Version vv = new Patches.Version(); Debug.LogError(pp + " " + p); vv.Parse(p); _patch_vs.Add(vv.PatchVersion); } _patch_vs.Sort(); VersionList _VL = new VersionList(); foreach (var p in _patch_vs) { string ver = this_version.MainVersion.ToString() + "." + this_version.SubVersion.ToString() + "." + p.ToString() + "/android_" + this_version.MainVersion.ToString() + "." + this_version.SubVersion.ToString() + "." + p.ToString() + ".json"; var this_v = File.ReadAllText("VersionFiles/" + ver); Debug.LogError(ver); Patches.Version x = new Patches.Version(); _VL.versions.Add(LitJson.JsonMapper.ToObject <Patches.Version>(this_v)); } { var ss = File.CreateText("VersionFiles/FULL_VERSIONS.json"); ss.Write(LitJson.JsonMapper.ToJson(_VL)); ss.Flush(); ss.Close(); } } }
private void ThreadFunc(object _param_call) { CurrentSize = 0; TotalSize = 0; DownloadParam _param = _param_call as DownloadParam; if (_param == null) { IsThreadRunning = false; _Status = Status.Error; return; } Debug.LogWarning("start to download " + _param.url); if (_param.IsRedirect) { //重定向的话 不处理 失败 重试次数 _param.IsRedirect = false; } else { //重新下载 或者 分批下载 都会重试计次 ++try_times; } if (try_times > 10) { //fatal error or net error _Status = Status.Error; return; } try { _Status = Status.Verifying; if (File.Exists(_param.path_to_save + "/" + _param.file_name)) { //文件存在的话 检查一下 是否成功 不成功的话 才开始下载 _Status = Status.Verifying; if (string.IsNullOrEmpty(_param.md5) == false && _param.md5 == MD5Code.GetMD5HashFromFile(_param.path_to_save + "/" + _param.file_name)) { //ok _Status = Status.OK; this.DownloadOK(); return; } else { //md5 verify error 需要处理下载 (or断点下载) 下载完成后 才 再次校验 } } } catch (Exception e) { } _Status = Status.Checking; if (_param.enable_break_point == false) { //无需断点下载 尝试暴力删除文件 try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { Debug.LogWarning(e.Message); } } if (Directory.Exists(_param.path_to_save) == false) { try { Directory.CreateDirectory(_param.path_to_save); } catch (Exception e) { Debug.LogWarning(e.Message); } } //先打开文件 Stream file = null; using (file = (File.Exists(_param.path_to_save + "/" + _param.file_name)) ? File.OpenWrite(_param.path_to_save + "/" + _param.file_name) : file = File.Create(_param.path_to_save + "/" + _param.file_name)) { /* try * { * if (File.Exists(_param.path_to_save + "/" + _param.file_name)) * { * file = File.OpenWrite(_param.path_to_save + "/" + _param.file_name); * } * else * { * file = File.Create(_param.path_to_save + "/" + _param.file_name); * } * } * catch (Exception e) * { * Debug.LogWarning(e.Message); * }*/ try { long current_size = file.Length; if (current_size > 0) { file.Seek(current_size, SeekOrigin.Begin); } HttpWebRequest request = null; //如果是发送HTTPS请求 if (_param.url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = (HttpWebRequest)WebRequest.Create(_param.url); } else { request = (HttpWebRequest)WebRequest.Create(_param.url); } request.ProtocolVersion = new System.Version(1, 1); if (current_size > 0) { request.AddRange((int)current_size); CurrentSize = (int)current_size; } HttpWebResponse response = null; request.Timeout = 10000; request.ReadWriteTimeout = 10000; request.Method = "GET"; request.KeepAlive = false; response = (HttpWebResponse)request.GetResponse(); var HttpRetCode = response.StatusCode; Debug.Log("InstallDownloader http " + HttpRetCode); if (HttpRetCode == HttpStatusCode.Redirect) { //重定向 _param.url = response.Headers["Location"].Trim(); response.Close(); response = null; request.Abort(); request = null; try { file.Close(); } catch (Exception e) { Debug.LogWarning(e.Message); } Debug.Log("Redirect " + _param.url); _param.IsRedirect = true; ThreadFunc(_param); return; } else if (HttpRetCode == HttpStatusCode.GatewayTimeout || HttpRetCode == HttpStatusCode.RequestTimeout) { //net error response.Close(); response = null; request.Abort(); request = null; try { file.Close(); } catch (Exception e) { Debug.LogWarning(e.Message); } Debug.Log("timeout"); return; } else if (HttpRetCode == HttpStatusCode.OK || HttpRetCode == HttpStatusCode.Created || HttpRetCode == HttpStatusCode.Accepted || HttpRetCode == HttpStatusCode.NonAuthoritativeInformation || HttpRetCode == HttpStatusCode.NoContent || HttpRetCode == HttpStatusCode.ResetContent || HttpRetCode == HttpStatusCode.PartialContent) { if (HttpRetCode != HttpStatusCode.PartialContent) { //如果不是断点下载 或者服务器不支持 那么需要 重新下载完整文件 try { file.Close(); file = null; } catch (Exception e) { } try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { } try { Directory.CreateDirectory(_param.path_to_save); } catch (Exception e) { } file = File.Create(_param.path_to_save + "/" + _param.file_name); } } else { //req error response.Close(); response = null; request.Abort(); request = null; try { file.Close(); } catch (Exception e) { Debug.LogWarning(e.Message); } try { File.Delete(_param.path_to_save + "/" + _param.file_name); } catch (Exception e) { } Debug.LogWarning("error"); return; } //web 请求处理完成了 开始处理 接受数据了 long total_len = response.ContentLength; TotalSize = (int)total_len + (int)current_size; if (current_size < TotalSize) { if (current_size > 0) { // request.AddRange((int)current_size); CurrentSize = (int)current_size; } Stream web = request.GetResponse().GetResponseStream(); byte[] _cache = new byte[10240]; // 10kb int down_size = 0; int read_size = web.Read(_cache, 0, 10240); int total_read_size = 0; _Status = Status.Downloading; while (read_size > 0) { _Status = Status.Downloading; file.Write(_cache, 0, read_size); total_read_size += read_size; down_size += read_size; CurrentSize += read_size; // Debug.LogError("download ing " + CurrentSize + " " + TotalSize); file.Flush(); read_size = web.Read(_cache, 0, 10240); } file.Close(); file = null; web.Close(); web = null; response.Close(); response = null; request.Abort(); request = null; if (current_size + down_size < TotalSize) { //下载文件 长度不够 需要重新下载 Debug.LogWarning("file is smaller will re-try"); ThreadFunc(_param); return; } else if (current_size + down_size > TotalSize) { //下载的长度 超过了 实际长度 文件已经损坏 重新下载把 try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { Debug.LogWarning(e.Message); } Debug.LogWarning("file is bigger will delete and re-download"); ThreadFunc(_param); return; } else { //下载文件成功 开始校验MD5 _Status = Status.Verifying; string download_md5 = MD5Code.GetMD5HashFromFile(_param.path_to_save + "/" + _param.file_name); if (string.IsNullOrEmpty(_param.md5) == false && _param.md5 == download_md5) { //ok } else { if (_param.md5 != null) { Debug.LogWarning("excepted md5=" + _param.md5 + " file=" + download_md5); } //md5 verify error 尝试重新下载 try { file.Close(); file = null; response.Close(); response = null; request.Abort(); request = null; } catch (Exception e) { } try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { Debug.LogWarning(e.Message); } ThreadFunc(_param); return; } _Status = Status.OK; } } else if (current_size == total_len) {//当前文件和 服务器文件大小一样 默认为 下载完成 需要校验MD5 try { file.Close(); file = null; response.Close(); response = null; request.Abort(); request = null; } catch (Exception e) { } Debug.LogWarning("file is req just done"); _Status = Status.Verifying; var download_md5 = MD5Code.GetMD5HashFromFile(_param.path_to_save + "/" + _param.file_name); if (string.IsNullOrEmpty(_param.md5) == false && _param.md5 == download_md5) { //ok } else { if (_param.md5 != null) { Debug.LogWarning("1excepted md5=" + _param.md5 + " file=" + download_md5); } //md5 verify error 尝试重新下载 try { file.Close(); file = null; response.Close(); response = null; request.Abort(); request = null; } catch (Exception e) { } try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { Debug.LogWarning(e.Message); } ThreadFunc(_param); return; } _Status = Status.OK; } else { //当前文件超过了 大小 需要重新下载 try { Directory.Delete(_param.path_to_save, true); } catch (Exception e) { Debug.LogWarning(e.Message); } Debug.LogWarning("file is bigger will delete and re-download 2"); try { file.Close(); file = null; response.Close(); response = null; request.Abort(); request = null; } catch (Exception e) { } ThreadFunc(_param); return; } //走到了这里 都当作文件下载成功 并且校验成功 可以开始安装了 _Status = Status.OK; this.DownloadOK(); } catch (Exception ee) { //整个下载流程出了异常错误 Debug.LogWarning(ee.Message); _Status = Status.Checking; try { if (file != null) { file.Close(); file = null; } } catch (Exception e) { Debug.LogWarning(e.Message); } try { File.Delete(_param.path_to_save + "/" + _param.file_name); } catch (Exception e) { } ThreadFunc(_param); return; } } }