private void SyncDataFromServer() { if (!Directory.Exists("resdatabase")) { Directory.CreateDirectory("resdatabase"); } try { string url = "http://code.putao.io/ptgame/Model_PTBloks/raw/master/client_unity/resdatabase"; var webClient = new WebClientPro(); var fileNames = new [] { "resconfig_modelres_ios.json", "resconfig_modelres_android.json", "resconfig_thumbs_primitive.json" }; foreach (var fileName in fileNames) { if (File.Exists("resdatabase/" + fileName)) { File.Delete("resdatabase/" + fileName); } webClient.DownloadFile(url + "/" + fileName, "resdatabase/" + fileName); } webClient.Dispose(); EditorUtility.DisplayDialog("同步数据", "同步数据成功", "确定"); } catch (Exception e) { EditorUtility.DisplayDialog("同步数据", "同步数据失败", "确定"); } }
/// <summary> /// 下载线程 /// </summary> private void _Download() { while (downloadlist.Count != 0) { string Lrc = ""; downloadlist[0].State = "正在下载音乐"; if (downloadlist[0].Url == null) { downloadlist[0].State = "无版权"; UpdateDownloadPage(); downloadlist.RemoveAt(0); continue; } UpdateDownloadPage(); string savepath = ""; string filename = "";; switch (setting.SaveNameStyle) { case 0: if (downloadlist[0].Url.IndexOf("flac") != -1) { filename = NameCheck(downloadlist[0].Title) + " - " + NameCheck(downloadlist[0].Singer) + ".flac"; } else { filename = NameCheck(downloadlist[0].Title) + " - " + NameCheck(downloadlist[0].Singer) + ".mp3"; } break; case 1: if (downloadlist[0].Url.IndexOf("flac") != -1) { filename = NameCheck(downloadlist[0].Singer) + " - " + NameCheck(downloadlist[0].Title) + ".flac"; } else { filename = NameCheck(downloadlist[0].Singer) + " - " + NameCheck(downloadlist[0].Title) + ".mp3"; } break; } switch (setting.SavePathStyle) { case 0: savepath = setting.SavePath; break; case 1: savepath = setting.SavePath + "\\" + NameCheck(downloadlist[0].Singer); break; case 2: savepath = setting.SavePath + "\\" + NameCheck(downloadlist[0].Singer) + "\\" + NameCheck(downloadlist[0].Album); break; } if (!Directory.Exists(savepath)) { Directory.CreateDirectory(savepath); } if (downloadlist[0].IfDownloadMusic) { if (System.IO.File.Exists(savepath + "\\" + filename)) { downloadlist[0].State = "音乐已存在"; UpdateDownloadPage(); } else { using (WebClientPro wc = new WebClientPro()) { try { wc.DownloadFile(downloadlist[0].Url, savepath + "\\" + filename); } catch { downloadlist[0].State = "音乐下载错误"; downloadlist.RemoveAt(0); UpdateDownloadPage(); continue; } } } } if (downloadlist[0].IfDownloadLrc) { downloadlist[0].State = "正在下载歌词"; UpdateDownloadPage(); using (WebClientPro wc = new WebClientPro()) { try { if (downloadlist[0].Api == 1) { string savename = savepath + "\\" + filename.Replace(".flac", ".lrc").Replace(".mp3", ".lrc"); StreamReader sr = new StreamReader(wc.OpenRead(downloadlist[0].LrcUrl)); string json = sr.ReadToEnd(); NeteaseLrc.Root lrc = JsonConvert.DeserializeObject <NeteaseLrc.Root>(json); Lrc = lrc.lrc.lyric ?? ""; if (Lrc != "") { StreamWriter sw = new StreamWriter(savename); sw.Write(Lrc); sw.Flush(); sw.Close(); } else { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } else if (downloadlist[0].Api == 2) { string savename = savepath + "\\" + filename.Replace(".flac", ".lrc").Replace(".mp3", ".lrc"); StreamReader sr = new StreamReader(wc.OpenRead(downloadlist[0].LrcUrl)); string json = sr.ReadToEnd(); QQLrc.Root lrc = JsonConvert.DeserializeObject <QQLrc.Root>(json); Lrc = lrc.data.lyric ?? ""; if (Lrc != "") { StreamWriter sw = new StreamWriter(savename); sw.Write(Lrc); sw.Flush(); sw.Close(); } else { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } } catch { downloadlist[0].State = "歌词下载错误"; UpdateDownloadPage(); } } } if (downloadlist[0].IfDownloadPic) { downloadlist[0].State = "正在下载图片"; UpdateDownloadPage(); using (WebClientPro wc = new WebClientPro()) { try { wc.DownloadFile(downloadlist[0].PicUrl, savepath + "\\" + filename.Replace(".flac", ".jpg").Replace(".mp3", ".jpg")); } catch { downloadlist[0].State = "图片下载错误"; UpdateDownloadPage(); } } } if (filename.IndexOf(".mp3") != -1) { using (var tfile = TagLib.File.Create(savepath + "\\" + filename)) { //tfile.Tag.Title = downloadlist[0].Title; //tfile.Tag.Performers = new string[] { downloadlist[0].Singer }; //tfile.Tag.Album = downloadlist[0].Album; //if (downloadlist[0].IfDownloadLrc && Lrc != "" && Lrc != null) //{ // tfile.Tag.Lyrics = Lrc; //} if (downloadlist[0].IfDownloadPic && System.IO.File.Exists(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg")) { Tool.PngToJpg(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); TagLib.Picture pic = new TagLib.Picture(); pic.Type = TagLib.PictureType.FrontCover; pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg; pic.Data = TagLib.ByteVector.FromPath(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); tfile.Tag.Pictures = new TagLib.IPicture[] { pic }; } tfile.Save(); } } else { using (var tfile = TagLib.Flac.File.Create(savepath + "\\" + filename)) { tfile.Tag.Title = downloadlist[0].Title; tfile.Tag.Performers = new string[] { downloadlist[0].Singer }; tfile.Tag.Album = downloadlist[0].Album; if (downloadlist[0].IfDownloadLrc && Lrc != "" && Lrc != null) { tfile.Tag.Lyrics = Lrc; } if (downloadlist[0].IfDownloadPic && System.IO.File.Exists(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg")) { Tool.PngToJpg(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); TagLib.Picture pic = new TagLib.Picture(); pic.Type = TagLib.PictureType.FrontCover; pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg; pic.Data = TagLib.ByteVector.FromPath(savepath + "\\" + filename.Replace(".flac", "").Replace(".mp3", "") + ".jpg"); tfile.Tag.Pictures = new TagLib.IPicture[] { pic }; } tfile.Save(); } } downloadlist[0].State = "下载完成"; UpdateDownloadPage(); downloadlist.RemoveAt(0); } }