Skip to content

Commit 653239a

Browse files
author
Brian MacIntosh
committed
wizard is now better at locating the server exe
1 parent e867f96 commit 653239a

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

ValheimServerWizard/Form1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ private void ServerStart()
7676
}
7777

7878
string exePath = WizardHelpers.GetServerPath();
79+
80+
if (string.IsNullOrEmpty(exePath))
81+
{
82+
MessageBox.Show("'valheim_server.exe' could not be found. It must be installed using Steam.", "EXE Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
83+
return;
84+
}
85+
7986
string argsFormat = "-nographics -batchmode -name \"{0}\" -port {1} -world \"{2}\" -password \"{3}\" -public {4}";
8087

8188
logTextBox.Text = "";

ValheimServerWizard/WizardHelpers.cs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
47

58
namespace ValheimServerWizard
69
{
@@ -12,23 +15,80 @@ public static class WizardHelpers
1215
private static string s_serverPath;
1316

1417
/// <summary>
15-
/// Returns the full path to the server EXE.
18+
/// Returns all the Steam library folders on this computer.
1619
/// </summary>
17-
public static string GetServerPath()
20+
public static IEnumerable<string> FindSteamLibraryFolders()
1821
{
19-
if (string.IsNullOrEmpty(s_serverPath))
22+
// try to find the Steam folder
23+
foreach (string drive in Directory.GetLogicalDrives())
2024
{
21-
// try to find the EXE
22-
foreach (string drive in Directory.GetLogicalDrives())
25+
string testSteamappsPath;
26+
string testFoldersFile;
27+
28+
testSteamappsPath = Path.Combine(drive, @"Program Files\Steam\steamapps");
29+
if (Directory.Exists(testSteamappsPath))
2330
{
24-
string testPath = Path.Combine(drive, @"SteamLibrary\steamapps\common\Valheim dedicated server\valheim_server.exe");
25-
if (File.Exists(testPath))
31+
yield return testSteamappsPath;
32+
33+
testFoldersFile = Path.Combine(testSteamappsPath, "libraryfolders.vdf");
34+
if (File.Exists(testFoldersFile))
2635
{
27-
s_serverPath = testPath;
28-
break;
36+
foreach (string externalLibrary in ReadLibraryFolders(testFoldersFile))
37+
{
38+
yield return externalLibrary;
39+
}
2940
}
41+
}
3042

31-
testPath = Path.Combine(drive, @"Program Files (x86)\Steam\steamapps\common\valheim_server.exe");
43+
testSteamappsPath = Path.Combine(drive, @"Program Files (x86)\Steam\steamapps");
44+
if (Directory.Exists(testSteamappsPath))
45+
{
46+
yield return testSteamappsPath;
47+
48+
testFoldersFile = Path.Combine(testSteamappsPath, "libraryfolders.vdf");
49+
if (File.Exists(testFoldersFile))
50+
{
51+
foreach (string externalLibrary in ReadLibraryFolders(testFoldersFile))
52+
{
53+
yield return externalLibrary;
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Returns all the external Steam library folders identified in the specified file.
62+
/// </summary>
63+
public static IEnumerable<string> ReadLibraryFolders(string sourceFile)
64+
{
65+
Regex lineRegex = new Regex("^\\s*\"[0-9]+\"\\s*\"(.+)\"$");
66+
67+
using (StreamReader reader = new StreamReader(new FileStream(sourceFile, FileMode.Open, FileAccess.Read), Encoding.UTF8))
68+
{
69+
while (!reader.EndOfStream)
70+
{
71+
string line = reader.ReadLine().Trim();
72+
Match match = lineRegex.Match(line);
73+
if (match.Success)
74+
{
75+
//HACK: it's not really a regex
76+
yield return Path.Combine(Regex.Unescape(match.Groups[1].Value), "steamapps");
77+
}
78+
}
79+
}
80+
}
81+
82+
/// <summary>
83+
/// Returns the full path to the server EXE.
84+
/// </summary>
85+
public static string GetServerPath()
86+
{
87+
if (string.IsNullOrEmpty(s_serverPath))
88+
{
89+
foreach (string library in FindSteamLibraryFolders())
90+
{
91+
string testPath = Path.Combine(library, @"common\Valheim dedicated server\valheim_server.exe");
3292
if (File.Exists(testPath))
3393
{
3494
s_serverPath = testPath;

0 commit comments

Comments
 (0)