| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* Misc system stuff needed by Doom, implemented for Linux. |
| 31 |
* Mainly timer handling. |
| 32 |
* |
| 33 |
*----------------------------------------------------------------------------- |
| 34 |
*/ |
| 35 |
|
| 36 |
#ifdef _WIN32 |
| 37 |
#define WIN32_LEAN_AND_MEAN |
| 38 |
#include <windows.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <stdio.h> |
| 42 |
#include <stdarg.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <ctype.h> |
| 45 |
#include <time.h> |
| 46 |
#include <signal.h> |
| 47 |
#include <string.h> |
| 48 |
#include <sys/stat.h> |
| 49 |
#include <errno.h> |
| 50 |
|
| 51 |
#include "SDL.h" |
| 52 |
|
| 53 |
#ifdef HAVE_CONFIG_H |
| 54 |
#include "config.h" |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef HAVE_UNISTD_H |
| 58 |
#include <unistd.h> |
| 59 |
#ifdef HAVE_GETPWUID |
| 60 |
#include <sys/types.h> |
| 61 |
#include <pwd.h> |
| 62 |
#endif |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifdef _MSC_VER |
| 66 |
#include <io.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#include "lprintf.h" |
| 70 |
#include "m_file.h" |
| 71 |
#include "doomtype.h" |
| 72 |
#include "doomdef.h" |
| 73 |
#include "d_player.h" |
| 74 |
#include "m_fixed.h" |
| 75 |
#include "r_fps.h" |
| 76 |
#include "e6y.h" |
| 77 |
#include "i_system.h" |
| 78 |
|
| 79 |
#ifdef HAVE_CONFIG_H |
| 80 |
#include "config.h" |
| 81 |
#endif |
| 82 |
|
| 83 |
#include "z_zone.h" |
| 84 |
|
| 85 |
#include "dsda/settings.h" |
| 86 |
#include "dsda/signal_context.h" |
| 87 |
#include "dsda/time.h" |
| 88 |
#include "dsda/utility.h" |
| 89 |
|
| 90 |
void I_uSleep(unsigned long usecs) |
| 91 |
{ |
| 92 |
SDL_Delay(usecs/1000); |
| 93 |
} |
| 94 |
|
| 95 |
static dboolean InDisplay = false; |
| 96 |
static int saved_gametic = -1; |
| 97 |
dboolean realframe = false; |
| 98 |
|
| 99 |
dboolean I_StartDisplay(void) |
| 100 |
{ |
| 101 |
if (InDisplay) |
| 102 |
return false; |
| 103 |
|
| 104 |
realframe = (!movement_smooth) || (gametic > saved_gametic); |
| 105 |
|
| 106 |
if (realframe) |
| 107 |
saved_gametic = gametic; |
| 108 |
|
| 109 |
InDisplay = true; |
| 110 |
DSDA_ADD_CONTEXT(sf_display); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
void I_EndDisplay(void) |
| 115 |
{ |
| 116 |
InDisplay = false; |
| 117 |
DSDA_REMOVE_CONTEXT(sf_display); |
| 118 |
} |
| 119 |
|
| 120 |
int interpolation_method; |
| 121 |
fixed_t I_GetTimeFrac (void) |
| 122 |
{ |
| 123 |
fixed_t frac; |
| 124 |
|
| 125 |
if (!movement_smooth) |
| 126 |
{ |
| 127 |
frac = FRACUNIT; |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
static fixed_t last_frac; |
| 132 |
static int last_gametic; |
| 133 |
unsigned long long tic_time; |
| 134 |
const double tics_per_usec = TICRATE / 1000000.0f; |
| 135 |
|
| 136 |
tic_time = dsda_TickElapsedTime(); |
| 137 |
|
| 138 |
frac = (fixed_t) (tic_time * FRACUNIT * tics_per_usec); |
| 139 |
frac = BETWEEN(0, FRACUNIT, frac); |
| 140 |
|
| 141 |
if (frac < last_frac && last_gametic == gametic) |
| 142 |
{ |
| 143 |
frac = FRACUNIT; |
| 144 |
} |
| 145 |
|
| 146 |
last_frac = frac; |
| 147 |
last_gametic = gametic; |
| 148 |
} |
| 149 |
|
| 150 |
return frac; |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* I_GetRandomTimeSeed |
| 155 |
* |
| 156 |
* CPhipps - extracted from G_ReloadDefaults because it is O/S based |
| 157 |
*/ |
| 158 |
unsigned long I_GetRandomTimeSeed(void) |
| 159 |
{ |
| 160 |
return (unsigned long)time(NULL); |
| 161 |
} |
| 162 |
|
| 163 |
/* cphipps - I_GetVersionString |
| 164 |
* Returns a version string in the given buffer |
| 165 |
*/ |
| 166 |
const char* I_GetVersionString(char* buf, size_t sz) |
| 167 |
{ |
| 168 |
snprintf(buf, sz, "%s v%s (https://github.com/kraflab/dsda-doom/)", PROJECT_NAME, PROJECT_VERSION); |
| 169 |
return buf; |
| 170 |
} |
| 171 |
|
| 172 |
/* cphipps - I_SigString |
| 173 |
* Returns a string describing a signal number |
| 174 |
*/ |
| 175 |
const char* I_SigString(char* buf, size_t sz, int signum) |
| 176 |
{ |
| 177 |
#ifdef HAVE_STRSIGNAL |
| 178 |
if (strsignal(signum) && strlen(strsignal(signum)) < sz) |
| 179 |
strcpy(buf,strsignal(signum)); |
| 180 |
else |
| 181 |
#endif |
| 182 |
snprintf(buf,sz,"signal %d",signum); |
| 183 |
return buf; |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
* I_Read |
| 188 |
* |
| 189 |
* cph 2001/11/18 - wrapper for read(2) which handles partial reads and aborts |
| 190 |
* on error. |
| 191 |
*/ |
| 192 |
void I_Read(int fd, void* vbuf, size_t sz) |
| 193 |
{ |
| 194 |
unsigned char* buf = (unsigned char*)vbuf; |
| 195 |
|
| 196 |
while (sz) { |
| 197 |
int rc = read(fd,buf,sz); |
| 198 |
if (rc <= 0) { |
| 199 |
I_Error("I_Read: read failed: %s", rc ? strerror(errno) : "EOF"); |
| 200 |
} |
| 201 |
sz -= rc; buf += rc; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* I_Filelength |
| 207 |
* |
| 208 |
* Return length of an open file. |
| 209 |
*/ |
| 210 |
|
| 211 |
int I_Filelength(int handle) |
| 212 |
{ |
| 213 |
struct stat fileinfo; |
| 214 |
if (fstat(handle,&fileinfo) == -1) |
| 215 |
I_Error("I_Filelength: %s",strerror(errno)); |
| 216 |
return fileinfo.st_size; |
| 217 |
} |
| 218 |
|
| 219 |
// Return the path where the executable lies -- Lee Killough |
| 220 |
// proff_fs 2002-07-04 - moved to i_system |
| 221 |
#ifdef _WIN32 |
| 222 |
|
| 223 |
void I_SwitchToWindow(HWND hwnd) |
| 224 |
{ |
| 225 |
typedef BOOL (WINAPI *TSwitchToThisWindow) (HWND wnd, BOOL restore); |
| 226 |
static TSwitchToThisWindow SwitchToThisWindow = NULL; |
| 227 |
|
| 228 |
if (!SwitchToThisWindow) |
| 229 |
SwitchToThisWindow = (TSwitchToThisWindow)GetProcAddress(GetModuleHandle("user32.dll"), "SwitchToThisWindow"); |
| 230 |
|
| 231 |
if (SwitchToThisWindow) |
| 232 |
{ |
| 233 |
HWND hwndLastActive = GetLastActivePopup(hwnd); |
| 234 |
|
| 235 |
if (IsWindowVisible(hwndLastActive)) |
| 236 |
hwnd = hwndLastActive; |
| 237 |
|
| 238 |
SetForegroundWindow(hwnd); |
| 239 |
Sleep(100); |
| 240 |
SwitchToThisWindow(hwnd, TRUE); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
const char *I_ConfigDir(void) |
| 245 |
{ |
| 246 |
return I_ExeDir(); |
| 247 |
} |
| 248 |
|
| 249 |
const char *I_ExeDir(void) |
| 250 |
{ |
| 251 |
extern char **dsda_argv; |
| 252 |
|
| 253 |
static char *base; |
| 254 |
if (!base) // cache multiple requests |
| 255 |
{ |
| 256 |
size_t len = strlen(*dsda_argv); |
| 257 |
char *p = (base = (char*)Z_Malloc(len+1)) + len - 1; |
| 258 |
strcpy(base,*dsda_argv); |
| 259 |
while (p > base && *p!='/' && *p!='\\') |
| 260 |
*p--=0; |
| 261 |
if (*p=='/' || *p=='\\') |
| 262 |
*p--=0; |
| 263 |
if (strlen(base) < 2 || !M_WriteAccess(base)) |
| 264 |
{ |
| 265 |
Z_Free(base); |
| 266 |
base = (char*)Z_Malloc(1024); |
| 267 |
if (!M_getcwd(base, 1024) || !M_WriteAccess(base)) |
| 268 |
strcpy(base, "."); |
| 269 |
} |
| 270 |
} |
| 271 |
return base; |
| 272 |
} |
| 273 |
|
| 274 |
const char* I_GetTempDir(void) |
| 275 |
{ |
| 276 |
static const char* tmp_path; |
| 277 |
|
| 278 |
if (!tmp_path) |
| 279 |
{ |
| 280 |
wchar_t wpath[PATH_MAX]; |
| 281 |
DWORD result; |
| 282 |
|
| 283 |
result = GetTempPathW(PATH_MAX, wpath); |
| 284 |
|
| 285 |
if (result == 0 || result > MAX_PATH) |
| 286 |
I_Error("I_GetTempDir: GetTempPathW failed"); |
| 287 |
else |
| 288 |
tmp_path = ConvertWideToUtf8(wpath); |
| 289 |
} |
| 290 |
|
| 291 |
return tmp_path; |
| 292 |
} |
| 293 |
|
| 294 |
#elif defined(AMIGA) |
| 295 |
|
| 296 |
const char *I_ConfigDir(void) |
| 297 |
{ |
| 298 |
return "PROGDIR:"; |
| 299 |
} |
| 300 |
|
| 301 |
const char *I_ExeDir(void) |
| 302 |
{ |
| 303 |
return "PROGDIR:"; |
| 304 |
} |
| 305 |
|
| 306 |
const char* I_GetTempDir(void) |
| 307 |
{ |
| 308 |
return "PROGDIR:"; |
| 309 |
} |
| 310 |
|
| 311 |
#else /* not Windows, not Amiga */ |
| 312 |
|
| 313 |
static const char *I_GetHomeDir(void) |
| 314 |
{ |
| 315 |
const char *home = M_getenv("HOME"); |
| 316 |
|
| 317 |
if (!home) |
| 318 |
{ |
| 319 |
#ifdef HAVE_GETPWUID |
| 320 |
struct passwd *user_info = getpwuid(getuid()); |
| 321 |
if (user_info != NULL) |
| 322 |
home = user_info->pw_dir; |
| 323 |
else |
| 324 |
#endif |
| 325 |
home = "/"; |
| 326 |
} |
| 327 |
|
| 328 |
return home; |
| 329 |
} |
| 330 |
|
| 331 |
// Reference for XDG directories: |
| 332 |
// <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html> |
| 333 |
static const char *I_GetXDGDataHome(void) |
| 334 |
{ |
| 335 |
static char *datahome = 0; |
| 336 |
|
| 337 |
if (!datahome) |
| 338 |
{ |
| 339 |
const char *xdgdatahome = M_getenv("XDG_DATA_HOME"); |
| 340 |
|
| 341 |
if (!xdgdatahome || !*xdgdatahome) |
| 342 |
{ |
| 343 |
size_t datahome_size; |
| 344 |
const char *home = I_GetHomeDir(); |
| 345 |
|
| 346 |
datahome_size = strlen(home) + 1 + sizeof(".local/share"); |
| 347 |
datahome = Z_Malloc(datahome_size); |
| 348 |
snprintf(datahome, datahome_size, "%s%s%s", home, !HasTrailingSlash(home) ? "/" : "", ".local/share"); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
datahome = Z_Strdup(xdgdatahome); |
| 353 |
} |
| 354 |
} |
| 355 |
return datahome; |
| 356 |
} |
| 357 |
|
| 358 |
static const char *I_GetXDGDataDirs(void) |
| 359 |
{ |
| 360 |
const char *datadirs = M_getenv("XDG_DATA_DIRS"); |
| 361 |
|
| 362 |
if (!datadirs || !*datadirs) |
| 363 |
return "/usr/local/share/:/usr/share/"; |
| 364 |
return datadirs; |
| 365 |
} |
| 366 |
|
| 367 |
const char *I_ConfigDir(void) |
| 368 |
{ |
| 369 |
static char *base; |
| 370 |
|
| 371 |
if (!base) |
| 372 |
{ |
| 373 |
const char *home = I_GetHomeDir(); |
| 374 |
|
| 375 |
// First, try legacy directory. |
| 376 |
base = dsda_ConcatDir(home, ".dsda-doom"); |
| 377 |
if (access(base, F_OK) != 0) |
| 378 |
{ |
| 379 |
// Legacy directory is not accessible. Use XDG directory. |
| 380 |
Z_Free(base); |
| 381 |
|
| 382 |
#ifdef __APPLE__ |
| 383 |
base = dsda_ConcatDir(home, "Library/Application Support/dsda-doom"); |
| 384 |
#else |
| 385 |
base = dsda_ConcatDir(I_GetXDGDataHome(), "dsda-doom"); |
| 386 |
#endif |
| 387 |
} |
| 388 |
|
| 389 |
M_MakeDir(base, false); |
| 390 |
} |
| 391 |
|
| 392 |
return base; |
| 393 |
} |
| 394 |
|
| 395 |
const char *I_ExeDir(void) |
| 396 |
{ |
| 397 |
extern char **dsda_argv; |
| 398 |
|
| 399 |
static char *base; |
| 400 |
if (!base) // cache multiple requests |
| 401 |
{ |
| 402 |
size_t len = strlen(*dsda_argv); |
| 403 |
char *p = (base = (char*)Z_Malloc(len+1)) + len - 1; |
| 404 |
strcpy(base,*dsda_argv); |
| 405 |
while (p > base && *p!='/' && *p!='\\') |
| 406 |
*p--=0; |
| 407 |
if (*p=='/' || *p=='\\') |
| 408 |
*p--=0; |
| 409 |
if (strlen(base) < 2 || !M_WriteAccess(base)) |
| 410 |
{ |
| 411 |
Z_Free(base); |
| 412 |
base = (char*)Z_Malloc(1024); |
| 413 |
if (!M_getcwd(base, 1024) || !M_WriteAccess(base)) |
| 414 |
strcpy(base, "."); |
| 415 |
} |
| 416 |
} |
| 417 |
return base; |
| 418 |
} |
| 419 |
|
| 420 |
const char *I_GetTempDir(void) |
| 421 |
{ |
| 422 |
return "/tmp"; |
| 423 |
} |
| 424 |
|
| 425 |
#endif |
| 426 |
|
| 427 |
/* |
| 428 |
* HasTrailingSlash |
| 429 |
* |
| 430 |
* cphipps - simple test for trailing slash on dir names |
| 431 |
*/ |
| 432 |
|
| 433 |
dboolean HasTrailingSlash(const char* dn) |
| 434 |
{ |
| 435 |
return ( (dn[strlen(dn)-1] == '/') |
| 436 |
#if defined(_WIN32) |
| 437 |
|| (dn[strlen(dn)-1] == '\\') |
| 438 |
#endif |
| 439 |
#if defined(AMIGA) |
| 440 |
|| (dn[strlen(dn)-1] == ':') |
| 441 |
#endif |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
static const char *I_GetBasePath(void) |
| 446 |
{ |
| 447 |
static char *executable_dir; |
| 448 |
/* SDL_GetBasePath is an expensive call */ |
| 449 |
if (!executable_dir) |
| 450 |
executable_dir = SDL_GetBasePath(); |
| 451 |
return executable_dir; |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
* I_FindFile |
| 456 |
* |
| 457 |
* proff_fs 2002-07-04 - moved to i_system |
| 458 |
* |
| 459 |
* cphipps 19/1999 - writen to unify the logic in FindIWADFile and the WAD |
| 460 |
* autoloading code. |
| 461 |
* Searches the standard dirs for a named WAD file |
| 462 |
* The dirs are listed at the start of the function |
| 463 |
*/ |
| 464 |
|
| 465 |
#ifdef _WIN32 |
| 466 |
#define PATH_SEPARATOR ";" |
| 467 |
#else |
| 468 |
#define PATH_SEPARATOR ":" |
| 469 |
#endif |
| 470 |
|
| 471 |
char* I_FindFileInternal(const char* wfname, const char* ext, dboolean isStatic) |
| 472 |
{ |
| 473 |
// lookup table of directories to search |
| 474 |
static struct { |
| 475 |
const char *dir; // directory |
| 476 |
const char *sub; // subdirectory |
| 477 |
const char *env; // environment variable |
| 478 |
const char *(*func)(void); // for functions that return the directory |
| 479 |
} search0[] = { |
| 480 |
{NULL, NULL, NULL, I_ExeDir}, // executable directory |
| 481 |
#if !defined(_WIN32) && !defined(AMIGA) |
| 482 |
{NULL, NULL, NULL, I_ConfigDir}, // config and autoload directory. on windows/amiga, this is the same as I_ExeDir |
| 483 |
#endif |
| 484 |
{NULL}, // current working directory |
| 485 |
{NULL, NULL, "DOOMWADDIR"}, // run-time $DOOMWADDIR |
| 486 |
{DOOMWADDIR}, // build-time configured DOOMWADDIR |
| 487 |
{DSDA_ABSOLUTE_PWAD_PATH}, // build-time configured absolute path to dsda-doom.wad |
| 488 |
{NULL, NULL, NULL, I_GetBasePath}, // search the base path provided by SDL |
| 489 |
{NULL, "../share/games/doom", NULL, I_GetBasePath}, // AppImage |
| 490 |
{NULL, "doom", "HOME"}, // ~/doom |
| 491 |
{NULL, NULL, "HOME"}, // ~ |
| 492 |
#if !defined(_WIN32) && !defined(AMIGA) |
| 493 |
{NULL, "games/doom", NULL, I_GetXDGDataHome}, // $HOME/.local/share/games/doom |
| 494 |
#endif |
| 495 |
}, *search; |
| 496 |
|
| 497 |
static size_t num_search; |
| 498 |
size_t i; |
| 499 |
size_t pl; |
| 500 |
|
| 501 |
static char static_p[PATH_MAX]; |
| 502 |
char * dinamic_p = NULL; |
| 503 |
char *p = (isStatic ? static_p : dinamic_p); |
| 504 |
|
| 505 |
if (!wfname) |
| 506 |
return NULL; |
| 507 |
|
| 508 |
if (!num_search) |
| 509 |
{ |
| 510 |
int extra = 0; |
| 511 |
#if !defined(_WIN32) && !defined(AMIGA) |
| 512 |
int datadirs = 0; |
| 513 |
#endif |
| 514 |
const char *dwp; |
| 515 |
|
| 516 |
// calculate how many extra entries we need to add to the table |
| 517 |
#if !defined(_WIN32) && !defined(AMIGA) |
| 518 |
dwp = I_GetXDGDataDirs(); |
| 519 |
datadirs++; |
| 520 |
while ((dwp = strchr(dwp, *PATH_SEPARATOR))) |
| 521 |
dwp++, datadirs++; |
| 522 |
extra += datadirs * 2; // two entries for each datadir |
| 523 |
#endif |
| 524 |
if ((dwp = M_getenv("DOOMWADPATH"))) |
| 525 |
{ |
| 526 |
extra++; |
| 527 |
while ((dwp = strchr(dwp, *PATH_SEPARATOR))) |
| 528 |
dwp++, extra++; |
| 529 |
} |
| 530 |
|
| 531 |
// initialize with the static lookup table |
| 532 |
num_search = sizeof(search0)/sizeof(*search0); |
| 533 |
search = Z_Malloc((num_search + extra) * sizeof(*search)); |
| 534 |
memcpy(search, search0, num_search * sizeof(*search)); |
| 535 |
memset(&search[num_search], 0, extra * sizeof(*search)); |
| 536 |
|
| 537 |
#if !defined(_WIN32) && !defined(AMIGA) |
| 538 |
// add $XDG_DATA_DIRS/games/doom and $XDG_DATA_DIRS/doom |
| 539 |
// by default this includes: |
| 540 |
// - /usr/local/share/games/doom |
| 541 |
// - /usr/share/games/doom |
| 542 |
// - /usr/local/share/doom |
| 543 |
// - /usr/share/doom |
| 544 |
{ |
| 545 |
char *ptr, *dup_dwp; |
| 546 |
|
| 547 |
dup_dwp = Z_Strdup(I_GetXDGDataDirs()); |
| 548 |
ptr = strtok(dup_dwp, PATH_SEPARATOR); |
| 549 |
while (ptr) |
| 550 |
{ |
| 551 |
search[num_search].dir = Z_Strdup(ptr); |
| 552 |
search[num_search].sub = "games/doom"; |
| 553 |
search[num_search + datadirs].dir = Z_Strdup(ptr); |
| 554 |
search[num_search + datadirs].sub = "doom"; |
| 555 |
num_search++; |
| 556 |
ptr = strtok(NULL, PATH_SEPARATOR); |
| 557 |
} |
| 558 |
Z_Free(dup_dwp); |
| 559 |
num_search += datadirs; |
| 560 |
} |
| 561 |
#endif |
| 562 |
|
| 563 |
// add each directory from the $DOOMWADPATH environment variable |
| 564 |
if ((dwp = M_getenv("DOOMWADPATH"))) |
| 565 |
{ |
| 566 |
char *ptr, *dup_dwp; |
| 567 |
|
| 568 |
dup_dwp = Z_Strdup(dwp); |
| 569 |
ptr = strtok(dup_dwp, PATH_SEPARATOR); |
| 570 |
while (ptr) |
| 571 |
{ |
| 572 |
search[num_search].dir = Z_Strdup(ptr); |
| 573 |
num_search++; |
| 574 |
ptr = strtok(NULL, PATH_SEPARATOR); |
| 575 |
} |
| 576 |
Z_Free(dup_dwp); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/* Precalculate a length we will need in the loop */ |
| 581 |
pl = strlen(wfname) + (ext ? strlen(ext) : 0) + 4; |
| 582 |
|
| 583 |
for (i = 0; i < num_search; i++) { |
| 584 |
const char * d = NULL; |
| 585 |
const char * s = NULL; |
| 586 |
size_t p_size = PATH_MAX; |
| 587 |
/* Each entry in the switch sets d to the directory to look in, |
| 588 |
* and optionally s to a subdirectory of d */ |
| 589 |
// switch replaced with lookup table |
| 590 |
if (search[i].env) { |
| 591 |
if (!(d = M_getenv(search[i].env))) |
| 592 |
continue; |
| 593 |
} else if (search[i].func) |
| 594 |
d = search[i].func(); |
| 595 |
else |
| 596 |
d = search[i].dir; |
| 597 |
s = search[i].sub; |
| 598 |
|
| 599 |
if (!isStatic) |
| 600 |
{ |
| 601 |
p_size = (d ? strlen(d) : 0) + (s ? strlen(s) : 0) + pl; |
| 602 |
p = (char*)Z_Malloc(p_size); |
| 603 |
} |
| 604 |
|
| 605 |
snprintf(p, p_size, "%s%s%s%s%s", d ? d : "", (d && !HasTrailingSlash(d)) ? "/" : "", |
| 606 |
s ? s : "", (s && !HasTrailingSlash(s)) ? "/" : "", |
| 607 |
wfname); |
| 608 |
|
| 609 |
if (ext && !M_FileExists(p)) |
| 610 |
strcat(p, ext); |
| 611 |
if (M_FileExists(p)) { |
| 612 |
if (!isStatic) |
| 613 |
lprintf(LO_DEBUG, " found %s\n", p); |
| 614 |
return p; |
| 615 |
} |
| 616 |
if (!isStatic) |
| 617 |
Z_Free(p); |
| 618 |
} |
| 619 |
return NULL; |
| 620 |
} |
| 621 |
|
| 622 |
char* I_RequireFile(const char* wfname, const char* ext) { |
| 623 |
char* result = I_FindFileInternal(wfname, ext, false); |
| 624 |
|
| 625 |
if (!result) |
| 626 |
I_Error("Unable to find required file \"%s\"", wfname); |
| 627 |
|
| 628 |
return result; |
| 629 |
} |
| 630 |
|
| 631 |
char* I_FindFile(const char* wfname, const char* ext) |
| 632 |
{ |
| 633 |
return I_FindFileInternal(wfname, ext, false); |
| 634 |
} |
| 635 |
|
| 636 |
const char* I_FindFile2(const char* wfname, const char* ext) |
| 637 |
{ |
| 638 |
return (const char*) I_FindFileInternal(wfname, ext, true); |
| 639 |
} |
| 640 |
|
| 641 |
char* I_RequireAnyFile(const char* wfname, const char** ext) |
| 642 |
{ |
| 643 |
char* result = NULL; |
| 644 |
|
| 645 |
for (; *ext; ext++) |
| 646 |
{ |
| 647 |
result = I_FindFile(wfname, *ext); |
| 648 |
if (result) |
| 649 |
return result; |
| 650 |
} |
| 651 |
|
| 652 |
I_Error("Unable to find required file \"%s\"", wfname); |
| 653 |
} |
| 654 |
|
| 655 |
char* I_RequireWad(const char* wfname) |
| 656 |
{ |
| 657 |
return I_RequireFile(wfname, ".wad"); |
| 658 |
} |
| 659 |
|
| 660 |
char* I_FindWad(const char* wfname) |
| 661 |
{ |
| 662 |
return I_FindFile(wfname, ".wad"); |
| 663 |
} |
| 664 |
|
| 665 |
char* I_RequireDeh(const char* wfname) |
| 666 |
{ |
| 667 |
char* result; |
| 668 |
|
| 669 |
result = I_FindFile(wfname, ".bex"); |
| 670 |
if (result) |
| 671 |
return result; |
| 672 |
|
| 673 |
return I_RequireFile(wfname, ".deh"); |
| 674 |
} |
| 675 |
|
| 676 |
char* I_FindDeh(const char* wfname) |
| 677 |
{ |
| 678 |
char* result; |
| 679 |
|
| 680 |
result = I_FindFile(wfname, ".bex"); |
| 681 |
if (result) |
| 682 |
return result; |
| 683 |
|
| 684 |
return I_FindFile(wfname, ".deh"); |
| 685 |
} |
| 686 |
|
| 687 |
char* I_RequireZip(const char* wfname) |
| 688 |
{ |
| 689 |
return I_RequireFile(wfname, ".zip"); |
| 690 |
} |
| 691 |
|
| 692 |
char* I_FindZip(const char* wfname) |
| 693 |
{ |
| 694 |
return I_FindFile(wfname, ".zip"); |
| 695 |
} |