net.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. #include "../common/Log.h"
  18. #include <iostream>
  19. using namespace std;
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <algorithm>
  24. #include <signal.h>
  25. #include "../common/queue.h"
  26. #include "../common/timer.h"
  27. #include "../common/EQStreamFactory.h"
  28. #include "../common/EQStream.h"
  29. #include "net.h"
  30. #include "Variables.h"
  31. #include "WorldDatabase.h"
  32. #include "../common/seperator.h"
  33. #include "../common/version.h"
  34. #include "../common/EQEMuError.h"
  35. #include "../common/opcodemgr.h"
  36. #include "../common/Common_Defines.h"
  37. #include "LoginServer.h"
  38. #include "Commands/Commands.h"
  39. #include "Factions.h"
  40. #include "World.h"
  41. #include "../common/ConfigReader.h"
  42. #include "Skills.h"
  43. #include "LuaInterface.h"
  44. #include "Guilds/Guild.h"
  45. #include "Commands/ConsoleCommands.h"
  46. #include "Traits/Traits.h"
  47. #include "IRC/IRC.h"
  48. #include "Transmute.h"
  49. #ifdef WIN32
  50. #include <process.h>
  51. #define snprintf _snprintf
  52. #define vsnprintf _vsnprintf
  53. #define strncasecmp _strnicmp
  54. #define strcasecmp _stricmp
  55. #include <conio.h>
  56. #else
  57. #include <pthread.h>
  58. #include "../common/unix.h"
  59. #endif
  60. #ifdef PROFILER
  61. #define SHINY_PROFILER TRUE
  62. #include "../Profiler/src/Shiny.h"
  63. #endif
  64. NetConnection net;
  65. World world;
  66. EQStreamFactory eqsf(LoginStream);
  67. LoginServer loginserver;
  68. LuaInterface* lua_interface = new LuaInterface();
  69. #include "MutexList.h"
  70. #include "Rules/Rules.h"
  71. #include "Titles.h"
  72. #include "Languages.h"
  73. #include "Achievements/Achievements.h"
  74. #include "Patch/patch.h"
  75. volatile bool RunLoops = true;
  76. sint32 numclients = 0;
  77. sint32 numzones = 0;
  78. extern ClientList client_list;
  79. extern ZoneList zone_list;
  80. extern MasterFactionList master_faction_list;
  81. extern WorldDatabase database;
  82. extern MasterSpellList master_spell_list;
  83. extern MasterTraitList master_trait_list;
  84. extern MasterSkillList master_skill_list;
  85. extern MasterItemList master_item_list;
  86. extern GuildList guild_list;
  87. extern Variables variables;
  88. extern IRC irc;
  89. ConfigReader configReader;
  90. int32 MasterItemList::next_unique_id = 0;
  91. int last_signal = 0;
  92. RuleManager rule_manager;
  93. MasterTitlesList master_titles_list;
  94. MasterLanguagesList master_languages_list;
  95. extern MasterAchievementList master_achievement_list;
  96. extern map<int16, int16> EQOpcodeVersions;
  97. PatchServer patch;
  98. ThreadReturnType ItemLoad (void* tmp);
  99. ThreadReturnType AchievmentLoad (void* tmp);
  100. ThreadReturnType SpellLoad (void* tmp);
  101. int main(int argc, char** argv) {
  102. #ifdef PROFILER
  103. PROFILE_FUNC();
  104. #endif
  105. LogParseConfigs();
  106. WelcomeHeader();
  107. int32 t_total = Timer::GetUnixTimeStamp();
  108. LogStart();
  109. LogWrite(INIT__INFO, 0, "Init", "Starting EQ2Emulator WorldServer...");
  110. //int32 server_startup = time(NULL);
  111. //remove this when all database calls are using the new database class
  112. if (!database.Init()) {
  113. LogStop();
  114. return EXIT_FAILURE;
  115. }
  116. if (!database.ConnectNewDatabase())
  117. return EXIT_FAILURE;
  118. #ifdef _DEBUG
  119. _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  120. #endif
  121. if (signal(SIGINT, CatchSignal) == SIG_ERR) {
  122. LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
  123. return 0;
  124. }
  125. if (signal(SIGSEGV, CatchSignal) == SIG_ERR) {
  126. LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
  127. return 0;
  128. }
  129. if (signal(SIGILL, CatchSignal) == SIG_ERR) {
  130. LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
  131. return 0;
  132. }
  133. #ifndef WIN32
  134. if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
  135. LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
  136. return 0;
  137. }
  138. #endif
  139. LogWrite(WORLD__DEBUG, 0, "World", "Randomizing World...");
  140. srand(time(NULL));
  141. net.ReadLoginINI();
  142. if(loginserver.UpdatesAuto() || loginserver.UpdatesAsk() || loginserver.UpdatesAutoData()){
  143. LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "Connecting to DB PatchServer...");
  144. // Old DB patch server
  145. /*int16 updateport = 0;
  146. char* updateaddress = net.GetUpdateServerInfo(&updateport);
  147. loginserver.ConnectToUpdateServer(updateaddress, updateport);
  148. LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "DB Update check completed...");*/
  149. //New patch server
  150. if (!patch.IsEnabled())
  151. LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "Not checking patch server for updates");
  152. else {
  153. bool success = patch.Start();
  154. if (success)
  155. success = patch.Process();
  156. patch.Stop();
  157. //if (patch.QuitAfter())
  158. //looping = false;
  159. }
  160. }
  161. // JA: Grouping all System (core) data loads together for timing purposes
  162. LogWrite(WORLD__INFO, 0, "World", "Loading System Data...");
  163. int32 t_now = Timer::GetUnixTimeStamp();
  164. LogWrite(WORLD__DEBUG, 1, "World", "-Loading opcodes...");
  165. EQOpcodeVersions = database.GetVersions();
  166. map<int16,int16>::iterator version_itr;
  167. int16 version1 = 0;
  168. for (version_itr = EQOpcodeVersions.begin(); version_itr != EQOpcodeVersions.end(); version_itr++) {
  169. version1 = version_itr->first;
  170. EQOpcodeManager[version1] = new RegularOpcodeManager();
  171. map<string, uint16> eq = database.GetOpcodes(version1);
  172. if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
  173. LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
  174. return false;
  175. }
  176. }
  177. LogWrite(WORLD__DEBUG, 1, "World", "-Loading structs...");
  178. if(!configReader.LoadFile("CommonStructs.xml") || !configReader.LoadFile("WorldStructs.xml") || !configReader.LoadFile("SpawnStructs.xml") || !configReader.LoadFile("ItemStructs.xml")) {
  179. LogWrite(INIT__ERROR, 0, "Init", "Loading structs failed. Make sure you have CommonStructs.xml, WorldStructs.xml, SpawnStructs.xml, and ItemStructs.xml in the working directory!");
  180. return false;
  181. }
  182. world.init();
  183. bool threadedLoad = rule_manager.GetGlobalRule(R_World, ThreadedLoad)->GetBool();
  184. LogWrite(WORLD__DEBUG, 1, "World", "-Loading EQ2 time of day...");
  185. loginserver.InitLoginServerVariables();
  186. LogWrite(WORLD__INFO, 0, "World", "Loaded System Data (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);
  187. // JA: End System Data loading functions
  188. if (threadedLoad) {
  189. LogWrite(WORLD__WARNING, 0, "Threaded", "Using Threaded loading of static data...");
  190. #ifdef WIN32
  191. _beginthread(ItemLoad, 0, &world);
  192. _beginthread(SpellLoad, 0, &world);
  193. //_beginthread(AchievmentLoad, 0, &world);
  194. #else
  195. pthread_t thread;
  196. pthread_create(&thread, NULL, ItemLoad, &world);
  197. pthread_detach(thread);
  198. pthread_t thread2;
  199. pthread_create(&thread2, NULL, SpellLoad, &world);
  200. pthread_detach(thread2);
  201. //pthread_t thread3;
  202. //pthread_create(&thread3, NULL, AchievmentLoad, &world);
  203. //pthread_detach(thread3);
  204. #endif
  205. }
  206. // Called as a function so we can use /reload spawns any time
  207. /*LogWrite(SPAWN__INFO, 0, "Spawn", "Initializing Spawn Subsystem...");
  208. t_now = Timer::GetUnixTimeStamp();
  209. world.LoadSpawnInformation();
  210. LogWrite(SPAWN__INFO, 0, "Spawn", "Initialize Spawn Subsystem complete (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);*/
  211. // JA temp logger
  212. LogWrite(MISC__TODO, 0, "Reformat", "JA: This is as far as I got reformatting the console logs.");
  213. if (!threadedLoad) {
  214. // JA: Load all Item info
  215. LogWrite(ITEM__INFO, 0, "Items", "Loading Items...");
  216. database.LoadItemList();
  217. MasterItemList::ResetUniqueID(database.LoadNextUniqueItemID());
  218. }
  219. if (!threadedLoad) {
  220. // JA: Load all Spell info
  221. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spells...");
  222. database.LoadSpells();
  223. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spell Errors...");
  224. database.LoadSpellErrors();
  225. // Jabantiz: Load traits
  226. LogWrite(WORLD__INFO, 0, "Traits", "Loading Traits...");
  227. database.LoadTraits();
  228. }
  229. if (!threadedLoad) {
  230. // JA: Load all Quest info
  231. LogWrite(QUEST__INFO, 0, "Quests", "Loading Quests...");
  232. database.LoadQuests();
  233. }
  234. if (!threadedLoad) {
  235. LogWrite(COLLECTION__INFO, 0, "Collect", "Loading Collections...");
  236. database.LoadCollections();
  237. }
  238. LogWrite(GUILD__INFO, 0, "Guilds", "Loading Guilds...");
  239. database.LoadGuilds();
  240. LogWrite(TRADESKILL__INFO, 0, "Recipes", "Loading Recipe Books...");
  241. database.LoadRecipeBooks();
  242. LogWrite(TRADESKILL__INFO, 0, "Recipes", "Loading Recipes...");
  243. database.LoadRecipes();
  244. LogWrite(TRADESKILL__INFO, 0, "Tradeskills", "Loading Tradeskill Events...");
  245. database.LoadTradeskillEvents();
  246. if (!threadedLoad) {
  247. LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Loading Achievements...");
  248. //database.LoadAchievements();
  249. //master_achievement_list.CreateMasterAchievementListPacket();
  250. }
  251. LogWrite(SPELL__INFO, 0, "AA", "Loading Alternate Advancements...");
  252. database.LoadAltAdvancements();
  253. LogWrite(SPELL__INFO, 0, "AA", "Loading AA Tree Nodes...");
  254. database.LoadTreeNodes();
  255. LogWrite(WORLD__INFO, 0, "Titles", "Loading Titles...");
  256. database.LoadTitles();
  257. LogWrite(WORLD__INFO, 0, "Languages", "Loading Languages...");
  258. database.LoadLanguages();
  259. LogWrite(CHAT__INFO, 0, "Chat", "Loading channels...");
  260. database.LoadChannels();
  261. if (!threadedLoad) {
  262. LogWrite(MERCHANT__INFO, 0, "Merchants", "Loading Merchants...");
  263. database.LoadMerchantInformation();
  264. }
  265. LogWrite(LUA__INFO, 0, "LUA", "Loading Spawn Scripts...");
  266. database.LoadSpawnScriptData();
  267. LogWrite(LUA__INFO, 0, "LUA", "Loading Zone Scripts...");
  268. database.LoadZoneScriptData();
  269. LogWrite(WORLD__INFO, 0, "World", "Loading House Zone Data...");
  270. database.LoadHouseZones();
  271. database.LoadPlayerHouses();
  272. LogWrite(WORLD__INFO, 0, "World", "Loading Heroic OP Data...");
  273. database.LoadHOStarters();
  274. database.LoadHOWheel();
  275. LogWrite(WORLD__INFO, 0, "World", "Loading Race Types Data...");
  276. database.LoadRaceTypes();
  277. LogWrite(WORLD__INFO, 0, "World", "Loading Transmuting Data...");
  278. database.LoadTransmuting();
  279. if (threadedLoad) {
  280. LogWrite(WORLD__INFO, 0, "World", "Waiting for load threads to finish.");
  281. while (!world.items_loaded || !world.spells_loaded /*|| !world.achievments_loaded*/)
  282. Sleep(10);
  283. LogWrite(WORLD__INFO, 0, "World", "Load threads finished.");
  284. }
  285. LogWrite(WORLD__INFO, 0, "World", "Total World startup time: %u seconds.", Timer::GetUnixTimeStamp() - t_total);
  286. if (eqsf.Open(net.GetWorldPort())) {
  287. if (strlen(net.GetWorldAddress()) == 0)
  288. LogWrite(NET__INFO, 0, "Net", "World server listening on port %i", net.GetWorldPort());
  289. else
  290. LogWrite(NET__INFO, 0, "Net", "World server listening on: %s:%i", net.GetWorldAddress(), net.GetWorldPort());
  291. if(strlen(net.GetInternalWorldAddress())>0)
  292. LogWrite(NET__INFO, 0, "Net", "World server listening on: %s:%i", net.GetInternalWorldAddress(), net.GetWorldPort());
  293. }
  294. else {
  295. LogWrite(NET__ERROR, 0, "Net", "Failed to open port %i.", net.GetWorldPort());
  296. return 1;
  297. }
  298. Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
  299. InterserverTimer.Trigger();
  300. Timer* TimeoutTimer = new Timer(5000);
  301. TimeoutTimer->Start();
  302. EQStream* eqs = 0;
  303. UpdateWindowTitle(0);
  304. LogWrite(ZONE__INFO, 0, "Zone", "Starting static zones...");
  305. database.LoadSpecialZones();
  306. map<EQStream*, int32> connecting_clients;
  307. map<EQStream*, int32>::iterator cc_itr;
  308. // Check to see if a global channel is enabled, if so try to connect to it
  309. if (rule_manager.GetGlobalRule(R_World, IRCGlobalEnabled)->GetBool()) {
  310. LogWrite(CHAT__INFO, 0, "IRC", "Starting global IRC server...");
  311. // Set the irc nick name to: ServerName[IRCBot]
  312. string world_name = net.GetWorldName();
  313. // Remove all white spaces from the server name
  314. world_name.erase(std::remove(world_name.begin(), world_name.end(), ' '), world_name.end());
  315. string nick = world_name + string("[IRCBot]");
  316. // Connect the global server
  317. irc.ConnectToGlobalServer(rule_manager.GetGlobalRule(R_World, IRCAddress)->GetString(), rule_manager.GetGlobalRule(R_World, IRCPort)->GetInt16(), nick.c_str());
  318. }
  319. // JohnAdams - trying to make multi-char console input
  320. LogWrite(WORLD__DEBUG, 0, "Thread", "Starting console command thread...");
  321. #ifdef WIN32
  322. _beginthread(EQ2ConsoleListener, 0, NULL);
  323. #else
  324. /*pthread_t thread;
  325. pthread_create(&thread, NULL, &EQ2ConsoleListener, NULL);
  326. pthread_detach(thread);*/
  327. #endif
  328. //
  329. // just before starting loops, announce how to get console help
  330. LogWrite(WORLD__INFO, 0, "Console", "Type 'help' or '?' and press enter for menu options.");
  331. while(RunLoops) {
  332. Timer::SetCurrentTime();
  333. #ifndef NO_CATCH
  334. try
  335. {
  336. #endif
  337. while ((eqs = eqsf.Pop())) {
  338. struct in_addr in;
  339. in.s_addr = eqs->GetRemoteIP();
  340. LogWrite(NET__DEBUG, 0, "Net", "New client from ip: %s port: %i", inet_ntoa(in), ntohs(eqs->GetRemotePort()));
  341. // JA: Check for BannedIPs
  342. if (rule_manager.GetGlobalRule(R_World, UseBannedIPsTable)->GetInt8() == 1)
  343. {
  344. LogWrite(WORLD__DEBUG, 0, "World", "Checking inbound connection %s against BannedIPs table", inet_ntoa(in));
  345. if (database.CheckBannedIPs(inet_ntoa(in)))
  346. {
  347. LogWrite(WORLD__DEBUG, 0, "World", "Connection from %s FAILED banned IPs check. Closing connection.", inet_ntoa(in));
  348. eqs->Close(); // JA: If the inbound IP is on the banned table, close the EQStream.
  349. }
  350. }
  351. if(eqs && eqs->CheckActive() && client_list.ContainsStream(eqs) == false){
  352. LogWrite(NET__DEBUG, 0, "Net", "Adding new client...");
  353. Client* client = new Client(eqs);
  354. client_list.Add(client);
  355. }
  356. else if(eqs && !client_list.ContainsStream(eqs)){
  357. LogWrite(NET__DEBUG, 0, "Net", "Adding client to waiting list...");
  358. connecting_clients[eqs] = Timer::GetCurrentTime2();
  359. }
  360. }
  361. if(connecting_clients.size() > 0){
  362. for(cc_itr = connecting_clients.begin(); cc_itr!=connecting_clients.end(); cc_itr++){
  363. if(cc_itr->first && cc_itr->first->CheckActive() && client_list.ContainsStream(cc_itr->first) == false){
  364. LogWrite(NET__DEBUG, 0, "Net", "Removing client from waiting list...");
  365. Client* client = new Client(cc_itr->first);
  366. client_list.Add(client);
  367. connecting_clients.erase(cc_itr);
  368. break;
  369. }
  370. else if(Timer::GetCurrentTime2() >= (cc_itr->second + 10000)){
  371. connecting_clients.erase(cc_itr);
  372. break;
  373. }
  374. }
  375. }
  376. world.Process();
  377. client_list.Process();
  378. loginserver.Process();
  379. if(TimeoutTimer->Check()){
  380. eqsf.CheckTimeout();
  381. }
  382. if (InterserverTimer.Check()) {
  383. InterserverTimer.Start();
  384. database.ping();
  385. if (net.LoginServerInfo && loginserver.Connected() == false && loginserver.CanReconnect()) {
  386. LogWrite(WORLD__DEBUG, 0, "Thread", "Starting autoinit loginserver thread...");
  387. #ifdef WIN32
  388. _beginthread(AutoInitLoginServer, 0, NULL);
  389. #else
  390. pthread_t thread;
  391. pthread_create(&thread, NULL, &AutoInitLoginServer, NULL);
  392. pthread_detach(thread);
  393. #endif
  394. }
  395. }
  396. #ifndef NO_CATCH
  397. }
  398. catch(...) {
  399. LogWrite(WORLD__ERROR, 0, "World", "Exception caught in net main loop!");
  400. }
  401. #endif
  402. if (numclients == 0) {
  403. Sleep(10);
  404. continue;
  405. }
  406. Sleep(1);
  407. }
  408. LogWrite(WORLD__DEBUG, 0, "World", "The world is ending!");
  409. LogWrite(WORLD__DEBUG, 0, "IRC", "Shutting IRC down");
  410. irc.SetRunning(false);
  411. LogWrite(WORLD__DEBUG, 0, "World", "Shutting down zones...");
  412. zone_list.ShutDownZones();
  413. LogWrite(WORLD__DEBUG, 0, "World", "Shutting down LUA interface...");
  414. safe_delete(lua_interface);
  415. safe_delete(TimeoutTimer);
  416. eqsf.Close();
  417. map<int16, OpcodeManager*>::iterator opcode_itr;
  418. for(opcode_itr=EQOpcodeManager.begin();opcode_itr!=EQOpcodeManager.end();opcode_itr++){
  419. safe_delete(opcode_itr->second);
  420. }
  421. CheckEQEMuErrorAndPause();
  422. #ifdef PROFILER
  423. PROFILER_UPDATE();
  424. PROFILER_OUTPUT();
  425. #endif
  426. LogWrite(WORLD__INFO, 0, "World", "Exiting... we hope you enjoyed your flight.");
  427. LogStop();
  428. return 0;
  429. }
  430. ThreadReturnType ItemLoad (void* tmp)
  431. {
  432. LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread started.");
  433. #ifdef WIN32
  434. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  435. #endif
  436. if (tmp == 0) {
  437. ThrowError("ItemLoad(): tmp = 0!");
  438. THREAD_RETURN(NULL);
  439. }
  440. World* world = (World*) tmp;
  441. WorldDatabase db;
  442. db.Init();
  443. db.ConnectNewDatabase();
  444. LogWrite(ITEM__INFO, 0, "Items", "Loading Items...");
  445. db.LoadItemList();
  446. MasterItemList::ResetUniqueID(db.LoadNextUniqueItemID());
  447. // Relies on the item list so needs to be in the item thread
  448. LogWrite(COLLECTION__INFO, 0, "Collect", "Loading Collections...");
  449. db.LoadCollections();
  450. LogWrite(MERCHANT__INFO, 0, "Merchants", "Loading Merchants...");
  451. db.LoadMerchantInformation();
  452. LogWrite(QUEST__INFO, 0, "Quests", "Loading Quests...");
  453. db.LoadQuests();
  454. world->items_loaded = true;
  455. LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread completed.");
  456. mysql_thread_end();
  457. THREAD_RETURN(NULL);
  458. }
  459. ThreadReturnType SpellLoad (void* tmp)
  460. {
  461. LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread started.");
  462. #ifdef WIN32
  463. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  464. #endif
  465. if (tmp == 0) {
  466. ThrowError("ItemLoad(): tmp = 0!");
  467. THREAD_RETURN(NULL);
  468. }
  469. World* world = (World*) tmp;
  470. WorldDatabase db;
  471. db.Init();
  472. db.ConnectNewDatabase();
  473. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spells...");
  474. db.LoadSpells();
  475. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spell Errors...");
  476. db.LoadSpellErrors();
  477. LogWrite(WORLD__INFO, 0, "Traits", "Loading Traits...");
  478. db.LoadTraits();
  479. world->spells_loaded = true;
  480. LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread completed.");
  481. mysql_thread_end();
  482. THREAD_RETURN(NULL);
  483. }
  484. ThreadReturnType AchievmentLoad (void* tmp)
  485. {
  486. LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread started.");
  487. #ifdef WIN32
  488. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  489. #endif
  490. if (tmp == 0) {
  491. ThrowError("ItemLoad(): tmp = 0!");
  492. THREAD_RETURN(NULL);
  493. }
  494. World* world = (World*) tmp;
  495. WorldDatabase db;
  496. db.Init();
  497. db.ConnectNewDatabase();
  498. LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Loading Achievements...");
  499. int32 t_now = Timer::GetUnixTimeStamp();
  500. db.LoadAchievements();
  501. master_achievement_list.CreateMasterAchievementListPacket();
  502. LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Achievements loaded (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);
  503. world->achievments_loaded = true;
  504. LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread completed.");
  505. mysql_thread_end();
  506. THREAD_RETURN(NULL);
  507. }
  508. ThreadReturnType EQ2ConsoleListener(void* tmp)
  509. {
  510. char cmd[300];
  511. size_t i = 0;
  512. size_t len;
  513. while( RunLoops )
  514. {
  515. // Read in single line from "stdin"
  516. memset( cmd, 0, sizeof( cmd ) );
  517. if( fgets( cmd, 300, stdin ) == NULL )
  518. continue;
  519. if( !RunLoops )
  520. break;
  521. len = strlen(cmd);
  522. for( i = 0; i < len; ++i )
  523. {
  524. if(cmd[i] == '\n' || cmd[i] == '\r')
  525. cmd[i] = '\0';
  526. }
  527. ProcessConsoleInput(cmd);
  528. }
  529. THREAD_RETURN(NULL);
  530. }
  531. #include <fstream>
  532. void CatchSignal(int sig_num) {
  533. // In rare cases this can be called after the log system is shut down causing a deadlock or crash
  534. // when the world shuts down, if this happens again comment out the LogWrite and uncomment the printf
  535. if (last_signal != sig_num){
  536. static Mutex lock;
  537. static ofstream signal_out;
  538. lock.lock();
  539. if (!signal_out.is_open())
  540. signal_out.open("signal_catching.log", ios::trunc);
  541. if (signal_out){
  542. signal_out << "Caught signal " << sig_num << "\n";
  543. signal_out.flush();
  544. }
  545. printf("Caught signal %i\n", sig_num);
  546. lock.unlock();
  547. last_signal = sig_num;
  548. RunLoops = false;
  549. }
  550. }
  551. bool NetConnection::ReadLoginINI() {
  552. char buf[201], type[201];
  553. int items[3] = {0, 0};
  554. FILE *f;
  555. if (!(f = fopen (MAIN_INI_FILE, "r"))) {
  556. LogWrite(INIT__ERROR, 0, "Init", "File '%s' could not be opened", MAIN_INI_FILE);
  557. return false;
  558. }
  559. do {
  560. if (fgets (buf, 200, f) == NULL || feof(f))
  561. {
  562. LogWrite(INIT__ERROR, 0, "Init", "[LoginServer] block not found in '%s'.", MAIN_INI_FILE);
  563. fclose (f);
  564. return false;
  565. }
  566. }
  567. while (strncasecmp (buf, "[LoginServer]\n", 14) != 0 && strncasecmp (buf, "[LoginServer]\r\n", 15) != 0);
  568. while (!feof (f))
  569. {
  570. #ifdef WIN32
  571. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  572. #else
  573. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  574. #endif
  575. {
  576. if (!strncasecmp (type, "worldname", 9)) {
  577. snprintf(worldname, sizeof(worldname), "%s", buf);
  578. items[1] = 1;
  579. if(strlen(worldname)<4)
  580. LogWrite(INIT__ERROR, 0, "Init", "Invalid worldname, please edit LoginServer.ini. Server name must be at least 4 characters.");
  581. }
  582. if (!strncasecmp (type, "account", 7)) {
  583. strncpy(worldaccount, buf, 30);
  584. }
  585. if (!strncasecmp (type, "logstats", 8)) {
  586. if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
  587. net.UpdateStats = true;
  588. }
  589. if (!strncasecmp (type, "password", 8)) {
  590. strncpy (worldpassword, buf, 30);
  591. for(int i=strlen(worldpassword);i>=0;i--){
  592. if(worldpassword[i] == ' ' || worldpassword[i] == '\n' || worldpassword[i] == '\t' || worldpassword[i] == '\r')
  593. worldpassword[i] = '\0';
  594. }
  595. }
  596. if (!strncasecmp (type, "locked", 6)) {
  597. if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
  598. world_locked = true;
  599. }
  600. if (!strncasecmp (type, "worldaddress", 12)) {
  601. if (strlen(buf) >= 3) {
  602. strncpy (worldaddress, buf, 250);
  603. }
  604. }
  605. if (!strncasecmp(type, "autotableupdates", 16)) {
  606. if(strlen(buf) >= 3 && !strncasecmp(buf, "ask", 3))
  607. loginserver.UpdatesAsk(true);
  608. else if(strlen(buf) >= 6 && !strncasecmp(buf, "always", 6))
  609. loginserver.UpdatesAuto(true);
  610. }
  611. if (!strncasecmp (type, "autotableverbose", 16)) {
  612. if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
  613. loginserver.UpdatesVerbose(true);
  614. }
  615. if (!strncasecmp (type, "autotabledata", 13)) {
  616. if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
  617. loginserver.UpdatesAutoData(true);
  618. }
  619. if (!strncasecmp (type, "internalworldaddress", 20)) {
  620. if (strlen(buf) >= 3) {
  621. strncpy(internalworldaddress, buf, 20);
  622. }
  623. }
  624. if (!strncasecmp (type, "worldport", 9)) {
  625. if(Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF)
  626. worldport = atoi(buf);
  627. }
  628. if ((!strcasecmp (type, "loginserver")) || (!strcasecmp (type, "loginserver1"))) {
  629. strncpy (loginaddress[0], buf, 100);
  630. items[0] = 1;
  631. }
  632. if ((!strcasecmp(type, "loginport")) || (!strcasecmp(type, "loginport1"))) {
  633. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  634. loginport[0] = atoi(buf);
  635. }
  636. }
  637. if (!strcasecmp (type, "loginserver2")) {
  638. strncpy (loginaddress[1], buf, 250);
  639. }
  640. if (!strcasecmp(type, "loginport2")) {
  641. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  642. loginport[1] = atoi(buf);
  643. }
  644. }
  645. if (!strcasecmp (type, "loginserver3")) {
  646. strncpy (loginaddress[2], buf, 250);
  647. }
  648. if (!strcasecmp(type, "loginport3")) {
  649. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  650. loginport[2] = atoi(buf);
  651. }
  652. }
  653. }
  654. }
  655. if (!items[0] || !items[1])
  656. {
  657. LogWrite(INIT__ERROR, 0, "Init", "Incomplete LoginServer.INI file.");
  658. fclose (f);
  659. return false;
  660. }
  661. /*
  662. if (strcasecmp(worldname, "Unnamed server") == 0) {
  663. cout << "LoginServer.ini: server unnamed, disabling uplink" << endl;
  664. fclose (f);
  665. return false;
  666. }
  667. */
  668. fclose(f);
  669. f=fopen (MAIN_INI_FILE, "r");
  670. do {
  671. if (fgets (buf, 200, f) == NULL || feof(f))
  672. {
  673. LogWrite(INIT__ERROR, 0, "Init", "[WorldServer] block not found in %s", MAIN_INI_FILE);
  674. fclose (f);
  675. return true;
  676. }
  677. }
  678. while (strncasecmp (buf, "[WorldServer]\n", 14) != 0 && strncasecmp (buf, "[WorldServer]\r\n", 15) != 0);
  679. while (!feof (f))
  680. {
  681. #ifdef WIN32
  682. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  683. #else
  684. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  685. #endif
  686. {
  687. if (!strcasecmp(type, "Defaultstatus")) {
  688. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  689. DEFAULTSTATUS = atoi(buf);
  690. }
  691. }
  692. }
  693. }
  694. fclose (f);
  695. f=fopen (MAIN_INI_FILE, "r");
  696. do {
  697. if (fgets (buf, 200, f) == NULL || feof(f))
  698. {
  699. LogWrite(INIT__ERROR, 0, "Init", "[UpdateServer] block not found in %s", MAIN_INI_FILE);
  700. fclose (f);
  701. return true;
  702. }
  703. }
  704. while (strncasecmp (buf, "[UpdateServer]\n", 15) != 0 && strncasecmp (buf, "[UpdateServer]\r\n", 16) != 0);
  705. while (!feof (f))
  706. {
  707. #ifdef WIN32
  708. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  709. #else
  710. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  711. #endif
  712. {
  713. if (!strcasecmp(type, "updateserveraddress")) {
  714. strncpy (updateaddress, buf, 250);
  715. patch.SetHost(buf);
  716. }
  717. if (!strcasecmp(type, "updateserverport")) {
  718. updateport=atoi(buf);
  719. patch.SetPort(buf);
  720. }
  721. }
  722. }
  723. fclose (f);
  724. LogWrite(INIT__DEBUG, 0, "Init", "%s read...", MAIN_INI_FILE);
  725. LoginServerInfo=1;
  726. return true;
  727. }
  728. char* NetConnection::GetUpdateServerInfo(int16* oPort) {
  729. if (oPort == 0)
  730. return 0;
  731. if (updateaddress[0] == 0)
  732. return 0;
  733. *oPort = updateport;
  734. return updateaddress;
  735. }
  736. char* NetConnection::GetLoginInfo(int16* oPort) {
  737. if (oPort == 0)
  738. return 0;
  739. if (loginaddress[0][0] == 0)
  740. return 0;
  741. int8 tmp[3] = { 0, 0, 0 };
  742. int8 count = 0;
  743. for (int i=0; i<3; i++) {
  744. if (loginaddress[i][0])
  745. tmp[count++] = i;
  746. }
  747. int x = rand() % count;
  748. *oPort = loginport[tmp[x]];
  749. return loginaddress[tmp[x]];
  750. }
  751. void UpdateWindowTitle(char* iNewTitle) {
  752. char tmp[500];
  753. if (iNewTitle) {
  754. snprintf(tmp, sizeof(tmp), "World: %s", iNewTitle);
  755. }
  756. else {
  757. snprintf(tmp, sizeof(tmp), "%s, Version: %s: %i Clients(s) in %i Zones(s)", EQ2EMU_MODULE, CURRENT_VERSION, numclients, numzones);
  758. }
  759. // Zero terminate ([max - 1] = 0) the string to prevent a warning
  760. tmp[499] = 0;
  761. #ifdef WIN32
  762. SetConsoleTitle(tmp);
  763. #else
  764. printf("%c]0;%s%c", '\033', tmp, '\007');
  765. #endif
  766. }
  767. ZoneAuthRequest::ZoneAuthRequest(int32 account_id, char* name, int32 access_key) {
  768. accountid = account_id;
  769. character_name = string(name);
  770. accesskey = access_key;
  771. timestamp = Timer::GetUnixTimeStamp();
  772. firstlogin = false;
  773. }
  774. ZoneAuthRequest::~ZoneAuthRequest ( )
  775. {
  776. }
  777. void ZoneAuth::AddAuth(ZoneAuthRequest *zar) {
  778. LogWrite(NET__DEBUG, 0, "Net", "AddAuth: %u Key: %u", zar->GetAccountID(), zar->GetAccessKey());
  779. list.Insert(zar);
  780. }
  781. ZoneAuthRequest* ZoneAuth::GetAuth(int32 account_id, int32 access_key) {
  782. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  783. iterator.Reset();
  784. while(iterator.MoreElements()) {
  785. if (iterator.GetData()->GetAccountID() == account_id && iterator.GetData()->GetAccessKey() == access_key) {
  786. ZoneAuthRequest* tmp = iterator.GetData();
  787. return tmp;
  788. }
  789. iterator.Advance();
  790. }
  791. return 0;
  792. }
  793. void ZoneAuth::PurgeInactiveAuth() {
  794. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  795. iterator.Reset();
  796. int32 current_time = Timer::GetUnixTimeStamp();
  797. while(iterator.MoreElements()) {
  798. if ((iterator.GetData()->GetTimeStamp()+60) < current_time) {
  799. iterator.RemoveCurrent();
  800. }
  801. iterator.Advance();
  802. }
  803. }
  804. void ZoneAuth::RemoveAuth(ZoneAuthRequest *zar) {
  805. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  806. iterator.Reset();
  807. while(iterator.MoreElements()) {
  808. if (iterator.GetData() == zar) {
  809. iterator.RemoveCurrent();
  810. break;
  811. }
  812. iterator.Advance();
  813. }
  814. }
  815. void WelcomeHeader()
  816. {
  817. #ifdef _WIN32
  818. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  819. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  820. #endif
  821. printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
  822. #ifdef _WIN32
  823. SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
  824. #endif
  825. printf("\n\nCopyright (C) 2007-2011 EQ2Emulator. http://eq2emulator.net/ \n\n");
  826. printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
  827. printf("it under the terms of the GNU General Public License as published by\n");
  828. printf("the Free Software Foundation, either version 3 of the License, or\n");
  829. printf("(at your option) any later version.\n\n");
  830. printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
  831. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  832. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  833. printf("GNU General Public License for more details.\n\n");
  834. #ifdef _WIN32
  835. SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
  836. #endif
  837. printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
  838. printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
  839. printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
  840. printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
  841. printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
  842. printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
  843. printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
  844. printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
  845. printf(" \\__/ \n\n");
  846. #ifdef _WIN32
  847. SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
  848. #endif
  849. printf(" Website : http://eq2emulator.net \n");
  850. printf(" Forums : http://eq2emulator.net/phpBB3/ \n");
  851. printf(" Contributors: http://eq2emulator.net/wiki/index.php/Developer:Contributors \n");
  852. printf(" SVN : http://svn.eq2emulator.net/svn/eq2server \n\n");
  853. #ifdef _WIN32
  854. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  855. #endif
  856. printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
  857. #ifdef _WIN32
  858. SetConsoleTextAttribute(console, FOREGROUND_WHITE);
  859. #endif
  860. fflush(stdout);
  861. }