Repository navigation
Expand file tree
/
Copy pathServerInit.cs
More file actions
executable file
·201 lines (164 loc) · 9.09 KB
/
Copy pathServerInit.cs
File metadata and controls
executable file
·201 lines (164 loc) · 9.09 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
using AMP.Data;
using AMP.DedicatedServer.Commands;
using AMP.Logging;
using AMP.Network.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
namespace AMP.DedicatedServer {
public class ServerInit {
internal static ServerConfig serverConfig;
public static int port = 13698;
public static string servername = "";
public static string description = "";
internal static string serverIcon = "";
internal static string gamemode_override = "";
public static void Start(CommandLine cmd) {
Log.loggerType = Log.LoggerType.CONSOLE;
Netamite.Logging.Log.loggerType = Netamite.Logging.Log.LoggerType.EVENT_ONLY;
Netamite.Logging.Log.onLogMessage += (type, message) => {
Log.Msg((Log.Type) type, message);
};
Console.OutputEncoding = System.Text.Encoding.UTF8;
Log.Info("");
Log.Info( "<color=#FF8C00>" +
",_._._._._._._._._|__________________________________________________________.\r\n" +
"|_X_X_X_X_X_X_X_X_|█████████████████████████████████████████████████████████▛\r\n" +
" '\r\n" +
"\t\t\t █████╗ ███╗ ███╗██████╗ \r\n" +
"\t\t\t ██╔══██╗████╗ ████║██╔══██╗ \r\n" +
"\t\t\t ███████║██╔████╔██║██████╔╝ \r\n" +
"\t\t\t ██╔══██║██║╚██╔╝██║██╔═══╝ \r\n" +
"\t\t\t ██║ ██║██║ ╚═╝ ██║██║ \r\n" +
"\t\t\t ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ \r\n" +
$"\t\t\t Server Version: { Data.Defines.SERVER_VERSION }\r\n" +
$"\t\t\t Mod Version: { Defines.FULL_MOD_VERSION }\r\n" +
".__________________________________________________________|_._._._._._._._._,\r\n" +
" ▜█████████████████████████████████████████████████████████|_X_X_X_X_X_X_X_X_|\r\n" +
" '\r\n" +
"</color>");
serverConfig = ServerConfig.Load("server.json");
ModManager.safeFile = new SafeFile();
ModManager.safeFile.hostingSettings = serverConfig.hostingSettings;
ModManager.banlist = Banlist.Load("banlist.json");
Server.DEFAULT_MAP = serverConfig.serverSettings.map;
Server.DEFAULT_MODE = serverConfig.serverSettings.mode;
port = serverConfig.serverSettings.port;
uint max_players = (uint) serverConfig.serverSettings.max_players;
string password = serverConfig.serverSettings.password;
if(cmd.HasArg("map") && cmd.HasArg("mode")) {
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [map : {Server.DEFAULT_MAP} => {cmd.GetArg("map")}]");
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [mode : {Server.DEFAULT_MODE} => {cmd.GetArg("mode")}]");
Server.DEFAULT_MAP = cmd.GetArg("map");
Server.DEFAULT_MODE = cmd.GetArg("mode");
}
if (cmd.HasArg("servername")) {
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [servername : {ServerInit.serverConfig.serverSettings.servername} => {cmd.GetArg("servername")}]");
servername = cmd.GetArg("servername");
} else {
servername = ServerInit.serverConfig.serverSettings.servername;
}
if (cmd.HasArg("description")) {
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [description : {ServerInit.serverConfig.serverSettings.serverdescription} => {cmd.GetArg("description")}]");
description = cmd.GetArg("description");
} else {
description = ServerInit.serverConfig.serverSettings.serverdescription;
}
if (cmd.HasArg("port")) {
port = ushort.Parse(cmd.GetArg("port"));
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [port : {serverConfig.serverSettings.port} => {port}]");
}
if (cmd.HasArg("max_players")) {
max_players = uint.Parse(cmd.GetArg("max_players"));
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [max_players : {serverConfig.serverSettings.port} => {max_players}]");
}
if(cmd.HasArg("pvp")) {
bool oldVal = serverConfig.hostingSettings.pvpEnable;
serverConfig.hostingSettings.pvpEnable = bool.Parse(cmd.GetArg("pvp"));
Log.Warn(Defines.SERVER, $"Overwritten Config with Parameter [pvp : {oldVal} => {serverConfig.hostingSettings.pvpEnable}]");
}
if(cmd.HasArg("password")) {
password = cmd.GetArg("password");
}
if(File.Exists("icon.jpg")) {
byte[] imageArray = File.ReadAllBytes("icon.jpg");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
serverIcon = base64ImageRepresentation;
//Log.Debug("Found custom server icon. Make sure its a jpg and 64x64.");
}
ModManager.SetupNetamite();
ModManager.HostDedicatedServer(max_players, (ushort) port, password, OnStart);
Console.CancelKeyPress += delegate {
new StopCommand().Process(new string[0]);
ServerlistPinger.Stop();
Environment.Exit(0);
};
while(ModManager.serverInstance != null) {
try {
var input = Console.ReadLine();
if(input == null || input.Length == 0) continue;
ProcessCommand(input);
Thread.Sleep(1);
} catch(Exception e) {
Log.Err(e);
}
}
ServerlistPinger.Stop();
PluginLoader.UnloadPlugins();
}
public static void OnStart(string message) {
if(message != null) {
Console.WriteLine($"Serverstart failed: {message}");
return;
}
RegisterCommands();
int default_command_count = CommandHandler.CommandHandlers.Count;
#region Plugins
PluginLoader.LoadPlugins("plugins");
if(serverConfig.serverSettings.plugin_autorefresh) {
PluginWatcher.InitWatcher();
}
int plugin_command_count = CommandHandler.CommandHandlers.Count - default_command_count;
#endregion
Log.Info(Defines.SERVER, $"Registered {default_command_count + plugin_command_count} commands (Default: {default_command_count} / Plugins: {plugin_command_count}).");
if(serverConfig.serverSettings.showInServerList) {
ServerlistPinger.Start();
}
}
public static void ProcessCommand(string input) {
string[] command_args = input.Split(' ');
string command = command_args[0].ToLower();
List<string> list = new List<string>(command_args);
list.RemoveAt(0);
CommandHandler foundCommand = CommandHandler.GetCommandHandler(command);
if(foundCommand != null) {
string response = foundCommand.Process(
list.ToArray()
);
if(response != null) Log.Info(response);
} else {
Log.Info($"Command \"{command}\" could not be found.");
}
}
static void RegisterCommands() {
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();
foreach(Type type in types) {
if(type.BaseType == typeof(CommandHandler)) {
CommandHandler handler = (CommandHandler)Activator.CreateInstance(type);
CommandHandler.RegisterCommandHandler(handler);
}
}
}
public static void SetGameModeOverride(string gamemode) {
Log.Info(Defines.SERVER, $"Gamemode changed to \"{gamemode}\".");
gamemode_override = gamemode;
}
public static void ResetGameModeOverride() {
gamemode_override = string.Empty;
Log.Info(Defines.SERVER, $"Gamemode set back to map default.");
}
}
}