-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontendBackend.cs
More file actions
92 lines (82 loc) · 3.51 KB
/
Copy pathFrontendBackend.cs
File metadata and controls
92 lines (82 loc) · 3.51 KB
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
/*
This file contains the code that allows the frontend to communicate with the backend.
This file was created by FloatingJacob for use with aMogusManager.
*/
namespace aMogusManager
{
class FrontendBackend : WebSocketBehavior
{
public static void startFrontendBackend()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[Info] Backend ready."); // Wheeeeeeee, logging.
Console.ForegroundColor = ConsoleColor.White;
WebSocketServer wssv = new("ws://localhost:6741");
wssv.AddWebSocketService<FrontendBackend>("/backend"); // It took me forever to figure out how to do this lol
wssv.Start();
}
protected override async void OnMessage(MessageEventArgs e)
{
string name;
string version;
int modID;
string modIDStr;
dynamic m = JObject.Parse(e.Data);
Console.WriteLine($"[Frontend] \"{m.action}\" requested");
switch ((string)m.action)
{
case "runMod":
modIDStr = m.modID;
if (string.IsNullOrEmpty(modIDStr) || !int.TryParse(modIDStr, out modID)) Send("Invalid Paramaters");
else { Program.runMod(modID); Send("gud"); }
break;
case "removeMod":
modIDStr = m.modID;
if (string.IsNullOrEmpty(modIDStr) || !int.TryParse(modIDStr, out modID)) Send("Invalid Paramaters");
else { await instanceStuffs.removeMod(modID); Send("gud"); }
break;
case "installModded":
name = m.name;
version = m.version;
string zipMod = m.zipMod;
Send(instanceStuffs.installMod(zipMod, name, version).Result);
break;
case "installPlugin":
modID = m.modID;
string zipPlugin = m.zipPlugin;
await instanceStuffs.installPlugin(modID, zipPlugin);
Send("gud");
break;
case "installVanilla":
name = m.name;
version = m.version;
Send(instanceStuffs.installVanilla(name, version).Result);
break;
case "changePath":
string newPath = m.newPath;
await Program.changePath(newPath);
Send("gud");
break;
case "mods":
Send(File.ReadAllText("mods.json"));
break;
case "versions":
Send(File.ReadAllText("versions.json"));
break;
case "cacheSize":
Send($"[{{\"cacheSize\":\"{instanceStuffs.cacheSize():F2} GB\"}}]"); // Why so literal?
break;
case "clearCache":
try { Directory.Delete("cache", true); }
catch (DirectoryNotFoundException) { } // Don't complain if there's NO cache
Send("cacheCleared");
break;
}
}
protected override void OnOpen()
{
Send("HELLO"); // Send a HELLO message to the frontend so it knows its connected.
Console.WriteLine("[INFO] Frontend connected!"); // Dramatic console text so you know the backend didn't die
}
}
}