Commit f572239c authored by Florian Oetke's avatar Florian Oetke
Browse files

added source directory to default search path so that the working directory...

added source directory to default search path so that the working directory doesn't need to be changed anymore
parent fe79d1a3
......@@ -6,6 +6,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
# required at top-level
set(MIRRAGE_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
set(MIRRAGE_ROOT_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${MIRRAGE_ROOT_DIR}/modules")
enable_language(C CXX ASM)
......
......@@ -19,15 +19,16 @@
namespace mirrage {
Game_engine::Game_engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
int argc,
char** argv,
char** env)
: Engine(org, title, version_major, version_minor, debug, false, argc, argv, env)
Game_engine::Game_engine(const std::string& org,
const std::string& title,
util::maybe<std::string> base_dir,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
int argc,
char** argv,
char** env)
: Engine(org, title, std::move(base_dir), version_major, version_minor, debug, false, argc, argv, env)
, _debug_ui(assets(), gui(), bus())
, _renderer_factory(std::make_unique<renderer::Deferred_renderer_factory>(
*this,
......
......@@ -22,14 +22,15 @@ namespace mirrage {
class Game_engine : public Engine {
public:
Game_engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
int argc,
char** argv,
char** env);
Game_engine(const std::string& org,
const std::string& title,
util::maybe<std::string> base_dir,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
int argc,
char** argv,
char** env);
~Game_engine() override;
auto renderer_factory() noexcept -> auto& { return *_renderer_factory; }
......
......@@ -73,14 +73,23 @@ int main(int argc, char** argv, char** env)
namespace {
constexpr auto org_name = "secondsystem";
constexpr auto app_name = "Mirrage";
int argc;
char** argv;
char** env;
auto base_dir() -> util::maybe<std::string>
{
#ifndef NDEBUG
return mirrage::version_info::engine_root + "/assets";
#else
return util::nothing;
#endif
}
int argc;
char** argv;
char** env;
void init_env(int argc, char** argv, char** env)
{
auto write_dir = asset::write_dir(argv[0], org_name, app_name);
auto write_dir = asset::write_dir(argv[0], org_name, app_name, base_dir());
static auto fileAppender = plog::RollingFileAppender<plog::TxtFormatter>(
(write_dir + "/mirrage.log").c_str(), 1024L * 1024L, 4);
......@@ -95,6 +104,7 @@ namespace {
::env = env;
LOG(plog::debug) << "Game started from: " << argv[0] << "\n"
<< "Base dir: " << base_dir().get_ref_or("<NONE>") << "\n"
<< "Working dir: " << asset::pwd() << "\n"
<< "Write dir: " << write_dir << "\n"
<< "Version: " << version_info::name << "\n"
......@@ -120,7 +130,7 @@ namespace {
}
engine = std::make_unique<Game_engine>(org_name, app_name, 0, 1, debug, argc, argv, env);
engine = std::make_unique<Game_engine>(org_name, app_name, base_dir(), 0, 1, debug, argc, argv, env);
global_commands = std::make_unique<util::Console_command_container>();
global_commands->add("screen.leave <count> | Pops the top <count> screens",
......
......@@ -44,9 +44,10 @@ namespace mirrage::asset {
}
extern std::string pwd();
extern std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name);
extern std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name,
util::maybe<std::string> additional_search_path);
template <class R>
......@@ -134,7 +135,10 @@ namespace mirrage::asset {
class Asset_manager {
public:
Asset_manager(const std::string& exe_name, const std::string& org_name, const std::string& app_name);
Asset_manager(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name,
util::maybe<std::string> additional_search_path);
~Asset_manager();
void reload();
......
......@@ -138,7 +138,7 @@ namespace {
}
}
void init_physicsfs(const std::string& exe_name)
void init_physicsfs(const std::string& exe_name, mirrage::util::maybe<std::string> additional_search_path)
{
if(PHYSFS_isInit())
return;
......@@ -153,6 +153,8 @@ namespace {
|| !PHYSFS_mount(mirrage::asset::pwd().c_str(), nullptr, 1))
throw std::system_error(static_cast<mirrage::asset::Asset_error>(PHYSFS_getLastErrorCode()),
"Unable to setup default search path.");
additional_search_path.process([&](auto& dir) { PHYSFS_mount(dir.c_str(), nullptr, 1); });
}
constexpr auto default_source = {std::make_tuple("assets", false), std::make_tuple("assets.zip", true)};
......@@ -174,11 +176,12 @@ namespace mirrage::asset {
return cCurrentPath;
}
std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name)
std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name,
util::maybe<std::string> additional_search_path)
{
init_physicsfs(exe_name);
init_physicsfs(exe_name, additional_search_path);
if(exists_dir("write_dir")) {
return std::string(PHYSFS_getRealDir("write_dir")) + "/write_dir";
......@@ -187,13 +190,14 @@ namespace mirrage::asset {
return PHYSFS_getPrefDir(org_name.c_str(), app_name.c_str());
}
Asset_manager::Asset_manager(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name)
Asset_manager::Asset_manager(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name,
util::maybe<std::string> additional_search_path)
{
init_physicsfs(exe_name);
init_physicsfs(exe_name, additional_search_path);
auto write_dir = ::mirrage::asset::write_dir(exe_name, org_name, app_name);
auto write_dir = ::mirrage::asset::write_dir(exe_name, org_name, app_name, additional_search_path);
create_dir(write_dir);
LOG(plog::debug) << "Write dir: " << write_dir;
......@@ -222,9 +226,14 @@ namespace mirrage::asset {
}
}
auto add_source = [](const char* path) {
auto add_source = [](std::string path) {
auto apath = PHYSFS_getRealDir(path.c_str());
if(apath) {
path = std::string(apath) + "/" + path;
}
LOG(plog::info) << "Added FS directory: " << path;
if(!PHYSFS_mount(path, nullptr, 1))
if(!PHYSFS_mount(path.c_str(), nullptr, 1))
throw std::system_error(static_cast<Asset_error>(PHYSFS_getLastErrorCode()),
"Error adding custom archive: "s + path);
};
......@@ -271,6 +280,7 @@ namespace mirrage::asset {
PHYSFS_unmount(PHYSFS_getBaseDir());
PHYSFS_unmount(append_file(PHYSFS_getBaseDir(), "..").c_str());
PHYSFS_unmount(mirrage::asset::pwd().c_str());
additional_search_path.process([&](auto& dir) { PHYSFS_unmount(dir.c_str()); });
_reload_dispatchers();
}
......
......@@ -46,15 +46,16 @@ namespace mirrage {
class Engine {
public:
Engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
bool headless,
int argc,
char** argv,
char** env);
Engine(const std::string& org,
const std::string& title,
util::maybe<std::string> base_dir,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
bool headless,
int argc,
char** argv,
char** env);
virtual ~Engine() noexcept;
bool running() const noexcept { return !_quit; }
......
......@@ -15,4 +15,6 @@ namespace mirrage::version_info {
extern const std::string hash;
extern const std::string date;
extern const std::string subject;
extern const std::string project_root;
extern const std::string engine_root;
} // namespace mirrage::version_info
......@@ -8,6 +8,8 @@ namespace mirrage {
const std::string hash = "@GIT_HASH@";
const std::string date = "@GIT_DATE@";
const std::string subject = "@GIT_SUBJECT@";
const std::string project_root = "@MIRRAGE_ROOT_PROJECT_DIR@";
const std::string engine_root = "@MIRRAGE_ROOT_DIR@";
}
......
......@@ -84,17 +84,18 @@ namespace mirrage {
Engine::Sdl_wrapper::~Sdl_wrapper() { SDL_Quit(); }
Engine::Engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
bool headless,
int argc,
char** argv,
Engine::Engine(const std::string& org,
const std::string& title,
util::maybe<std::string> base_dir,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
bool headless,
int argc,
char** argv,
char**)
: _screens(*this)
, _asset_manager(std::make_unique<asset::Asset_manager>(argc > 0 ? argv[0] : "", org, title))
, _asset_manager(std::make_unique<asset::Asset_manager>(argc > 0 ? argv[0] : "", org, title, base_dir))
, _translator(std::make_unique<Translator>(*_asset_manager))
, _sdl(headless)
, _graphics_context(headless ? std::unique_ptr<graphic::Context>()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment