Add code to find luprex root directory

This commit is contained in:
2023-05-10 15:24:47 -04:00
parent 9e74e67277
commit 64b4fc465d
9 changed files with 68 additions and 19 deletions

View File

@@ -198,7 +198,7 @@ obj/$(OS)/eris/%.obj: ext/eris-master/src/%.c
obj/$(OS)/core/%.obj: cpp/core/%.cpp obj/$(OS)/core/%.obj: cpp/core/%.cpp
$(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $< $(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $<
$(COMPILE) $@ -I./ext/eris-master/src -I./cpp\wrap -I./cpp/core $< $(COMPILE) $@ -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $<
obj/$(OS)/drv/%.obj: cpp/drv/%.cpp obj/$(OS)/drv/%.obj: cpp/drv/%.cpp
$(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv $< $(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv $<

View File

View File

View File

View File

@@ -12,6 +12,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <poll.h> #include <poll.h>
#include <sys/time.h> #include <sys/time.h>
@@ -33,6 +34,11 @@ const int INVALID_SOCKET = -1;
struct termios orig_termios; struct termios orig_termios;
std::filesystem::path get_exe_path() {
char result[ PATH_MAX ];
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
return std::filesystem::path(std::string( result, (count > 0) ? count : 0 ));
}
void set_nonblocking(int fd) { void set_nonblocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0); int flags = fcntl(fd, F_GETFL, 0);
@@ -219,11 +225,12 @@ static int console_read(char *bytes, int nbytes) {
return read(0, bytes, nbytes); return read(0, bytes, nbytes);
} }
static void call_init_engine_wrapper(EngineWrapper *w) { static void call_init_engine_wrapper(const std::filesystem::path &luprexroot, EngineWrapper *w) {
using InitFn = void (*)(EngineWrapper *); using InitFn = void (*)(EngineWrapper *);
InitFn initfn = (InitFn)dlsym(nullptr, "init_engine_wrapper"); InitFn initfn = (InitFn)dlsym(nullptr, "init_engine_wrapper");
if (initfn == nullptr) { if (initfn == nullptr) {
void *dll_handle = dlopen("./luprexlib.so", RTLD_NOW | RTLD_LOCAL); std::string path = luprexroot / "build/linux/luprexlib.so";
void *dll_handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
assert(dll_handle != nullptr); assert(dll_handle != nullptr);
initfn = (InitFn)dlsym(dll_handle, "init_engine_wrapper"); initfn = (InitFn)dlsym(dll_handle, "init_engine_wrapper");
} }

View File

@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
@@ -42,6 +43,13 @@ std::string path_to_plain_ascii(const std::filesystem::path &path) {
return oss.str(); return oss.str();
} }
std::filesystem::path get_exe_path() {
WCHAR exepath[MAX_PATH];
DWORD status = GetModuleFileNameW( NULL, exepath, MAX_PATH );
assert(status != 0);
return std::filesystem::path(exepath);
}
static void set_nonblocking(SOCKET sock) { static void set_nonblocking(SOCKET sock) {
u_long mode = 1; // 1 to enable non-blocking socket u_long mode = 1; // 1 to enable non-blocking socket
int status = ioctlsocket(sock, FIONBIO, &mode); int status = ioctlsocket(sock, FIONBIO, &mode);
@@ -278,12 +286,19 @@ static void ssl_load_certificate_authorities(SSL_CTX *ctx) {
CertCloseStore(hStore, 0); CertCloseStore(hStore, 0);
} }
static void call_init_engine_wrapper(EngineWrapper *w) { static void call_init_engine_wrapper(const std::filesystem::path &luprexroot, EngineWrapper *w) {
HMODULE exe = GetModuleHandleA(NULL); HMODULE exe = GetModuleHandleA(NULL);
using InitFn = void (*)(EngineWrapper *); using InitFn = void (*)(EngineWrapper *);
InitFn initfn = (InitFn)GetProcAddress(exe, "init_engine_wrapper"); InitFn initfn = (InitFn)GetProcAddress(exe, "init_engine_wrapper");
if (initfn == nullptr) { if (initfn == nullptr) {
HMODULE dll = LoadLibraryA(".\\luprexlib.dll"); #if defined(_MSC_VER)
std::wstring path = luprexroot / "build/visual/luprexlib.dll";
#elif defined(__GNUC__)
std::wstring path = luprexroot / "build/mingw/luprexlib.dll";
#else
#error "Cannot detect OS type"
#endif
HMODULE dll = LoadLibraryW(path.c_str());
assert(dll != nullptr); assert(dll != nullptr);
initfn = (InitFn)GetProcAddress(dll, "init_engine_wrapper"); initfn = (InitFn)GetProcAddress(dll, "init_engine_wrapper");
} }

View File

@@ -23,6 +23,27 @@ static void dprint_callback(const char *oneline, size_t size) {
fflush(stderr); fflush(stderr);
} }
inline bool file_exists(const std::filesystem::path &name) {
std::ifstream f(name);
return f.good();
}
std::filesystem::path find_luprex_root(std::filesystem::path exepath) {
std::filesystem::path pp = exepath.parent_path();
if (file_exists(pp / "lua/control.lst")) {
return pp;
}
pp = pp.parent_path();
if (file_exists(pp / "lua/control.lst")) {
return pp;
}
pp = pp.parent_path();
if (file_exists(pp / "lua/control.lst")) {
return pp;
}
assert(false && "Could not find lua/control.lst");
}
class Driver { class Driver {
public: public:
enum ChanState { enum ChanState {
@@ -60,6 +81,7 @@ class Driver {
bool marked_for_deletion() const { return state == CHAN_INACTIVE; } bool marked_for_deletion() const { return state == CHAN_INACTIVE; }
}; };
std::string luprexroot;
EngineWrapper engw; EngineWrapper engw;
std::vector<ChanInfo> chans_; std::vector<ChanInfo> chans_;
std::map<int, SOCKET> listen_sockets_; std::map<int, SOCKET> listen_sockets_;
@@ -557,8 +579,11 @@ class Driver {
argc -= 1; argc -= 1;
argv += 1; argv += 1;
// Find the root of the luprex tree.
luprexroot = find_luprex_root(get_exe_path());
// Load the DLL and gain access to its functions. // Load the DLL and gain access to its functions.
call_init_engine_wrapper(&engw); call_init_engine_wrapper(luprexroot, &engw);
engw.replay_cb_sent_outgoing = replay_cb_sent_outgoing; engw.replay_cb_sent_outgoing = replay_cb_sent_outgoing;
engw.hook_dprint(dprint_callback); engw.hook_dprint(dprint_callback);
@@ -605,7 +630,7 @@ class Driver {
// Read the initial lua source code. // Read the initial lua source code.
drvutil::ostringstream srcpak; drvutil::ostringstream srcpak;
std::string srcpakerr = drvutil::package_lua_source(".", &srcpak); std::string srcpakerr = drvutil::package_lua_source(luprexroot, &srcpak);
if_error_print_and_exit(srcpakerr); if_error_print_and_exit(srcpakerr);
// Initialize the engine. // Initialize the engine.

View File

@@ -8,6 +8,7 @@
#include <fstream> #include <fstream>
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
#include <filesystem>
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
@@ -91,10 +92,10 @@ static std::vector<std::string> parse_control_lst(std::string_view ctrl) {
// Read a source file into a string. // Read a source file into a string.
// //
static std::string read_file(const char *fn, std::string &err) { static std::string read_file(const std::filesystem::path &fn, std::string &err) {
std::ifstream t(fn); std::ifstream t(fn);
if (t.fail()) { if (t.fail()) {
err = std::string("Could not open ") + fn; err = std::string("Could not open ") + std::string(fn);
return ""; return "";
} }
t.seekg(0, std::ios::end); t.seekg(0, std::ios::end);
@@ -103,7 +104,7 @@ static std::string read_file(const char *fn, std::string &err) {
t.seekg(0); t.seekg(0);
t.read(&result[0], size); t.read(&result[0], size);
if ((t.fail()) || (size_t(t.tellg()) != size)) { if ((t.fail()) || (size_t(t.tellg()) != size)) {
err = std::string("Could not read ") + fn; err = std::string("Could not read ") + std::string(fn);
return ""; return "";
} }
err = ""; err = "";
@@ -132,7 +133,7 @@ static void sbwrite_string(std::ostream *s, std::string_view sv) {
// This encoding can be read by StreamBuffer::read_string. // This encoding can be read by StreamBuffer::read_string.
// //
static bool sbwrite_file(std::ostream *s, const char *fn) { static bool sbwrite_file(std::ostream *s, const std::filesystem::path &fn) {
s->put(0xFF); s->put(0xFF);
uint64_t pos1 = s->tellp(); uint64_t pos1 = s->tellp();
sbwrite_uint64(s, 0); sbwrite_uint64(s, 0);
@@ -152,10 +153,10 @@ static bool sbwrite_file(std::ostream *s, const char *fn) {
return true; return true;
} }
std::string package_lua_source(const std::string &base, std::ostream *s) { std::string package_lua_source(const std::filesystem::path &base, std::ostream *s) {
std::string err; std::string err;
std::string cfn = base + "/../../lua/control.lst"; std::filesystem::path cfn = base / "lua/control.lst";
std::string ctrl = read_file(cfn.c_str(), err); std::string ctrl = read_file(cfn, err);
if (!err.empty()) { if (!err.empty()) {
return err; return err;
} }
@@ -166,9 +167,9 @@ std::string package_lua_source(const std::string &base, std::ostream *s) {
sbwrite_string(s, names[i]); sbwrite_string(s, names[i]);
} }
for (int i = 0; i < int(names.size()); i++) { for (int i = 0; i < int(names.size()); i++) {
std::string lfn = base + "/../../lua/" + names[i]; std::filesystem::path lfn = base / "lua" / names[i];
if (!sbwrite_file(s, lfn.c_str())) { if (!sbwrite_file(s, lfn)) {
return std::string("Cannot read source file: ") + lfn; return std::string("Cannot read source file: ") + std::string(lfn);
} }
} }
return ""; return "";

View File

@@ -15,6 +15,7 @@
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <filesystem>
namespace drvutil { namespace drvutil {
@@ -37,7 +38,7 @@ namespace drvutil {
// returns an error message. In this case, the ostream contains garbage. If // returns an error message. In this case, the ostream contains garbage. If
// there is no error, returns the empty string. // there is no error, returns the empty string.
// //
std::string package_lua_source(const std::string &base, std::ostream *oss); std::string package_lua_source(const std::filesystem::path &base, std::ostream *oss);
// Parse a target designation. // Parse a target designation.
// //