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

@@ -23,6 +23,27 @@ static void dprint_callback(const char *oneline, size_t size) {
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 {
public:
enum ChanState {
@@ -60,6 +81,7 @@ class Driver {
bool marked_for_deletion() const { return state == CHAN_INACTIVE; }
};
std::string luprexroot;
EngineWrapper engw;
std::vector<ChanInfo> chans_;
std::map<int, SOCKET> listen_sockets_;
@@ -557,8 +579,11 @@ class Driver {
argc -= 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.
call_init_engine_wrapper(&engw);
call_init_engine_wrapper(luprexroot, &engw);
engw.replay_cb_sent_outgoing = replay_cb_sent_outgoing;
engw.hook_dprint(dprint_callback);
@@ -605,7 +630,7 @@ class Driver {
// Read the initial lua source code.
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);
// Initialize the engine.