9
3

net.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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.PingNewDB();
  396. database.PingAsyncDatabase();
  397. if (net.LoginServerInfo && loginserver.Connected() == false && loginserver.CanReconnect()) {
  398. LogWrite(WORLD__DEBUG, 0, "Thread", "Starting autoinit loginserver thread...");
  399. #ifdef WIN32
  400. _beginthread(AutoInitLoginServer, 0, NULL);
  401. #else
  402. pthread_t thread;
  403. pthread_create(&thread, NULL, &AutoInitLoginServer, NULL);
  404. pthread_detach(thread);
  405. #endif
  406. }
  407. }
  408. #ifndef NO_CATCH
  409. }
  410. catch(...) {
  411. LogWrite(WORLD__ERROR, 0, "World", "Exception caught in net main loop!");
  412. }
  413. #endif
  414. if (numclients == 0) {
  415. Sleep(10);
  416. continue;
  417. }
  418. Sleep(1);
  419. }
  420. LogWrite(WORLD__DEBUG, 0, "World", "The world is ending!");
  421. LogWrite(WORLD__DEBUG, 0, "IRC", "Shutting IRC down");
  422. irc.SetRunning(false);
  423. LogWrite(WORLD__DEBUG, 0, "World", "Shutting down zones...");
  424. zone_list.ShutDownZones();
  425. LogWrite(WORLD__DEBUG, 0, "World", "Shutting down LUA interface...");
  426. safe_delete(lua_interface);
  427. safe_delete(TimeoutTimer);
  428. eqsf.Close();
  429. map<int16, OpcodeManager*>::iterator opcode_itr;
  430. for(opcode_itr=EQOpcodeManager.begin();opcode_itr!=EQOpcodeManager.end();opcode_itr++){
  431. safe_delete(opcode_itr->second);
  432. }
  433. CheckEQEMuErrorAndPause();
  434. #ifdef PROFILER
  435. PROFILER_UPDATE();
  436. PROFILER_OUTPUT();
  437. #endif
  438. LogWrite(WORLD__INFO, 0, "World", "Exiting... we hope you enjoyed your flight.");
  439. LogStop();
  440. return 0;
  441. }
  442. ThreadReturnType ItemLoad (void* tmp)
  443. {
  444. LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread started.");
  445. #ifdef WIN32
  446. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  447. #endif
  448. if (tmp == 0) {
  449. ThrowError("ItemLoad(): tmp = 0!");
  450. THREAD_RETURN(NULL);
  451. }
  452. World* world = (World*) tmp;
  453. WorldDatabase db;
  454. db.Init();
  455. db.ConnectNewDatabase();
  456. LogWrite(ITEM__INFO, 0, "Items", "Loading Items...");
  457. db.LoadItemList();
  458. MasterItemList::ResetUniqueID(db.LoadNextUniqueItemID());
  459. // Relies on the item list so needs to be in the item thread
  460. LogWrite(COLLECTION__INFO, 0, "Collect", "Loading Collections...");
  461. db.LoadCollections();
  462. LogWrite(MERCHANT__INFO, 0, "Merchants", "Loading Merchants...");
  463. db.LoadMerchantInformation();
  464. LogWrite(QUEST__INFO, 0, "Quests", "Loading Quests...");
  465. db.LoadQuests();
  466. world->items_loaded = true;
  467. LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread completed.");
  468. mysql_thread_end();
  469. THREAD_RETURN(NULL);
  470. }
  471. ThreadReturnType SpellLoad (void* tmp)
  472. {
  473. LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread started.");
  474. #ifdef WIN32
  475. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  476. #endif
  477. if (tmp == 0) {
  478. ThrowError("ItemLoad(): tmp = 0!");
  479. THREAD_RETURN(NULL);
  480. }
  481. World* world = (World*) tmp;
  482. WorldDatabase db;
  483. db.Init();
  484. db.ConnectNewDatabase();
  485. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spells...");
  486. db.LoadSpells();
  487. LogWrite(SPELL__INFO, 0, "Spells", "Loading Spell Errors...");
  488. db.LoadSpellErrors();
  489. LogWrite(WORLD__INFO, 0, "Traits", "Loading Traits...");
  490. db.LoadTraits();
  491. world->spells_loaded = true;
  492. LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread completed.");
  493. mysql_thread_end();
  494. THREAD_RETURN(NULL);
  495. }
  496. ThreadReturnType AchievmentLoad (void* tmp)
  497. {
  498. LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread started.");
  499. #ifdef WIN32
  500. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  501. #endif
  502. if (tmp == 0) {
  503. ThrowError("ItemLoad(): tmp = 0!");
  504. THREAD_RETURN(NULL);
  505. }
  506. World* world = (World*) tmp;
  507. WorldDatabase db;
  508. db.Init();
  509. db.ConnectNewDatabase();
  510. LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Loading Achievements...");
  511. int32 t_now = Timer::GetUnixTimeStamp();
  512. db.LoadAchievements();
  513. master_achievement_list.CreateMasterAchievementListPacket();
  514. LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Achievements loaded (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);
  515. world->achievments_loaded = true;
  516. LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread completed.");
  517. mysql_thread_end();
  518. THREAD_RETURN(NULL);
  519. }
  520. ThreadReturnType EQ2ConsoleListener(void* tmp)
  521. {
  522. char cmd[300];
  523. size_t i = 0;
  524. size_t len;
  525. while( RunLoops )
  526. {
  527. // Read in single line from "stdin"
  528. memset( cmd, 0, sizeof( cmd ) );
  529. if( fgets( cmd, 300, stdin ) == NULL )
  530. continue;
  531. if( !RunLoops )
  532. break;
  533. len = strlen(cmd);
  534. for( i = 0; i < len; ++i )
  535. {
  536. if(cmd[i] == '\n' || cmd[i] == '\r')
  537. cmd[i] = '\0';
  538. }
  539. ProcessConsoleInput(cmd);
  540. }
  541. THREAD_RETURN(NULL);
  542. }
  543. #include <fstream>
  544. void CatchSignal(int sig_num) {
  545. // In rare cases this can be called after the log system is shut down causing a deadlock or crash
  546. // when the world shuts down, if this happens again comment out the LogWrite and uncomment the printf
  547. if (last_signal != sig_num){
  548. static Mutex lock;
  549. static ofstream signal_out;
  550. lock.lock();
  551. if (!signal_out.is_open())
  552. signal_out.open("signal_catching.log", ios::trunc);
  553. if (signal_out){
  554. signal_out << "Caught signal " << sig_num << "\n";
  555. signal_out.flush();
  556. }
  557. printf("Caught signal %i\n", sig_num);
  558. lock.unlock();
  559. last_signal = sig_num;
  560. RunLoops = false;
  561. }
  562. }
  563. bool NetConnection::ReadLoginINI() {
  564. char buf[201], type[201];
  565. int items[3] = {0, 0};
  566. FILE *f;
  567. if (!(f = fopen (MAIN_INI_FILE, "r"))) {
  568. LogWrite(INIT__ERROR, 0, "Init", "File '%s' could not be opened", MAIN_INI_FILE);
  569. return false;
  570. }
  571. do {
  572. if (fgets (buf, 200, f) == NULL || feof(f))
  573. {
  574. LogWrite(INIT__ERROR, 0, "Init", "[LoginServer] block not found in '%s'.", MAIN_INI_FILE);
  575. fclose (f);
  576. return false;
  577. }
  578. }
  579. while (strncasecmp (buf, "[LoginServer]\n", 14) != 0 && strncasecmp (buf, "[LoginServer]\r\n", 15) != 0);
  580. while (!feof (f))
  581. {
  582. #ifdef WIN32
  583. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  584. #else
  585. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  586. #endif
  587. {
  588. if (!strncasecmp(type, "[", 1)) {
  589. // new block after LoginServer, skip
  590. break;
  591. }
  592. if (!strncasecmp (type, "worldname", 9)) {
  593. snprintf(worldname, sizeof(worldname), "%s", buf);
  594. items[1] = 1;
  595. if(strlen(worldname)<4)
  596. LogWrite(INIT__ERROR, 0, "Init", "Invalid worldname, please edit LoginServer.ini. Server name must be at least 4 characters.");
  597. }
  598. if (!strncasecmp (type, "account", 7)) {
  599. strncpy(worldaccount, buf, 30);
  600. }
  601. if (!strncasecmp (type, "logstats", 8)) {
  602. if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
  603. net.UpdateStats = true;
  604. }
  605. if (!strncasecmp (type, "password", 8)) {
  606. strncpy (worldpassword, buf, 30);
  607. for(int i=strlen(worldpassword);i>=0;i--){
  608. if(worldpassword[i] == ' ' || worldpassword[i] == '\n' || worldpassword[i] == '\t' || worldpassword[i] == '\r')
  609. worldpassword[i] = '\0';
  610. }
  611. }
  612. if (!strncasecmp (type, "locked", 6)) {
  613. if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
  614. world_locked = true;
  615. }
  616. if (!strncasecmp (type, "worldaddress", 12)) {
  617. if (strlen(buf) >= 3) {
  618. strncpy (worldaddress, buf, 250);
  619. }
  620. }
  621. if (!strncasecmp(type, "autotableupdates", 16)) {
  622. if(strlen(buf) >= 3 && !strncasecmp(buf, "ask", 3))
  623. loginserver.UpdatesAsk(true);
  624. else if(strlen(buf) >= 6 && !strncasecmp(buf, "always", 6))
  625. loginserver.UpdatesAuto(true);
  626. }
  627. if (!strncasecmp (type, "autotableverbose", 16)) {
  628. if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
  629. loginserver.UpdatesVerbose(true);
  630. }
  631. if (!strncasecmp (type, "autotabledata", 13)) {
  632. if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
  633. loginserver.UpdatesAutoData(true);
  634. }
  635. if (!strncasecmp (type, "internalworldaddress", 20)) {
  636. if (strlen(buf) >= 3) {
  637. strncpy(internalworldaddress, buf, 20);
  638. }
  639. }
  640. if (!strncasecmp (type, "worldport", 9)) {
  641. if(Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF)
  642. worldport = atoi(buf);
  643. }
  644. if ((!strcasecmp (type, "loginserver")) || (!strcasecmp (type, "loginserver1"))) {
  645. strncpy (loginaddress[0], buf, 100);
  646. items[0] = 1;
  647. }
  648. if ((!strcasecmp(type, "loginport")) || (!strcasecmp(type, "loginport1"))) {
  649. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  650. loginport[0] = atoi(buf);
  651. }
  652. }
  653. if (!strcasecmp (type, "loginserver2")) {
  654. strncpy (loginaddress[1], buf, 250);
  655. }
  656. if (!strcasecmp(type, "loginport2")) {
  657. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  658. loginport[1] = atoi(buf);
  659. }
  660. }
  661. if (!strcasecmp (type, "loginserver3")) {
  662. strncpy (loginaddress[2], buf, 250);
  663. }
  664. if (!strcasecmp(type, "loginport3")) {
  665. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  666. loginport[2] = atoi(buf);
  667. }
  668. }
  669. }
  670. }
  671. if (!items[0] || !items[1])
  672. {
  673. LogWrite(INIT__ERROR, 0, "Init", "Incomplete LoginServer.INI file.");
  674. fclose (f);
  675. return false;
  676. }
  677. /*
  678. if (strcasecmp(worldname, "Unnamed server") == 0) {
  679. cout << "LoginServer.ini: server unnamed, disabling uplink" << endl;
  680. fclose (f);
  681. return false;
  682. }
  683. */
  684. fclose(f);
  685. f=fopen (MAIN_INI_FILE, "r");
  686. do {
  687. if (fgets (buf, 200, f) == NULL || feof(f))
  688. {
  689. LogWrite(INIT__ERROR, 0, "Init", "[WorldServer] block not found in %s", MAIN_INI_FILE);
  690. fclose (f);
  691. return true;
  692. }
  693. }
  694. while (strncasecmp (buf, "[WorldServer]\n", 14) != 0 && strncasecmp (buf, "[WorldServer]\r\n", 15) != 0);
  695. while (!feof (f))
  696. {
  697. #ifdef WIN32
  698. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  699. #else
  700. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  701. #endif
  702. {
  703. if (!strcasecmp(type, "Defaultstatus")) {
  704. if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
  705. DEFAULTSTATUS = atoi(buf);
  706. }
  707. }
  708. }
  709. }
  710. fclose (f);
  711. f=fopen (MAIN_INI_FILE, "r");
  712. do {
  713. if (fgets (buf, 200, f) == NULL || feof(f))
  714. {
  715. LogWrite(INIT__ERROR, 0, "Init", "[UpdateServer] block not found in %s", MAIN_INI_FILE);
  716. fclose (f);
  717. return true;
  718. }
  719. }
  720. while (strncasecmp (buf, "[UpdateServer]\n", 15) != 0 && strncasecmp (buf, "[UpdateServer]\r\n", 16) != 0);
  721. while (!feof (f))
  722. {
  723. #ifdef WIN32
  724. if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
  725. #else
  726. if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
  727. #endif
  728. {
  729. if (!strcasecmp(type, "updateserveraddress")) {
  730. strncpy (updateaddress, buf, 250);
  731. patch.SetHost(buf);
  732. }
  733. if (!strcasecmp(type, "updateserverport")) {
  734. updateport=atoi(buf);
  735. patch.SetPort(buf);
  736. }
  737. }
  738. }
  739. fclose (f);
  740. LogWrite(INIT__DEBUG, 0, "Init", "%s read...", MAIN_INI_FILE);
  741. LoginServerInfo=1;
  742. return true;
  743. }
  744. char* NetConnection::GetUpdateServerInfo(int16* oPort) {
  745. if (oPort == 0)
  746. return 0;
  747. if (updateaddress[0] == 0)
  748. return 0;
  749. *oPort = updateport;
  750. return updateaddress;
  751. }
  752. char* NetConnection::GetLoginInfo(int16* oPort) {
  753. if (oPort == 0)
  754. return 0;
  755. if (loginaddress[0][0] == 0)
  756. return 0;
  757. int8 tmp[3] = { 0, 0, 0 };
  758. int8 count = 0;
  759. for (int i=0; i<3; i++) {
  760. if (loginaddress[i][0])
  761. tmp[count++] = i;
  762. }
  763. int x = rand() % count;
  764. *oPort = loginport[tmp[x]];
  765. return loginaddress[tmp[x]];
  766. }
  767. void UpdateWindowTitle(char* iNewTitle) {
  768. char tmp[500];
  769. if (iNewTitle) {
  770. snprintf(tmp, sizeof(tmp), "World: %s", iNewTitle);
  771. }
  772. else {
  773. snprintf(tmp, sizeof(tmp), "%s, Version: %s: %i Clients(s) in %i Zones(s)", EQ2EMU_MODULE, CURRENT_VERSION, numclients, numzones);
  774. }
  775. // Zero terminate ([max - 1] = 0) the string to prevent a warning
  776. tmp[499] = 0;
  777. #ifdef WIN32
  778. SetConsoleTitle(tmp);
  779. #else
  780. printf("%c]0;%s%c", '\033', tmp, '\007');
  781. #endif
  782. }
  783. ZoneAuthRequest::ZoneAuthRequest(int32 account_id, char* name, int32 access_key) {
  784. accountid = account_id;
  785. character_name = string(name);
  786. accesskey = access_key;
  787. timestamp = Timer::GetUnixTimeStamp();
  788. firstlogin = false;
  789. }
  790. ZoneAuthRequest::~ZoneAuthRequest ( )
  791. {
  792. }
  793. void ZoneAuth::AddAuth(ZoneAuthRequest *zar) {
  794. LogWrite(NET__DEBUG, 0, "Net", "AddAuth: %u Key: %u", zar->GetAccountID(), zar->GetAccessKey());
  795. list.Insert(zar);
  796. }
  797. ZoneAuthRequest* ZoneAuth::GetAuth(int32 account_id, int32 access_key) {
  798. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  799. iterator.Reset();
  800. while(iterator.MoreElements()) {
  801. if (iterator.GetData()->GetAccountID() == account_id && iterator.GetData()->GetAccessKey() == access_key) {
  802. ZoneAuthRequest* tmp = iterator.GetData();
  803. return tmp;
  804. }
  805. iterator.Advance();
  806. }
  807. return 0;
  808. }
  809. void ZoneAuth::PurgeInactiveAuth() {
  810. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  811. iterator.Reset();
  812. int32 current_time = Timer::GetUnixTimeStamp();
  813. while(iterator.MoreElements()) {
  814. if ((iterator.GetData()->GetTimeStamp()+60) < current_time) {
  815. iterator.RemoveCurrent();
  816. }
  817. iterator.Advance();
  818. }
  819. }
  820. void ZoneAuth::RemoveAuth(ZoneAuthRequest *zar) {
  821. LinkedListIterator<ZoneAuthRequest*> iterator(list);
  822. iterator.Reset();
  823. while(iterator.MoreElements()) {
  824. if (iterator.GetData() == zar) {
  825. iterator.RemoveCurrent();
  826. break;
  827. }
  828. iterator.Advance();
  829. }
  830. }
  831. void WelcomeHeader()
  832. {
  833. #ifdef _WIN32
  834. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  835. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  836. #endif
  837. printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
  838. #ifdef _WIN32
  839. SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
  840. #endif
  841. printf("\n\nCopyright (C) 2007-2011 EQ2Emulator. http://eq2emulator.net/ \n\n");
  842. printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
  843. printf("it under the terms of the GNU General Public License as published by\n");
  844. printf("the Free Software Foundation, either version 3 of the License, or\n");
  845. printf("(at your option) any later version.\n\n");
  846. printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
  847. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  848. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  849. printf("GNU General Public License for more details.\n\n");
  850. #ifdef _WIN32
  851. SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
  852. #endif
  853. printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
  854. printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
  855. printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
  856. printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
  857. printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
  858. printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
  859. printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
  860. printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
  861. printf(" \\__/ \n\n");
  862. #ifdef _WIN32
  863. SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
  864. #endif
  865. printf(" Website : http://eq2emulator.net \n");
  866. printf(" Forums : http://eq2emulator.net/phpBB3/ \n");
  867. printf(" Contributors: http://eq2emulator.net/wiki/index.php/Developer:Contributors \n");
  868. printf(" SVN : http://svn.eq2emulator.net/svn/eq2server \n\n");
  869. #ifdef _WIN32
  870. SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
  871. #endif
  872. printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
  873. #ifdef _WIN32
  874. SetConsoleTextAttribute(console, FOREGROUND_WHITE);
  875. #endif
  876. fflush(stdout);
  877. }