[World] World Bootup Consolidation (#2294)

This commit is contained in:
Chris Miles
2022-07-14 02:39:01 -05:00
committed by GitHub
parent fb2df0e570
commit c8b3ca53fe
7 changed files with 756 additions and 694 deletions
+76 -448
View File
@@ -56,12 +56,8 @@
#include <conio.h>
#else
#include <pthread.h>
#include "../common/unix.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#if not defined (FREEBSD) && not defined (DARWIN)
union semun {
@@ -76,16 +72,13 @@ union semun {
#endif
#include "../common/patches/patches.h"
#include "../common/random.h"
#include "zoneserver.h"
#include "login_server.h"
#include "login_server_list.h"
#include "world_config.h"
#include "zoneserver.h"
#include "zonelist.h"
#include "clientlist.h"
#include "launcher_list.h"
#include "wguild_mgr.h"
#include "lfplist.h"
#include "adventure_manager.h"
#include "ucs.h"
@@ -95,18 +88,13 @@ union semun {
#include "dynamic_zone_manager.h"
#include "expedition_database.h"
#include "../common/net/servertalk_server.h"
#include "../zone/data_bucket.h"
#include "world_server_command_handler.h"
#include "../common/content/world_content_service.h"
#include "../common/repositories/character_task_timers_repository.h"
#include "../common/repositories/merchantlist_temp_repository.h"
#include "world_store.h"
#include "world_event_scheduler.h"
#include "shared_task_manager.h"
#include "../common/ip_util.h"
#include "../common/http/httplib.h"
#include "../common/http/uri.h"
#include "world_boot.h"
WorldStore world_store;
ClientList client_list;
@@ -129,7 +117,6 @@ WorldContentService content_service;
WebInterfaceList web_interface;
void CatchSignal(int sig_num);
void CheckForServerScript(bool force_download = false);
inline void UpdateWindowTitle(std::string new_title)
{
@@ -138,148 +125,6 @@ inline void UpdateWindowTitle(std::string new_title)
#endif
}
void LoadDatabaseConnections()
{
LogInfo(
"Connecting to MySQL [{}]@[{}]:[{}]",
Config->DatabaseUsername.c_str(),
Config->DatabaseHost.c_str(),
Config->DatabasePort
);
if (!database.Connect(
Config->DatabaseHost.c_str(),
Config->DatabaseUsername.c_str(),
Config->DatabasePassword.c_str(),
Config->DatabaseDB.c_str(),
Config->DatabasePort
)) {
LogError("Cannot continue without a database connection");
std::exit(1);
}
/**
* Multi-tenancy: Content database
*/
if (!Config->ContentDbHost.empty()) {
if (!content_db.Connect(
Config->ContentDbHost.c_str(),
Config->ContentDbUsername.c_str(),
Config->ContentDbPassword.c_str(),
Config->ContentDbName.c_str(),
Config->ContentDbPort,
"content"
)) {
LogError("Cannot continue without a content database connection");
std::exit(1);
}
}
else {
content_db.SetMysql(database.getMySQL());
}
}
void CheckForXMLConfigUpgrade()
{
if (!std::ifstream("eqemu_config.json") && std::ifstream("eqemu_config.xml")) {
CheckForServerScript(true);
if (system("perl eqemu_server.pl convert_xml")) {}
}
else {
CheckForServerScript();
}
}
void LoadServerConfig()
{
LogInfo("Loading server configuration");
if (!WorldConfig::LoadConfig()) {
LogError("Loading server configuration failed");
std::exit(1);
}
}
void RegisterLoginservers()
{
if (Config->LoginCount == 0) {
if (Config->LoginHost.length()) {
loginserverlist.Add(
Config->LoginHost.c_str(),
Config->LoginPort,
Config->LoginAccount.c_str(),
Config->LoginPassword.c_str(),
Config->LoginLegacy
);
LogInfo("Added loginserver [{}]:[{}]", Config->LoginHost.c_str(), Config->LoginPort);
}
}
else {
LinkedList<LoginConfig *> loginlist = Config->loginlist;
LinkedListIterator<LoginConfig *> iterator(loginlist);
iterator.Reset();
while (iterator.MoreElements()) {
if (iterator.GetData()->LoginHost.length()) {
loginserverlist.Add(
iterator.GetData()->LoginHost.c_str(),
iterator.GetData()->LoginPort,
iterator.GetData()->LoginAccount.c_str(),
iterator.GetData()->LoginPassword.c_str(),
iterator.GetData()->LoginLegacy
);
LogInfo(
"Added loginserver [{}]:[{}]",
iterator.GetData()->LoginHost.c_str(),
iterator.GetData()->LoginPort
);
}
iterator.Advance();
}
}
}
static void GMSayHookCallBackProcessWorld(uint16 log_category, std::string message)
{
// Cut messages down to 4000 max to prevent client crash
if (!message.empty()) {
message = message.substr(0, 4000);
}
// Replace Occurrences of % or MessageStatus will crash
Strings::FindReplace(message, std::string("%"), std::string("."));
if (message.find('\n') != std::string::npos) {
auto message_split = Strings::Split(message, '\n');
for (size_t iter = 0; iter < message_split.size(); ++iter) {
zoneserver_list.SendEmoteMessage(
0,
0,
AccountStatus::QuestTroupe,
LogSys.GetGMSayColorFromCategory(log_category),
fmt::format(
" {}{}",
(iter == 0 ? " ---" : ""),
message_split[iter]
).c_str()
);
}
return;
}
zoneserver_list.SendEmoteMessage(
0,
0,
AccountStatus::QuestTroupe,
LogSys.GetGMSayColorFromCategory(log_category),
"%s",
message.c_str()
);
}
/**
* World process entrypoint
*
@@ -293,35 +138,15 @@ int main(int argc, char **argv)
LogSys.LoadLogSettingsDefaults();
set_exception_handler();
/**
* Database version
*/
uint32 database_version = CURRENT_BINARY_DATABASE_VERSION;
uint32 bots_database_version = CURRENT_BINARY_BOTS_DATABASE_VERSION;
if (argc >= 2) {
if (strcasecmp(argv[1], "db_version") == 0) {
std::cout << "Binary Database Version: " << database_version << " : " << bots_database_version << std::endl;
return 0;
}
if (!WorldBoot::LoadServerConfig()) {
return 0;
}
/**
* Command handler
*/
if (argc > 1) {
LogSys.SilenceConsoleLogging();
WorldConfig::LoadConfig();
Config = WorldConfig::get();
LoadDatabaseConnections();
LogSys.EnableConsoleLogging();
WorldserverCommandHandler::CommandHandler(argc, argv);
if (WorldBoot::HandleCommandInput(argc, argv)) {
return 0;
}
CheckForXMLConfigUpgrade();
LoadServerConfig();
WorldBoot::CheckForXMLConfigUpgrade();
Config = WorldConfig::get();
@@ -344,190 +169,30 @@ int main(int argc, char **argv)
}
#endif
RegisterLoginservers();
LoadDatabaseConnections();
guild_mgr.SetDatabase(&database);
// logging system init
auto logging = LogSys.SetDatabase(&database)
->LoadLogDatabaseSettings();
if (RuleB(Logging, WorldGMSayLogging)) {
logging->SetGMSayHandler(&GMSayHookCallBackProcessWorld);
WorldBoot::RegisterLoginservers();
WorldBoot::LoadDatabaseConnections();
if (!WorldBoot::DatabaseLoadRoutines(argc, argv)) {
return 1;
}
logging->StartFileLogs();
/**
* Parse simple CLI passes
*/
bool ignore_db = false;
if (argc >= 2) {
if (strcasecmp(argv[1], "ignore_db") == 0) {
ignore_db = true;
}
else {
std::cerr << "Error, unknown command line option" << std::endl;
return 1;
}
}
if (!ignore_db) {
LogInfo("Checking Database Conversions");
database.CheckDatabaseConversions();
}
LogInfo("Loading variables");
database.LoadVariables();
std::string hotfix_name;
if (database.GetVariable("hotfix_name", hotfix_name)) {
if (!hotfix_name.empty()) {
LogInfo("Current hotfix in use: [{}]", hotfix_name.c_str());
}
}
LogInfo("Purging expired data buckets");
database.PurgeAllDeletedDataBuckets();
LogInfo("Loading zones");
world_store.LoadZones();
LogInfo("Clearing groups");
database.ClearGroup();
LogInfo("Clearing raids");
database.ClearRaid();
database.ClearRaidDetails();
database.ClearRaidLeader();
LogInfo("Clearing inventory snapshots");
database.ClearInvSnapshots();
LogInfo("Loading items");
if (!content_db.LoadItems(hotfix_name)) {
LogError("Error: Could not load item data. But ignoring");
}
LogInfo("Loading skill caps");
if (!content_db.LoadSkillCaps(std::string(hotfix_name))) {
LogError("Error: Could not load skill cap data. But ignoring");
}
LogInfo("Loading guilds");
guild_mgr.LoadGuilds();
//rules:
{
if (!RuleManager::Instance()->UpdateOrphanedRules(&database)) {
LogInfo("Failed to process 'Orphaned Rules' update operation.");
}
if (!RuleManager::Instance()->UpdateInjectedRules(&database, "default")) {
LogInfo("Failed to process 'Injected Rules' for ruleset 'default' update operation.");
}
std::string tmp;
if (database.GetVariable("RuleSet", tmp)) {
LogInfo("Loading rule set [{}]", tmp.c_str());
if (!RuleManager::Instance()->LoadRules(&database, tmp.c_str(), false)) {
LogInfo("Failed to load ruleset [{}], falling back to defaults", tmp.c_str());
}
}
else {
if (!RuleManager::Instance()->LoadRules(&database, "default", false)) {
LogInfo("No rule set configured, using default rules");
}
else {
LogInfo("Loaded default rule set [default]", tmp.c_str());
}
}
if (!RuleManager::Instance()->RestoreRuleNotes(&database)) {
LogInfo("Failed to process 'Restore Rule Notes' update operation.");
}
}
EQ::InitializeDynamicLookups();
LogInfo("Initialized dynamic dictionary entries");
if (RuleB(World, ClearTempMerchantlist)) {
LogInfo("Clearing temporary merchant lists");
database.ClearMerchantTemp();
}
LogInfo("Loading EQ time of day");
TimeOfDay_Struct eqTime;
time_t realtime;
eqTime = database.LoadTime(realtime);
zoneserver_list.worldclock.SetCurrentEQTimeOfDay(eqTime, realtime);
// timers
Timer PurgeInstanceTimer(450000);
PurgeInstanceTimer.Start(450000);
Timer EQTimeTimer(600000);
EQTimeTimer.Start(600000);
// global loads
LogInfo("Loading launcher list");
launcher_list.LoadList();
LogInfo("Deleted [{}] stale player corpses from database", database.DeleteStalePlayerCorpses());
LogInfo("Loading adventures");
if (!adventure_manager.LoadAdventureTemplates()) {
LogInfo("Unable to load adventure templates");
}
if (!adventure_manager.LoadAdventureEntries()) {
LogInfo("Unable to load adventure templates");
}
adventure_manager.LoadLeaderboardInfo();
LogInfo("Purging expired dynamic zones and members");
dynamic_zone_manager.PurgeExpiredDynamicZones();
LogInfo("Purging expired expeditions");
ExpeditionDatabase::PurgeExpiredExpeditions();
ExpeditionDatabase::PurgeExpiredCharacterLockouts();
LogInfo("Purging expired character task timers");
CharacterTaskTimersRepository::DeleteWhere(database, "expire_time <= NOW()");
LogInfo("Purging expired instances");
database.PurgeExpiredInstances();
Timer PurgeInstanceTimer(450000);
PurgeInstanceTimer.Start(450000);
LogInfo("Loading dynamic zones");
dynamic_zone_manager.CacheAllFromDatabase();
LogInfo("Loading char create info");
content_db.LoadCharacterCreateAllocations();
content_db.LoadCharacterCreateCombos();
LogInfo("Initializing [EventScheduler]");
event_scheduler.SetDatabase(&database)->LoadScheduledEvents();
LogInfo("Initializing [WorldContentService]");
content_service.SetDatabase(&database)
->SetExpansionContext()
->ReloadContentFlags();
LogInfo("Initializing [SharedTaskManager]");
shared_task_manager.SetDatabase(&database)
->SetContentDatabase(&content_db)
->LoadTaskData()
->LoadSharedTaskState();
shared_task_manager.PurgeExpiredSharedTasks();
zoneserver_list.Init();
std::unique_ptr<EQ::Net::ConsoleServer> console;
if (Config->TelnetEnabled) {
LogInfo("Console (TCP) listener started");
LogInfo("Console (TCP) listener started on [{}:{}]", Config->TelnetIP, Config->TelnetTCPPort);
console = std::make_unique<EQ::Net::ConsoleServer>(Config->TelnetIP, Config->TelnetTCPPort);
RegisterConsoleFunctions(console);
}
zoneserver_list.Init();
std::unique_ptr<EQ::Net::ServertalkServer> server_connection;
server_connection = std::make_unique<EQ::Net::ServertalkServer>();
@@ -536,32 +201,44 @@ int main(int argc, char **argv)
server_opts.ipv6 = false;
server_opts.credentials = Config->SharedKey;
server_connection->Listen(server_opts);
LogInfo("Server (TCP) listener started");
LogInfo("Server (TCP) listener started on port [{}]", Config->WorldTCPPort);
server_connection->OnConnectionIdentified(
"Zone", [&console](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("New Zone Server connection from [{2}] at [{0}:{1}]",
connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID());
numzones++;
zoneserver_list.Add(new ZoneServer(connection, console.get()));
LogInfo(
"New Zone Server connection from [{}] at [{}:{}] zone_count ({})",
connection->Handle()->RemoteIP(),
connection->Handle()->RemotePort(),
connection->GetUUID(),
numzones
);
}
);
server_connection->OnConnectionRemoved(
"Zone", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("Removed Zone Server connection from [{0}]",
connection->GetUUID());
numzones--;
zoneserver_list.Remove(connection->GetUUID());
LogInfo(
"Removed Zone Server connection from [{}] total zone_count [{}]",
connection->GetUUID(),
numzones
);
}
);
server_connection->OnConnectionIdentified(
"Launcher", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("New Launcher connection from [{2}] at [{0}:{1}]",
connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID());
LogInfo(
"New Launcher connection from [{}] at [{}:{}]",
connection->Handle()->RemoteIP(),
connection->Handle()->RemotePort(),
connection->GetUUID()
);
launcher_list.Add(connection);
}
@@ -569,8 +246,10 @@ int main(int argc, char **argv)
server_connection->OnConnectionRemoved(
"Launcher", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("Removed Launcher connection from [{0}]",
connection->GetUUID());
LogInfo(
"Removed Launcher connection from [{0}]",
connection->GetUUID()
);
launcher_list.Remove(connection);
}
@@ -578,8 +257,11 @@ int main(int argc, char **argv)
server_connection->OnConnectionIdentified(
"QueryServ", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("New Query Server connection from [{2}] at [{0}:{1}]",
connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID());
LogInfo(
"New Query Server connection from [{}] at [{}:{}]",
connection->Handle()->RemoteIP(),
connection->Handle()->RemotePort(),
connection->GetUUID());
QSLink.AddConnection(connection);
}
@@ -587,8 +269,10 @@ int main(int argc, char **argv)
server_connection->OnConnectionRemoved(
"QueryServ", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("Removed Query Server connection from [{0}]",
connection->GetUUID());
LogInfo(
"Removed Query Server connection from [{}]",
connection->GetUUID()
);
QSLink.RemoveConnection(connection);
}
@@ -596,8 +280,12 @@ int main(int argc, char **argv)
server_connection->OnConnectionIdentified(
"UCS", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("New UCS Server connection from [{2}] at [{0}:{1}]",
connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID());
LogInfo(
"New UCS Server connection from [{}] at [{}:{}]",
connection->Handle()->RemoteIP(),
connection->Handle()->RemotePort(),
connection->GetUUID()
);
UCSLink.SetConnection(connection);
@@ -607,7 +295,7 @@ int main(int argc, char **argv)
server_connection->OnConnectionRemoved(
"UCS", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("Connection lost from UCS Server [{0}]", connection->GetUUID());
LogInfo("Connection lost from UCS Server [{}]", connection->GetUUID());
auto ucs_connection = UCSLink.GetConnection();
@@ -621,8 +309,12 @@ int main(int argc, char **argv)
server_connection->OnConnectionIdentified(
"WebInterface", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("New WebInterface Server connection from [{2}] at [{0}:{1}]",
connection->Handle()->RemoteIP(), connection->Handle()->RemotePort(), connection->GetUUID());
LogInfo(
"New WebInterface Server connection from [{}] at [{}:{}]",
connection->Handle()->RemoteIP(),
connection->Handle()->RemotePort(),
connection->GetUUID()
);
web_interface.AddConnection(connection);
}
@@ -630,14 +322,16 @@ int main(int argc, char **argv)
server_connection->OnConnectionRemoved(
"WebInterface", [](std::shared_ptr<EQ::Net::ServertalkServerConnection> connection) {
LogInfo("Removed WebInterface Server connection from [{0}]",
connection->GetUUID());
LogInfo(
"Removed WebInterface Server connection from [{}]",
connection->GetUUID()
);
web_interface.RemoveConnection(connection);
}
);
WorldConfig::CheckForPossibleConfigurationIssues();
WorldBoot::CheckForPossibleConfigurationIssues();
EQStreamManagerInterfaceOptions opts(9000, false, false);
opts.daybreak_options.resend_delay_ms = RuleI(Network, ResendDelayBaseMS);
@@ -664,7 +358,11 @@ int main(int argc, char **argv)
eqsm.OnNewConnection(
[&stream_identifier](std::shared_ptr<EQ::Net::EQStream> stream) {
stream_identifier.AddStream(stream);
LogInfo("New connection from IP {}:{}", long2ip(stream->GetRemoteIP()), ntohs(stream->GetRemotePort()));
LogInfo(
"New [EverQuest Client] connection from IP [{}:{}]",
long2ip(stream->GetRemoteIP()),
ntohs(stream->GetRemotePort())
);
}
);
@@ -736,9 +434,9 @@ int main(int argc, char **argv)
database.ping();
content_db.ping();
std::string window_title = StringFormat(
"World: %s Clients: %i",
Config->LongName.c_str(),
std::string window_title = fmt::format(
"World [{}] Clients [{}]",
Config->LongName,
client_list.GetClientCount()
);
UpdateWindowTitle(window_title);
@@ -762,73 +460,3 @@ void CatchSignal(int sig_num)
LogInfo("Caught signal [{}]", sig_num);
RunLoops = false;
}
void UpdateWindowTitle(char *iNewTitle)
{
#ifdef _WINDOWS
char tmp[500];
if (iNewTitle) {
snprintf(tmp, sizeof(tmp), "World: %s", iNewTitle);
}
else {
snprintf(tmp, sizeof(tmp), "World");
}
SetConsoleTitle(tmp);
#endif
}
int get_file_size(const std::string& filename) // path to file
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
void CheckForServerScript(bool force_download)
{
const std::string file = "eqemu_server.pl";
std::ifstream f(file);
/* Fetch EQEmu Server script */
if (!f || get_file_size(file) < 100 || force_download) {
if (force_download) {
std::remove("eqemu_server.pl");
} /* Delete local before fetch */
std::cout << "Pulling down EQEmu Server Maintenance Script (eqemu_server.pl)..." << std::endl;
// http get request
uri u("https://raw.githubusercontent.com/EQEmu/Server/master/utils/scripts/eqemu_server.pl");
httplib::Client r(
fmt::format(
"{}://{}",
u.get_scheme(),
u.get_host()
).c_str()
);
r.set_connection_timeout(1, 0);
r.set_read_timeout(1, 0);
r.set_write_timeout(1, 0);
if (auto res = r.Get(u.get_path().c_str())) {
if (res->status == 200) {
// write file
std::ofstream out("eqemu_server.pl");
out << res->body;
out.close();
#ifdef _WIN32
#else
system("chmod 755 eqemu_server.pl");
system("chmod +x eqemu_server.pl");
#endif
}
}
}
}