Skip to content

Commit

Permalink
Tweak Working/Install directory determination
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Jun 30, 2024
1 parent f4c89e5 commit c79a553
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 121 deletions.
11 changes: 1 addition & 10 deletions source_files/obsidian_main/m_addons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ std::map<std::filesystem::path, int> initial_enabled_addons;
std::vector<addon_info_t> all_addons;

void VFS_AddFolder(std::string name) {
#ifdef _WIN32
std::filesystem::path path = physfs_dir;
#else
std::filesystem::path path = install_dir;
#endif
path /= name;
std::string mount = fmt::format("/{}", name);

Expand Down Expand Up @@ -94,14 +90,9 @@ bool VFS_AddArchive(std::filesystem::path filename, bool options_file) {
return true; // Ok
}

void VFS_InitAddons(const char *argv0) {
void VFS_InitAddons() {
LogPrintf("Initializing VFS...\n");

if (!PHYSFS_init(argv0)) {
Main::FatalError("Failed to init PhysFS:\n{}\n",
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}

VFS_AddFolder("scripts");
VFS_AddFolder("games");
VFS_AddFolder("engines");
Expand Down
2 changes: 1 addition & 1 deletion source_files/obsidian_main/m_addons.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "sys_type.h"

void VFS_InitAddons(const char *argv0);
void VFS_InitAddons();
void VFS_ParseCommandLine();
void VFS_ScanForAddons();

Expand Down
122 changes: 17 additions & 105 deletions source_files/obsidian_main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ constexpr size_t TICKER_TIME = 50;

std::filesystem::path home_dir;
std::filesystem::path install_dir;
#ifdef WIN32
std::filesystem::path physfs_dir;
#endif

std::filesystem::path config_file;
std::filesystem::path options_file;
std::filesystem::path theme_file;
Expand Down Expand Up @@ -334,63 +330,13 @@ char32_t *ucs4_path(const char *path) {
}
#endif

void Determine_WorkingPath(const char *argv0) {
// firstly find the "Working directory" : that's the place where
// the CONFIG.txt and LOGS.txt files are, as well the temp files.

if (const int home_arg = argv::Find(0, "home"); home_arg >= 0) {
if (home_arg + 1 >= argv::list.size() || argv::IsOption(home_arg + 1)) {
fmt::print(stderr, "OBSIDIAN ERROR: missing path for --home\n");
exit(EXIT_FAILURE);
}

home_dir = argv::list[home_arg + 1];
return;
}

#ifdef WIN32
home_dir = ucs4_path(argv0);
home_dir.remove_filename();
physfs_dir = argv0;
physfs_dir.remove_filename();
void Determine_WorkingPath()
{
#ifdef _WIN32
home_dir = std::filesystem::u8path(PHYSFS_getBaseDir());
#else
const char *xdg_config_home = std::getenv("XDG_CONFIG_HOME");
if (xdg_config_home == nullptr) {
xdg_config_home = std::getenv("HOME");
if (xdg_config_home == nullptr) {
home_dir = ".";
} else {
home_dir = xdg_config_home;
home_dir /= ".config";
}
} else {
home_dir = xdg_config_home;
}
home_dir /= "obsidian";
if (!home_dir.is_absolute()) {
home_dir = std::getenv("HOME");
home_dir /= ".config/obsidian";

if (!home_dir.is_absolute()) {
Main::FatalError("Unable to find $HOME directory!\n");
}
}
// FLTK is going to want a ~/.config directory as well I think - Dasho
#ifdef __OpenBSD__
std::filesystem::path config_checker = std::getenv("HOME");
config_checker /= ".config";
if (!std::filesystem::exists(config_checker)) {
std::filesystem::create_directory(config_checker);
}
home_dir = PHYSFS_getPrefDir("Obsidian Team", "Obsidian");
#endif
// try to create it (doesn't matter if it already exists)
if (!std::filesystem::exists(home_dir)) {
std::filesystem::create_directory(home_dir);
}
#endif
if (home_dir.empty()) {
home_dir = ".";
}
}

std::filesystem::path Resolve_DefaultOutputPath() {
Expand All @@ -417,48 +363,9 @@ static bool Verify_InstallDir(const std::filesystem::path &path) {
return exists(filename);
}

void Determine_InstallDir(const char *argv0) {
// secondly find the "Install directory", and store the
// result in the global variable 'install_dir'. This is
// where all the LUA scripts and other data files are.

if (const int inst_arg = argv::Find(0, "install"); inst_arg >= 0) {
if (inst_arg + 1 >= argv::list.size() || argv::IsOption(inst_arg + 1)) {
fmt::print(stderr, "OBSIDIAN ERROR: missing path for --install\n");
exit(EXIT_FAILURE);
}

install_dir = argv::list[inst_arg + 1];

if (Verify_InstallDir(install_dir)) {
return;
}

Main::FatalError("Bad install directory specified!\n");
}

// if run from current directory, look there
if (argv0[0] == '.' && Verify_InstallDir(".")) {
install_dir = ".";
return;
}

#ifdef WIN32
install_dir = home_dir;
#else
if (Verify_InstallDir(CMAKE_INSTALL_PREFIX "/share/obsidian")) {
return;
}

// Last resort
if (Verify_InstallDir(std::filesystem::current_path().c_str())) {
install_dir = std::filesystem::current_path();
}
#endif

if (install_dir.empty()) {
Main::FatalError("Unable to find Obsidian's install directory!\n");
}
void Determine_InstallDir()
{
install_dir = std::filesystem::u8path(PHYSFS_getBaseDir());
}

void Determine_ConfigFile() {
Expand Down Expand Up @@ -1367,10 +1274,15 @@ int main(int argc, char **argv) {
argv::short_flags.emplace('u');

// parse the flags
argv::Init(argc - 1, argv + 1);
argv::Init(argc, argv);

hardrestart:;

if (!PHYSFS_init(argv::list[0].c_str())) {
Main::FatalError("Failed to init PhysFS:\n{}\n",
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
}

if (argv::Find('?', NULL) >= 0 || argv::Find('h', "help") >= 0) {
#if defined WIN32 && !defined CONSOLE_ONLY
if (AllocConsole()) {
Expand Down Expand Up @@ -1476,8 +1388,8 @@ hardrestart:;
#endif
}

Determine_WorkingPath(argv[0]);
Determine_InstallDir(argv[0]);
Determine_WorkingPath();
Determine_InstallDir();

Determine_ConfigFile();
Determine_OptionsFile();
Expand Down Expand Up @@ -1564,7 +1476,7 @@ softrestart:;
// TX_TestSynth(next_rand_seed); - Fractal testing stuff

if (main_action != MAIN_SOFT_RESTART) {
VFS_InitAddons(argv[0]);
VFS_InitAddons();

if (const int load_arg = argv::Find('l', "load"); load_arg >= 0) {
if (load_arg + 1 >= argv::list.size() ||
Expand Down
6 changes: 1 addition & 5 deletions source_files/obsidian_main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ constexpr const char *LOG_FILENAME = "LOGS.txt";
constexpr const char *REF_FILENAME = "REFERENCE.txt";

#ifdef _WIN32
char32_t *ucs4_path(const char *path);
#ifndef CONSOLE_ONLY
HEDLEY_ALWAYS_INLINE
int i_load_private_font(const char *path) {
Expand Down Expand Up @@ -88,11 +89,6 @@ int v_unload_private_font(const char *path) {

extern std::filesystem::path home_dir;
extern std::filesystem::path install_dir;
#ifdef WIN32
char32_t *ucs4_path(const char *path);
extern std::filesystem::path physfs_dir;
#endif

extern std::filesystem::path config_file;
extern std::filesystem::path options_file;
extern std::filesystem::path theme_file;
Expand Down

0 comments on commit c79a553

Please sign in to comment.