forked from brianmiller/phvalheim-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhValheimPrep.cs
97 lines (79 loc) · 3.16 KB
/
PhValheimPrep.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
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace PhValheim.Prep
{
public class PhValheim
{
public static bool MakeDir(string inputDir, string desc)
{
inputDir = Environment.ExpandEnvironmentVariables(inputDir);
bool inputDirExists = System.IO.Directory.Exists(inputDir);
if (!inputDirExists)
{
Console.WriteLine(desc + " directory is missing, creating: " + inputDir);
try
{
Directory.CreateDirectory(inputDir);
return true;
}
catch
{
Console.WriteLine(desc + " directory could not be created, exiting...");
return false;
}
}
else
{
Console.WriteLine(desc + " directory was found: " + inputDir);
return true;
}
}
//ensure PhValheim's root dir exists, else create it.
public static bool PhValheimPrep()
{
if (!MakeDir(Platform.State.PhValheimDir, "PhValheim root"))
{
return false;
}
if (!MakeDir(Platform.State.PhValheimServerRoot, "World root"))
{
return false;
}
return true;
}
// write backenfile to bepinex directory. phvalheim-companion uses this file to send "Player" scene data to phvalheim-server's backend (e.g., world progression (boss heads hung at spawn point)
public static bool WriteBackendFile(string worldName, string phvalheimHostNoPort, string phvalheimURL)
{
string bepInExRoot = "";
string backendFile;
OSPlatform osPlatform = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? OSPlatform.Windows : OSPlatform.Linux;
if (osPlatform == OSPlatform.Windows)
{
bepInExRoot = Environment.ExpandEnvironmentVariables("%appdata%\\PhValheim\\worlds\\" + phvalheimHostNoPort + "\\" + worldName + "\\" + worldName + "\\BepInEx\\");
}
else if (osPlatform == OSPlatform.Linux)
{
bepInExRoot = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/.config/PhValheim/worlds/" + phvalheimHostNoPort + "/" + worldName + "/" + worldName + "/BepInEx/";
}
else
{
Console.WriteLine("Unsupported Operating System");
return false;
}
// set bepinex root directory
backendFile = Environment.ExpandEnvironmentVariables(bepInExRoot + "phvalheim.backend" );
// ensure the bepinex directory exists
if (!Directory.Exists(bepInExRoot))
{
Directory.CreateDirectory(bepInExRoot);
}
// write new backend file
using (StreamWriter outputFile = new StreamWriter(backendFile))
{
outputFile.Write(phvalheimURL);
}
return true;
}
}
}