mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-06 10:44:08 +00:00
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:
71
dCommon/BinaryPathFinder.cpp
Normal file
71
dCommon/BinaryPathFinder.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include "BinaryPathFinder.h"
|
||||
#include "dPlatforms.h"
|
||||
|
||||
#if defined(DARKFLAME_PLATFORM_WIN32)
|
||||
#include <Windows.h>
|
||||
#elif defined(DARKFLAME_PLATFORM_MACOS) || defined(DARKFLAME_PLATFORM_IOS)
|
||||
#include <mach-o/dyld.h>
|
||||
#elif defined(DARKFLAME_PLATFORM_FREEBSD)
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
std::filesystem::path BinaryPathFinder::binaryDir;
|
||||
|
||||
std::filesystem::path BinaryPathFinder::GetBinaryDir() {
|
||||
if (!binaryDir.empty()) {
|
||||
return binaryDir;
|
||||
}
|
||||
|
||||
std::string pathStr;
|
||||
|
||||
// Derived from boost::dll::program_location, licensed under the Boost Software License: http://www.boost.org/LICENSE_1_0.txt
|
||||
#if defined(DARKFLAME_PLATFORM_WIN32)
|
||||
char path[MAX_PATH];
|
||||
GetModuleFileName(NULL, path, MAX_PATH);
|
||||
pathStr = std::string(path);
|
||||
#elif defined(DARKFLAME_PLATFORM_MACOS) || defined(DARKFLAME_PLATFORM_IOS)
|
||||
char path[1024];
|
||||
uint32_t size = sizeof(path);
|
||||
if (_NSGetExecutablePath(path, &size) == 0) {
|
||||
pathStr = std::string(path);
|
||||
} else {
|
||||
// The filepath size is greater than our initial buffer size, so try again with the size
|
||||
// that _NSGetExecutablePath told us it actually is
|
||||
char *p = new char[size];
|
||||
if (_NSGetExecutablePath(p, &size) != 0) {
|
||||
throw std::runtime_error("Failed to get binary path from _NSGetExecutablePath");
|
||||
}
|
||||
|
||||
pathStr = std::string(p);
|
||||
delete[] p;
|
||||
}
|
||||
#elif defined(DARKFLAME_PLATFORM_FREEBSD)
|
||||
int mib[4];
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_PROC;
|
||||
mib[2] = KERN_PROC_PATHNAME;
|
||||
mib[3] = -1;
|
||||
char buf[10240];
|
||||
size_t cb = sizeof(buf);
|
||||
sysctl(mib, 4, buf, &cb, NULL, 0);
|
||||
pathStr = std::string(buf);
|
||||
#else // DARKFLAME_PLATFORM_LINUX || DARKFLAME_PLATFORM_UNIX || DARKFLAME_PLATFORM_ANDROID
|
||||
pathStr = std::filesystem::read_symlink("/proc/self/exe");
|
||||
#endif
|
||||
|
||||
// Some methods like _NSGetExecutablePath could return a symlink
|
||||
// Either way, we need to get the parent path because we want the directory, not the binary itself
|
||||
// We also ensure that it is an absolute path so that it is valid if we need to construct a path
|
||||
// to exucute on unix systems (eg sudo BinaryPathFinder::GetBinaryDir() / WorldServer)
|
||||
if (std::filesystem::is_symlink(pathStr)) {
|
||||
binaryDir = std::filesystem::absolute(std::filesystem::read_symlink(pathStr).parent_path());
|
||||
} else {
|
||||
binaryDir = std::filesystem::absolute(std::filesystem::path(pathStr).parent_path());
|
||||
}
|
||||
|
||||
return binaryDir;
|
||||
}
|
15
dCommon/BinaryPathFinder.h
Normal file
15
dCommon/BinaryPathFinder.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef __BINARYPATHFINDER__H__
|
||||
#define __BINARYPATHFINDER__H__
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
class BinaryPathFinder {
|
||||
private:
|
||||
static std::filesystem::path binaryDir;
|
||||
public:
|
||||
static std::filesystem::path GetBinaryDir();
|
||||
};
|
||||
|
||||
#endif //!__BINARYPATHFINDER__H__
|
@@ -15,6 +15,7 @@ set(DCOMMON_SOURCES "AMFFormat.cpp"
|
||||
"Type.cpp"
|
||||
"ZCompression.cpp"
|
||||
"BrickByBrickFix.cpp"
|
||||
"BinaryPathFinder.cpp"
|
||||
)
|
||||
|
||||
add_subdirectory(dClient)
|
||||
|
@@ -1,15 +1,17 @@
|
||||
#include <filesystem>
|
||||
|
||||
#include "AssetManager.h"
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
AssetManager::AssetManager(const std::string& path) {
|
||||
AssetManager::AssetManager(const std::filesystem::path& path) {
|
||||
if (!std::filesystem::is_directory(path)) {
|
||||
throw std::runtime_error("Attempted to load asset bundle (" + path + ") however it is not a valid directory.");
|
||||
throw std::runtime_error("Attempted to load asset bundle (" + path.string() + ") however it is not a valid directory.");
|
||||
}
|
||||
|
||||
m_Path = std::filesystem::path(path);
|
||||
m_Path = path;
|
||||
|
||||
if (std::filesystem::exists(m_Path / "client") && std::filesystem::exists(m_Path / "versions")) {
|
||||
m_AssetBundleType = eAssetBundleType::Packed;
|
||||
|
@@ -48,7 +48,7 @@ struct AssetMemoryBuffer : std::streambuf {
|
||||
|
||||
class AssetManager {
|
||||
public:
|
||||
AssetManager(const std::string& path);
|
||||
AssetManager(const std::filesystem::path& path);
|
||||
~AssetManager();
|
||||
|
||||
std::filesystem::path GetResPath();
|
||||
|
@@ -1,9 +1,10 @@
|
||||
#include "dConfig.h"
|
||||
#include <sstream>
|
||||
#include "BinaryPathFinder.h"
|
||||
|
||||
dConfig::dConfig(const std::string& filepath) {
|
||||
m_EmptyString = "";
|
||||
std::ifstream in(filepath);
|
||||
std::ifstream in(BinaryPathFinder::GetBinaryDir() / filepath);
|
||||
if (!in.good()) return;
|
||||
|
||||
std::string line;
|
||||
@@ -13,7 +14,7 @@ dConfig::dConfig(const std::string& filepath) {
|
||||
}
|
||||
}
|
||||
|
||||
std::ifstream sharedConfig("sharedconfig.ini", std::ios::in);
|
||||
std::ifstream sharedConfig(BinaryPathFinder::GetBinaryDir() / "sharedconfig.ini", std::ios::in);
|
||||
if (!sharedConfig.good()) return;
|
||||
|
||||
while (std::getline(sharedConfig, line)) {
|
||||
|
Reference in New Issue
Block a user