-
Notifications
You must be signed in to change notification settings - Fork 6
/
AkiledEnvironment.cs
464 lines (417 loc) · 17.3 KB
/
AkiledEnvironment.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using Akiled.Communication.Encryption;
using Akiled.Communication.Encryption.Keys;
using Akiled.Communication.Packets.Outgoing;
using Akiled.Communication.WebSocket;
using Akiled.Core;
using Akiled.Core.FigureData;
using Akiled.Core.Settings;
using Akiled.Database;
using Akiled.Database.Interfaces;
using Akiled.HabboHotel;
using Akiled.HabboHotel.Cache;
using Akiled.HabboHotel.GameClients;
using Akiled.HabboHotel.Rooms.Chat.Commands.Cmd;
using Akiled.HabboHotel.Users;
using Akiled.HabboHotel.Users.UserData;
using Akiled.Net;
using Akiled.Utilities;
using ConnectionManager;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Akiled
{
public static class AkiledEnvironment
{
private static readonly ConcurrentDictionary<int, Habbo> _usersCached = new();
public static Random Random = new();
private static readonly List<char> Allowedchars = new(new char[82]
{
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
'-',
'.',
'=',
'?',
'!',
':',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'á',
'é',
'í',
'ó',
'ú',
'Á',
'É',
'Í',
'Ó',
'Ú',
'ñ',
'Ñ',
'ü',
'Ü'
});
private static Encoding _defaultEncoding;
private static ConfigurationData _configuration;
private static ConnectionHandeling _connectionManager;
private static WebSocketManager _webSocketManager;
private static Game _game;
private static SettingsManager _settingsManager;
private static DatabaseManager _datebasemanager;
private static RCONSocket _rcon;
private static FigureDataManager _figureManager;
private static LanguageManager _languageManager;
public static DateTime ServerStarted;
public static bool StaticEvents;
public static string PatchDir;
public static Task<bool> Initialize()
{
Console.Clear();
ServerStarted = DateTime.Now;
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Clear();
Console.WriteLine(@"");
Console.WriteLine(@"");
Console.WriteLine(@"");
Console.WriteLine(@" _ _ _____ _ ___ ___ ___ __ __ _ _ _ _ _____ ___ ___");
Console.WriteLine(@" /_\ | |/ /_ _| | | __| \ | __| \/ | | | | | /_\_ _/ _ \| _ \");
Console.WriteLine(@" / _ \| ' < | || |__| _|| |) | | _|| |\/| | |_| | |__ / _ \| || (_) | /");
Console.WriteLine(@" / / \_\_|\_\___|____|___|___/ |___|_| |_|\___/|____/_/ \_\_| \___/|_|_\");
Console.WriteLine(@"");
Console.WriteLine(@" Basado en PlusEmulator y Wibbo Emulator - Akiled Emulator ");
Console.WriteLine(@"");
Console.WriteLine(@" Versión: 2.9.55 ");
Console.WriteLine(@"");
Console.WriteLine(@" Desarrollado por Carlos Mota --- Todos los derechos reservados");
Console.WriteLine();
_defaultEncoding = Encoding.Default;
Console.ForegroundColor = ConsoleColor.Cyan;
PatchDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/";
Console.Title = "Cargando Emulator";
try
{
_configuration = new ConfigurationData(PatchDir + "Settings/configuration.ini", false);
_datebasemanager = new DatabaseManager(
uint.Parse(GetConfig().data["db.pool.maxsize"]),
uint.Parse(GetConfig().data["db.pool.minsize"]),
GetConfig().data["db.hostname"],
uint.Parse(GetConfig().data["db.port"]),
GetConfig().data["db.username"],
GetConfig().data["db.password"],
GetConfig().data["db.name"]);
int num = 0;
while (!_datebasemanager.IsConnected())
{
++num;
Thread.Sleep(5000);
if (num > 10)
{
Logging.WriteLine("Error al conectar con el Mysql Server.");
Console.ReadKey(true);
Environment.Exit(1);
return Task.FromResult(false);
}
}
_settingsManager = new SettingsManager();
_settingsManager.Init();
HabboEncryptionV2.Initialize(new RSAKeys());
_languageManager = new LanguageManager();
_languageManager.Init();
_game = new Game();
_game.StartGameLoop();
_figureManager = new FigureDataManager();
_figureManager.Init();
if (_configuration.data["Websocketenable"] == "true")
_webSocketManager = new WebSocketManager(int.Parse(GetConfig().data["game.websocketsport"]), int.Parse(GetConfig().data["game.tcp.conlimit"]));
_connectionManager = new ConnectionHandeling(int.Parse(GetConfig().data["game.tcp.port"]), int.Parse(GetConfig().data["game.tcp.conlimit"]), int.Parse(GetConfig().data["game.tcp.conperip"]));
if (_configuration.data["Musenable"] == "true")
_rcon = new RCONSocket(int.Parse(GetConfig().data["mus.tcp.port"]), GetConfig().data["mus.tcp.allowedaddr"].Split(';'));
StaticEvents = _configuration.data["static.events"] == "true";
Logging.WriteLine("VARIABLES -> CARGADAS y LISTAS!");
// Allow services to self initialize
TimeSpan TimeUsed = DateTime.Now - ServerStarted;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Logging.WriteLine("Akiled Emulador -> Activo! (" + TimeUsed.Seconds + " s, " + TimeUsed.Milliseconds + " ms)");
if (Debugger.IsAttached)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Logging.WriteLine("Server is debugging: Activo!");
Console.ForegroundColor = ConsoleColor.Blue;
}
else
{
Logging.WriteLine("Server is not debugging: Inactivo");
Logging.DisablePrimaryWriting(false);
}
}
catch (KeyNotFoundException ex)
{
Logging.WriteLine("Please check your configuration file - some values appear to be missing.");
Logging.WriteLine("Press any key to shut down ...");
Logging.WriteLine(ex.ToString());
Console.ReadKey(true);
return Task.FromResult(false);
}
catch (InvalidOperationException ex)
{
Logging.WriteLine("Failed to initialize AkiledEmulator: " + ex.Message);
Logging.WriteLine("Press any key to shut down ...");
Console.ReadKey(true);
return Task.FromResult(false);
}
catch (Exception ex)
{
Console.WriteLine("Fatal error during startup: " + ex.ToString());
Console.WriteLine("Press a key to exit");
Console.ReadKey();
Environment.Exit(1);
return Task.FromResult(false);
}
return Task.FromResult(true);
}
public static bool EnumToBool(string Enum) => Enum == "1";
public static Encoding GetDefaultEncoding() => _defaultEncoding;
public static string BoolToEnum(bool Bool) => Bool ? "1" : "0";
public static int GetRandomNumber(int Min, int Max) => Random.Next(Min, Max + 1);
public static int GetRandomNumberMulti(int Min, int Max) => RandomNumber.GenerateLockedRandom(Min, Max);
public static int GetUnixTimestamp() => (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
internal static int GetIUnixTimestamp() => Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
public static FigureDataManager GetFigureManager() => _figureManager;
private static bool IsValid(char character) => Allowedchars.Contains(character);
public static bool IsValidAlphaNumeric(string inputStr)
{
if (string.IsNullOrEmpty(inputStr))
return false;
foreach (var t in inputStr)
{
if (!IsValid(t))
return false;
}
return true;
}
public static bool UsernameExists(string username)
{
int integer;
using (IQueryAdapter queryReactor = GetDatabaseManager().GetQueryReactor())
{
queryReactor.SetQuery("SELECT id FROM users WHERE username = @username LIMIT 1");
queryReactor.AddParameter(nameof(username), username);
integer = queryReactor.GetInteger();
}
return integer > 0;
}
public static string GetUsernameById(int UserId)
{
string str = "Unknown User";
GameClient clientByUserId = GetGame().GetClientManager().GetClientByUserID(UserId);
if (clientByUserId != null && clientByUserId.GetHabbo() != null)
return clientByUserId.GetHabbo().Username;
if (_usersCached.ContainsKey(UserId))
return _usersCached[UserId].Username;
using (IQueryAdapter queryReactor = GetDatabaseManager().GetQueryReactor())
{
queryReactor.SetQuery("SELECT `username` FROM `users` WHERE `id` = @id LIMIT 1");
queryReactor.AddParameter("id", UserId);
str = queryReactor.GetString();
}
if (string.IsNullOrEmpty(str))
str = "Unknown User";
return str;
}
public static Habbo GetHabboByUsername(string UserName)
{
try
{
using IQueryAdapter queryReactor = GetDatabaseManager().GetQueryReactor();
queryReactor.SetQuery("SELECT `id` FROM `users` WHERE `username` = @user LIMIT 1");
queryReactor.AddParameter("user", UserName);
int integer = queryReactor.GetInteger();
if (integer > 0)
return GetHabboById(Convert.ToInt32(integer));
return null;
}
catch
{
return null;
}
}
public static Habbo GetHabboById(int UserId)
{
if (UserId == 0)
{
return null;
}
try
{
GameClient clientByUserId = GetGame()?.GetClientManager()?.GetClientByUserID(UserId);
if (clientByUserId != null)
{
Habbo habbo = clientByUserId.GetHabbo();
if (habbo is { Id: > 0 })
{
if (_usersCached.ContainsKey(UserId))
_usersCached.TryRemove(UserId, out habbo);
return habbo;
}
}
else
{
try
{
if (_usersCached.ContainsKey(UserId))
return _usersCached[UserId];
UserData userData = UserDataFactory.GetUserData(UserId);
if (userData != null)
{
Habbo user = userData.user;
if (user != null)
{
_usersCached.TryAdd(UserId, user);
return user;
}
}
}
catch
{
return null;
}
}
return null;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
public static LanguageManager GetLanguageManager() => _languageManager;
public static ConfigurationData GetConfig()
=> _configuration;
public static ConnectionHandeling GetConnectionManager() => _connectionManager;
public static RCONSocket GetRCONSocket() => _rcon;
public static Game GetGame() => _game;
public static DatabaseManager GetDatabaseManager() => _datebasemanager;
public static SettingsManager GetSettingsManager() => _settingsManager;
public static ICollection<Habbo> GetUsersCached() => _usersCached.Values;
public static bool RemoveFromCache(int Id, out Habbo Data) => _usersCached.TryRemove(Id, out Data);
internal static void PreformShutDown() => PreformShutDown(false);
internal static void PreformRestart() => PreformShutDown(true);
public static void PreformShutDown(bool restart)
{
StringBuilder builder = new();
DateTime now1 = DateTime.Now;
DateTime now2 = DateTime.Now;
ServerPacket Packet = new(3801);
Packet.WriteString("<b><font color=\"#ba3733\" size=\"14\">Hotel Apagado</font></b><br><br>El hotel se reiniciará en 10 segundos. Disculpe las molestias. <br> Gracias por visitarnos, volveremos en unos 5 minutos.</br>");
GetGame().GetClientManager().SendMessage(Packet, "");
Thread.Sleep(10000);
AppendTimeStampWithComment(ref builder, now2, "Hotel pre-warning");
Console.Write("Game loop stopped");
DateTime now3 = DateTime.Now;
Console.WriteLine("Server shutting down...");
Console.Title = "<<- SERVER SHUTDOWN ->>";
GetConnectionManager().destroy();
AppendTimeStampWithComment(ref builder, now3, "Socket close");
DateTime now4 = DateTime.Now;
Console.WriteLine("<<- SERVER SHUTDOWN ->> ROOM SAVE");
_game.GetRoomManager().RemoveAllRooms();
AppendTimeStampWithComment(ref builder, now4, "Room destructor");
DateTime now5 = DateTime.Now;
GetGame().GetClientManager().CloseAll();
AppendTimeStampWithComment(ref builder, now5, "Furni pre-save and connection close");
DateTime now6 = DateTime.Now;
if (_connectionManager != null)
_connectionManager.destroy();
if (_webSocketManager != null)
_webSocketManager.Destroy();
GetGame().Destroy(); // E+N+D+T+I+M+E
AppendTimeStampWithComment(ref builder, now6, "Connection shutdown");
DateTime now7 = DateTime.Now;
AppendTimeStampWithComment(ref builder, now7, "Game destroy");
DateTime now8 = DateTime.Now;
TimeSpan span = DateTime.Now - now1;
builder.AppendLine("Total time on shutdown " + TimeSpanToString(span));
builder.AppendLine("You have reached ==> [END OF SESSION]");
builder.AppendLine();
builder.AppendLine();
builder.AppendLine();
Logging.LogShutdown(builder);
Console.WriteLine("System disposed, goodbye!");
if (restart) Process.Start(Assembly.GetEntryAssembly().Location);
if (!restart)
return;
Environment.Exit(Environment.ExitCode);
}
public static string TimeSpanToString(TimeSpan span) => span.Seconds + " s, " + span.Milliseconds + " ms";
public static void AppendTimeStampWithComment(
ref StringBuilder builder,
DateTime time,
string text) => builder?.AppendLine(text + " =>[" + TimeSpanToString(DateTime.Now - time) + "]");
}
}