-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathServerDetailsWindow.xaml.cs
931 lines (887 loc) · 35.4 KB
/
ServerDetailsWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
//using RazzLogging;
using RazzTools;
namespace ValheimServerWarden
{
/// <summary>
/// Interaction logic for ServerDetailsWindow.xaml
/// </summary>
public partial class ServerDetailsWindow : Window
{
//public event EventHandler<ServerEventArgs> Starting;
//public event EventHandler<ServerEventArgs> Stopping;
public event EventHandler<ServerEventArgs> EditedServer;
public event EventHandler<ServerEventArgs> ShowLog;
private ValheimServer _server;
private string _steamPath;
private System.Timers.Timer resourceTimer;
public ValheimServer Server
{
get
{
return this._server;
}
}
public ServerDetailsWindow(ValheimServer server)
{
InitializeComponent();
this._server = server;
txtServerLog.Document.Blocks.Clear();
foreach (var installmethod in Enum.GetValues(typeof(ValheimServer.ServerInstallMethod)))
{
cmbServerType.Items.Add(installmethod);
}
resourceTimer = new();
resourceTimer.Interval = 10000;
resourceTimer.AutoReset = true;
resourceTimer.Elapsed += ResourceTimer_Elapsed;
if (Server.Status == ValheimServer.ServerStatus.Running)
{
UpdateServerResourcesUsed();
resourceTimer.Enabled = true;
}
RefreshControls();
attachServerEventHandlers();
ServerToControls();
_steamPath = null;
try
{
string filePath = @"Program Files (x86)\Steam\steam.exe";
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
string testpath = $@"{drive.Name}{filePath}";
if (File.Exists(testpath))
{
_steamPath = testpath;
RefreshControls();
break;
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Error searching for Steam path");
Debug.WriteLine(ex);
}
LoadLists();
foreach (var entry in Server.LogEntries)
{
if (txtServerLog.Document.Blocks.Count > 0)
{
txtServerLog.Document.Blocks.InsertBefore(txtServerLog.Document.Blocks.FirstBlock, (Paragraph)entry);
}
else
{
txtServerLog.Document.Blocks.Add((Paragraph)entry);
}
}
cmbPriority.Items.Add(ProcessPriorityClass.Normal);
cmbPriority.Items.Add(ProcessPriorityClass.AboveNormal);
cmbPriority.Items.Add(ProcessPriorityClass.High);
}
public void RefreshControls()
{
this.Dispatcher.Invoke(() =>
{
try
{
Title = $"{this.Server.DisplayName}";
lblPlayerCount.Content = this.Server.PlayerCount;
dgPlayers.ItemsSource = this.Server.Players;
dgPlayers.Items.Refresh();
ValheimServer.ServerStatus status = this.Server.Status;
lblStatus.Content = status;
if (Server.StartTime.Equals(new DateTime()))
{
lblStartTime.Content = "N/A";
}
else
{
lblStartTime.Content = Server.StartTime.ToString();
}
btnStart.Visibility = Visibility.Collapsed;
btnStop.Visibility = Visibility.Collapsed;
btnWorking.Visibility = Visibility.Collapsed;
btnuModInstall.IsEnabled = false;
btnuModUpdate.IsEnabled = false;
menuConnectPlay.Visibility = Visibility.Collapsed;
menuConnectCheckExternal.Visibility = Visibility.Collapsed;
if (status == ValheimServer.ServerStatus.Running)
{
btnStop.Visibility = Visibility.Visible;
if (_steamPath != null)
{
menuConnectPlay.Visibility = Visibility.Visible;
}
menuConnectCheckExternal.Visibility = Visibility.Visible;
menuSteamCmdUpdate.Visibility = Visibility.Collapsed;
}
else if (status == ValheimServer.ServerStatus.Stopped)
{
btnStart.Visibility = Visibility.Visible;
menuSteamCmdUpdate.Visibility = Visibility.Visible;
btnuModInstall.IsEnabled = true;
btnuModUpdate.IsEnabled = true;
}
else
{
btnWorking.Visibility = Visibility.Visible;
if (status == ValheimServer.ServerStatus.Starting)
{
btnWorking.ToolTip = "Starting...";
}
else if (status == ValheimServer.ServerStatus.Stopping)
{
btnWorking.ToolTip = "Stopping...";
}
else if (status == ValheimServer.ServerStatus.Updating)
{
btnWorking.ToolTip = "Updating...";
}
menuSteamCmdUpdate.Visibility = Visibility.Collapsed;
}
//btnLog.IsEnabled = (File.Exists(Server.LogRawName));
btnLog.Visibility = (File.Exists(Server.LogRawName)) ? Visibility.Visible : Visibility.Collapsed;
if (Properties.Settings.Default.SteamCMDPath != null && Properties.Settings.Default.SteamCMDPath.Length > 0 && File.Exists(Properties.Settings.Default.SteamCMDPath) && Server.InstallMethod == ValheimServer.ServerInstallMethod.SteamCMD)
{
btnSteamCmd.Visibility = Visibility.Visible;
chkUpdateOnRestart.Visibility = Visibility.Visible;
gridAutoUpdate.Visibility = Visibility.Visible;
}
else
{
btnSteamCmd.Visibility = Visibility.Collapsed;
chkUpdateOnRestart.Visibility = Visibility.Collapsed;
gridAutoUpdate.Visibility = Visibility.Collapsed;
}
if (uMod.AgentInstalled)
{
tabuMod.Visibility = Visibility.Visible;
}
else
{
tabuMod.Visibility = Visibility.Collapsed;
}
}
catch (Exception ex)
{
LogMessage($"Error refreshing controls: {ex.Message}", LogEntryType.Error);
}
});
}
private void RefreshControls(object sender, EventArgs e)
{
RefreshControls();
}
private void RefreshControls(object sender, ServerStoppedEventArgs e)
{
RefreshControls();
}
private void RefreshControls(object sender, PlayerEventArgs e)
{
RefreshControls();
}
private void RefreshControls(object sender, ServerErrorEventArgs e)
{
RefreshControls();
}
private void attachServerEventHandlers()
{
Server.LoggedMessage += ((object sender, LoggedMessageEventArgs e) => {
this.Dispatcher.Invoke(() =>
{
LogMessage(e.LogEntry);
});
});
Server.Stopped += ((sender, e) => {
RefreshControls();
UpdateServerResourcesUsed();
resourceTimer.Enabled = false;
});
Server.PlayerConnected += RefreshControls;
Server.PlayerDisconnected += RefreshControls;
Server.PlayerDied += RefreshControls;
Server.FailedPassword += RefreshControls;
Server.Starting += RefreshControls;
Server.Stopping += RefreshControls;
Server.Started += ((sender, e) => {
RefreshControls();
UpdateServerResourcesUsed();
resourceTimer.Enabled = true;
});
Server.StartFailed += RefreshControls;
Server.StopFailed += RefreshControls;
Server.Updated += ((object sender, UpdatedEventArgs e) =>
{
RefreshControls();
ServerToControls();
});
}
private void ResourceTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
UpdateServerResourcesUsed();
}
private void ServerToControls()
{
try
{
txtName.Text = Server.Name;
txtPort.Text = Server.Port.ToString();
txtWorld.Text = Server.World;
txtPassword.Text = Server.Password;
txtSaveDir.Text = Server.SaveDir;
chkPublic.IsChecked = Server.Public;
if (Server.InstallPath != null)
{
txtServerDir.Text = Server.InstallPath;
}
else if (Properties.Settings.Default.ServerFilePath != null)
{
txtServerDir.Text = Properties.Settings.Default.ServerFilePath;
}
cmbServerType.SelectedIndex = (int)Server.InstallMethod;
chkAutostart.IsChecked = Server.Autostart;
chkRawLog.IsChecked = Server.RawLog;
if (Server.RestartHours > 0)
{
chkAutoRestart.IsChecked = true;
txtRestartInterval.Text = Server.RestartHours.ToString();
txtRestartInterval.IsEnabled = true;
}
else
{
chkAutoRestart.IsChecked = false;
txtRestartInterval.IsEnabled = false;
}
chkUpdateOnRestart.IsChecked = Server.UpdateOnRestart;
if (Server.UpdateCheckMinutes > 0)
{
chkAutoUpdate.IsChecked = true;
txtUpdateCheckInterval.Text = Server.UpdateCheckMinutes.ToString();
txtUpdateCheckInterval.IsEnabled = true;
}
cmbPriority.SelectedItem = Server.ProcessPriority;
chkuModUpdate.IsChecked = Server.AutoUpdateuMod;
}
catch (Exception ex)
{
LogMessage($"Error reading server settings: {ex.Message}", LogEntryType.Error);
}
}
private void UpdateServerResourcesUsed()
{
this.Dispatcher.Invoke(() =>
{
lblMemory.Content = Server.MemoryUsed.ToString("N0") + " MB";
});
}
private void LoadLists()
{
try
{
string savedir;
if (Server.SaveDir.Length > 0)
{
savedir = Server.SaveDir;
}
else
{
savedir = ValheimServer.DefaultSaveDir;
}
txtAdminList.Text = "";
txtBannedList.Text = "";
txtPermittedList.Text = "";
if (File.Exists(savedir+"\\adminlist.txt"))
{
txtAdminList.Text = File.ReadAllText(savedir + "\\adminlist.txt");
}
if (File.Exists(savedir + "\\bannedlist.txt"))
{
txtBannedList.Text = File.ReadAllText(savedir + "\\bannedlist.txt");
}
if (File.Exists(savedir + "\\permittedlist.txt"))
{
txtPermittedList.Text = File.ReadAllText(savedir + "\\permittedlist.txt");
}
}
catch (Exception ex)
{
LogMessage($"Error loading admin/banned/permitted list: {ex.Message}");
}
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
//btnStart.IsEnabled = false;
//btnStart.Content = FindResource("StartGrey");
//OnStarting(new ServerEventArgs(this.Server));
Server.Start();
}
/*private void OnStarting(ServerEventArgs args)
{
EventHandler<ServerEventArgs> handler = Starting;
if (null != handler) handler(this, args);
}
private void OnStopping(ServerEventArgs args)
{
EventHandler<ServerEventArgs> handler = Stopping;
if (null != handler) handler(this, args);
}*/
private void OnShowLog(ServerEventArgs args)
{
EventHandler<ServerEventArgs> handler = ShowLog;
if (null != handler) handler(this, args);
}
private void OnEditedServer(ServerEventArgs args)
{
EventHandler<ServerEventArgs> handler = EditedServer;
if (null != handler) handler(this, args);
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
//btnStop.IsEnabled = false;
//btnStop.Content = FindResource("StopGrey");
//OnStopping(new ServerEventArgs(this.Server));
Server.Stop();
}
private void btnLog_Click(object sender, RoutedEventArgs e)
{
OnShowLog(new ServerEventArgs(this.Server));
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (txtName.Text.Length > 0)
{
Server.Name = txtName.Text;
Title = Server.DisplayName;
} else
{
txtName.Text = Server.Name;
}
int port;
if (int.TryParse(txtPort.Text, out port))
{
Server.Port = port;
} else
{
txtPort.Text = Server.Port.ToString();
}
if (txtWorld.Text.Length > 0)
{
Server.World = txtWorld.Text;
} else
{
txtWorld.Text = Server.World;
}
if (txtPassword.Text.Length == 0) {
LogMessage("Warning: Servers must have passwords unless modded to remove that requirement.");
}
else if (txtPassword.Text.Length >= 5)
{
if (!Server.World.Contains(txtPassword.Text))
{
Server.Password = txtPassword.Text;
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Passwords must be at least 5 characters, and cannot be contained in your world name.", "Invalid Password", MessageBoxButton.OK, MessageBoxImage.Warning);
txtPassword.Text = Server.Password;
}
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Passwords must be at least 5 characters, and cannot be contained in your world name.", "Invalid Password", MessageBoxButton.OK, MessageBoxImage.Warning);
txtPassword.Text = Server.Password;
}
Server.Password = txtPassword.Text;
Server.SaveDir = txtSaveDir.Text;
Server.Public = chkPublic.IsChecked.GetValueOrDefault();
Server.InstallPath = txtServerDir.Text;
Server.InstallMethod = (ValheimServer.ServerInstallMethod)cmbServerType.SelectedIndex;
Server.Autostart = chkAutostart.IsChecked.GetValueOrDefault();
Server.RawLog = chkRawLog.IsChecked.GetValueOrDefault();
Server.ProcessPriority = (ProcessPriorityClass)cmbPriority.SelectedItem;
int restartHours = Server.RestartHours;
if (chkAutoRestart.IsChecked.GetValueOrDefault())
{
int.TryParse(txtRestartInterval.Text, out restartHours);
}
else
{
restartHours = 0;
}
if (restartHours > -1)
{
Server.RestartHours = restartHours;
}
if (restartHours == 0)
{
txtRestartInterval.Text = "";
chkAutoRestart.IsChecked = false;
}
Server.UpdateOnRestart = chkUpdateOnRestart.IsChecked.GetValueOrDefault();
int updateCheckMinutes = Server.UpdateCheckMinutes;
if (chkAutoUpdate.IsChecked.GetValueOrDefault())
{
int.TryParse(txtUpdateCheckInterval.Text, out updateCheckMinutes);
}
else
{
updateCheckMinutes = 0;
}
if (updateCheckMinutes > -1)
{
Server.UpdateCheckMinutes = updateCheckMinutes;
}
if (updateCheckMinutes == 0)
{
txtUpdateCheckInterval.Text = "";
chkAutoUpdate.IsChecked = false;
}
OnEditedServer(new ServerEventArgs(this.Server));
RefreshControls();
if (Server.Running)
{
var mmb = new ModernMessageBox(this);
mmb.Show("You must restart the server for the server name, port, world, password, save folder, or public status to change.", "Server Restart", MessageBoxButton.OK, MessageBoxImage.Information);
}
LoadLists();
}
private void btnSaveDir_Click(object sender, RoutedEventArgs e)
{
string saveDirPath = txtSaveDir.Text;
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
openFolderDialog.SelectedPath = saveDirPath;
openFolderDialog.UseDescriptionForTitle = true;
openFolderDialog.Description = "Select save folder";
System.Windows.Forms.DialogResult result = openFolderDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string folderName = openFolderDialog.SelectedPath;
if (folderName.Equals(saveDirPath))
{
return;
}
/*if (!Directory.Exists($@"{folderName}\worlds"))
{
var mmb = new ModernMessageBox(this);
mmb.Show("Please select the folder where your Valheim save files are located. This folder should contain a \"worlds\" folder.",
"Invalid Folder", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
return;
}*/
txtSaveDir.Text = folderName;
}
RefreshControls();
}
private void menuSaveDirReset_Click(object sender, RoutedEventArgs e)
{
txtSaveDir.Text = "";
}
private void dgPlayers_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
menuCopySteamId.IsEnabled = (dgPlayers.SelectedIndex != -1);
}
private void menuCopySteamId_Click(object sender, RoutedEventArgs e)
{
Player player = (Player)dgPlayers.SelectedItem;
Clipboard.SetText(player.SteamID);
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
menuConnect.IsOpen = true;
}
private void menuConnectLink_Click(object sender, RoutedEventArgs e)
{
// also test steam://run/892970//%2Bconnect%20{_externalIP}%3A{this.Server.Port}
Clipboard.SetText($"steam://connect/{ValheimServer.ExternalIP}:{this.Server.Port + 1}");
}
private void chkAutoRestart_Checked(object sender, RoutedEventArgs e)
{
txtRestartInterval.IsEnabled = chkAutoRestart.IsChecked.GetValueOrDefault();
if (chkAutoRestart.IsChecked.GetValueOrDefault() && txtRestartInterval.Text == "")
{
txtRestartInterval.Text = "4";
}
}
private void btnDiscordWebhook_Click(object sender, RoutedEventArgs e)
{
var webhookWin = new DiscordWebhookWindow(this.Server);
webhookWin.ShowDialog();
}
private void menuConnectCheckExternal_Click(object sender, RoutedEventArgs e)
{
//Process.Start("cmd", $"/C start https://southnode.net/form_get.php?ip={_externalIP}");
Process.Start("cmd", $"/C start https://geekstrom.de/valheim/check/?host={WebUtility.UrlEncode($"{ValheimServer.ExternalIP}:{Server.Port + 1}")}");
}
private void btnSteamCmd_Click(object sender, RoutedEventArgs e)
{
btnSteamCmd.ContextMenu.IsOpen = true;
}
private void menuSteamCmdUpdate_Click(object sender, RoutedEventArgs e)
{
if (!Server.Running)
{
Server.Update();
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Please stop the server before updating.", "Stop Server to Update", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
}
}
public void LogMessage(string msg)
{
LogMessage(msg, LogEntryType.Normal);
}
public void LogMessage(string msg, LogEntryType lt)
{
LogMessage(new LogEntry(msg, lt));
}
public void LogMessage(LogEntry entry)
{
try
{
this.Dispatcher.Invoke(() =>
{
if (txtServerLog.Document.Blocks.Count > 0)
{
txtServerLog.Document.Blocks.InsertBefore(txtServerLog.Document.Blocks.FirstBlock, (Paragraph)entry);
}
else
{
txtServerLog.Document.Blocks.Add((Paragraph)entry);
}
});
}
catch (Exception ex)
{
Debug.WriteLine($"Error logging message: {ex.Message}");
}
}
private void Window_ContentRendered(object sender, EventArgs e)
{
/*tabsServer.SelectedIndex = 1;
UpdateLayout();
SizeToContent = SizeToContent.Manual;
tabsServer.SelectedIndex = 0;*/
}
private void btnServerDir_Click(object sender, RoutedEventArgs e)
{
var openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (txtServerDir.Text != "")
{
var serverpath = new FileInfo(txtServerDir.Text).Directory.FullName;
if (Directory.Exists(serverpath))
{
openFolderDialog.SelectedPath = serverpath;
}
}
openFolderDialog.UseDescriptionForTitle = true;
openFolderDialog.Description = "Server installation folder";
var result = openFolderDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
var folderName = openFolderDialog.SelectedPath;
/*if (folderName.Equals(serverpath))
{
return;
}*/
if (!File.Exists($@"{folderName}\{ValheimServer.ExecutableName}") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
{
var mmb = new ModernMessageBox(this);
var install = mmb.Show($"{ValheimServer.ExecutableName} was not found in this folder, do you want to install it via SteamCMD?",
"Install Valheim dedicated server?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (install == MessageBoxResult.Yes)
{
try
{
var process = new Process();
process.StartInfo.FileName = Properties.Settings.Default.SteamCMDPath;
process.StartInfo.Arguments = $"+login anonymous +force_install_dir \"{folderName}\" +app_update {ValheimServer.SteamID} +quit";
//process.EnableRaisingEvents = true;
//process.Exited += SteamCmdProcess_Exited;
process.Start();
process.WaitForExit();
}
catch (Exception ex)
{
LogMessage($"Error installing dedicated server: {ex.Message}", LogEntryType.Error);
}
}
}
folderName += $@"\{ValheimServer.ExecutableName}";
txtServerDir.Text = folderName;
}
RefreshControls();
}
private void menuServerDirReset_Click(object sender, RoutedEventArgs e)
{
txtServerDir.Text = Properties.Settings.Default.ServerFilePath;
}
private void txtServerDir_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
if (!File.Exists(txtServerDir.Text) && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
{
menuServerDirInstall.IsEnabled = true;
menuServerDirInstall.Visibility = Visibility.Visible;
}
else
{
menuServerDirInstall.IsEnabled = false;
menuServerDirInstall.Visibility = Visibility.Collapsed;
}
}
private void menuLogClear_Click(object sender, RoutedEventArgs e)
{
txtServerLog.Document.Blocks.Clear();
Server.LogEntries.Clear();
}
private void menuLogSelectAll_Click(object sender, RoutedEventArgs e)
{
txtServerLog.SelectAll();
}
private void menuLog_Opened(object sender, RoutedEventArgs e)
{
if (txtServerLog.Selection.IsEmpty)
{
menuLogCopy.Visibility = Visibility.Collapsed;
}
else
{
menuLogCopy.Visibility = Visibility.Visible;
}
}
private void menuLogCopy_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(txtServerLog.Selection.Text);
}
private void btnSaveAdminList_Click(object sender, RoutedEventArgs e)
{
try
{
string savedir;
if (Server.SaveDir.Length > 0)
{
savedir = Server.SaveDir;
}
else
{
savedir = ValheimServer.DefaultSaveDir;
}
File.WriteAllTextAsync(savedir + "\\adminlist.txt", txtAdminList.Text);
LogMessage("Admin list updated.", LogEntryType.Success);
}
catch (Exception ex)
{
LogMessage($"Error updating admin list: {ex.Message}", LogEntryType.Error);
}
}
private void btnSaveBannedList_Click(object sender, RoutedEventArgs e)
{
try
{
string savedir;
if (Server.SaveDir.Length > 0)
{
savedir = Server.SaveDir;
}
else
{
savedir = ValheimServer.DefaultSaveDir;
}
File.WriteAllTextAsync(savedir + "\\bannedlist.txt", txtBannedList.Text);
LogMessage("Banned list updated.", LogEntryType.Success);
}
catch (Exception ex)
{
LogMessage($"Error updating banned list: {ex.Message}", LogEntryType.Error);
}
}
private void btnSavePermittedList_Click(object sender, RoutedEventArgs e)
{
try
{
string savedir;
if (Server.SaveDir.Length > 0)
{
savedir = Server.SaveDir;
}
else
{
savedir = ValheimServer.DefaultSaveDir;
}
File.WriteAllTextAsync(savedir + "\\permittedlist.txt", txtPermittedList.Text);
LogMessage("Permitted list updated.", LogEntryType.Success);
}
catch (Exception ex)
{
LogMessage($"Error updating permitted list: {ex.Message}", LogEntryType.Error);
}
}
public void ThemeUpdated()
{
txtServerLog.Document.Blocks.Clear();
foreach (var entry in Server.LogEntries)
{
if (txtServerLog.Document.Blocks.Count > 0)
{
txtServerLog.Document.Blocks.InsertBefore(txtServerLog.Document.Blocks.FirstBlock, (Block)entry);
}
else
{
txtServerLog.Document.Blocks.Add((Block)entry);
}
}
}
private void menuSteamCmdCheckUpdate_Click(object sender, RoutedEventArgs e)
{
Server.CheckForUpdate(true);
}
private void chkAutoUpdate_Checked(object sender, RoutedEventArgs e)
{
txtUpdateCheckInterval.IsEnabled = chkAutoUpdate.IsChecked.GetValueOrDefault();
if (chkAutoUpdate.IsChecked.GetValueOrDefault() && txtUpdateCheckInterval.Text == "")
{
txtUpdateCheckInterval.Text = "20";
}
}
private void btnuModUpdate_Click(object sender, RoutedEventArgs e)
{
if (Server.Status == ValheimServer.ServerStatus.Stopped)
{
this.Dispatcher.Invoke(() =>
{
btnuModUpdate.IsEnabled = false;
});
var umod = new uMod(Server.InstallPath, "valheim");
umod.UpdateEnded += Umod_UpdateEnded;
umod.LoggedMessage += (object sender, LoggedMessageEventArgs args) =>
{
LogMessage("uMod: " + args.LogEntry.Message, args.LogEntry.Type);
};
umod.Update("core apps extensions");
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Stop server before updating.", "Server Running", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Umod_UpdateEnded(object sender, uMod.ProcessEndedEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
btnuModUpdate.IsEnabled = true;
if (e.ExitCode == 0)
{
var mmb = new ModernMessageBox(this);
mmb.Show("uMod update compelete.", "uMod Updated", MessageBoxButton.OK, MessageBoxImage.Information);
}
else if (e.ExitCode == 2)
{
var mmb = new ModernMessageBox(this);
mmb.Show($"uMod did not need to install any updates.", "No Update Needed", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show($"uMod terminated with code {e.ExitCode}; unable to update uMod core or uMod apps.", "uMod Update Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
}
private void btnuModInstall_Click(object sender, RoutedEventArgs e)
{
if (Server.Status == ValheimServer.ServerStatus.Stopped)
{
this.Dispatcher.Invoke(() =>
{
btnuModInstall.IsEnabled = false;
});
var umod = new uMod(Server.InstallPath, "valheim");
umod.InstallEnded += Umod_InstallEnded;
umod.LoggedMessage += (object sender, LoggedMessageEventArgs args) =>
{
LogMessage("uMod: "+args.LogEntry.Message, args.LogEntry.Type);
};
umod.Install("umod");
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show("Stop server before updating.", "Server Running", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Umod_InstallEnded(object sender, uMod.ProcessEndedEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
btnuModInstall.IsEnabled = true;
if (e.ExitCode == 0)
{
var mmb = new ModernMessageBox(this);
mmb.Show("uMod installation compelete.", "uMod Installed", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
var mmb = new ModernMessageBox(this);
mmb.Show($"uMod terminated with code {e.ExitCode}, which indicates an error.", "uMod Installation Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
});
}
private void chkuModUpdate_Checked(object sender, RoutedEventArgs e)
{
Server.AutoUpdateuMod = chkuModUpdate.IsChecked.GetValueOrDefault();
}
private void menuConnectPlay_Click(object sender, RoutedEventArgs e)
{
try
{
if (_steamPath != null)
{
//Process.Start(_steamPath, $"steam://connect/127.0.0.1:{this.Server.Port + 1}");
Process.Start(_steamPath, $"-applaunch 892970 +connect 127.0.0.1:{this.Server.Port} +password \"{this.Server.Password}\"");
}
else
{
Debug.WriteLine("Steam path not found");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
public class ServerEventArgs : EventArgs
{
private readonly ValheimServer _server;
public ServerEventArgs(ValheimServer server)
{
_server = server;
}
public ValheimServer Server
{
get { return _server; }
}
}
}