123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996 |
- /*
- EQ2Emulator: Everquest II Server Emulator
- Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
- This file is part of EQ2Emulator.
- EQ2Emulator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- EQ2Emulator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "../common/debug.h"
- #include "../common/Log.h"
- #include <iostream>
- using namespace std;
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <algorithm>
- #include <chrono>
- #include <signal.h>
- #include "../common/queue.h"
- #include "../common/timer.h"
- #include "../common/EQStreamFactory.h"
- #include "../common/EQStream.h"
- #include "net.h"
- #include "Variables.h"
- #include "WorldDatabase.h"
- #include "../common/seperator.h"
- #include "../common/version.h"
- #include "../common/EQEMuError.h"
- #include "../common/opcodemgr.h"
- #include "../common/Common_Defines.h"
- #include "LoginServer.h"
- #include "Commands/Commands.h"
- #include "Factions.h"
- #include "World.h"
- #include "../common/ConfigReader.h"
- #include "Skills.h"
- #include "LuaInterface.h"
- #include "Guilds/Guild.h"
- #include "Commands/ConsoleCommands.h"
- #include "Traits/Traits.h"
- #include "Transmute.h"
- #include "Zone/ChestTrap.h"
- double frame_time = 0.0;
- #ifdef WIN32
- #include <process.h>
- #define strncasecmp _strnicmp
- #define strcasecmp _stricmp
- #include <conio.h>
- #else
- #include <pthread.h>
- #include "../common/unix.h"
- #endif
- #ifdef PROFILER
- #define SHINY_PROFILER TRUE
- #include "../Profiler/src/Shiny.h"
- #endif
- NetConnection net;
- World world;
- EQStreamFactory eqsf(LoginStream);
- LoginServer loginserver;
- LuaInterface* lua_interface = new LuaInterface();
- #include "MutexList.h"
- #include "Rules/Rules.h"
- #include "Titles.h"
- #include "Languages.h"
- #include "Achievements/Achievements.h"
- #include "Patch/patch.h"
- volatile bool RunLoops = true;
- sint32 numclients = 0;
- sint32 numzones = 0;
- extern ClientList client_list;
- extern ZoneList zone_list;
- extern MasterFactionList master_faction_list;
- extern WorldDatabase database;
- extern MasterSpellList master_spell_list;
- extern MasterTraitList master_trait_list;
- extern MasterSkillList master_skill_list;
- extern MasterItemList master_item_list;
- extern GuildList guild_list;
- extern Variables variables;
- ConfigReader configReader;
- int32 MasterItemList::next_unique_id = 0;
- int last_signal = 0;
- RuleManager rule_manager;
- MasterTitlesList master_titles_list;
- MasterLanguagesList master_languages_list;
- ChestTrapList chest_trap_list;
- extern MasterAchievementList master_achievement_list;
- extern map<int16, int16> EQOpcodeVersions;
- PatchServer patch;
- ThreadReturnType ItemLoad (void* tmp);
- ThreadReturnType AchievmentLoad (void* tmp);
- ThreadReturnType SpellLoad (void* tmp);
- int main(int argc, char** argv) {
- #ifdef PROFILER
- PROFILE_FUNC();
- #endif
- int32 t_total = Timer::GetUnixTimeStamp();
- LogStart();
- LogParseConfigs();
- net.WelcomeHeader();
- LogWrite(INIT__INFO, 0, "Init", "Starting EQ2Emulator WorldServer...");
- //int32 server_startup = time(NULL);
- //remove this when all database calls are using the new database class
- if (!database.Init()) {
- LogStop();
- return EXIT_FAILURE;
- }
- if (!database.ConnectNewDatabase())
- return EXIT_FAILURE;
- #ifdef _DEBUG
- _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
- #endif
-
- if (signal(SIGINT, CatchSignal) == SIG_ERR) {
- LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
- return 0;
- }
- if (signal(SIGSEGV, CatchSignal) == SIG_ERR) {
- LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
- return 0;
- }
- if (signal(SIGILL, CatchSignal) == SIG_ERR) {
- LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
- return 0;
- }
- #ifndef WIN32
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
- LogWrite(INIT__ERROR, 0, "Init", "Could not set signal handler");
- return 0;
- }
- #endif
-
- LogWrite(WORLD__DEBUG, 0, "World", "Randomizing World...");
- srand(time(NULL));
- net.ReadLoginINI();
- if(loginserver.UpdatesAuto() || loginserver.UpdatesAsk() || loginserver.UpdatesAutoData()){
- LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "Connecting to DB PatchServer...");
- // Old DB patch server
- /*int16 updateport = 0;
- char* updateaddress = net.GetUpdateServerInfo(&updateport);
- loginserver.ConnectToUpdateServer(updateaddress, updateport);
- LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "DB Update check completed...");*/
-
- //New patch server
- if (!patch.IsEnabled())
- LogWrite(INIT__PATCHER_INFO, 0, "Patcher", "Not checking patch server for updates");
- else {
- bool success = patch.Start();
- if (success)
- success = patch.Process();
- patch.Stop();
- //if (patch.QuitAfter())
- //looping = false;
- }
- }
-
- // JA: Grouping all System (core) data loads together for timing purposes
- LogWrite(WORLD__INFO, 0, "World", "Loading System Data...");
- int32 t_now = Timer::GetUnixTimeStamp();
- LogWrite(WORLD__DEBUG, 1, "World", "-Loading opcodes...");
- EQOpcodeVersions = database.GetVersions();
- map<int16,int16>::iterator version_itr;
- int16 version1 = 0;
- for (version_itr = EQOpcodeVersions.begin(); version_itr != EQOpcodeVersions.end(); version_itr++) {
- version1 = version_itr->first;
- EQOpcodeManager[version1] = new RegularOpcodeManager();
- map<string, uint16> eq = database.GetOpcodes(version1);
- if(!EQOpcodeManager[version1]->LoadOpcodes(&eq)) {
- LogWrite(INIT__ERROR, 0, "Init", "Loading opcodes failed. Make sure you have sourced the opcodes.sql file!");
- return false;
- }
- }
- LogWrite(WORLD__DEBUG, 1, "World", "-Loading structs...");
- if(!configReader.LoadFile("CommonStructs.xml") || !configReader.LoadFile("WorldStructs.xml") || !configReader.LoadFile("SpawnStructs.xml") || !configReader.LoadFile("ItemStructs.xml")) {
- 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!");
- return false;
- }
- world.init();
- bool threadedLoad = rule_manager.GetGlobalRule(R_World, ThreadedLoad)->GetBool();
- LogWrite(WORLD__DEBUG, 1, "World", "-Loading EQ2 time of day...");
- loginserver.InitLoginServerVariables();
- LogWrite(WORLD__INFO, 0, "World", "Loaded System Data (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);
- // JA: End System Data loading functions
- if (threadedLoad) {
- LogWrite(WORLD__WARNING, 0, "Threaded", "Using Threaded loading of static data...");
- #ifdef WIN32
- _beginthread(ItemLoad, 0, &world);
- _beginthread(SpellLoad, 0, &world);
- //_beginthread(AchievmentLoad, 0, &world);
- #else
- pthread_t thread;
- pthread_create(&thread, NULL, ItemLoad, &world);
- pthread_detach(thread);
- pthread_t thread2;
- pthread_create(&thread2, NULL, SpellLoad, &world);
- pthread_detach(thread2);
- //pthread_t thread3;
- //pthread_create(&thread3, NULL, AchievmentLoad, &world);
- //pthread_detach(thread3);
- #endif
- }
- // Called as a function so we can use /reload spawns any time
- /*LogWrite(SPAWN__INFO, 0, "Spawn", "Initializing Spawn Subsystem...");
- t_now = Timer::GetUnixTimeStamp();
- world.LoadSpawnInformation();
- LogWrite(SPAWN__INFO, 0, "Spawn", "Initialize Spawn Subsystem complete (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);*/
- // JA temp logger
- LogWrite(MISC__TODO, 0, "Reformat", "JA: This is as far as I got reformatting the console logs.");
- if (!threadedLoad) {
- // JA: Load all Item info
- LogWrite(ITEM__INFO, 0, "Items", "Loading Items...");
- database.LoadItemList();
- MasterItemList::ResetUniqueID(database.LoadNextUniqueItemID());
- }
- if (!threadedLoad) {
- // JA: Load all Spell info
- LogWrite(SPELL__INFO, 0, "Spells", "Loading Spells...");
- database.LoadSpells();
- LogWrite(SPELL__INFO, 0, "Spells", "Loading Spell Errors...");
- database.LoadSpellErrors();
- // Jabantiz: Load traits
- LogWrite(WORLD__INFO, 0, "Traits", "Loading Traits...");
- database.LoadTraits();
- }
- if (!threadedLoad) {
- // JA: Load all Quest info
- LogWrite(QUEST__INFO, 0, "Quests", "Loading Quests...");
- database.LoadQuests();
- }
- if (!threadedLoad) {
- LogWrite(COLLECTION__INFO, 0, "Collect", "Loading Collections...");
- database.LoadCollections();
- }
- LogWrite(GUILD__INFO, 0, "Guilds", "Loading Guilds...");
- database.LoadGuilds();
- LogWrite(TRADESKILL__INFO, 0, "Recipes", "Loading Recipe Books...");
- database.LoadRecipeBooks();
- LogWrite(TRADESKILL__INFO, 0, "Recipes", "Loading Recipes...");
- database.LoadRecipes();
- LogWrite(TRADESKILL__INFO, 0, "Tradeskills", "Loading Tradeskill Events...");
- database.LoadTradeskillEvents();
- if (!threadedLoad) {
- LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Loading Achievements...");
- //database.LoadAchievements();
- //master_achievement_list.CreateMasterAchievementListPacket();
- }
-
- LogWrite(SPELL__INFO, 0, "AA", "Loading Alternate Advancements...");
- database.LoadAltAdvancements();
- LogWrite(SPELL__INFO, 0, "AA", "Loading AA Tree Nodes...");
- database.LoadTreeNodes();
- LogWrite(WORLD__INFO, 0, "Titles", "Loading Titles...");
- database.LoadTitles();
- LogWrite(WORLD__INFO, 0, "Languages", "Loading Languages...");
- database.LoadLanguages();
- LogWrite(CHAT__INFO, 0, "Chat", "Loading channels...");
- database.LoadChannels();
- if (!threadedLoad) {
- LogWrite(MERCHANT__INFO, 0, "Merchants", "Loading Merchants...");
- database.LoadMerchantInformation();
- }
- LogWrite(LUA__INFO, 0, "LUA", "Loading Spawn Scripts...");
- database.LoadSpawnScriptData();
- LogWrite(LUA__INFO, 0, "LUA", "Loading Zone Scripts...");
- database.LoadZoneScriptData();
- LogWrite(WORLD__INFO, 0, "World", "Loading House Zone Data...");
- database.LoadHouseZones();
- database.LoadPlayerHouses();
- LogWrite(WORLD__INFO, 0, "World", "Loading Heroic OP Data...");
- database.LoadHOStarters();
- database.LoadHOWheel();
- LogWrite(WORLD__INFO, 0, "World", "Loading Race Types Data...");
- database.LoadRaceTypes();
- LogWrite(WORLD__INFO, 0, "World", "Loading Transmuting Data...");
- database.LoadTransmuting();
- LogWrite(WORLD__INFO, 0, "World", "Loading Chest Trap Data...");
- database.LoadChestTraps();
- if (threadedLoad) {
- LogWrite(WORLD__INFO, 0, "World", "Waiting for load threads to finish.");
- while (!world.items_loaded || !world.spells_loaded /*|| !world.achievments_loaded*/)
- Sleep(10);
- LogWrite(WORLD__INFO, 0, "World", "Load threads finished.");
- }
- LogWrite(WORLD__INFO, 0, "World", "Total World startup time: %u seconds.", Timer::GetUnixTimeStamp() - t_total);
- int ret_code = 0;
- if (eqsf.Open(net.GetWorldPort())) {
- if (strlen(net.GetWorldAddress()) == 0)
- LogWrite(NET__INFO, 0, "Net", "World server listening on port %i", net.GetWorldPort());
- else
- LogWrite(NET__INFO, 0, "Net", "World server listening on: %s:%i", net.GetWorldAddress(), net.GetWorldPort());
- if(strlen(net.GetInternalWorldAddress())>0)
- LogWrite(NET__INFO, 0, "Net", "World server listening on: %s:%i", net.GetInternalWorldAddress(), net.GetWorldPort());
- }
- else {
- LogWrite(NET__ERROR, 0, "Net", "Failed to open port %i.", net.GetWorldPort());
- ret_code = 1;
- }
- Timer* TimeoutTimer = 0;
- if (ret_code == 0) {
- Timer InterserverTimer(INTERSERVER_TIMER); // does MySQL pings and auto-reconnect
- InterserverTimer.Trigger();
- TimeoutTimer = new Timer(5000);
- TimeoutTimer->Start();
- EQStream* eqs = 0;
- UpdateWindowTitle(0);
- LogWrite(ZONE__INFO, 0, "Zone", "Starting static zones...");
- database.LoadSpecialZones();
- map<EQStream*, int32> connecting_clients;
- map<EQStream*, int32>::iterator cc_itr;
-
- LogWrite(WORLD__DEBUG, 0, "Thread", "Starting console command thread...");
- #ifdef WIN32
- _beginthread(EQ2ConsoleListener, 0, NULL);
- #else
- /*pthread_t thread;
- pthread_create(&thread, NULL, &EQ2ConsoleListener, NULL);
- pthread_detach(thread);*/
- #endif
- //
- // just before starting loops, announce how to get console help
- LogWrite(WORLD__INFO, 0, "Console", "Type 'help' or '?' and press enter for menu options.");
- std::chrono::time_point<std::chrono::system_clock> frame_prev = std::chrono::system_clock::now();
- while (RunLoops) {
- Timer::SetCurrentTime();
- std::chrono::time_point<std::chrono::system_clock> frame_now = std::chrono::system_clock::now();
- frame_time = std::chrono::duration_cast<std::chrono::duration<double>>(frame_now - frame_prev).count();
- frame_prev = frame_now;
- #ifndef NO_CATCH
- try
- {
- #endif
- while ((eqs = eqsf.Pop())) {
- struct in_addr in;
- in.s_addr = eqs->GetRemoteIP();
- LogWrite(NET__DEBUG, 0, "Net", "New client from ip: %s port: %i", inet_ntoa(in), ntohs(eqs->GetRemotePort()));
- // JA: Check for BannedIPs
- if (rule_manager.GetGlobalRule(R_World, UseBannedIPsTable)->GetInt8() == 1)
- {
- LogWrite(WORLD__DEBUG, 0, "World", "Checking inbound connection %s against BannedIPs table", inet_ntoa(in));
- if (database.CheckBannedIPs(inet_ntoa(in)))
- {
- LogWrite(WORLD__DEBUG, 0, "World", "Connection from %s FAILED banned IPs check. Closing connection.", inet_ntoa(in));
- eqs->Close(); // JA: If the inbound IP is on the banned table, close the EQStream.
- }
- }
- if (eqs && eqs->CheckActive() && client_list.ContainsStream(eqs) == false) {
- LogWrite(NET__DEBUG, 0, "Net", "Adding new client...");
- Client* client = new Client(eqs);
- client_list.Add(client);
- }
- else if (eqs && !client_list.ContainsStream(eqs)) {
- LogWrite(NET__DEBUG, 0, "Net", "Adding client to waiting list...");
- connecting_clients[eqs] = Timer::GetCurrentTime2();
- }
- }
- if (connecting_clients.size() > 0) {
- for (cc_itr = connecting_clients.begin(); cc_itr != connecting_clients.end(); cc_itr++) {
- if (cc_itr->first && cc_itr->first->CheckActive() && client_list.ContainsStream(cc_itr->first) == false) {
- LogWrite(NET__DEBUG, 0, "Net", "Removing client from waiting list...");
- Client* client = new Client(cc_itr->first);
- client_list.Add(client);
- connecting_clients.erase(cc_itr);
- break;
- }
- else if (Timer::GetCurrentTime2() >= (cc_itr->second + 10000)) {
- connecting_clients.erase(cc_itr);
- break;
- }
- }
- }
- world.Process();
- client_list.Process();
- loginserver.Process();
- if (TimeoutTimer->Check()) {
- eqsf.CheckTimeout();
- }
- if (InterserverTimer.Check()) {
- InterserverTimer.Start();
- database.ping();
- database.PingNewDB();
- database.PingAsyncDatabase();
- if (net.LoginServerInfo && loginserver.Connected() == false && loginserver.CanReconnect()) {
- LogWrite(WORLD__DEBUG, 0, "Thread", "Starting autoinit loginserver thread...");
- #ifdef WIN32
- _beginthread(AutoInitLoginServer, 0, NULL);
- #else
- pthread_t thread;
- pthread_create(&thread, NULL, &AutoInitLoginServer, NULL);
- pthread_detach(thread);
- #endif
- }
- }
- #ifndef NO_CATCH
- }
- catch (...) {
- LogWrite(WORLD__ERROR, 0, "World", "Exception caught in net main loop!");
- }
- #endif
- if (numclients == 0) {
- Sleep(10);
- continue;
- }
- Sleep(1);
- }
- }
- LogWrite(WORLD__DEBUG, 0, "World", "The world is ending!");
- LogWrite(WORLD__DEBUG, 0, "World", "Shutting down zones...");
- zone_list.ShutDownZones();
- LogWrite(WORLD__DEBUG, 0, "World", "Shutting down LUA interface...");
- safe_delete(lua_interface);
- safe_delete(TimeoutTimer);
- eqsf.Close();
- map<int16, OpcodeManager*>::iterator opcode_itr;
- for(opcode_itr=EQOpcodeManager.begin();opcode_itr!=EQOpcodeManager.end();opcode_itr++){
- safe_delete(opcode_itr->second);
- }
- CheckEQEMuErrorAndPause();
- #ifdef PROFILER
- PROFILER_UPDATE();
- PROFILER_OUTPUT();
- #endif
- LogWrite(WORLD__INFO, 0, "World", "Exiting... we hope you enjoyed your flight.");
- LogStop();
- return ret_code;
- }
- ThreadReturnType ItemLoad (void* tmp)
- {
- LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread started.");
- #ifdef WIN32
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
- #endif
- if (tmp == 0) {
- ThrowError("ItemLoad(): tmp = 0!");
- THREAD_RETURN(NULL);
- }
- World* world = (World*) tmp;
- WorldDatabase db;
- db.Init();
- db.ConnectNewDatabase();
-
- LogWrite(ITEM__INFO, 0, "Items", "Loading Items...");
- db.LoadItemList();
- MasterItemList::ResetUniqueID(db.LoadNextUniqueItemID());
- // Relies on the item list so needs to be in the item thread
- LogWrite(COLLECTION__INFO, 0, "Collect", "Loading Collections...");
- db.LoadCollections();
- LogWrite(MERCHANT__INFO, 0, "Merchants", "Loading Merchants...");
- db.LoadMerchantInformation();
- LogWrite(QUEST__INFO, 0, "Quests", "Loading Quests...");
- db.LoadQuests();
- world->items_loaded = true;
- LogWrite(WORLD__WARNING, 0, "Thread", "Item Loading Thread completed.");
- mysql_thread_end();
- THREAD_RETURN(NULL);
- }
- ThreadReturnType SpellLoad (void* tmp)
- {
- LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread started.");
- #ifdef WIN32
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
- #endif
- if (tmp == 0) {
- ThrowError("ItemLoad(): tmp = 0!");
- THREAD_RETURN(NULL);
- }
- World* world = (World*) tmp;
- WorldDatabase db;
- db.Init();
- db.ConnectNewDatabase();
- LogWrite(SPELL__INFO, 0, "Spells", "Loading Spells...");
- db.LoadSpells();
- LogWrite(SPELL__INFO, 0, "Spells", "Loading Spell Errors...");
- db.LoadSpellErrors();
- LogWrite(WORLD__INFO, 0, "Traits", "Loading Traits...");
- db.LoadTraits();
- world->spells_loaded = true;
- LogWrite(WORLD__WARNING, 0, "Thread", "Spell Loading Thread completed.");
- mysql_thread_end();
- THREAD_RETURN(NULL);
- }
- ThreadReturnType AchievmentLoad (void* tmp)
- {
- LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread started.");
- #ifdef WIN32
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
- #endif
- if (tmp == 0) {
- ThrowError("ItemLoad(): tmp = 0!");
- THREAD_RETURN(NULL);
- }
- World* world = (World*) tmp;
- WorldDatabase db;
- db.Init();
- db.ConnectNewDatabase();
- LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Loading Achievements...");
- int32 t_now = Timer::GetUnixTimeStamp();
- db.LoadAchievements();
- master_achievement_list.CreateMasterAchievementListPacket();
- LogWrite(ACHIEVEMENT__INFO, 0, "Achievements", "Achievements loaded (took %u seconds)", Timer::GetUnixTimeStamp() - t_now);
- world->achievments_loaded = true;
- LogWrite(WORLD__WARNING, 0, "Thread", "Achievement Loading Thread completed.");
-
- mysql_thread_end();
- THREAD_RETURN(NULL);
- }
- ThreadReturnType EQ2ConsoleListener(void* tmp)
- {
- char cmd[300];
- size_t i = 0;
- size_t len;
- while( RunLoops )
- {
- // Read in single line from "stdin"
- memset( cmd, 0, sizeof( cmd ) );
- if( fgets( cmd, 300, stdin ) == NULL )
- continue;
- if( !RunLoops )
- break;
- len = strlen(cmd);
- for( i = 0; i < len; ++i )
- {
- if(cmd[i] == '\n' || cmd[i] == '\r')
- cmd[i] = '\0';
- }
- ProcessConsoleInput(cmd);
- }
- THREAD_RETURN(NULL);
- }
- #include <fstream>
- void CatchSignal(int sig_num) {
- // In rare cases this can be called after the log system is shut down causing a deadlock or crash
- // when the world shuts down, if this happens again comment out the LogWrite and uncomment the printf
- if (last_signal != sig_num){
- static Mutex lock;
- static std::ofstream signal_out;
- lock.lock();
- if (!signal_out.is_open())
- signal_out.open("signal_catching.log", ios::trunc);
- if (signal_out){
- signal_out << "Caught signal " << sig_num << "\n";
- signal_out.flush();
- }
- printf("Caught signal %i\n", sig_num);
- lock.unlock();
- last_signal = sig_num;
- RunLoops = false;
- }
- }
- bool NetConnection::ReadLoginINI() {
- char buf[201], type[201];
- int items[3] = {0, 0};
- FILE *f;
-
- if (!(f = fopen (MAIN_INI_FILE, "r"))) {
- LogWrite(INIT__ERROR, 0, "Init", "File '%s' could not be opened", MAIN_INI_FILE);
- return false;
- }
- do {
- if (fgets (buf, 200, f) == NULL || feof(f))
- {
- LogWrite(INIT__ERROR, 0, "Init", "[LoginServer] block not found in '%s'.", MAIN_INI_FILE);
- fclose (f);
- return false;
- }
- }
- while (strncasecmp (buf, "[LoginServer]\n", 14) != 0 && strncasecmp (buf, "[LoginServer]\r\n", 15) != 0);
- while (!feof (f))
- {
- #ifdef WIN32
- if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
- #else
- if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
- #endif
- {
- if (!strncasecmp(type, "[", 1)) {
- // new block after LoginServer, skip
- break;
- }
- if (!strncasecmp (type, "worldname", 9)) {
- snprintf(worldname, sizeof(worldname), "%s", buf);
- items[1] = 1;
- if(strlen(worldname)<4)
- LogWrite(INIT__ERROR, 0, "Init", "Invalid worldname, please edit LoginServer.ini. Server name must be at least 4 characters.");
- }
- if (!strncasecmp (type, "account", 7)) {
- strncpy(worldaccount, buf, 30);
- }
- if (!strncasecmp (type, "logstats", 8)) {
- if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
- net.UpdateStats = true;
- }
- if (!strncasecmp (type, "password", 8)) {
- strncpy (worldpassword, buf, 30);
- for(int i=strlen(worldpassword);i>=0;i--){
- if(worldpassword[i] == ' ' || worldpassword[i] == '\n' || worldpassword[i] == '\t' || worldpassword[i] == '\r')
- worldpassword[i] = '\0';
- }
- }
- if (!strncasecmp (type, "locked", 6)) {
- if (strcasecmp(buf, "true") == 0 || (buf[0] == '1' && buf[1] == 0))
- world_locked = true;
- }
- if (!strncasecmp (type, "worldaddress", 12)) {
- if (strlen(buf) >= 3) {
- strncpy (worldaddress, buf, 250);
- }
- }
- if (!strncasecmp(type, "autotableupdates", 16)) {
- if(strlen(buf) >= 3 && !strncasecmp(buf, "ask", 3))
- loginserver.UpdatesAsk(true);
- else if(strlen(buf) >= 6 && !strncasecmp(buf, "always", 6))
- loginserver.UpdatesAuto(true);
- }
- if (!strncasecmp (type, "autotableverbose", 16)) {
- if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
- loginserver.UpdatesVerbose(true);
- }
- if (!strncasecmp (type, "autotabledata", 13)) {
- if(strlen(buf) >= 4 && !strncasecmp(buf, "true", 4))
- loginserver.UpdatesAutoData(true);
- }
- if (!strncasecmp (type, "internalworldaddress", 20)) {
- if (strlen(buf) >= 3) {
- strncpy(internalworldaddress, buf, 20);
- }
- }
- if (!strncasecmp (type, "worldport", 9)) {
- if(Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF)
- worldport = atoi(buf);
- }
- if ((!strcasecmp (type, "loginserver")) || (!strcasecmp (type, "loginserver1"))) {
- strncpy (loginaddress[0], buf, 100);
- items[0] = 1;
- }
- if ((!strcasecmp(type, "loginport")) || (!strcasecmp(type, "loginport1"))) {
- if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
- loginport[0] = atoi(buf);
- }
- }
- if (!strcasecmp (type, "loginserver2")) {
- strncpy (loginaddress[1], buf, 250);
- }
- if (!strcasecmp(type, "loginport2")) {
- if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
- loginport[1] = atoi(buf);
- }
- }
- if (!strcasecmp (type, "loginserver3")) {
- strncpy (loginaddress[2], buf, 250);
- }
- if (!strcasecmp(type, "loginport3")) {
- if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
- loginport[2] = atoi(buf);
- }
- }
- }
- }
- if (!items[0] || !items[1])
- {
- LogWrite(INIT__ERROR, 0, "Init", "Incomplete LoginServer.INI file.");
- fclose (f);
- return false;
- }
-
- /*
- if (strcasecmp(worldname, "Unnamed server") == 0) {
- cout << "LoginServer.ini: server unnamed, disabling uplink" << endl;
- fclose (f);
- return false;
- }
- */
-
- fclose(f);
- f=fopen (MAIN_INI_FILE, "r");
- do {
- if (fgets (buf, 200, f) == NULL || feof(f))
- {
- LogWrite(INIT__ERROR, 0, "Init", "[WorldServer] block not found in %s", MAIN_INI_FILE);
- fclose (f);
- return true;
- }
- }
- while (strncasecmp (buf, "[WorldServer]\n", 14) != 0 && strncasecmp (buf, "[WorldServer]\r\n", 15) != 0);
- while (!feof (f))
- {
- #ifdef WIN32
- if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
- #else
- if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
- #endif
- {
-
- if (!strcasecmp(type, "Defaultstatus")) {
- if (Seperator::IsNumber(buf) && atoi(buf) > 0 && atoi(buf) < 0xFFFF) {
- DEFAULTSTATUS = atoi(buf);
- }
- }
- }
- }
- fclose (f);
- f=fopen (MAIN_INI_FILE, "r");
- do {
- if (fgets (buf, 200, f) == NULL || feof(f))
- {
- LogWrite(INIT__ERROR, 0, "Init", "[UpdateServer] block not found in %s", MAIN_INI_FILE);
- fclose (f);
- return true;
- }
- }
- while (strncasecmp (buf, "[UpdateServer]\n", 15) != 0 && strncasecmp (buf, "[UpdateServer]\r\n", 16) != 0);
- while (!feof (f))
- {
- #ifdef WIN32
- if (fscanf (f, "%[^=]=%[^\n]\r\n", type, buf) == 2)
- #else
- if (fscanf (f, "%[^=]=%[^\r\n]\n", type, buf) == 2)
- #endif
- {
- if (!strcasecmp(type, "updateserveraddress")) {
- strncpy (updateaddress, buf, 250);
- patch.SetHost(buf);
- }
- if (!strcasecmp(type, "updateserverport")) {
- updateport=atoi(buf);
- patch.SetPort(buf);
- }
- }
- }
- fclose (f);
-
- LogWrite(INIT__DEBUG, 0, "Init", "%s read...", MAIN_INI_FILE);
- LoginServerInfo=1;
- return true;
- }
- char* NetConnection::GetUpdateServerInfo(int16* oPort) {
- if (oPort == 0)
- return 0;
- if (updateaddress[0] == 0)
- return 0;
- *oPort = updateport;
- return updateaddress;
- }
- char* NetConnection::GetLoginInfo(int16* oPort) {
- if (oPort == 0)
- return 0;
- if (loginaddress[0][0] == 0)
- return 0;
- int8 tmp[3] = { 0, 0, 0 };
- int8 count = 0;
- for (int i=0; i<3; i++) {
- if (loginaddress[i][0])
- tmp[count++] = i;
- }
- int x = rand() % count;
- *oPort = loginport[tmp[x]];
- return loginaddress[tmp[x]];
- }
- void UpdateWindowTitle(char* iNewTitle) {
- char tmp[500];
- if (iNewTitle) {
- snprintf(tmp, sizeof(tmp), "World: %s", iNewTitle);
- }
- else {
- string servername = net.GetWorldName();
- snprintf(tmp, sizeof(tmp), "%s (%s), Version: %s: %i Clients(s) in %i Zones(s)", EQ2EMU_MODULE, servername.c_str(), CURRENT_VERSION, numclients, numzones);
- }
- // Zero terminate ([max - 1] = 0) the string to prevent a warning
- tmp[499] = 0;
- #ifdef WIN32
- SetConsoleTitle(tmp);
- #else
- printf("%c]0;%s%c", '\033', tmp, '\007');
- #endif
- }
- ZoneAuthRequest::ZoneAuthRequest(int32 account_id, char* name, int32 access_key) {
- accountid = account_id;
- character_name = string(name);
- accesskey = access_key;
- timestamp = Timer::GetUnixTimeStamp();
- firstlogin = false;
- }
- ZoneAuthRequest::~ZoneAuthRequest ( )
- {
- }
- void ZoneAuth::AddAuth(ZoneAuthRequest *zar) {
- LogWrite(NET__DEBUG, 0, "Net", "AddAuth: %u Key: %u", zar->GetAccountID(), zar->GetAccessKey());
- list.Insert(zar);
- }
- ZoneAuthRequest* ZoneAuth::GetAuth(int32 account_id, int32 access_key) {
- LinkedListIterator<ZoneAuthRequest*> iterator(list);
- iterator.Reset();
- while(iterator.MoreElements()) {
- if (iterator.GetData()->GetAccountID() == account_id && iterator.GetData()->GetAccessKey() == access_key) {
- ZoneAuthRequest* tmp = iterator.GetData();
- return tmp;
- }
- iterator.Advance();
- }
- return 0;
- }
- void ZoneAuth::PurgeInactiveAuth() {
- LinkedListIterator<ZoneAuthRequest*> iterator(list);
- iterator.Reset();
- int32 current_time = Timer::GetUnixTimeStamp();
- while(iterator.MoreElements()) {
- if ((iterator.GetData()->GetTimeStamp()+60) < current_time) {
- iterator.RemoveCurrent();
- }
- iterator.Advance();
- }
- }
- void ZoneAuth::RemoveAuth(ZoneAuthRequest *zar) {
- LinkedListIterator<ZoneAuthRequest*> iterator(list);
- iterator.Reset();
- while(iterator.MoreElements()) {
- if (iterator.GetData() == zar) {
- iterator.RemoveCurrent();
- break;
- }
- iterator.Advance();
- }
- }
- void NetConnection::WelcomeHeader()
- {
- #ifdef _WIN32
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
- #endif
- printf("Module: %s, Version: %s", EQ2EMU_MODULE, CURRENT_VERSION);
- #ifdef _WIN32
- SetConsoleTextAttribute(console, FOREGROUND_YELLOW_BOLD);
- #endif
- printf("\n\nCopyright (C) 2007-2021 EQ2Emulator. https://www.eq2emu.com \n\n");
- printf("EQ2Emulator is free software: you can redistribute it and/or modify\n");
- printf("it under the terms of the GNU General Public License as published by\n");
- printf("the Free Software Foundation, either version 3 of the License, or\n");
- printf("(at your option) any later version.\n\n");
- printf("EQ2Emulator is distributed in the hope that it will be useful,\n");
- printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
- printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
- printf("GNU General Public License for more details.\n\n");
- #ifdef _WIN32
- SetConsoleTextAttribute(console, FOREGROUND_GREEN_BOLD);
- #endif
- printf(" /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$$ \n");
- printf("| $$_____/ /$$__ $$ /$$__ $$| $$_____/ \n");
- printf("| $$ | $$ \\ $$|__/ \\ $$| $$ /$$$$$$/$$$$ /$$ /$$\n");
- printf("| $$$$$ | $$ | $$ /$$$$$$/| $$$$$ | $$_ $$_ $$| $$ | $$\n");
- printf("| $$__/ | $$ | $$ /$$____/ | $$__/ | $$ \\ $$ \\ $$| $$ | $$\n");
- printf("| $$ | $$/$$ $$| $$ | $$ | $$ | $$ | $$| $$ | $$\n");
- printf("| $$$$$$$$| $$$$$$/| $$$$$$$$| $$$$$$$$| $$ | $$ | $$| $$$$$$/\n");
- printf("|________/ \\____ $$$|________/|________/|__/ |__/ |__/ \\______/ \n");
- printf(" \\__/ \n\n");
- #ifdef _WIN32
- SetConsoleTextAttribute(console, FOREGROUND_MAGENTA_BOLD);
- #endif
- printf(" Website : https://eq2emu.com \n");
- printf(" Wiki : https://wiki.eq2emu.com \n");
- printf(" Git : https://git.eq2emu.com \n");
- printf(" Discord : https://discord.gg/5Cavm9NYQf \n\n");
- #ifdef _WIN32
- SetConsoleTextAttribute(console, FOREGROUND_WHITE_BOLD);
- #endif
- printf("For more detailed logging, modify 'Level' param the log_config.xml file.\n\n");
- #ifdef _WIN32
- SetConsoleTextAttribute(console, FOREGROUND_WHITE);
- #endif
- fflush(stdout);
- }
|