net.cpp 30 KB

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