using UnityEngine; using UnityEditor; using System.IO; using LitJson; using vnxbqy.UI; using System; using System.Threading; using StartAot; using StartAotSDK; using UnityEngine.Networking; using Codice.CM.Client.Differences; using System.Collections; using System.Collections.Generic; public class CheckCDN : EditorWindow { [MenuItem("Tools/检查CDN")] public static void ShowWindow() { EditorWindow.GetWindow("检查CDN").Show(); } [SerializeField] private string centerUrl; VersionUtility.VersionInfo versionInfo { get; set; } long remoteLength; private System.DateTime remoteLastModified; //远端文件最后修改时间 private void OnGUI() { //从version获得CDN地址,检查 EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("中心地址"); EditorGUILayout.EndHorizontal(); centerUrl = GUILayout.TextField(centerUrl, GUILayout.MinWidth(300)); GUILayout.BeginHorizontal(); GUI.color = Color.red; GUILayout.Label("必须在游戏运行的情况下才能检测!!!!! "); GUILayout.EndHorizontal(); GUI.color = Color.white; GUILayout.BeginHorizontal(); GUILayout.Label("没有配置默认取本版本代码的中心地址 " + VersionUtility.VERSION_URL[0]); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (string.IsNullOrEmpty(centerUrl)) GUILayout.Label("当前测试地址为:" + VersionUtility.VERSION_URL[0]); else GUILayout.Label("当前测试地址为:" + centerUrl); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("根据versionsconfig 检查CDN的 AssetsVersion.txt配置文件和资源 1.是否存在文件,2.大小是否一致"); GUILayout.EndHorizontal(); if (GUILayout.Button("检测")) { QueryCDN(); } } private void QueryCDN() { string url = string.IsNullOrEmpty(centerUrl) ? VersionUtility.VERSION_URL[0] : centerUrl; var tables = new Dictionary(); tables["channel"] = VersionConfig.Get().appId; tables["versioncode"] = VersionConfig.Get().version; if (VersionConfig.Get().branch != 0) { tables["branch"] = VersionConfig.Get().branch.ToString(); } tables["game"] = VersionConfig.Get().gameId; url = StringUtility.Contact(url, HttpRequest.HashtablaToString(tables)); HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 1, OnVersionCheckResult); } private void OnVersionCheckResult(bool _ok, string _result) { if (_ok) { versionInfo = JsonMapper.ToObject(_result); var assetVersionUrl = StringUtility.Contact(GetResourcesURL(), Language.fixPath, "/AssetsVersion.txt"); Debug.Log("请求CDN的AssetsVersion:" + assetVersionUrl); HttpRequest.Instance.RequestHttpGet(assetVersionUrl, HttpRequest.defaultHttpContentType, 3, OnGetAssetVersionFile); } else { Debug.Log("请求失败" + _result); } } public string GetResourcesURL() { int _branch = VersionConfig.Get().branch; if (versionInfo.resource_url != null) { return versionInfo.resource_url[_branch.ToString()].ToString(); } else { return string.Empty; } } private void OnGetAssetVersionFile(bool ok, string result) { Debug.LogFormat("获取资源版本文件结果:时间 {0},结果 {1}, 文件大小 {2}", DateTime.Now, ok, result.Length); if (ok) { AssetVersionUtility.UpdateAssetVersions(result); BeginCheckAssets(); } else { Debug.Log("请求CDN的AssetsVersion失败!"); } } private void BeginCheckAssets() { foreach (var assetVersion in AssetVersionUtility.GetVersionsInfo().Values) { SnxxzGame.Instance.StartCoroutine(Co_GetHeader(assetVersion)); } } private IEnumerator Co_GetHeader(AssetVersion assetVersion) { var remoteUrl = StringUtility.Contact(GetResourcesURL(), Language.fixPath, "/", assetVersion.relativePath); using (var www = UnityWebRequest.Head(remoteUrl)) { www.timeout = DownloadMgr.TimeOut; yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) { DebugEx.LogErrorFormat("头信息获取失败:{0};error:{1}", remoteUrl, www.error); www.Dispose(); yield break; } long.TryParse(www.GetResponseHeader(HttpHeader.ContentLength), out this.remoteLength); this.remoteLastModified = DateTime.Parse(www.GetResponseHeader(HttpHeader.LastModified)); var acceptRange = www.GetResponseHeader(HttpHeader.AcceptRanges); DebugEx.LogFormat("头信息获取成功:{0};大小:{1};修改时间:{2};是否支持续传:{3}", remoteUrl, remoteLength, remoteLastModified, acceptRange); if (remoteLength != assetVersion.size) { DebugEx.LogErrorFormat("文件信息不一致:{0};记录大小:{1};远端大小:{2}", remoteUrl, assetVersion.size, remoteLength); www.Dispose(); yield break; } www.Dispose(); } } }