/// <summary> /// 服务器mod文件夹发生改变 /// </summary> private void OnServerModsDirChanged(object source, FileSystemEventArgs e) { if (Global.ServerModsDirWatcherIsEnable) { Application.Current.Dispatcher.Invoke(() => { Global.ServerModsDirWatcherIsEnable = false; var dialogWindowWithButton = new DialogWindowWithButton("服务器mod文件夹发生变化,是否重新加载mod列表", DialogWindowWithButton.DialogButtons.OKCancel, true); dialogWindowWithButton.InitializeComponent(); dialogWindowWithButton.OKbuttonEvent += sender => sender.Result = true; dialogWindowWithButton.ShowDialog(); if ((bool?)dialogWindowWithButton.Result == true) { RefreshModButton_OnClick(null, null); } var timer = new System.Windows.Forms.Timer { Interval = 1000, Enabled = true }; timer.Tick += (sender, eventArgs) => { Global.ServerModsDirWatcherIsEnable = true; ((System.Windows.Forms.Timer)sender).Stop(); }; timer.Start(); }); } }
/// <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()); } }