net.cpp 30 KB

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