11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Runtime . InteropServices ;
5+ using System . Text ;
6+ using System . Text . RegularExpressions ;
47
58namespace 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