Skip to content

Commit

Permalink
Merge TrinityCore 3.3.5 to ElunaTrinityWotlk [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Rochet2 committed Apr 8, 2024
2 parents c0a926f + 2fca554 commit e29f977
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 28 deletions.
31 changes: 30 additions & 1 deletion src/common/Configuration/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
#include "Config.h"
#include "Log.h"
#include "StringConvert.h"
#include "Util.h"
#include <boost/filesystem/operations.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <mutex>

namespace bpt = boost::property_tree;
namespace fs = boost::filesystem;

namespace
{
Expand Down Expand Up @@ -159,6 +160,8 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
if (!LoadFile(file, fullTree, error))
return false;

std::lock_guard<std::mutex> lock(_configLock);

for (bpt::ptree::value_type const& child : fullTree.begin()->second)
_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);

Expand All @@ -168,6 +171,32 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
return true;
}

bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector<std::string>& loadedFiles, std::vector<std::string>& errors)
{
fs::path dirPath = dir;
if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
return true;

for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath))
{
if (!fs::is_regular_file(f))
continue;

fs::path configFile = fs::absolute(f);
if (configFile.extension() != ".conf")
continue;

std::string fileName = configFile.generic_string();
std::string error;
if (LoadAdditionalFile(fileName, keepOnReload, error))
loadedFiles.push_back(std::move(fileName));
else
errors.push_back(std::move(error));
}

return errors.empty();
}

std::vector<std::string> ConfigMgr::OverrideWithEnvVariablesIfAny()
{
std::lock_guard<std::mutex> lock(_configLock);
Expand Down
2 changes: 2 additions & 0 deletions src/common/Configuration/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Define.h"
#include <string>
#include <string_view>
#include <vector>

class TC_COMMON_API ConfigMgr
Expand All @@ -33,6 +34,7 @@ class TC_COMMON_API ConfigMgr
/// Method used only for loading main configuration files (authserver.conf and worldserver.conf)
bool LoadInitial(std::string file, std::vector<std::string> args, std::string& error);
bool LoadAdditionalFile(std::string file, bool keepOnReload, std::string& error);
bool LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector<std::string>& loadedFiles, std::vector<std::string>& errors);

/// Overrides configuration with environment variables and returns overridden keys
std::vector<std::string> OverrideWithEnvVariablesIfAny();
Expand Down
5 changes: 3 additions & 2 deletions src/server/authserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ add_executable(authserver
)

if(NOT WIN32)
set_target_properties(authserver PROPERTIES
COMPILE_DEFINITIONS _TRINITY_REALM_CONFIG="${CONF_DIR}/authserver.conf"
target_compile_definitions(authserver PRIVATE
_TRINITY_REALM_CONFIG="${CONF_DIR}/authserver.conf"
_TRINITY_REALM_CONFIG_DIR="${CONF_DIR}/authserver.conf.d"
)
endif()

Expand Down
46 changes: 34 additions & 12 deletions src/server/authserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace fs = boost::filesystem;
#ifndef _TRINITY_REALM_CONFIG
# define _TRINITY_REALM_CONFIG "authserver.conf"
#endif
#ifndef _TRINITY_REALM_CONFIG_DIR
#define _TRINITY_REALM_CONFIG_DIR "authserver.conf.d"
#endif

#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
#include "ServiceWin32.h"
Expand All @@ -79,7 +82,7 @@ void StopDB();
void SignalHandler(std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error, int signalNumber);
void KeepDatabaseAliveHandler(std::weak_ptr<Trinity::Asio::DeadlineTimer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error);
void BanExpiryHandler(std::weak_ptr<Trinity::Asio::DeadlineTimer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, std::string& winServiceAction);

int main(int argc, char** argv)
{
Expand All @@ -91,18 +94,19 @@ int main(int argc, char** argv)
Trinity::Locale::Init();

auto configFile = fs::absolute(_TRINITY_REALM_CONFIG);
std::string configService;
auto vm = GetConsoleArguments(argc, argv, configFile, configService);
auto configDir = fs::absolute(_TRINITY_REALM_CONFIG_DIR);
std::string winServiceAction;
auto vm = GetConsoleArguments(argc, argv, configFile, configDir, winServiceAction);
// exit if help or version is enabled
if (vm.count("help") || vm.count("version"))
return 0;

#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
if (configService.compare("install") == 0)
if (winServiceAction == "install")
return WinServiceInstall() == true ? 0 : 1;
else if (configService.compare("uninstall") == 0)
if (winServiceAction == "uninstall")
return WinServiceUninstall() == true ? 0 : 1;
else if (configService.compare("run") == 0)
if (winServiceAction == "run")
return WinServiceRun() ? 0 : 1;
#endif

Expand All @@ -115,6 +119,20 @@ int main(int argc, char** argv)
return 1;
}

std::vector<std::string> loadedConfigFiles;
std::vector<std::string> configDirErrors;
bool additionalConfigFileLoadSuccess = sConfigMgr->LoadAdditionalDir(configDir.generic_string(), true, loadedConfigFiles, configDirErrors);
for (std::string const& loadedConfigFile : loadedConfigFiles)
printf("Loaded additional config file %s\n", loadedConfigFile.c_str());

if (!additionalConfigFileLoadSuccess)
{
for (std::string const& configDirError : configDirErrors)
printf("Error in additional config files: %s\n", configDirError.c_str());

return 1;
}

std::vector<std::string> overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny();

sLog->RegisterAppender<AppenderDB>();
Expand Down Expand Up @@ -157,13 +175,16 @@ int main(int argc, char** argv)
if (!StartDB())
return 1;

std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });

if (vm.count("update-databases-only"))
return 0;

sSecretMgr->Initialize();

// Load IP Location Database
sIPLocation->Load();

std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });

std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();

// Get the list of realms for the server
Expand Down Expand Up @@ -325,24 +346,25 @@ void ServiceStatusWatcher(std::weak_ptr<Trinity::Asio::DeadlineTimer> serviceSta
}
#endif

variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService)
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
{
options_description all("Allowed options");
all.add_options()
("help,h", "print usage message")
("version,v", "print version build info")
("config,c", value<fs::path>(&configFile)->default_value(fs::absolute(_TRINITY_REALM_CONFIG)),
"use <arg> as configuration file")
("config-dir,cd", value<fs::path>(&configDir)->default_value(fs::absolute(_TRINITY_REALM_CONFIG_DIR)),
"use <arg> as directory with additional config files")
("update-databases-only,u", "updates databases only")
;
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
options_description win("Windows platform specific options");
win.add_options()
("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
("service,s", value<std::string>(&winServiceAction)->default_value(""), "Windows service options: [install | uninstall]")
;

all.add(win);
#else
(void)configService;
#endif
variables_map variablesMap;
try
Expand Down
5 changes: 3 additions & 2 deletions src/server/worldserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ add_executable(worldserver
)

if(NOT WIN32)
set_target_properties(worldserver PROPERTIES
COMPILE_DEFINITIONS _TRINITY_CORE_CONFIG="${CONF_DIR}/worldserver.conf"
target_compile_definitions(worldserver PRIVATE
_TRINITY_CORE_CONFIG="${CONF_DIR}/worldserver.conf"
_TRINITY_CORE_CONFIG_DIR="${CONF_DIR}/worldserver.conf.d"
)
endif()

Expand Down
40 changes: 29 additions & 11 deletions src/server/worldserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ namespace fs = boost::filesystem;
#define _TRINITY_CORE_CONFIG "worldserver.conf"
#endif

#ifndef _TRINITY_CORE_CONFIG_DIR
#define _TRINITY_CORE_CONFIG_DIR "worldserver.conf.d"
#endif

#ifdef _WIN32
#include "ServiceWin32.h"
char serviceName[] = "worldserver";
Expand Down Expand Up @@ -122,7 +126,7 @@ void WorldUpdateLoop();
void ClearOnlineAccounts();
void ShutdownCLIThread(std::thread* cliThread);
bool LoadRealmInfo(Trinity::Asio::IoContext& ioContext);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService);
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, std::string& winServiceAction);

/// Launch the Trinity server
extern int main(int argc, char** argv)
Expand All @@ -135,19 +139,20 @@ extern int main(int argc, char** argv)
Trinity::Locale::Init();

auto configFile = fs::absolute(_TRINITY_CORE_CONFIG);
std::string configService;
auto configDir = fs::absolute(_TRINITY_CORE_CONFIG_DIR);
std::string winServiceAction;

auto vm = GetConsoleArguments(argc, argv, configFile, configService);
auto vm = GetConsoleArguments(argc, argv, configFile, configDir, winServiceAction);
// exit if help or version is enabled
if (vm.count("help") || vm.count("version"))
return 0;

#ifdef _WIN32
if (configService == "install")
if (winServiceAction == "install")
return WinServiceInstall() ? 0 : 1;
else if (configService == "uninstall")
if (winServiceAction == "uninstall")
return WinServiceUninstall() ? 0 : 1;
else if (configService == "run")
if (winServiceAction == "run")
return WinServiceRun() ? 0 : 1;

Optional<UINT> newTimerResolution;
Expand Down Expand Up @@ -198,6 +203,20 @@ extern int main(int argc, char** argv)
return 1;
}

std::vector<std::string> loadedConfigFiles;
std::vector<std::string> configDirErrors;
bool additionalConfigFileLoadSuccess = sConfigMgr->LoadAdditionalDir(configDir.generic_string(), true, loadedConfigFiles, configDirErrors);
for (std::string const& loadedConfigFile : loadedConfigFiles)
printf("Loaded additional config file %s\n", loadedConfigFile.c_str());

if (!additionalConfigFileLoadSuccess)
{
for (std::string const& configDirError : configDirErrors)
printf("Error in additional config files: %s\n", configDirError.c_str());

return 1;
}

std::vector<std::string> overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny();

std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();
Expand Down Expand Up @@ -692,23 +711,22 @@ void ClearOnlineAccounts()

/// @}

variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService)
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
{
// Silences warning about configService not be used if the OS is not Windows
(void)configService;

options_description all("Allowed options");
all.add_options()
("help,h", "print usage message")
("version,v", "print version build info")
("config,c", value<fs::path>(&configFile)->default_value(fs::absolute(_TRINITY_CORE_CONFIG)),
"use <arg> as configuration file")
("config-dir,cd", value<fs::path>(&configDir)->default_value(fs::absolute(_TRINITY_CORE_CONFIG_DIR)),
"use <arg> as directory with additional config files")
("update-databases-only,u", "updates databases only")
;
#ifdef _WIN32
options_description win("Windows platform specific options");
win.add_options()
("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
("service,s", value<std::string>(&winServiceAction)->default_value(""), "Windows service options: [install | uninstall]")
;

all.add(win);
Expand Down

0 comments on commit e29f977

Please sign in to comment.