-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGhostNetModule.cs
More file actions
245 lines (207 loc) · 8.58 KB
/
GhostNetModule.cs
File metadata and controls
245 lines (207 loc) · 8.58 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
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
using Celeste.Mod;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FMOD.Studio;
using Microsoft.Xna.Framework.Input;
namespace Celeste.Mod.Ghost.Net {
public class GhostNetModule : EverestModule {
public static GhostNetModule Instance;
public GhostNetModule() {
Instance = this;
}
public override Type SettingsType => typeof(GhostNetModuleSettings);
public static GhostNetModuleSettings Settings => (GhostNetModuleSettings) Instance._Settings;
public GhostNetServer Server;
public GhostNetClient Client;
public VirtualButton ButtonPlayerList;
public VirtualJoystick JoystickEmoteWheel;
public VirtualButton ButtonEmoteSend;
public VirtualButton ButtonChat;
private bool _StartServer;
private bool _StartHeadless;
public override void LoadSettings() {
base.LoadSettings();
if (Settings.EmoteFavs == null || Settings.EmoteFavs.Length == 0) {
Settings.EmoteFavs = new string[] {
"i:collectables/heartgem/0/spin",
"i:collectables/strawberry",
"Hi!",
"Too slow!",
"p:madeline/normal04",
"p:ghost/scoff03",
"p:theo/yolo03 theo/yolo02 theo/yolo01 theo/yolo02 END",
"p:granny/laugh",
};
}
}
public override void Load() {
Everest.Events.Input.OnInitialize += OnInputInitialize;
Everest.Events.Input.OnDeregister += OnInputDeregister;
GhostNetHooks.Load();
// Example of a MP server mod.
GhostNetServer.OnCreate += GhostNetRaceManager.OnCreateServer;
base.Initialize();
Queue<string> args = new Queue<string>(Everest.Args);
while (args.Count > 0) {
string arg = args.Dequeue();
if (arg == "--server") {
_StartServer = true;
} else if (arg == "--headless") {
_StartHeadless = true;
}
}
GhostModule.SettingsOverridden = true;
ResetGhostModuleSettings();
if (_StartServer && _StartHeadless) {
// We don't care about other mods.
GhostNetFrame.RegisterChunksFromModule(this);
Start(true, true);
RunDedicated();
Environment.Exit(0);
}
}
public override void Initialize() {
base.Initialize();
// Register after all mods have loaded.
foreach (EverestModule module in Everest.Modules)
GhostNetFrame.RegisterChunksFromModule(module);
if (_StartServer && !_StartHeadless) {
Start(true, true);
}
}
public override void Unload() {
Everest.Events.Input.OnInitialize -= OnInputInitialize;
Everest.Events.Input.OnDeregister -= OnInputDeregister;
Stop();
OnInputDeregister();
}
public override void CreateModMenuSection(TextMenu menu, bool inGame, EventInstance snapshot) {
base.CreateModMenuSection(menu, inGame, snapshot);
menu.Add(new TextMenu.Button("modoptions_ghostnetmodule_reloadhint".DialogCleanOrNull() ?? "More in ModSettings/...") {
Disabled = true
});
menu.Add(new TextMenu.Button("modoptions_ghostnetmodule_reload".DialogCleanOrNull() ?? "Reload Settings").Pressed(() => {
string server = Settings.Server;
LoadSettings();
if (Settings.Server != server)
Settings.Server = Settings._Server;
}));
}
public static void ResetGhostModuleSettings() {
string name = GhostModule.Settings.Name;
GhostModule.Instance._Settings = new GhostModuleSettings();
GhostModule.Settings.Mode = GhostModuleMode.Off;
GhostModule.Settings.Name = name;
GhostModule.Settings.NameFilter = "";
GhostModule.Settings.ShowNames = true;
GhostModule.Settings.ShowDeaths = true;
GhostModule.Settings.InnerOpacity = 8;
GhostModule.Settings.InnerHairOpacity = 8;
GhostModule.Settings.OuterOpacity = 8;
GhostModule.Settings.OuterHairOpacity = 8;
}
public void OnInputInitialize() {
ButtonPlayerList = new VirtualButton(
new VirtualButton.KeyboardKey(Keys.Tab),
new VirtualButton.PadButton(Input.Gamepad, Buttons.Back)
);
AddButtonsTo(ButtonPlayerList, Settings.ButtonPlayerList);
JoystickEmoteWheel = new VirtualJoystick(true,
new VirtualJoystick.PadRightStick(Input.Gamepad, 0.2f)
);
ButtonEmoteSend = new VirtualButton(
new VirtualButton.KeyboardKey(Keys.Q),
new VirtualButton.PadButton(Input.Gamepad, Buttons.RightStick)
);
AddButtonsTo(ButtonEmoteSend, Settings.ButtonEmoteSend);
ButtonChat = new VirtualButton(
new VirtualButton.KeyboardKey(Keys.T)
);
AddButtonsTo(ButtonEmoteSend, Settings.ButtonChat);
}
public void OnInputDeregister() {
ButtonPlayerList?.Deregister();
JoystickEmoteWheel?.Deregister();
ButtonEmoteSend?.Deregister();
ButtonChat?.Deregister();
}
private static void AddButtonsTo(VirtualButton vbtn, List<Buttons> buttons) {
if (buttons == null)
return;
foreach (Buttons button in buttons) {
if (button == Buttons.LeftTrigger) {
vbtn.Nodes.Add(new VirtualButton.PadLeftTrigger(Input.Gamepad, 0.25f));
} else if (button == Buttons.RightTrigger) {
vbtn.Nodes.Add(new VirtualButton.PadRightTrigger(Input.Gamepad, 0.25f));
} else {
vbtn.Nodes.Add(new VirtualButton.PadButton(Input.Gamepad, button));
}
}
}
public void Start(bool server = false, bool client = false) {
Stop();
if (Settings.IsHost || server) {
Server = new GhostNetServer(Celeste.Instance);
if (!_StartHeadless)
Celeste.Instance.Components.Add(Server);
Server.OPs.Add(0);
Server.Start();
GhostNetWatchdog.InitializeWatchdog();
}
if (!Settings.IsHost && server && !client)
return;
try {
Client = new GhostNetClient(Celeste.Instance);
if (!_StartHeadless)
Celeste.Instance.Components.Add(Client);
Client.Start();
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "ghostnet", "Failed starting client");
e.LogDetailed();
if (Settings.EnabledEntry != null) {
Settings.EnabledEntry.LeftPressed();
}
Stop();
}
}
public void Stop() {
if (Client != null) {
Client.Stop();
Client = null;
}
if (Server != null) {
Server.Stop();
Server = null;
GhostNetWatchdog.StopWatchdog();
}
}
public void RunDedicated() {
Logger.Log("ghostnet-s", "GhostNet headless server is online.");
Logger.Log("ghostnet-s", $"Make sure to forward the ports {Settings.Port} TCP and UDP");
Logger.Log("ghostnet-s", "and to let your firewall allow incoming connections.");
Console.WriteLine("");
Client.OnHandle += (con, frame) => {
if (frame.Get<ChunkMChat>() != null)
Logger.Log("ghostnet-chat", new GhostNetClient.ChatLine(frame).ToString());
};
while (Server.IsRunning) {
string line = Console.ReadLine();
if (string.IsNullOrWhiteSpace(line))
continue;
line = line.TrimEnd();
if (line == "/quit") {
Stop();
return;
}
Client.SendMChat(line);
}
}
}
}