diff --git a/code/cfile/cfile.cpp b/code/cfile/cfile.cpp index 6fac45be3c0..9965059cc9b 100644 --- a/code/cfile/cfile.cpp +++ b/code/cfile/cfile.cpp @@ -29,6 +29,7 @@ #include "cfile/cfile.h" #include "cfile/cfilearchive.h" #include "cfile/cfilesystem.h" +#include "cfile/mod_bootstrap.h" #include "osapi/osapi.h" #include "parse/encrypt.h" #include "cfilesystem.h" @@ -183,41 +184,31 @@ int cfile_init(const char *exe_dir, const char *cdrom_dir) return 0; } - char buf[CFILE_ROOT_DIRECTORY_LEN]; - - #ifndef __ANDROID__ + if (const auto bootstrap = mod_bootstrap_get()) { + // Keep relative settings paths anchored to the launch directory. + strcpy_s(Cfile_root_dir, bootstrap->workfolder.c_str()); + } else { + char buf[CFILE_ROOT_DIRECTORY_LEN]; +#ifndef __ANDROID__ strncpy(buf, exe_dir, CFILE_ROOT_DIRECTORY_LEN - 1); - #else +#else (void)exe_dir; strncpy(buf, os_android_get_working_folder_path().c_str(), CFILE_ROOT_DIRECTORY_LEN - 1); - #endif - - buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0'; +#endif + buf[CFILE_ROOT_DIRECTORY_LEN - 1] = '\0'; + if (cfile_in_root_dir(buf)) { + os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, "FreeSpace2/Fred2 cannot be run from a drive root directory!"); + return 1; + } - // are we in a root directory? - if(cfile_in_root_dir(buf)){ - os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, "FreeSpace2/Fred2 cannot be run from a drive root directory!"); - return 1; - } + // Remove the executable name to get the game directory. + *strrchr(buf, DIR_SEPARATOR_CHAR) = '\0'; + cfile_chdir(buf); + strcpy_s(Cfile_root_dir, buf); + } - // This needs to be set here because cf_build_secondary_filelist assumes it to be true + // The index builder requires CFILE to be marked initialized. cfile_inited = 1; - - /* - * Determine the executable's directory. Note that DIR_SEPARATOR_CHAR - * is guaranteed to be found in the string else cfile_in_root_dir() - * would have failed. - */ - - char *p; - - p = strrchr(buf, DIR_SEPARATOR_CHAR); - *p = '\0'; - - cfile_chdir(buf); - - // set root directory - strcpy_s(Cfile_root_dir, buf); strcpy_s(Cfile_user_dir, os_get_config_path().c_str()); // Initialize the block list with proper data @@ -467,7 +458,9 @@ int cf_delete(const char *filename, int path_type, uint32_t location_flags) Assert(CF_TYPE_SPECIFIED(path_type)); - cf_create_default_path_string(longname, path_type, filename, location_flags); + if (!cf_create_default_path_string(longname, path_type, filename, location_flags, true)) { + return 0; + } return (_unlink(longname.c_str()) != -1); } @@ -531,7 +524,9 @@ void cf_attrib(const char *filename, int set, int clear, int dir_type) Assert( CF_TYPE_SPECIFIED(dir_type) ); - cf_create_default_path_string(longname, dir_type, filename); + if (!cf_create_default_path_string(longname, dir_type, filename, static_cast(CF_LOCATION_ALL), true)) { + return; + } FILE *fp = fopen(longname.c_str(), "rb"); if (fp) { @@ -552,8 +547,11 @@ int cf_rename(const char *old_name, const char *name, int dir_type) SCP_string old_longname; SCP_string new_longname; - cf_create_default_path_string(old_longname, dir_type, old_name); - cf_create_default_path_string(new_longname, dir_type, name); + const auto flags = static_cast(CF_LOCATION_ALL); + if (!cf_create_default_path_string(old_longname, dir_type, old_name, flags, true) || + !cf_create_default_path_string(new_longname, dir_type, name, flags, true)) { + return CF_RENAME_FAIL_ACCESS; + } ret_code = rename(old_longname.c_str(), new_longname.c_str()); if(ret_code != 0){ @@ -617,7 +615,9 @@ void cf_create_directory(int dir_type, uint32_t location_flags) int i; for (i=num_dirs-1; i>=0; i-- ) { - cf_create_default_path_string(longname, dir_tree[i], nullptr, location_flags); + if (!cf_create_default_path_string(longname, dir_tree[i], nullptr, location_flags, true)) { + return; + } if (stat(longname.c_str(), &statbuf) != 0) { mprintf(( "CFILE: Creating new directory '%s'\n", longname.c_str() )); mkdir_recursive(longname.c_str()); @@ -674,7 +674,9 @@ CFILE* _cfopen(const char* source, int line, const char* file_path, const char* // Create the directory if necessary cf_create_directory(dir_type, location_flags); - cf_create_default_path_string(longname, dir_type, file_path, location_flags); + if (!cf_create_default_path_string(longname, dir_type, file_path, location_flags, true)) { + return nullptr; + } } // JOHN: TODO, you should create the path if it doesn't exist. diff --git a/code/cfile/cfilesystem.cpp b/code/cfile/cfilesystem.cpp index 545ab62d105..16a821b9fc7 100644 --- a/code/cfile/cfilesystem.cpp +++ b/code/cfile/cfilesystem.cpp @@ -36,6 +36,7 @@ #include "cfile/cfile.h" #include "cfile/cfilesystem.h" +#include "cfile/mod_bootstrap.h" #include "cmdline/cmdline.h" #include "globalincs/pstypes.h" #include "def_files/def_files.h" @@ -57,6 +58,7 @@ typedef struct cf_root { SCP_string path; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file, CF_ROOTTYPE_MEMORY=In memory uint32_t location_flags; + bool read_only = false; #ifdef SCP_UNIX // map of existing case sensitive paths @@ -569,6 +571,20 @@ void normalize_directory_separators(SCP_string &str) std::replace(str.begin(), str.end(), bad_sep, DIR_SEPARATOR_CHAR); } +static void cf_add_mod_root(SCP_string path, uint32_t location, bool read_only = false) +{ + if (path.back() != DIR_SEPARATOR_CHAR) { + path += DIR_SEPARATOR_CHAR; + } + cf_root* root = cf_create_root(); + root->path = std::move(path); + root->location_flags = location; + root->roottype = CF_ROOTTYPE_PATH; + root->read_only = read_only; + cf_init_root_pathtypes(root); + cf_build_pack_list(root); +} + static void cf_add_mod_roots(const char* rootDirectory, uint32_t basic_location) { if (Cmdline_mod) @@ -594,18 +610,8 @@ static void cf_add_mod_roots(const char* rootDirectory, uint32_t basic_location) // normalize the path to the native path format normalize_directory_separators(rootPath); - cf_root* root = cf_create_root(); - - root->path = std::move(rootPath); - if (primary) { - root->location_flags = basic_location | CF_LOCATION_TYPE_PRIMARY_MOD; - } else { - root->location_flags = basic_location | CF_LOCATION_TYPE_SECONDARY_MODS; - } - - root->roottype = CF_ROOTTYPE_PATH; - cf_init_root_pathtypes(root); - cf_build_pack_list(root); + const auto mod_type = primary ? CF_LOCATION_TYPE_PRIMARY_MOD : CF_LOCATION_TYPE_SECONDARY_MODS; + cf_add_mod_root(std::move(rootPath), basic_location | mod_type); primary = false; } @@ -618,6 +624,8 @@ void cf_build_root_list(const char *cdrom_dir) Num_path_roots = 0; cf_root *root = nullptr; + const auto bootstrap = mod_bootstrap_get(); + const bool has_mods = bootstrap ? !bootstrap->roots.empty() : Cmdline_mod && *Cmdline_mod; if (os_is_legacy_mode()) { @@ -625,13 +633,15 @@ void cf_build_root_list(const char *cdrom_dir) #ifdef WIN32 // Nothing to do here, Windows uses the current directory as the base #else - cf_add_mod_roots(os_get_legacy_user_dir(), CF_LOCATION_ROOT_USER); + if (!bootstrap) { + cf_add_mod_roots(os_get_legacy_user_dir(), CF_LOCATION_ROOT_USER); + } root = cf_create_root(); root->path = os_get_legacy_user_dir(); root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT; - if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) { + if (!has_mods) { // If there are no mods then the root is the primary mod root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD; } @@ -653,7 +663,9 @@ void cf_build_root_list(const char *cdrom_dir) { // ========================================================================= // now look for mods under the users HOME directory to use before system ones - cf_add_mod_roots(Cfile_user_dir, CF_LOCATION_ROOT_USER); + if (!bootstrap) { + cf_add_mod_roots(Cfile_user_dir, CF_LOCATION_ROOT_USER); + } // ========================================================================= // ========================================================================= @@ -662,7 +674,7 @@ void cf_build_root_list(const char *cdrom_dir) root->path = Cfile_user_dir; root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT; - if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) { + if (!has_mods) { // If there are no mods then the root is the primary mod root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD; } @@ -680,11 +692,11 @@ void cf_build_root_list(const char *cdrom_dir) // ========================================================================= } - char working_directory[CF_MAX_PATHNAME_LENGTH]; - - if ( !_getcwd(working_directory, CF_MAX_PATHNAME_LENGTH ) ) { + char cwd[CF_MAX_PATHNAME_LENGTH]; + if (!bootstrap && !_getcwd(cwd, sizeof(cwd))) { Error(LOCATION, "Can't get current working directory -- %d", errno ); } + const char* working_directory = bootstrap ? Cfile_root_dir : cwd; if (Cmdline_override_data) { @@ -709,13 +721,21 @@ void cf_build_root_list(const char *cdrom_dir) } //now that the _override root is finished(or not), handle all mods - cf_add_mod_roots(working_directory, CF_LOCATION_ROOT_GAME); + if (bootstrap) { + auto mod_type = CF_LOCATION_TYPE_PRIMARY_MOD; + for (const auto& path : bootstrap->roots) { + cf_add_mod_root(path, CF_LOCATION_ROOT_GAME | mod_type, true); + mod_type = CF_LOCATION_TYPE_SECONDARY_MODS; + } + } else { + cf_add_mod_roots(working_directory, CF_LOCATION_ROOT_GAME); + } //finally, handle the default root. root = cf_create_root(); root->location_flags |= CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT; - if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) { + if (!has_mods) { // If there are no mods then the root is the primary mod root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD; } @@ -2399,7 +2419,8 @@ int cf_create_default_path_string(char* path, uint path_max, int pathtype, const // filename - optional, if set, tacks the filename onto end of path. // Output: path - Fully qualified pathname. //Returns 0 if the result would be too long (invalid result) -int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename, uint32_t _location_flags) +int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename, + uint32_t _location_flags, bool for_write) { uint32_t location_flags = _location_flags; @@ -2424,7 +2445,7 @@ int cf_create_default_path_string(SCP_string& path, int pathtype, const char* fi for (auto i = 0; i < Num_roots; ++i) { auto current_root = cf_get_root(i); - if (current_root->roottype != CF_ROOTTYPE_PATH) { + if (current_root->roottype != CF_ROOTTYPE_PATH || (for_write && current_root->read_only)) { // We want a "real" path here so only path roots are valid continue; } @@ -2437,7 +2458,7 @@ int cf_create_default_path_string(SCP_string& path, int pathtype, const char* fi } if (!root) { - if (filename == nullptr) { + if (filename == nullptr || (for_write && mod_bootstrap_get())) { return 0; } path.assign(filename); diff --git a/code/cfile/cfilesystem.h b/code/cfile/cfilesystem.h index b251d28f56f..0f2c2975a6a 100644 --- a/code/cfile/cfilesystem.h +++ b/code/cfile/cfilesystem.h @@ -44,6 +44,6 @@ bool cf_check_location_flags(uint32_t check_flags, uint32_t desired_flags); int cf_create_default_path_string(char* path, uint path_max, int pathtype, const char* filename = nullptr, uint32_t location_flags = CF_LOCATION_ALL); int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename = nullptr, - uint32_t location_flags = CF_LOCATION_ALL); + uint32_t location_flags = CF_LOCATION_ALL, bool for_write = false); #endif //_CFILESYSTEM_H diff --git a/code/cfile/mod_bootstrap.cpp b/code/cfile/mod_bootstrap.cpp new file mode 100644 index 00000000000..eb5e33c1ef0 --- /dev/null +++ b/code/cfile/mod_bootstrap.cpp @@ -0,0 +1,128 @@ +#include "cfile/mod_bootstrap.h" + +#include "libs/jansson.h" +#include "osapi/osapi.h" + +#include +#include +#include + +namespace { + +namespace fs = std::filesystem; +std::optional Active_bootstrap; + +SCP_string resolve_directory(fs::path path, const fs::path& base, const SCP_string& field) +{ + if (!path.is_absolute()) { + if (base.empty() || path.has_root_path()) { + throw std::runtime_error(field + " requires a fully qualified path: " + path.u8string()); + } + path = base / path; + } + path = path.lexically_normal().make_preferred(); + + std::error_code error; + fs::directory_iterator entries(path, error); + if (error) { + throw std::runtime_error(field + " cannot be opened as a directory: " + path.u8string() + + " (" + error.message() + ")"); + } + + auto result = path.string(); + // CFILE appends a separator when it installs the root. + if (result.size() + 1 >= CF_MAX_PATHNAME_LENGTH) { + throw std::runtime_error(field + " exceeds CFILE's path limit: " + path.u8string()); + } + return result; +} + +ModBootstrap read_bootstrap(const char* filename) +{ + if (!fs::u8path(filename).is_absolute()) { + throw std::runtime_error("-bootstrap requires an absolute path"); + } + + size_t size = 0; + std::unique_ptr bytes(SDL_LoadFile(filename, &size), SDL_free); + if (!bytes) { + throw std::runtime_error(SDL_GetError()); + } + + json_error_t error; + std::unique_ptr document(json_loadb(static_cast(bytes.get()), size, JSON_REJECT_DUPLICATES, &error)); + if (!document) { + throw json_exception(error); + } + if (!json_is_object(document.get())) { + throw std::runtime_error("the top-level value must be a JSON object"); + } + const auto version = json_object_get(document.get(), "version"); + const auto format_version = json_integer_value(version); + if (!json_is_integer(version) || format_version != 1) { + throw std::runtime_error("expected bootstrap version 1"); + } + const auto roots = json_object_get(document.get(), "roots"); + if (!json_is_array(roots)) { + throw std::runtime_error("roots must be an array of directory paths"); + } + + ModBootstrap result; + result.version = static_cast(format_version); + const auto mod_id = json_object_get(document.get(), "mod_id"); + if (mod_id && !json_is_null(mod_id)) { + if (!json_is_string(mod_id)) { + throw std::runtime_error("mod_id must be a string or null"); + } + result.mod_id = json_string_value(mod_id); + if (result.mod_id.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.") != SCP_string::npos || + result.mod_id == "." || result.mod_id == "..") { + throw std::runtime_error("mod_id contains characters that cannot be used in a settings filename"); + } + } + if (json_array_size(roots) != 0 && result.mod_id.empty()) { + throw std::runtime_error("mod_id is required when roots contains mods"); + } + + const auto workfolder = json_object_get(document.get(), "workfolder"); + if (workfolder && !json_is_null(workfolder) && !json_is_string(workfolder)) { + throw std::runtime_error("workfolder must be an absolute path or null"); + } + fs::path base; + if (json_is_string(workfolder) && json_string_length(workfolder) != 0) { + base = fs::u8path(json_string_value(workfolder)); + } else { +#ifdef __ANDROID__ + base = fs::u8path(os_android_get_working_folder_path()); +#else + base = fs::current_path(); +#endif + } + result.workfolder = resolve_directory(base, {}, "workfolder"); + + size_t index; + json_t* root; + json_array_foreach(roots, index, root) { + const auto field = "roots[" + std::to_string(index) + "]"; + if (!json_is_string(root) || json_string_length(root) == 0) { + throw std::runtime_error(field + " must be a nonempty directory path"); + } + result.roots.push_back(resolve_directory(fs::u8path(json_string_value(root)), base, field)); + } + return result; +} + +} // namespace + +void mod_bootstrap_init(const char* filename) +{ + Active_bootstrap.reset(); + if (filename) { + Active_bootstrap = read_bootstrap(filename); + } +} + +const ModBootstrap* mod_bootstrap_get() +{ + return Active_bootstrap ? &*Active_bootstrap : nullptr; +} diff --git a/code/cfile/mod_bootstrap.h b/code/cfile/mod_bootstrap.h new file mode 100644 index 00000000000..7f404e1348f --- /dev/null +++ b/code/cfile/mod_bootstrap.h @@ -0,0 +1,14 @@ +#pragma once + +#include "globalincs/pstypes.h" + +struct ModBootstrap { + int version = 0; + SCP_string mod_id; + SCP_string workfolder; + SCP_vector roots; +}; + +const char* cmdline_bootstrap_path(); +void mod_bootstrap_init(const char* filename); +const ModBootstrap* mod_bootstrap_get(); diff --git a/code/cmdline/cmdline.cpp b/code/cmdline/cmdline.cpp index 6ff17e1cbaa..043f7d859c8 100644 --- a/code/cmdline/cmdline.cpp +++ b/code/cmdline/cmdline.cpp @@ -8,6 +8,7 @@ */ #include "cmdline/cmdline.h" +#include "cfile/mod_bootstrap.h" #include "camera/camera.h" //VIEWER_ZOOM_DEFAULT #include "cfile/cfilesystem.h" #include "fireball/fireballs.h" @@ -423,6 +424,12 @@ int Cmdline_no_enhanced_sound = 0; // MOD related cmdline_parm mod_arg("-mod", "List of folders to overwrite/add-to the default data", AT_STRING, true); // Cmdline_mod -- DTP modsupport +cmdline_parm bootstrap_arg("-bootstrap", "Load the ordered mod roots from an absolute bootstrap.json path", AT_STRING); + +const char* cmdline_bootstrap_path() +{ + return bootstrap_arg.found() ? bootstrap_arg.str() : nullptr; +} cmdline_parm campaign_arg("-campaign", "Set current campaign", AT_STRING); // Cmdline_campaign char *Cmdline_mod = NULL; //DTP for mod argument @@ -1917,7 +1924,7 @@ bool SetCmdlineParams() } - if(mod_arg.found() ) { + if (mod_arg.found() && !bootstrap_arg.found()) { Cmdline_mod = mod_arg.str(); // strip off blank space if it's there diff --git a/code/osapi/osregistry.cpp b/code/osapi/osregistry.cpp index 61a524d7e1b..aa95471d8f4 100644 --- a/code/osapi/osregistry.cpp +++ b/code/osapi/osregistry.cpp @@ -9,6 +9,7 @@ #include "globalincs/pstypes.h" #include "osapi/osregistry.h" +#include "cfile/mod_bootstrap.h" #include "osapi/osapi.h" #include "cmdline/cmdline.h" #include "osregistry.h" @@ -895,7 +896,11 @@ void os_init_registry_stuff(const char* company, const char* app) // Handle mod specific settings and in-game options through a mod specific file // This gets the mod file using the mod cmdline string, stripping Launcher version data // so that mod settings are mod-version agnostic but still specific to a unique mod. - if (Cmdline_mod != nullptr) { + const auto bootstrap = mod_bootstrap_get(); + SCP_string mod_id; + if (bootstrap) { + mod_id = bootstrap->mod_id; + } else if (Cmdline_mod != nullptr) { SCP_string str = Cmdline_mod; // Trim any trailing folders so we get just the name of the root mod folder str = str.substr(0, str.find_first_of(DIR_SEPARATOR_CHAR)); @@ -951,21 +956,9 @@ void os_init_registry_stuff(const char* company, const char* app) // Now we know we have just the mod root and the version. So drop the version and we're done! size_t pos = str.rfind("-"); - str = (pos != std::string::npos) ? str.substr(0, pos) : str; - - // Make sure we have a usable string - if (str.length() > 0) { - // Append "_settings.ini" and use the data/ directory - Mod_options_file_name = "data/" + str + "_settings.ini"; - - // Test if the new save file exists - FILE* file = fopen(os_get_config_path(Mod_options_file_name).c_str(), "r"); - if (file != nullptr) { - fclose(file); - } else { - Ingame_options_save_found = false; - } - } else { + mod_id = (pos != std::string::npos) ? str.substr(0, pos) : str; + + if (mod_id.empty()) { // If we can't find the mod specific string then fallback to the fs2_open ini // We don't set Ingame_options_save_found to false here because if we do that // then we are assuming we have a proper save file to use later during runtime, @@ -973,6 +966,15 @@ void os_init_registry_stuff(const char* company, const char* app) Mod_options_file_name = Osreg_config_file_name; } } + if (!mod_id.empty()) { + Mod_options_file_name = "data/" + mod_id + "_settings.ini"; + FILE* file = fopen(os_get_config_path(Mod_options_file_name).c_str(), "r"); + if (file != nullptr) { + fclose(file); + } else { + Ingame_options_save_found = false; + } + } mprintf(("Setting local mod settings ini file to '%s'\n", Mod_options_file_name.c_str())); diff --git a/code/source_groups.cmake b/code/source_groups.cmake index 4912c06a57e..663c43d9f3d 100644 --- a/code/source_groups.cmake +++ b/code/source_groups.cmake @@ -132,6 +132,8 @@ add_file_folder("CFile" cfile/cfilearchive.h cfile/cfilelist.cpp cfile/cfilesystem.cpp + cfile/mod_bootstrap.cpp + cfile/mod_bootstrap.h cfile/cfilesystem.h cfile/cfilecompression.cpp cfile/cfilecompression.h diff --git a/freespace2/freespace.cpp b/freespace2/freespace.cpp index cd2caa3ea1a..b55a60f6a8a 100644 --- a/freespace2/freespace.cpp +++ b/freespace2/freespace.cpp @@ -44,6 +44,7 @@ #include "bmpman/bmpman.h" #include "camera/photomode.h" #include "cfile/cfile.h" +#include "cfile/mod_bootstrap.h" #include "cheats_table/cheats_table.h" #include "cmdline/cmdline.h" #include "cmeasure/cmeasure.h" @@ -1918,6 +1919,13 @@ void game_init() mprintf(("Memory: %d MiB\n", SDL_GetSystemRAM())); mprintf(("Build: %s, " SIZE_T_ARG "-bit, %s-endian\n", get_exe_name(nullptr), sizeof(void*) << 3, (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big")); + if (const auto bootstrap = mod_bootstrap_get()) { + mprintf(("Bootstrap: '%s' (version %d, mod '%s', workfolder '%s')\n", cmdline_bootstrap_path(), + bootstrap->version, bootstrap->mod_id.c_str(), bootstrap->workfolder.c_str())); + for (size_t i = 0; i < bootstrap->roots.size(); ++i) { + mprintf((" roots[%zu]: '%s'\n", i, bootstrap->roots[i].c_str())); + } + } // init os stuff next os_init( Osreg_class_name, Window_title.c_str(), Osreg_app_name ); @@ -7034,6 +7042,17 @@ int game_main(int argc, char *argv[]) if ( !parse_cmdline(argc, argv) ) { return 1; } + const char* bootstrap_path = nullptr; + try { + bootstrap_path = cmdline_bootstrap_path(); + mod_bootstrap_init(bootstrap_path); + } catch (const std::exception& error) { + const auto message = SCP_string("Could not load -bootstrap") + + (bootstrap_path ? SCP_string(" '") + bootstrap_path + "'" : "") + ": " + error.what(); + fprintf(stderr, "%s\n", message.c_str()); + os::dialogs::Message(os::dialogs::MESSAGEBOX_ERROR, message.c_str()); + return 1; + } if (LoggingEnabled) { // passing an argument will set the name @@ -8324,4 +8343,4 @@ extern "C" int android_main(int argc, char* argv[]) { return main(argc, argv); } -#endif \ No newline at end of file +#endif diff --git a/test/src/cfile/cfile.cpp b/test/src/cfile/cfile.cpp index 597a4a1ef5e..c7acc43e407 100644 --- a/test/src/cfile/cfile.cpp +++ b/test/src/cfile/cfile.cpp @@ -1,12 +1,18 @@ #include #include +#include #include #include #include +#include #include #include +#include +#include +#include + #include "util/FSTestFixture.h" class CFileInitTest : public test::FSTestFixture { @@ -46,6 +52,37 @@ TEST_F(CFileInitTest, right_data_case) { ASSERT_TRUE(cf_exists("ships.tbl", CF_TYPE_TABLES)); } +TEST_F(CFileInitTest, bootstrap_resolves_relative_roots_in_order) +{ + namespace fs = std::filesystem; + const auto base = (fs::path(TEST_DATA_PATH) / "cfile").lexically_normal(); + const auto primary = base / "right_data_case"; + const auto manifest = fs::temp_directory_path() / ("fso-bootstrap-" + std::to_string(SDL_GetTicksNS()) + ".json"); + auto cleanup = util::finally([&] { + mod_bootstrap_init(nullptr); + std::error_code error; + fs::remove(manifest, error); + }); + + const auto json = json_dump_string_new(json_pack("{s:i,s:s,s:s,s:[s,s,s]}", + "version", 1, "mod_id", "test-mod", "workfolder", base.u8string().c_str(), + "roots", primary.u8string().c_str(), "wrong_data_case", "list_files_in_vps_and_dirs"), 0); + ASSERT_TRUE(std::ofstream(manifest, std::ios::binary) << json); + ASSERT_NO_THROW(mod_bootstrap_init(manifest.u8string().c_str())); + ASSERT_FALSE(cfile_init("")); + + const auto winning_table = cf_find_file_location("ships.tbl", CF_TYPE_TABLES); + ASSERT_TRUE(winning_table.found); + EXPECT_EQ(primary / "data" / "tables" / "ships.tbl", fs::path(winning_table.full_name)); + EXPECT_TRUE(cf_find_file_location("ships.tbl", CF_TYPE_TABLES, CF_LOCATION_TYPE_SECONDARY_MODS).found); + EXPECT_TRUE(cf_find_file_location("test.tbl", CF_TYPE_TABLES).offset); + + SCP_string output; + ASSERT_TRUE(cf_create_default_path_string(output, CF_TYPE_TABLES, "output.tbl", static_cast(CF_LOCATION_ALL), true)); + EXPECT_EQ(base / "data" / "tables" / "output.tbl", fs::path(output)); + EXPECT_FALSE(cf_create_default_path_string(output, CF_TYPE_TABLES, "output.tbl", CF_LOCATION_TYPE_PRIMARY_MOD, true)); +} + class CFileTest : public test::FSTestFixture { public: CFileTest() : test::FSTestFixture(INIT_CFILE) {