net.cpp 29 KB

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