示例#1
0
        private void CheckPack(object number)
        {
            //建立暫存
            string tempfile = Private_Function.CreateTmpFile();

            Log.AddLine(String.Format("從{0}下載Minecraft的Mod清單...", URL), Colors.Black);
            CrossThread_EditeLabelContent(label1, String.Format("從{0}下載Minecraft的Mod清單...", URL));
            try
            {
                using (WebClient myWebClient = new WebClient())
                {
                    myWebClient.DownloadFile(URL, tempfile);
                }
                CrossThread_EditeLabelContent(label1, String.Format("解析中..."));
                using (StreamReader reader = new StreamReader(tempfile, Encoding.UTF8))
                {
                    while (!reader.EndOfStream)
                    {
                        string temp = reader.ReadLine();
                        if (temp != "")
                        {
                            list.Add(Packs.reslove(temp));
                        }
                    }
                }
            }
            catch (System.Net.WebException e) { Log.AddLine(String.Format("取得最新最新PackList時失敗,出現以下訊息:{0}", e.Message), Colors.Red); }
            catch (Exception e) { Log.AddLine(String.Format("取得最新PackList時失敗,出現以下訊息:{0}", e.Message), Colors.Red); }

            //刪除暫存
            Private_Function.DeleteTmpFile(tempfile);
            int totalCount = list.Where(x => !x.Delete).ToList().Count;

            Log.AddLine(String.Format("Minecraft的Mod清單下載完成,在清單上共有{0}個檔案...", totalCount), Colors.Black);
            CrossThread_EditeLabelContent(label1, String.Format("0/{0}", totalCount));
            CrossThread_EditeProgressBar(progressBar, totalCount, 0, 0);
            int haveUpdate = 0;

            try
            {
                DirectoryInfo di    = new DirectoryInfo(AppPath);
                List <string> files = di.EnumerateFiles("*", SearchOption.AllDirectories).Select(x => x.FullName).ToList <string>();
                //刪除檔案
                var    templist  = list.Where(x => x.Delete).ToList();
                char[] Delimiter = new char[] { '+', '-', '_' };
                templist.ForEach(x => files.Where(y =>
                {
                    string temp = y.Substring(AppPath.Length + 1);
                    if (temp.Length > x.Path.Length + 1 && Delimiter.Contains(temp[x.Path.Length]) && temp.StartsWith(x.Path, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }).ToList().ForEach(z =>
                {
                    if (Private_Function.GetMD5(z) != x.MD5)
                    {
                        try { File.Delete(z); }
                        catch (IOException) { Log.AddLine(String.Format("刪除{0}時失敗,檔案正在使用中", System.IO.Path.GetFileName(z)), Colors.Red); }
                        catch (Exception e) { Log.AddLine(String.Format("刪除{0}時失敗,出現以下訊息:{0}", e.Message), Colors.Red); }
                    }
                }));

                //新增/取代檔案
                templist = list.Where(x => !x.Delete && !x.DownloadWhenNotExist).ToList();
                string filepath;
                foreach (var temp in templist)
                {
                    filepath = Path.Combine(AppPath, temp.Path);
                    if ((File.Exists(filepath) && (temp.DownloadWhenNotExist || (Private_Function.GetMD5(filepath) != temp.MD5))) ||
                        !File.Exists(filepath))
                    {
                        if (!File.Exists(filepath))
                        {
                            Log.AddLine(String.Format("{0}不存在,檢查最新版本", filepath), Colors.Black);
                        }
                        if (Private_Function.DownloadFile(temp.URL, Path.Combine(AppPath, temp.Path), String.Format("{0}需要更新,開始下載更新...", Path.GetFileName(temp.Path))))
                        {
                            Log.AddLine(String.Format("{0}更新完成", "\\mods" + temp.Path + Path.GetFileName(temp.Path)), Colors.Black);
                            if (Math.Round(((double)(haveUpdate + 1) / (double)totalCount), 2) - Math.Round((((double)haveUpdate / (double)totalCount)), 2) > 0.01)
                            {
                                haveUpdate++;
                                CrossThread_EditeLabelContent(label1, String.Format("{0}/{1}", haveUpdate, totalCount));
                                CrossThread_EditeProgressBar(progressBar, totalCount, 0, haveUpdate);
                            }
                            else
                            {
                                haveUpdate++;
                            }
                        }
                        else
                        {
                            Log.AddLine(String.Format("{0}更新失敗", "\\mods" + temp.Path + Path.GetFileName(temp.Path)), Colors.Red);
                        }
                    }
                    else
                    {
                        Log.AddLine(String.Format("{0}更新完成", "\\mods" + temp.Path + Path.GetFileName(temp.Path)), Colors.Black);
                        if (Math.Round(((double)(haveUpdate + 1) / (double)totalCount), 2) - Math.Round((((double)haveUpdate / (double)totalCount)), 2) > 0.01)
                        {
                            haveUpdate++;
                            CrossThread_EditeLabelContent(label1, String.Format("{0}/{1}", haveUpdate, totalCount));
                            CrossThread_EditeProgressBar(progressBar, totalCount, 0, haveUpdate);
                        }
                        else
                        {
                            haveUpdate++;
                        }
                    }
                }
                Log.AddLine("同步完成!", Colors.Green);
                haveUpdate = totalCount;
                CrossThread_EditeLabelContent(label1, String.Format("{0}/{1}", haveUpdate, totalCount));
                CrossThread_EditeProgressBar(progressBar, totalCount, 0, haveUpdate);
            }
            catch (Exception e) { Log.AddLine(String.Format("出現以下訊息:{0}", e.Message), Colors.Red); }

            if (AutoClose_AfterFinishd)
            {
                CrossThread_Close();
            }
            else
            {
                MessageBox.Show("同步完成");
            }
        }