示例#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("同步完成");
            }
        }
示例#2
0
        private void TextBlock_Drop(object sender, DragEventArgs e)
        {
            TextBlock textBlock = ((TextBlock)e.Source);

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                if (!((string[])e.Data.GetData(DataFormats.FileDrop))[0].StartsWith(AppDomain.CurrentDomain.BaseDirectory))
                {
                    MessageBox.Show("請將Minecraft_updater置於該目錄/檔案於同一目錄下\r\n詳情請查閱wiki");
                    return;
                }
                if (textBlock.Text.Trim() != "")
                {
                    switch (MessageBox.Show("不清除直接加入到最後?", "已經含有資料", MessageBoxButton.YesNoCancel))
                    {
                    case MessageBoxResult.No:
                        textBlock.Text = "";
                        break;

                    case MessageBoxResult.Yes:
                        break;

                    default:
                        return;
                    }
                }
                string[]      docPath = (string[])e.Data.GetData(DataFormats.FileDrop);
                int           basepathLength = AppDomain.CurrentDomain.BaseDirectory.Length;
                string        name, MD5, URL;
                StringBuilder sb = new StringBuilder(), sb2 = new StringBuilder(), sb3 = new StringBuilder();
                bool          addmodtodelete    = checkbox_addmodtodelete.IsChecked.Value;
                bool          addconfigtodelete = checkbox_addconfigtodelete.IsChecked.Value;
                int           TextblockIndex    = -1;
                if (textBlock.Equals(TextBlock1))
                {
                    TextblockIndex = 0;
                }
                else if (textBlock.Equals(TextBlock2))
                {
                    TextblockIndex = 1;
                }
                else if (textBlock.Equals(TextBlock3))
                {
                    TextblockIndex = 2;
                }
                foreach (string path in docPath)
                {
                    if (File.Exists(path))
                    {
                        name = path.Substring(basepathLength, path.Length - basepathLength);
                        MD5  = Private_Function.GetMD5(path);
                        URL  = textBox.Text + name;
                        int temp = name.IndexOfAny(Delimiter);
                        switch (TextblockIndex)
                        {
                        case 0:
                            sb.AppendLine(String.Format("{0}||{1}||{2}", name, MD5, URL));
                            if ((addmodtodelete && name.Contains("mod")) || (addconfigtodelete && name.Contains("config")))
                            {
                                sb2.AppendLine(String.Format("#{0}||{1}||", name.Substring(0, temp == -1 ? name.Length : temp), MD5));
                            }
                            break;

                        case 1:
                            sb2.AppendLine(String.Format("#{0}||{1}||", name.Substring(0, temp == -1 ? name.Length : temp), MD5));
                            break;

                        case 2:
                            sb3.AppendLine(String.Format(":{0}||{1}||{2}", name.Substring(0, temp == -1 ? name.Length : temp), MD5, URL));
                            break;
                        }
                    }
                    else if (Directory.Exists(path))
                    {
                        DirectoryInfo di = new DirectoryInfo(path);
                        foreach (var fi in di.EnumerateFiles("*", SearchOption.AllDirectories))
                        {
                            name = fi.FullName.Substring(basepathLength, fi.FullName.Length - basepathLength);
                            MD5  = Private_Function.GetMD5(fi.FullName);
                            URL  = textBox.Text + name.Replace("\\", "/");
                            int temp = name.IndexOfAny(Delimiter);
                            switch (TextblockIndex)
                            {
                            case 0:
                                sb.AppendLine(String.Format("{0}||{1}||{2}", name, MD5, URL));
                                if ((addmodtodelete && name.Contains("mod")) || (addconfigtodelete && name.Contains("config")))
                                {
                                    sb2.AppendLine(String.Format("#{0}||{1}||", name.Substring(0, temp == -1 ? name.Length : temp), MD5));
                                }
                                break;

                            case 1:
                                sb2.AppendLine(String.Format("#{0}||{1}||", name.Substring(0, temp == -1 ? name.Length : temp), MD5));
                                break;

                            case 2:
                                sb3.AppendLine(String.Format(":{0}||{1}||{2}", name.Substring(0, temp == -1 ? name.Length : temp), MD5, URL));
                                break;
                            }
                        }
                    }
                }
                TextBlock1.Text += (sb.ToString());
                TextBlock2.Text += (sb2.ToString());
                TextBlock3.Text += (sb3.ToString());
            }
        }