public static void SaveGroupToLocal(string filename) { if (!Directory.Exists(localcachepath)) { try { Directory.CreateDirectory(localcachepath); } catch (Exception e) { Log_Debug.LogError(e.ToString()); } } string header = "ver:"; StringBuilder in_write = new StringBuilder(); in_write.Append(header).Append(Version.ToString()); foreach (KeyValuePair <string, VerGroup> kv in RemoteVersion.RemoteVerGroup) { VerGroup ver = kv.Value; in_write.Append("\r\n").Append(ver.RootName).Append("|").Append(ver.hash).Append("|").Append(ver.filecount); ver.SaveLocal(ver.RootName); } string path = System.IO.Path.Combine(localcachepath, filename); using (var s = System.IO.File.Create(path)) { byte[] b = Encoding.UTF8.GetBytes(in_write.ToString()); s.Write(b, 0, b.Length); } }
public void BeginDownLoad(string path, Action callback) { Action CompareDiff = () => { foreach (KeyValuePair <string, VerGroup> index in RemoteVersion.RemoteVerGroup) { if (LocalVersion.LocalVerGroup.ContainsKey(index.Key))//比较hash,size { VerGroup local = LocalVersion.LocalVerGroup[index.Key]; //if (local.hash != index.Value.hash || local.filecount != index.Value.filecount) //{ // LocalVersion.LocalVerGroup[index.Key] = index.Value; //} foreach (KeyValuePair <string, RootGroup> KV in index.Value.rootlist) { if (local.rootlist.ContainsKey(KV.Key)) { RootGroup group = local.rootlist[KV.Key]; if (group.hash != KV.Value.hash || group.filesize != KV.Value.filesize) { local.rootlist[KV.Key] = KV.Value; local.rootlist[KV.Key].needdownload = true; } } else { RootGroup g = new RootGroup(KV.Value.FileName, KV.Value.hash, KV.Value.filesize); g.needdownload = true; local.rootlist[KV.Key] = g; } } } else { VerGroup newgroup = new VerGroup(index.Value.RootName, index.Value.hash, index.Value.filecount); index.Value.SetAllFilesNeedDownLoad(); newgroup.rootlist = index.Value.rootlist; LocalVersion.LocalVerGroup[index.Key] = newgroup; } } //LocalVersion.SaveGroupToLocal("allver.ver.txt"); //TODO::与本地解析比较,找出需要下载的 if (callback != null) { callback(); } }; Action <WWW, int> AllVerCallBack = (www, udata) => { if (www.error != null || www.text == null) { Log_Debug.LogError("www download error"); return; } string info = www.text; string[] splited = info.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (splited.Length <= 1)//数据错误 { ShowLoadingErrTips("服务器下载数据错误"); return; } for (int i = 0; i < splited.Length; i++) { if (i == 0) { string[] first = splited[0].Split(new string[] { ":" }, StringSplitOptions.None); int.TryParse(first[1], out RemoteVersion.Version); } else { string[] first = splited[i].Split(new string[] { "|" }, StringSplitOptions.None); VerGroup ver = new VerGroup(first[0], first[1], int.Parse(first[2])); RemoteVersion.RemoteVerGroup[first[0]] = ver; } } Debug.Log(RootHashDiff()); if (RemoteVersion.Version > LocalVersion.Version || RootHashDiff())// { LocalVersion.Version = RemoteVersion.Version; foreach (KeyValuePair <string, VerGroup> group in RemoteVersion.RemoteVerGroup) { StringBuilder str = new StringBuilder(); str.Append(RemoteVersion.remoteurl).Append("/" + group.Key + ".ver.txt"); group.Value.CreateDownLoad(str.ToString(), CompareDiff); } } else { if (callback != null) { callback(); } } }; Action MergeLocal = () => { RemoteVersion.AddDownLoadTask(path, AllVerCallBack); }; if (DataManager.Instance.IsFirstEnterGame)//首次进入游戏 { LocalVersion.ClearLocalCache(); RemoteVersion.AddDownLoadTask(path, AllVerCallBack); } else { LocalVersion.ReadLocalAllVer(path, MergeLocal); } }
public static void ReadLocalAllVer(string path, Action callback) { Action <string> ReadRoot = (Path) => { StringBuilder rootname = new StringBuilder(); string readtext = null; rootname.Append(localcachepath).Append("/").Append(Path).Append(".ver.txt"); using (var s = System.IO.File.OpenRead(rootname.ToString())) { byte[] b = new byte[s.Length]; s.Read(b, 0, b.Length); readtext = System.Text.Encoding.UTF8.GetString(b); } string[] ArraySplited = readtext.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (ArraySplited.Length <= 1)//数据错误 { DownLoadManager.Instance.ShowLoadingErrTips("读取本地数据错误"); return; } for (int i = 0; i < ArraySplited.Length; i++) { if (i == 0) { string[] first = ArraySplited[0].Split(new string[] { "ver:", "|FileCount:" }, StringSplitOptions.RemoveEmptyEntries); int ver = int.Parse(first[0]); int filecount = int.Parse(first[1]); FileCount += filecount; if (filecount != LocalVerGroup[Path].filecount) { LocalVerGroup[Path].filecount = filecount; } } else { string[] first = ArraySplited[i].Split(new string[] { "|", "@" }, StringSplitOptions.RemoveEmptyEntries); RootGroup root = new RootGroup(first[0], first[1], int.Parse(first[2])); LocalVerGroup[Path].rootlist[first[0]] = root; } } ; if (callback != null) { callback(); } }; string info = null; string localpath = localcachepath + path; using (var s = System.IO.File.OpenRead(localpath)) { byte[] b = new byte[s.Length]; s.Read(b, 0, b.Length); info = System.Text.Encoding.UTF8.GetString(b); } string[] splited = info.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (splited.Length <= 1)//数据错误 { DownLoadManager.Instance.ShowLoadingErrTips("本地读取数据错误"); return; } for (int i = 0; i < splited.Length; i++) { if (i == 0) { string[] first = splited[0].Split(new string[] { ":" }, StringSplitOptions.None); int.TryParse(first[1], out Version); } else { string[] first = splited[i].Split(new string[] { "|" }, StringSplitOptions.None); VerGroup ver = new VerGroup(first[0], first[1], int.Parse(first[2])); LocalVerGroup[first[0]] = ver; } } foreach (KeyValuePair <string, VerGroup> group in LocalVerGroup) { ReadRoot(group.Value.RootName); } }