forked from dank074/PlusEMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlusEnvironment.cs
executable file
·426 lines (356 loc) · 15.2 KB
/
PlusEnvironment.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using MySql.Data.MySqlClient;
using Plus.Core;
using Plus.HabboHotel;
using Plus.HabboHotel.GameClients;
using Plus.HabboHotel.Users;
using Plus.Utilities;
using log4net;
using System.Collections.Concurrent;
using Plus.Communication.Packets.Outgoing.Moderation;
using Plus.Communication.Encryption.Keys;
using Plus.Communication.Encryption;
using Plus.Database.Interfaces;
using Plus.Database;
using Plus.HabboHotel.Cache.Type;
using Plus.HabboHotel.Users.UserData;
using Plus.Communication.Rcon;
using Plus.Core.FigureData;
using Plus.Core.Language;
using Plus.Core.Settings;
using Plus.Network;
namespace Plus
{
public static class PlusEnvironment
{
private static readonly ILog log = LogManager.GetLogger(typeof(PlusEnvironment));
public const string PrettyVersion = "Plus Emulator";
public const string PrettyBuild = "3.4.3.0";
private static Encoding _defaultEncoding;
public static CultureInfo CultureInfo;
private static Game _game;
private static ConfigurationData _configuration;
private static LanguageManager _languageManager;
private static SettingsManager _settingsManager;
private static DatabaseManager _manager;
private static RconSocket _Rcon;
private static FigureDataManager _figureManager;
private static NetworkBootstrap _bootstrap;
// TODO: Get rid?
public static bool Event = false;
public static DateTime lastEvent;
public static DateTime ServerStarted;
private static readonly List<char> Allowedchars = new List<char>(new[]
{
'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', '-', '.'
});
private static ConcurrentDictionary<int, Habbo> _usersCached = new ConcurrentDictionary<int, Habbo>();
public static string SWFRevision = "";
public static void Initialize()
{
ServerStarted = DateTime.Now;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine();
Console.WriteLine(" ____ __ ________ _____ __");
Console.WriteLine(@" / __ \/ /_ _______/ ____/ |/ / / / /");
Console.WriteLine(" / /_/ / / / / / ___/ __/ / /|_/ / / / / ");
Console.WriteLine(" / ____/ / /_/ (__ ) /___/ / / / /_/ / ");
Console.WriteLine(@" /_/ /_/\__,_/____/_____/_/ /_/\____/ ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" " + PrettyVersion + " <Build " + PrettyBuild + ">");
Console.WriteLine(" http://PlusIndustry.com");
Console.WriteLine("");
Console.Title = "Loading Plus Emulator";
_defaultEncoding = Encoding.Default;
Console.WriteLine("");
Console.WriteLine("");
CultureInfo = CultureInfo.CreateSpecificCulture("en-GB");
try
{
_configuration = new ConfigurationData($"Config{Path.DirectorySeparatorChar}config.ini");
var connectionString = new MySqlConnectionStringBuilder
{
ConnectionTimeout = 10,
Database = GetConfig().data["db.name"],
DefaultCommandTimeout = 30,
Logging = false,
MaximumPoolSize = uint.Parse(GetConfig().data["db.pool.maxsize"]),
MinimumPoolSize = uint.Parse(GetConfig().data["db.pool.minsize"]),
Password = GetConfig().data["db.password"],
Pooling = true,
Port = uint.Parse(GetConfig().data["db.port"]),
Server = GetConfig().data["db.hostname"],
UserID = GetConfig().data["db.username"],
AllowZeroDateTime = true,
ConvertZeroDateTime = true,
SslMode = MySqlSslMode.None
};
_manager = new DatabaseManager(connectionString.ToString());
if (!_manager.IsConnected())
{
log.Error("Failed to Connect to the specified MySQL server.");
Console.ReadKey(true);
Environment.Exit(1);
return;
}
log.Info("Connected to Database!");
//Reset our statistics first.
using (IQueryAdapter dbClient = GetDatabaseManager().GetQueryReactor())
{
dbClient.RunQuery("TRUNCATE `catalog_marketplace_data`");
dbClient.RunQuery("UPDATE `rooms` SET `users_now` = '0' WHERE `users_now` > '0';");
dbClient.RunQuery("UPDATE `users` SET `online` = '0' WHERE `online` = '1'");
dbClient.RunQuery("UPDATE `server_status` SET `users_online` = '0', `loaded_rooms` = '0'");
}
//Get the configuration & Game set.
_languageManager = new LanguageManager();
_languageManager.Init();
_settingsManager = new SettingsManager();
_settingsManager.Init();
_figureManager = new FigureDataManager();
_figureManager.Init();
//Have our encryption ready.
HabboEncryptionV2.Initialize(new RSAKeys());
//Make sure Rcon is connected before we allow clients to Connect.
_Rcon = new RconSocket(GetConfig().data["rcon.tcp.bindip"], int.Parse(GetConfig().data["rcon.tcp.port"]), GetConfig().data["rcon.tcp.allowedaddr"].Split(Convert.ToChar(";")));
_game = new Game();
_game.StartGameLoop();
//Accept connections.
_bootstrap = new NetworkBootstrap(GetConfig().data["game.tcp.bindip"], int.Parse(GetConfig().data["game.tcp.port"]));
_bootstrap.InitAsync().Wait();
TimeSpan TimeUsed = DateTime.Now - ServerStarted;
Console.WriteLine();
log.Info("EMULATOR -> READY! (" + TimeUsed.Seconds + " s, " + TimeUsed.Milliseconds + " ms)");
}
#pragma warning disable CS0168 // The variable 'e' is declared but never used
catch (KeyNotFoundException e)
#pragma warning restore CS0168 // The variable 'e' is declared but never used
{
log.Error("Please check your configuration file - some values appear to be missing.");
log.Error("Press any key to shut down ...");
Console.ReadKey(true);
Environment.Exit(1);
return;
}
catch (InvalidOperationException e)
{
log.Error("Failed to initialize PlusEmulator: " + e.Message);
log.Error("Press any key to shut down ...");
Console.ReadKey(true);
Environment.Exit(1);
return;
}
catch (Exception e)
{
log.Error("Fatal error during startup: " + e);
log.Error("Press a key to exit");
Console.ReadKey();
Environment.Exit(1);
}
}
public static bool EnumToBool(string Enum)
{
return (Enum == "1");
}
public static string BoolToEnum(bool Bool)
{
return (Bool == true ? "1" : "0");
}
public static int GetRandomNumber(int Min, int Max)
{
return RandomNumber.GenerateNewRandom(Min, Max);
}
public static double GetUnixTimestamp()
{
TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
return ts.TotalSeconds;
}
public static long Now()
{
TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
double unixTime = ts.TotalMilliseconds;
return (long)unixTime;
}
public static string FilterFigure(string figure)
{
foreach (char character in figure)
{
if (!IsValid(character))
return "sh-3338-93.ea-1406-62.hr-831-49.ha-3331-92.hd-180-7.ch-3334-93-1408.lg-3337-92.ca-1813-62";
}
return figure;
}
private static bool IsValid(char character)
{
return Allowedchars.Contains(character);
}
public static bool IsValidAlphaNumeric(string inputStr)
{
inputStr = inputStr.ToLower();
if (string.IsNullOrEmpty(inputStr))
{
return false;
}
for (int i = 0; i < inputStr.Length; i++)
{
if (!IsValid(inputStr[i]))
{
return false;
}
}
return true;
}
public static string GetUsernameById(int UserId)
{
string Name = "Unknown User";
GameClient Client = GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client != null && Client.GetHabbo() != null)
return Client.GetHabbo().Username;
UserCache User = GetGame().GetCacheManager().GenerateUser(UserId);
if (User != null)
return User.Username;
using (IQueryAdapter dbClient = GetDatabaseManager().GetQueryReactor())
{
dbClient.SetQuery("SELECT `username` FROM `users` WHERE `id` = @id LIMIT 1");
dbClient.AddParameter("id", UserId);
Name = dbClient.GetString();
}
if (string.IsNullOrEmpty(Name))
Name = "Unknown User";
return Name;
}
public static Habbo GetHabboById(int UserId)
{
try
{
GameClient Client = GetGame().GetClientManager().GetClientByUserId(UserId);
if (Client != null)
{
Habbo User = Client.GetHabbo();
if (User != null && User.Id > 0)
{
if (_usersCached.ContainsKey(UserId))
_usersCached.TryRemove(UserId, out User);
return User;
}
}
else
{
try
{
if (_usersCached.ContainsKey(UserId))
return _usersCached[UserId];
else
{
UserData data = UserDataFactory.GetUserData(UserId);
if (data != null)
{
Habbo Generated = data.user;
if (Generated != null)
{
Generated.InitInformation(data);
_usersCached.TryAdd(UserId, Generated);
return Generated;
}
}
}
}
catch { return null; }
}
return null;
}
catch
{
return null;
}
}
public static Habbo GetHabboByUsername(String UserName)
{
try
{
using (IQueryAdapter dbClient = GetDatabaseManager().GetQueryReactor())
{
dbClient.SetQuery("SELECT `id` FROM `users` WHERE `username` = @user LIMIT 1");
dbClient.AddParameter("user", UserName);
int id = dbClient.GetInteger();
if (id > 0)
return GetHabboById(Convert.ToInt32(id));
}
return null;
}
catch { return null; }
}
public static void PerformShutDown()
{
Console.Clear();
log.Info("Server shutting down...");
Console.Title = "PLUS EMULATOR: SHUTTING DOWN!";
GetGame().GetClientManager().SendPacket(new BroadcastMessageAlertComposer(GetLanguageManager().TryGetValue("server.shutdown.message")));
GetGame().StopGameLoop();
Thread.Sleep(2500);
GetGame().GetPacketManager().UnregisterAll();//Unregister the packets.
GetGame().GetPacketManager().WaitForAllToComplete();
GetGame().GetClientManager().CloseAll();//Close all connections
_bootstrap.Shutdown().Wait();
_bootstrap.ShutdownWorkers();
GetGame().GetRoomManager().Dispose();//Stop the game loop.
using (IQueryAdapter dbClient = _manager.GetQueryReactor())
{
dbClient.RunQuery("TRUNCATE `catalog_marketplace_data`");
dbClient.RunQuery("UPDATE `users` SET `online` = '0', `auth_ticket` = NULL");
dbClient.RunQuery("UPDATE `rooms` SET `users_now` = '0' WHERE `users_now` > '0'");
dbClient.RunQuery("UPDATE `server_status` SET `users_online` = '0', `loaded_rooms` = '0'");
}
log.Info("Plus Emulator has successfully shutdown.");
Thread.Sleep(1000);
Environment.Exit(0);
}
public static ConfigurationData GetConfig()
{
return _configuration;
}
public static Encoding GetDefaultEncoding()
{
return _defaultEncoding;
}
public static Game GetGame()
{
return _game;
}
public static RconSocket GetRconSocket()
{
return _Rcon;
}
public static FigureDataManager GetFigureManager()
{
return _figureManager;
}
public static DatabaseManager GetDatabaseManager()
{
return _manager;
}
public static LanguageManager GetLanguageManager()
{
return _languageManager;
}
public static SettingsManager GetSettingsManager()
{
return _settingsManager;
}
public static ICollection<Habbo> GetUsersCached()
{
return _usersCached.Values;
}
public static bool RemoveFromCache(int Id, out Habbo Data)
{
return _usersCached.TryRemove(Id, out Data);
}
}
}