-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedM.cs
380 lines (334 loc) · 15.1 KB
/
RedM.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using WindowsGSM.Functions;
using WindowsGSM.GameServer.Query;
using System;
using System.Linq;
using System.IO.Compression;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace WindowsGSM.Plugins
{
public class REDM
{
// - Plugin Details
public Plugin Plugin = new Plugin
{
name = "WindowsGSM.RedM", // WindowsGSM.XXXX
author = "ByBlackDeath",
description = "WindowsGSM plugin for supporting Red Dead Redemption 2 Dedicated Server",
version = "1.0.2",
url = "https://github.com/IOxee/WindowsGSM.RedM", // Github repository link (Best practice)
color = "#fa010c" // Color Hex
};
public static string GenerateShortUUID()
{
Guid guid = Guid.NewGuid();
string shortUUID = guid.ToString("N").Substring(0, 8);
return shortUUID;
}
private string GetTxAdminPort(int serverId)
{
/*
30120 + 10000 + 1 (serverId) = 40121
30120 + 10000 + 2 (serverId) = 40122
*/
return (int.Parse(Port) + txAdminPortIncrement + serverId).ToString();
}
// - Standard Constructor and properties
public REDM(ServerConfig serverData)
{
_serverData = serverData;
Additional = $"+set serverProfile \"{GenerateShortUUID()}\" +set txAdminVerbose true "; // I DO NOT ADD ANYTHING HERE OR DELETE THIS LINE
}
private readonly ServerConfig _serverData;
public string Error, Notice;
// - Game server Fixed variables
public string StartPath => @"server\FXServer.exe"; // Game server start path
public string FullName = "Red Dead Redemption 2 Server"; // Game server FullName
public bool AllowsEmbedConsole = true; // Does this server support output redirect?
public int PortIncrements = 1; // This tells WindowsGSM how many ports should skip after installation
public object QueryMethod = new FIVEM(); // Query method should be use on current server type. Accepted value: null or new A2S() or new FIVEM() or new UT3()
// - Game server default values
public string Port = "30120"; // Default port
public int txAdminPortIncrement = 10000;
public string QueryPort = "30120"; // Default query port
public string Defaultmap = "redm-map-one"; // Default map name
public string Maxplayers = "32"; // Default maxplayers
public string Additional { get; set; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public async void CreateServerCFG() {}
public async Task<string> GetCacheDirectory()
{
string serverDataPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID);
string[] directories = Directory.GetDirectories(serverDataPath);
foreach (string directory in directories)
{
if (Directory.Exists(Path.Combine(directory, "cache")))
{
return Path.Combine(directory, "cache");
}
}
return null;
}
public async Task<Process> Start()
{
string cachePath = await GetCacheDirectory();
if (cachePath != null)
if (Directory.Exists(cachePath))
Directory.Delete(cachePath, true);
string fxServerPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, @"server\FXServer.exe");
if (!File.Exists(fxServerPath))
{
Error = $"FXServer.exe not found ({fxServerPath})";
return null;
}
string citizenPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, @"server\citizen");
if (!Directory.Exists(citizenPath))
{
Error = $"Directory citizen not found ({citizenPath})";
return null;
}
string serverDataPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID);
if (!Directory.Exists(serverDataPath))
{
Error = $"Directory not found ({serverDataPath})";
return null;
}
Process p;
if (!AllowsEmbedConsole)
{
p = new Process
{
StartInfo =
{
WorkingDirectory = serverDataPath,
FileName = fxServerPath,
Arguments = $"+set citizen_dir \"{citizenPath}\" {_serverData.ServerParam}",
WindowStyle = ProcessWindowStyle.Minimized,
UseShellExecute = false
},
EnableRaisingEvents = true
};
p.Start();
}
else
{
p = new Process
{
StartInfo =
{
WorkingDirectory = serverDataPath,
FileName = fxServerPath,
Arguments = $"+set citizen_dir \"{citizenPath}\" {_serverData.ServerParam}",
WindowStyle = ProcessWindowStyle.Minimized,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
},
EnableRaisingEvents = true
};
var serverConsole = new Functions.ServerConsole(_serverData.ServerID);
p.OutputDataReceived += serverConsole.AddOutput;
p.ErrorDataReceived += serverConsole.AddOutput;
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
return p;
}
public async Task Stop(Process p)
{
await Task.Run(() =>
{
if (p.StartInfo.RedirectStandardInput)
{
p.StandardInput.WriteLine("quit");
}
else
{
Functions.ServerConsole.SendMessageToMainWindow(p.MainWindowHandle, "quit");
}
});
}
public async Task<Process> Install()
{
string log = "";
try
{
using (WebClient webClient = new WebClient())
{
string html = await webClient.DownloadStringTaskAsync("https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/");
Regex regex = new Regex(@"[0-9]{4}-[a-f0-9]{40}");
var matches = regex.Matches(html);
if (matches.Count <= 0)
{
log += "No versions found. \n";
return null;
}
// Find the highest version
string highestVersionInfo = null;
int highestVersion = 0;
foreach (Match match in matches)
{
string versionInfo = match.Value;
int versionNumber = int.Parse(versionInfo.Split('-')[0]);
if (versionNumber > highestVersion)
{
highestVersion = versionNumber;
highestVersionInfo = versionInfo;
}
}
string recommended = highestVersionInfo;
log += $"Highest version found: {recommended} \n";
//Download server.zip and extract then delete server.zip
string serverPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "server");
Directory.CreateDirectory(serverPath);
string zipPath = Path.Combine(serverPath, "server.zip");
await webClient.DownloadFileTaskAsync($"https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/{recommended}/server.zip", zipPath);
await Task.Run(async () =>
{
try
{
await FileManagement.ExtractZip(zipPath, serverPath);
}
catch
{
log += "Path too long \n";
}
});
await Task.Run(() => File.Delete(zipPath));
//Create FiveM-version.txt and write the downloaded version with hash
File.WriteAllText(Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "RedM-version.txt"), recommended);
// zipPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "cfx-server-data-master.zip");
// await webClient.DownloadFileTaskAsync("https://github.com/citizenfx/cfx-server-data/archive/master.zip", zipPath);
// await Task.Run(() => FileManagement.ExtractZip(zipPath, Functions.ServerPath.GetServersServerFiles(_serverData.ServerID)));
// await Task.Run(() => File.Delete(zipPath));
Additional += $"+set txAdminPort {GetTxAdminPort(int.Parse(_serverData.ServerID))} +set txDataPath \"{Functions.ServerPath.GetServersServerFiles(_serverData.ServerID)}\"";
}
return null;
}
catch (Exception e)
{
log += e.Message;
File.WriteAllText(Path.Combine("servers", "installLog.txt"), log);
return null;
}
}
public async Task<Process> Update()
{
try
{
using (WebClient webClient = new WebClient())
{
string remoteBuild = await GetRemoteBuild();
//Download server.zip and extract then delete server.zip
string serverPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "server");
await Task.Run(() =>
{
try
{
Directory.Delete(serverPath, true);
}
catch (Exception e)
{
Error = $"Unable to delete server folder. Path: {serverPath} Error: {e.Message}";
Console.WriteLine(Error);
}
});
if (Directory.Exists(serverPath))
{
Error = $"Unable to delete server folder. Path: {serverPath}";
return null;
}
Directory.CreateDirectory(serverPath);
string zipPath = Path.Combine(serverPath, "server.zip");
await webClient.DownloadFileTaskAsync($"https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/{remoteBuild}/server.zip", zipPath);
await Task.Run(async () =>
{
try
{
await FileManagement.ExtractZip(zipPath, serverPath);
}
catch (Exception e)
{
Error = "Path too long \n" + e.Message;
Console.WriteLine(Error);
}
});
await Task.Run(() => File.Delete(zipPath));
File.WriteAllText(Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "RedM-version.txt"), remoteBuild);
}
return null;
}
catch (Exception e)
{
Error = e.Message;
Console.WriteLine(Error);
return null;
}
}
public bool IsInstallValid()
{
string exeFile = @"server\FXServer.exe";
string exePath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, exeFile);
return File.Exists(exePath);
}
public bool IsImportValid(string path)
{
string exeFile = @"server\FXServer.exe";
string exePath = Path.Combine(path, exeFile);
Error = $"Invalid Path! Fail to find {exeFile}";
return File.Exists(exePath);
}
public string GetLocalBuild()
{
string versionPath = Functions.ServerPath.GetServersServerFiles(_serverData.ServerID, "RedM-version.txt");
// Error = $"Fail to get local build";
return File.Exists(versionPath) ? File.ReadAllText(versionPath) : string.Empty;
}
public async Task<string> GetRemoteBuild()
{
try
{
using (WebClient webClient = new WebClient())
{
string html = await webClient.DownloadStringTaskAsync("https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/");
Regex regex = new Regex(@"[0-9]{4}-[a-f0-9]{40}");
var matches = regex.Matches(html);
if (matches.Count <= 0)
{
Console.WriteLine("No versions found.");
return null;
}
// Find the highest version
string highestVersionInfo = null;
int highestVersion = 0;
foreach (Match match in matches)
{
string versionInfo = match.Value;
int versionNumber = int.Parse(versionInfo.Split('-')[0]);
if (versionNumber > highestVersion)
{
highestVersion = versionNumber;
highestVersionInfo = versionInfo;
}
}
return highestVersionInfo;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Error = $"Fail to get remote build";
Console.WriteLine(Error);
return string.Empty;
}
}
}