From c4a0774d367b5e8ff7a2aeebf1bbe4386621b51f Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Fri, 3 May 2019 20:33:00 +0200 Subject: [PATCH] Search paths for mods and games --- src/Makefile.am | 2 +- src/mods.cpp | 7 +++++++ src/paths.cpp | 28 ++++++++++++++++++++++++++++ src/paths.h | 7 +++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/paths.cpp create mode 100644 src/paths.h diff --git a/src/Makefile.am b/src/Makefile.am index a15f108..e11bd0d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ bin_PROGRAMS = $(top_builddir)/DungeonMaster -__top_builddir__DungeonMaster_SOURCES = main.cpp mods.cpp +__top_builddir__DungeonMaster_SOURCES = main.cpp mods.cpp paths.cpp __top_builddir__DungeonMaster_CPPFLAGS = @GUILE_CFLAGS@ __top_builddir__DungeonMaster_LDFLAGS = @GUILE_LIBS@ diff --git a/src/mods.cpp b/src/mods.cpp index a1dccfa..bbbd6c4 100644 --- a/src/mods.cpp +++ b/src/mods.cpp @@ -1,5 +1,7 @@ +#include #include #include "mods.h" +#include "paths.h" SCM register_generator(SCM name, SCM proc) { @@ -21,6 +23,11 @@ void scm_init_dungeon_master_module() void load_mods() { + std::set paths = get_dm_paths(); + for (const std::string &path : paths) { + std::cout << path << std::endl; + } + scm_init_guile(); scm_init_dungeon_master_module(); scm_primitive_load(scm_from_locale_string("mods/default/main.scm")); diff --git a/src/paths.cpp b/src/paths.cpp new file mode 100644 index 0000000..ae05f3b --- /dev/null +++ b/src/paths.cpp @@ -0,0 +1,28 @@ +#include +#include "paths.h" + +std::string get_exec_path() +{ + char pBuf[256]; + size_t len = sizeof(pBuf); + char szTmp[32]; + + sprintf(szTmp, "/proc/%d/exe", getpid()); + int bytes = readlink(szTmp, pBuf, len); + if (bytes > len - 1) + bytes = len - 1; + if (bytes >= 0) + pBuf[bytes] = '\0'; + + std::string exec_path = pBuf; + return exec_path.substr(0, exec_path.rfind(DIR_DELIM) + 1); +} + + +std::set get_dm_paths() { + std::set paths; + + paths.insert(get_exec_path()); + + return paths; +} diff --git a/src/paths.h b/src/paths.h new file mode 100644 index 0000000..c8ecc8e --- /dev/null +++ b/src/paths.h @@ -0,0 +1,7 @@ +#include +#include + +#define DIR_DELIM "/" + +std::string get_exec_path(); +std::set get_dm_paths(); -- 2.39.5