| | 1 | #include <stdlib.h> |
| | 2 | #include <string.h> |
| | 3 | #include <sys/stat.h> |
| | 4 | #include <dirent.h> |
| | 5 | #include <fnmatch.h> |
| | 6 | #include <fcntl.h> |
| | 7 | #include <unistd.h> |
| | 8 | #include <limits.h> |
| | 9 | #include <assert.h> |
| | 10 | #include "files.h" |
| | 11 | |
| | 12 | static char *user_dir = NULL; |
| | 13 | static char *gamedata_dir = NULL; |
| | 14 | #define OPEN_EXISTING 0x0010 |
| | 15 | |
| | 16 | static char *FixFilePath(const char *filename, const char *prefix) |
| | 17 | { |
| | 18 | int plen = strlen(prefix) + 1; |
| | 19 | size_t flen = strlen(filename) + plen + 1; |
| | 20 | char *f = malloc(flen); |
| | 21 | |
| | 22 | if (NULL != f) |
| | 23 | sprintf(f, "%s%c%s", prefix, DIR_SEPARATOR, filename); |
| | 24 | |
| | 25 | return f; |
| | 26 | } |
| | 27 | |
| | 28 | FILE *OpenGameFile(const char *filename, int readonly, int type) |
| | 29 | { |
| | 30 | char *rfilename = NULL; |
| | 31 | FILE *fp = NULL; |
| | 32 | |
| | 33 | switch(type) |
| | 34 | { |
| | 35 | case FILETYPE_GAMEDATA: |
| | 36 | readonly = 1; |
| | 37 | rfilename = FixFilePath(filename, gamedata_dir); |
| | 38 | break; |
| | 39 | case FILETYPE_CONFIG: |
| | 40 | rfilename = FixFilePath(filename, user_dir); |
| | 41 | break; |
| | 42 | default: |
| | 43 | return NULL; |
| | 44 | } |
| | 45 | |
| | 46 | fp = fopen(rfilename, (readonly ? "rb" : "wb")); |
| | 47 | free(rfilename); |
| | 48 | |
| | 49 | return fp; |
| | 50 | } |
| | 51 | |
| | 52 | char *FullFilePath(const char *filename) |
| | 53 | { |
| | 54 | return FixFilePath(filename, gamedata_dir); |
| | 55 | } |
| | 56 | |
| | 57 | static int GetFA(const char *filename) |
| | 58 | { |
| | 59 | struct stat buf; |
| | 60 | int attr = 0; |
| | 61 | |
| | 62 | if (!stat(filename, &buf)) |
| | 63 | { |
| | 64 | if (S_ISDIR(buf.st_mode)) |
| | 65 | attr |= FILEATTR_DIRECTORY; |
| | 66 | |
| | 67 | if (!access(filename, R_OK)) |
| | 68 | attr |= FILEATTR_READABLE; |
| | 69 | |
| | 70 | if (!access(filename, W_OK)) |
| | 71 | attr |= FILEATTR_WRITABLE; |
| | 72 | } |
| | 73 | |
| | 74 | return attr; |
| | 75 | } |
| | 76 | |
| | 77 | static time_t GetTS(const char *filename) |
| | 78 | { |
| | 79 | struct stat buf; |
| | 80 | return !stat(filename, &buf) ? buf.st_mtime : 0; |
| | 81 | } |
| | 82 | |
| | 83 | /* |
| | 84 | Create a directory: local dir only |
| | 85 | |
| | 86 | TODO: maybe also mkdir parent directories, if they do not exist? |
| | 87 | */ |
| | 88 | |
| | 89 | int CreateGameDirectory(const char *dirname) |
| | 90 | { |
| | 91 | char *rfilename = FixFilePath(dirname, user_dir); |
| | 92 | int ret = mkdir(rfilename, S_IRWXU); |
| | 93 | |
| | 94 | free(rfilename); |
| | 95 | |
| | 96 | if (ret == -1) |
| | 97 | { |
| | 98 | rfilename = FixFilePath(dirname, user_dir); |
| | 99 | ret = mkdir(rfilename, S_IRWXU); |
| | 100 | free(rfilename); |
| | 101 | } |
| | 102 | |
| | 103 | return ret; |
| | 104 | } |
| | 105 | |
| | 106 | /* This struct is private. */ |
| | 107 | typedef struct GameDirectory |
| | 108 | { |
| | 109 | DIR *localdir; /* directory opened with opendir */ |
| | 110 | DIR *globaldir; |
| | 111 | |
| | 112 | char *localdirname; |
| | 113 | char *globaldirname; |
| | 114 | |
| | 115 | char *pat; /* pattern to match */ |
| | 116 | |
| | 117 | GameDirectoryFile tmp; /* Temp space */ |
| | 118 | |
| | 119 | } GameDirectory; |
| | 120 | |
| | 121 | /* |
| | 122 | "Open" a directory dirname, with type type |
| | 123 | Returns a pointer to a directory datatype |
| | 124 | |
| | 125 | Pattern is the pattern to match |
| | 126 | */ |
| | 127 | void *OpenGameDirectory(const char *dirname, const char *pattern, int type) |
| | 128 | { |
| | 129 | char *localdirname = NULL, *globaldirname = NULL; |
| | 130 | DIR *localdir = NULL, *globaldir = NULL; |
| | 131 | |
| | 132 | if (type != FILETYPE_CONFIG) |
| | 133 | { |
| | 134 | globaldirname = FixFilePath(dirname, gamedata_dir); |
| | 135 | globaldir = opendir(globaldirname); |
| | 136 | |
| | 137 | if (globaldir == NULL) |
| | 138 | { |
| | 139 | free(globaldirname); |
| | 140 | |
| | 141 | globaldirname = FixFilePath(dirname, gamedata_dir); |
| | 142 | |
| | 143 | globaldir = opendir(globaldirname); |
| | 144 | |
| | 145 | if (globaldir == NULL) |
| | 146 | free(globaldirname); |
| | 147 | |
| | 148 | return NULL; |
| | 149 | } |
| | 150 | } |
| | 151 | |
| | 152 | if (type != FILETYPE_GAMEDATA) |
| | 153 | { |
| | 154 | localdirname = FixFilePath(dirname, user_dir); |
| | 155 | localdir = opendir(localdirname); |
| | 156 | |
| | 157 | if (localdir == NULL) |
| | 158 | { |
| | 159 | free(localdirname); |
| | 160 | |
| | 161 | localdirname = FixFilePath(dirname, user_dir); |
| | 162 | |
| | 163 | localdir = opendir(localdirname); |
| | 164 | |
| | 165 | if (localdir == NULL) |
| | 166 | free(localdirname); |
| | 167 | |
| | 168 | return NULL; |
| | 169 | } |
| | 170 | } |
| | 171 | |
| | 172 | GameDirectory *gd = malloc(sizeof(GameDirectory)); |
| | 173 | |
| | 174 | if(NULL != gd) |
| | 175 | { |
| | 176 | gd->localdir = localdir; |
| | 177 | gd->globaldir = globaldir; |
| | 178 | gd->localdirname = localdirname; |
| | 179 | gd->globaldirname = globaldirname; |
| | 180 | gd->pat = strdup(pattern); |
| | 181 | } |
| | 182 | |
| | 183 | return gd; |
| | 184 | } |
| | 185 | |
| | 186 | GameDirectoryFile *ScanGameDirectory(void *dir) |
| | 187 | { |
| | 188 | GameDirectory *directory = (GameDirectory *)dir; |
| | 189 | char *ptr; |
| | 190 | struct dirent *file; |
| | 191 | |
| | 192 | if (directory->globaldir) |
| | 193 | { |
| | 194 | while ((file = readdir(directory->globaldir)) != NULL) |
| | 195 | { |
| | 196 | if (!fnmatch(directory->pat, file->d_name, FNM_PATHNAME)) |
| | 197 | { |
| | 198 | ptr = FixFilePath(file->d_name, directory->globaldirname); |
| | 199 | directory->tmp.attr = GetFA(ptr); |
| | 200 | free(ptr); |
| | 201 | |
| | 202 | directory->tmp.filename = file->d_name; |
| | 203 | |
| | 204 | return &directory->tmp; |
| | 205 | } |
| | 206 | } |
| | 207 | |
| | 208 | closedir(directory->globaldir); |
| | 209 | free(directory->globaldirname); |
| | 210 | |
| | 211 | directory->globaldir = NULL; |
| | 212 | directory->globaldirname = NULL; |
| | 213 | } |
| | 214 | |
| | 215 | if (directory->localdir) |
| | 216 | { |
| | 217 | while ((file = readdir(directory->localdir)) != NULL) |
| | 218 | { |
| | 219 | if (!fnmatch(directory->pat, file->d_name, FNM_PATHNAME)) |
| | 220 | { |
| | 221 | ptr = FixFilePath(file->d_name, directory->localdirname); |
| | 222 | directory->tmp.attr = GetFA(ptr); |
| | 223 | directory->tmp.timestamp = GetTS(ptr); |
| | 224 | free(ptr); |
| | 225 | |
| | 226 | directory->tmp.filename = file->d_name; |
| | 227 | |
| | 228 | return &directory->tmp; |
| | 229 | } |
| | 230 | } |
| | 231 | |
| | 232 | closedir(directory->localdir); |
| | 233 | free(directory->localdirname); |
| | 234 | |
| | 235 | directory->localdir = NULL; |
| | 236 | directory->localdirname = NULL; |
| | 237 | } |
| | 238 | |
| | 239 | return NULL; |
| | 240 | } |
| | 241 | |
| | 242 | int CloseGameDirectory(void *dir) |
| | 243 | { |
| | 244 | GameDirectory *directory = (GameDirectory *)dir; |
| | 245 | |
| | 246 | if (directory) |
| | 247 | { |
| | 248 | free(directory->pat); |
| | 249 | |
| | 250 | if (directory->localdirname) |
| | 251 | free(directory->localdirname); |
| | 252 | |
| | 253 | if (directory->globaldirname) |
| | 254 | free(directory->globaldirname); |
| | 255 | |
| | 256 | if (directory->localdir) |
| | 257 | closedir(directory->localdir); |
| | 258 | |
| | 259 | if (directory->globaldir) |
| | 260 | closedir(directory->globaldir); |
| | 261 | |
| | 262 | return 0; |
| | 263 | } |
| | 264 | |
| | 265 | return -1; |
| | 266 | } |
| | 267 | |
| | 268 | static int try_game_directory(char *dir, char *file) |
| | 269 | { |
| | 270 | char tmppath[FILENAME_MAX]; |
| | 271 | size_t len = FILENAME_MAX-32; |
| | 272 | |
| | 273 | snprintf(tmppath, len, "%s%c%s", dir, DIR_SEPARATOR, file); |
| | 274 | //strncpy(tmppath, dir, len); |
| | 275 | tmppath[len] = 0; |
| | 276 | //strcat(tmppath, file); |
| | 277 | |
| | 278 | return !access(tmppath, R_OK); |
| | 279 | } |
| | 280 | |
| | 281 | static int check_game_directory(char *dir) |
| | 282 | { |
| | 283 | return |
| | 284 | ( |
| | 285 | (NULL != dir) |
| | 286 | && |
| | 287 | (NULL != *dir) |
| | 288 | && |
| | 289 | try_game_directory(dir, "avp_huds") |
| | 290 | && |
| | 291 | try_game_directory(dir, "avp_huds/dropship.rif") |
| | 292 | && |
| | 293 | try_game_directory(dir, "avp_rifs") |
| | 294 | && |
| | 295 | try_game_directory(dir, "avp_rifs/temple.rif") |
| | 296 | && |
| | 297 | try_game_directory(dir, "fastfile") |
| | 298 | && |
| | 299 | try_game_directory(dir, "graphics/common/lights.png") |
| | 300 | && |
| | 301 | try_game_directory(dir, "sound/hud/predator/pred_injection.wav") |
| | 302 | ); |
| | 303 | } |
| | 304 | |
| | 305 | void InitGameDirectories(char *argv0) |
| | 306 | { |
| | 307 | char tmppath[FILENAME_MAX]; |
| | 308 | char *tmp = NULL; |
| | 309 | size_t len, copylen; |
| | 310 | |
| | 311 | char *homedir = getenv("HOME"); |
| | 312 | |
| | 313 | if (homedir == NULL) |
| | 314 | homedir = "."; |
| | 315 | |
| | 316 | char *localdir = malloc(strlen(homedir) + 10); |
| | 317 | sprintf(localdir, "%s%c.avp", homedir, DIR_SEPARATOR); |
| | 318 | |
| | 319 | /* |
| | 320 | 1. $AVP_DATA overrides all |
| | 321 | 2. executable path from argv[0] |
| | 322 | 3. realpath of executable path from argv[0] |
| | 323 | 4. $PATH |
| | 324 | 5. current directory |
| | 325 | */ |
| | 326 | |
| | 327 | char *gamedir = getenv("AVP_DATA"); |
| | 328 | |
| | 329 | /* $AVP_DATA overrides all, so no check */ |
| | 330 | |
| | 331 | if (gamedir == NULL) |
| | 332 | { |
| | 333 | /* 2. executable path from argv[0] */ |
| | 334 | tmp = strdup(argv0); |
| | 335 | |
| | 336 | if (tmp == NULL) |
| | 337 | { |
| | 338 | fprintf(stderr, "InitGameDirectories failure\n"); |
| | 339 | exit(EXIT_FAILURE); |
| | 340 | } |
| | 341 | |
| | 342 | gamedir = strrchr(tmp, '/'); |
| | 343 | |
| | 344 | if (NULL != gamedir) |
| | 345 | { |
| | 346 | *gamedir = NULL; |
| | 347 | gamedir = tmp; |
| | 348 | |
| | 349 | if (!check_game_directory(gamedir)) |
| | 350 | gamedir = NULL; |
| | 351 | } |
| | 352 | |
| | 353 | if (gamedir == NULL) |
| | 354 | { |
| | 355 | /* 3. realpath of executable path from argv[0] */ |
| | 356 | |
| | 357 | assert(tmp != NULL); |
| | 358 | |
| | 359 | gamedir = realpath(tmp, tmppath); |
| | 360 | |
| | 361 | if (!check_game_directory(gamedir)) |
| | 362 | gamedir = NULL; |
| | 363 | } |
| | 364 | |
| | 365 | if (gamedir == NULL) |
| | 366 | { |
| | 367 | char *path = getenv("PATH"); |
| | 368 | |
| | 369 | if (path) |
| | 370 | { |
| | 371 | while (*path) |
| | 372 | { |
| | 373 | len = strcspn(path, ":"); |
| | 374 | |
| | 375 | copylen = (len < FILENAME_MAX-1) ? len : FILENAME_MAX-1; |
| | 376 | |
| | 377 | strncpy(tmppath, path, copylen); |
| | 378 | tmppath[copylen] = 0; |
| | 379 | |
| | 380 | if (check_game_directory(tmppath)) |
| | 381 | { |
| | 382 | gamedir = tmppath; |
| | 383 | break; |
| | 384 | } |
| | 385 | |
| | 386 | path += len; |
| | 387 | path += strspn(path, ":"); |
| | 388 | } |
| | 389 | } |
| | 390 | } |
| | 391 | |
| | 392 | /* last chance sanity check */ |
| | 393 | if (gamedir == NULL) |
| | 394 | gamedir = "."; |
| | 395 | } |
| | 396 | |
| | 397 | size_t len1 = strlen(gamedir); |
| | 398 | |
| | 399 | if(DIR_SEPARATOR == gamedir[len1-1]) |
| | 400 | gamedir[len1-1] = '\0'; |
| | 401 | |
| | 402 | if (check_game_directory(gamedir)) |
| | 403 | { |
| | 404 | /* |
| | 405 | Sets the local and global directories used by the other functions. |
| | 406 | Local = ~/.dir, where config and user-installed files are kept. |
| | 407 | Global = installdir, where installed data is stored. |
| | 408 | */ |
| | 409 | struct stat buf; |
| | 410 | |
| | 411 | user_dir = strdup(localdir); |
| | 412 | gamedata_dir = strdup(gamedir); |
| | 413 | |
| | 414 | if (stat(user_dir, &buf) == -1) |
| | 415 | { |
| | 416 | printf("Creating local directory %s...\n", user_dir); |
| | 417 | |
| | 418 | mkdir(user_dir, S_IRWXU); |
| | 419 | } |
| | 420 | } |
| | 421 | else |
| | 422 | { |
| | 423 | fprintf(stderr, "Unable to find the AvP gamedata.\n"); |
| | 424 | fprintf(stderr, "The directory last examined was: %s\n", gamedir); |
| | 425 | fprintf(stderr, "Has the game been installed and\n"); |
| | 426 | fprintf(stderr, "are all game files lowercase?\n"); |
| | 427 | exit(EXIT_FAILURE); |
| | 428 | } |
| | 429 | |
| | 430 | if (tmp) |
| | 431 | free(tmp); |
| | 432 | |
| | 433 | free(localdir); |
| | 434 | } |