| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* |
| 9 |
* Some simple physfs extensions |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <cstdlib> |
| 14 |
#if !defined(_MSC_VER) |
| 15 |
#include <sys/param.h> |
| 16 |
#endif |
| 17 |
#if defined(__APPLE__) && defined(__MACH__) |
| 18 |
#include <sys/mount.h> |
| 19 |
#include <unistd.h> // for chdir hack |
| 20 |
#include <ApplicationServices/ApplicationServices.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "args.h" |
| 24 |
#include "newdemo.h" |
| 25 |
#include "console.h" |
| 26 |
#include "strutil.h" |
| 27 |
#include "ignorecase.h" |
| 28 |
#include "physfs_list.h" |
| 29 |
|
| 30 |
#include "compiler-range_for.h" |
| 31 |
#include "compiler-poison.h" |
| 32 |
#include "d_enumerate.h" |
| 33 |
#include "partial_range.h" |
| 34 |
|
| 35 |
#ifndef DXX_ENABLE_ENVIRONMENT_VARIABLE_DXX_REBIRTH_HOME |
| 36 |
#if defined(__unix__) |
| 37 |
#define DXX_ENABLE_ENVIRONMENT_VARIABLE_DXX_REBIRTH_HOME 1 |
| 38 |
#else |
| 39 |
#define DXX_ENABLE_ENVIRONMENT_VARIABLE_DXX_REBIRTH_HOME 0 |
| 40 |
#endif |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace dcx { |
| 44 |
|
| 45 |
namespace { |
| 46 |
|
| 47 |
static void add_data_directory_to_search_path() |
| 48 |
{ |
| 49 |
std::array<char, PATH_MAX> pathname; |
| 50 |
static char relname[]{"data"}; |
| 51 |
PHYSFSX_addRelToSearchPath(relname, pathname, physfs_search_path::append); // 'Data' subdirectory |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
namespace dsx { |
| 59 |
|
| 60 |
#if DXX_ENABLE_ENVIRONMENT_VARIABLE_DXX_REBIRTH_HOME |
| 61 |
static void check_add_environment_rebirth_home(const char *const home_environ_var, const char *const fallback_home_path) |
| 62 |
{ |
| 63 |
char fullPath[PATH_MAX + 5]; |
| 64 |
const char *path = getenv(home_environ_var); |
| 65 |
if (!path) |
| 66 |
{ |
| 67 |
path = fallback_home_path; |
| 68 |
} |
| 69 |
if (path[0] == '~') // yes, this tilde can be put before non-unix paths. |
| 70 |
{ |
| 71 |
const char *home = PHYSFS_getUserDir(); |
| 72 |
path++; |
| 73 |
// prepend home to the path |
| 74 |
if (*path == *PHYSFS_getDirSeparator()) |
| 75 |
path++; |
| 76 |
snprintf(fullPath, sizeof(fullPath), "%s%s", home, path); |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
fullPath[sizeof(fullPath) - 1] = 0; |
| 81 |
strncpy(fullPath, path, sizeof(fullPath) - 1); |
| 82 |
} |
| 83 |
PHYSFS_setWriteDir(fullPath); |
| 84 |
auto writedir{PHYSFS_getWriteDir()}; |
| 85 |
if (!writedir) |
| 86 |
{ // need to make it |
| 87 |
char *p; |
| 88 |
char ancestor[PATH_MAX + 5]; // the directory which actually exists |
| 89 |
char child[PATH_MAX + 5]; // the directory relative to the above we're trying to make |
| 90 |
strcpy(ancestor, fullPath); |
| 91 |
const auto separator = *PHYSFS_getDirSeparator(); |
| 92 |
while (!PHYSFS_getWriteDir() && (p = strrchr(ancestor, separator))) |
| 93 |
{ |
| 94 |
if (p[1] == 0) |
| 95 |
{ // separator at the end (intended here, for safety) |
| 96 |
*p = 0; // kill this separator |
| 97 |
if (!(p = strrchr(ancestor, separator))) |
| 98 |
break; // give up, this is (usually) the root directory |
| 99 |
} |
| 100 |
p[1] = 0; // go to parent |
| 101 |
PHYSFS_setWriteDir(ancestor); |
| 102 |
} |
| 103 |
strcpy(child, fullPath + strlen(ancestor)); |
| 104 |
if (separator != '/') |
| 105 |
for (p = child; (p = strchr(p, separator)); p++) |
| 106 |
*p = '/'; |
| 107 |
PHYSFS_mkdir(child); |
| 108 |
PHYSFS_setWriteDir(fullPath); |
| 109 |
writedir = PHYSFS_getWriteDir(); |
| 110 |
} |
| 111 |
con_printf(CON_DEBUG, "PHYSFS: append write directory \"%s\" to search path", writedir); |
| 112 |
PHYSFS_mount(writedir, nullptr, 1); |
| 113 |
} |
| 114 |
#endif |
| 115 |
|
| 116 |
static void setup_hogdir_path() |
| 117 |
{ |
| 118 |
//tell PHYSFS where hogdir is |
| 119 |
if (!CGameArg.SysHogDir.empty()) |
| 120 |
{ |
| 121 |
const auto p{CGameArg.SysHogDir.c_str()}; |
| 122 |
con_printf(CON_DEBUG, "PHYSFS: append argument hog directory \"%s\" to search path", p); |
| 123 |
PHYSFS_mount(p, nullptr, 1); |
| 124 |
} |
| 125 |
#if DXX_USE_SHAREPATH |
| 126 |
else if (!CGameArg.SysNoHogDir) |
| 127 |
{ |
| 128 |
con_puts(CON_DEBUG, "PHYSFS: append built-in sharepath directory \"" DXX_SHAREPATH "\" to search path"); |
| 129 |
PHYSFS_mount(DXX_SHAREPATH, nullptr, 1); |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
con_puts(CON_DEBUG, "PHYSFS: skipping built-in sharepath \"" DXX_SHAREPATH "\""); |
| 134 |
} |
| 135 |
#else |
| 136 |
else |
| 137 |
{ |
| 138 |
con_puts(CON_DEBUG, "PHYSFS: no built-in sharepath"); |
| 139 |
} |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
static void setup_final_fallback_write_directory(const char *const base_dir) |
| 144 |
{ |
| 145 |
if (!PHYSFS_getWriteDir()) |
| 146 |
{ |
| 147 |
PHYSFS_setWriteDir(base_dir); |
| 148 |
auto writedir{PHYSFS_getWriteDir()}; |
| 149 |
if (!writedir) |
| 150 |
Error("can't set write dir: %s\n", PHYSFS_getLastError()); |
| 151 |
PHYSFS_mount(writedir, nullptr, 0); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
#if defined(__APPLE__) && defined(__MACH__) |
| 156 |
static void setup_osx_resource_path() |
| 157 |
{ |
| 158 |
CFBundleRef mainBundle = CFBundleGetMainBundle(); |
| 159 |
if (mainBundle) |
| 160 |
{ |
| 161 |
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); |
| 162 |
if (resourcesURL) |
| 163 |
{ |
| 164 |
char fullPath[PATH_MAX + 5]; |
| 165 |
if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, reinterpret_cast<uint8_t *>(fullPath), sizeof(fullPath))) |
| 166 |
{ |
| 167 |
con_printf(CON_DEBUG, "PHYSFS: append resources directory \"%s\" to search path", fullPath); |
| 168 |
PHYSFS_mount(fullPath, nullptr, 1); |
| 169 |
} |
| 170 |
CFRelease(resourcesURL); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
// Initialise PhysicsFS, set up basic search paths and add arguments from .ini file. |
| 177 |
// The .ini file can be in either the user directory or the same directory as the program. |
| 178 |
// The user directory is searched first. |
| 179 |
bool PHYSFSX_init(int argc, char *argv[]) |
| 180 |
{ |
| 181 |
if (!PHYSFS_init(argv[0])) |
| 182 |
Error("Failed to init PhysFS: %s", PHYSFS_getLastError()); |
| 183 |
PHYSFS_permitSymbolicLinks(1); |
| 184 |
const auto base_dir{PHYSFS_getBaseDir()}; |
| 185 |
#if (defined(__APPLE__) && defined(__MACH__)) // others? |
| 186 |
chdir(base_dir); // make sure relative hogdir paths work |
| 187 |
#endif |
| 188 |
|
| 189 |
#if DXX_ENABLE_ENVIRONMENT_VARIABLE_DXX_REBIRTH_HOME |
| 190 |
#if DXX_BUILD_DESCENT == 1 |
| 191 |
#define DESCENT_PATH_NUMBER "1" |
| 192 |
#elif DXX_BUILD_DESCENT == 2 |
| 193 |
#define DESCENT_PATH_NUMBER "2" |
| 194 |
#endif |
| 195 |
#if !(defined(__APPLE__) && defined(__MACH__)) |
| 196 |
#define DXX_FALLBACK_HOME_PATH "~/.d" DESCENT_PATH_NUMBER "x-rebirth/" |
| 197 |
#else |
| 198 |
#define DXX_FALLBACK_HOME_PATH "~/Library/Preferences/D" DESCENT_PATH_NUMBER "X Rebirth/" |
| 199 |
#endif |
| 200 |
#define DXX_HOME_ENVIRONMENT_VARIABLE "D" DESCENT_PATH_NUMBER "X_REBIRTH_HOME" |
| 201 |
check_add_environment_rebirth_home(DXX_HOME_ENVIRONMENT_VARIABLE, DXX_FALLBACK_HOME_PATH); |
| 202 |
#undef DXX_FALLBACK_HOME_PATH |
| 203 |
#undef DXX_HOME_ENVIRONMENT_VARIABLE |
| 204 |
#endif |
| 205 |
con_printf(CON_DEBUG, "PHYSFS: temporarily append base directory \"%s\" to search path", base_dir); |
| 206 |
PHYSFS_mount(base_dir, nullptr, 1); |
| 207 |
if (!InitArgs(std::span(argv, argc).template subspan<1>())) |
| 208 |
return false; |
| 209 |
PHYSFS_unmount(base_dir); |
| 210 |
|
| 211 |
setup_final_fallback_write_directory(base_dir); |
| 212 |
setup_hogdir_path(); |
| 213 |
|
| 214 |
add_data_directory_to_search_path(); |
| 215 |
|
| 216 |
// For Macintosh, add the 'Resources' directory in the .app bundle to the searchpaths |
| 217 |
#if defined(__APPLE__) && defined(__MACH__) |
| 218 |
setup_osx_resource_path(); |
| 219 |
#endif |
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
#if DXX_BUILD_DESCENT == 2 |
| 224 |
RAIIPHYSFS_ComputedPathMount make_PHYSFSX_ComputedPathMount(char *const name1, char *const name2, physfs_search_path position) |
| 225 |
{ |
| 226 |
auto pathname{std::make_unique<std::array<char, PATH_MAX>>()}; |
| 227 |
if (PHYSFSX_addRelToSearchPath(name1, *pathname.get(), position) == PHYSFS_ERR_OK || |
| 228 |
PHYSFSX_addRelToSearchPath(name2, *pathname.get(), position) == PHYSFS_ERR_OK) |
| 229 |
return RAIIPHYSFS_ComputedPathMount{std::move(pathname)}; |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
#endif |
| 233 |
|
| 234 |
// checks which archives are supported by PhysFS. Return 0 if some essential (HOG) is not supported |
| 235 |
int PHYSFSX_checkSupportedArchiveTypes() |
| 236 |
{ |
| 237 |
int hog_sup{0}; |
| 238 |
#if DXX_BUILD_DESCENT == 2 |
| 239 |
int mvl_sup{0}; |
| 240 |
#endif |
| 241 |
|
| 242 |
con_puts(CON_DEBUG, "PHYSFS: Checking supported archive types."); |
| 243 |
range_for (const auto i, null_sentinel_array(PHYSFS_supportedArchiveTypes())) |
| 244 |
{ |
| 245 |
const auto iextension = i->extension; |
| 246 |
con_printf(CON_DEBUG, "PHYSFS: Supported archive: [%s], which is [%s].", iextension, i->description); |
| 247 |
if (!d_stricmp(iextension, "HOG")) |
| 248 |
hog_sup = 1; |
| 249 |
#if DXX_BUILD_DESCENT == 2 |
| 250 |
else if (!d_stricmp(iextension, "MVL")) |
| 251 |
mvl_sup = 1; |
| 252 |
#endif |
| 253 |
} |
| 254 |
|
| 255 |
if (!hog_sup) |
| 256 |
con_puts(CON_CRITICAL, "PHYSFS: HOG not supported. The game will not work without!"); |
| 257 |
#if DXX_BUILD_DESCENT == 2 |
| 258 |
if (!mvl_sup) |
| 259 |
con_puts(CON_URGENT, "PHYSFS: MVL not supported. Won't be able to play movies!"); |
| 260 |
#endif |
| 261 |
|
| 262 |
return hog_sup; |
| 263 |
} |
| 264 |
|
| 265 |
} |