Exemple #1
0
        /// <summary>
        /// 从创意工坊添加mod
        /// </summary>
        private async void AddModButton_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                Global.ServerModsDirWatcherIsEnable = false;
                var tempPath = Environment.CurrentDirectory + @"\Temp\";
                if (Directory.Exists(tempPath))
                {
                    Directory.Delete(tempPath, true);
                }
                Directory.CreateDirectory(tempPath);

                var dialogWindowWithButton = new DialogWindowWithButton("请输入mod ID", DialogWindowWithButton.DialogButtons.OKCancel);
                dialogWindowWithButton.InitializeComponent();
                dialogWindowWithButton.OKbuttonEvent += Sender =>
                {
                    var modIdString = Sender.InputTextBox.Text;
                    if (modIdString.Length >= 9 && modIdString.Length <= 10)
                    {
                        double.TryParse(modIdString, out var modId);
                        Sender.Result = modId;
                    }
                    else
                    {
                        Sender.Result = (double)0;
                    }
                };
                dialogWindowWithButton.ShowDialog();
                var modIdFromResult = (double)dialogWindowWithButton.Result;
                if (modIdFromResult == 0)
                {
                    Debug.WriteLine("无效的mod ID");
                    return;
                }
                var modDirName = CommonPath.ServerModsDirPath + "\\workshop-" + modIdFromResult;

                var dialogWindow = new DialogWindow(modIdFromResult + "下载中")
                {
                    Owner = Application.Current.MainWindow
                };
                dialogWindow.InitializeComponent();
                MainGrid.IsEnabled  = false;
                dialogWindow.Width  = 450;
                dialogWindow.Height = 350;
                dialogWindow.DialogWindowCanvas.Width  = 430;
                dialogWindow.DialogWindowCanvas.Height = 340;
                dialogWindow.Show();
                await Task.Run(() =>
                {
                    try
                    {
                        // 下载
                        var modDownloadObject = ModDownloadHelper.DownloadModFromId(modIdFromResult.ToString(CultureInfo.InvariantCulture));
                        ModDownloadHelper.DownloadModFile(modDownloadObject);
                        // 解压
                        ZipFile.ExtractToDirectory(@".\Temp\ModUpdate\workshop-" + modIdFromResult + ".zip",
                                                   modDirName + ".tmp");
                        if (Directory.Exists(modDirName))
                        {
                            Directory.Delete(modDirName, true);
                        }
                        new FileInfo(modDirName + ".tmp").MoveTo(modDirName);
                    }
                    catch
                    {
                        // ignored
                    }
                });

                MainGrid.IsEnabled = true;
                dialogWindow.Close();
                RefreshModButton_OnClick(null, null);
                Global.ServerModsDirWatcherIsEnable = true;
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.ToString());
            }
        }