Allow servers to be run from directories other than build. Read/write files relative to binary instead of cwd (#834)

Allows the server to be run from a non-build directory.  Also only read or write files relative to the build directory, regardless of where the server is run from
This commit is contained in:
Jonathan Romano
2022-11-27 06:59:59 -05:00
committed by GitHub
parent e40a597f18
commit f8f5b731f1
16 changed files with 158 additions and 46 deletions

View File

@@ -10,6 +10,7 @@
#include "dMessageIdentifiers.h"
#include "MasterPackets.h"
#include "PacketUtils.h"
#include "BinaryPathFinder.h"
InstanceManager::InstanceManager(dLogger* logger, const std::string& externalIP) {
mLogger = logger;
@@ -48,13 +49,13 @@ Instance* InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LW
//Start the actual process:
#ifdef _WIN32
std::string cmd = "start ./WorldServer.exe -zone ";
std::string cmd = "start " + (BinaryPathFinder::GetBinaryDir() / "WorldServer.exe").string() + " -zone ";
#else
std::string cmd;
if (std::atoi(Game::config->GetValue("use_sudo_world").c_str())) {
cmd = "sudo ./WorldServer -zone ";
cmd = "sudo " + (BinaryPathFinder::GetBinaryDir() / "WorldServer").string() + " -zone ";
} else {
cmd = "./WorldServer -zone ";
cmd = (BinaryPathFinder::GetBinaryDir() / "WorldServer").string() + " -zone ";
}
#endif
@@ -300,10 +301,10 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password);
//Start the actual process:
std::string cmd = "start ./WorldServer.exe -zone ";
std::string cmd = "start " + (BinaryPathFinder::GetBinaryDir() / "WorldServer").string() + " -zone ";
#ifndef _WIN32
cmd = "./WorldServer -zone ";
cmd = (BinaryPathFinder::GetBinaryDir() / "WorldServer").string() + " -zone ";
#endif
cmd.append(std::to_string(mapID));

View File

@@ -26,6 +26,7 @@
#include "dLogger.h"
#include "dServer.h"
#include "AssetManager.h"
#include "BinaryPathFinder.h"
//RakNet includes:
#include "RakNetDefines.h"
@@ -102,9 +103,14 @@ int main(int argc, char** argv) {
}
try {
std::string client_path = config.GetValue("client_location");
if (client_path.empty()) client_path = "./res";
Game::assetManager = new AssetManager(client_path);
std::string clientPathStr = config.GetValue("client_location");
if (clientPathStr.empty()) clientPathStr = "./res";
std::filesystem::path clientPath = std::filesystem::path(clientPathStr);
if (clientPath.is_relative()) {
clientPath = BinaryPathFinder::GetBinaryDir() / clientPath;
}
Game::assetManager = new AssetManager(clientPath);
} catch (std::runtime_error& ex) {
Game::logger->Log("MasterServer", "Got an error while setting up assets: %s", ex.what());
@@ -127,18 +133,16 @@ int main(int argc, char** argv) {
stmt->executeUpdate();
delete stmt;
std::string res = "python3 ../thirdparty/docker-utils/utils/fdb_to_sqlite.py " + (Game::assetManager->GetResPath() / "cdclient.fdb").string();
std::string res = "python3 "
+ (BinaryPathFinder::GetBinaryDir() / "../thirdparty/docker-utils/utils/fdb_to_sqlite.py").string()
+ " --sqlite_path " + (Game::assetManager->GetResPath() / "CDServer.sqlite").string()
+ " " + (Game::assetManager->GetResPath() / "cdclient.fdb").string();
int result = system(res.c_str());
if (result != 0) {
Game::logger->Log("MasterServer", "Failed to convert fdb to sqlite");
return EXIT_FAILURE;
}
if (std::rename("./cdclient.sqlite", (Game::assetManager->GetResPath() / "CDServer.sqlite").string().c_str()) != 0) {
Game::logger->Log("MasterServer", "Failed to move cdclient file.");
return EXIT_FAILURE;
}
}
//Connect to CDClient
@@ -363,7 +367,7 @@ int main(int argc, char** argv) {
dLogger* SetupLogger() {
std::string logPath =
"./logs/MasterServer_" + std::to_string(time(nullptr)) + ".log";
(BinaryPathFinder::GetBinaryDir() / ("logs/MasterServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
@@ -738,28 +742,28 @@ void HandlePacket(Packet* packet) {
void StartChatServer() {
#ifdef __APPLE__
//macOS doesn't need sudo to run on ports < 1024
system("./ChatServer&");
system(((BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
#elif _WIN32
system("start ./ChatServer.exe");
system(("start " + (BinaryPathFinder::GetBinaryDir() / "ChatServer.exe").string()).c_str());
#else
if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) {
system("sudo ./ChatServer&");
system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
} else {
system("./ChatServer&");
system(((BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
}
#endif
}
void StartAuthServer() {
#ifdef __APPLE__
system("./AuthServer&");
system(((BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
#elif _WIN32
system("start ./AuthServer.exe");
system(("start " + (BinaryPathFinder::GetBinaryDir() / "AuthServer.exe").string()).c_str());
#else
if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) {
system("sudo ./AuthServer&");
system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
} else {
system("./AuthServer&");
system(((BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
}
#endif
}