summaryrefslogtreecommitdiff
path: root/src/files.c
diff options
context:
space:
mode:
authorSteven Fuller <relnev@icculus.org>2008-05-09 02:11:36 -0700
committerPatryk Obara <dreamer.tan@gmail.com>2019-08-20 02:22:37 +0200
commite9788e390d5fe0e326a39762a04c628111bc0e84 (patch)
tree65175f0dfe53aa821efad1bd47cb70df2416458a /src/files.c
parentc51b91cfe79a1ffd5da3f6a6ce202982cdfcdf85 (diff)
Windows WIP.
Diffstat (limited to 'src/files.c')
-rw-r--r--src/files.c175
1 files changed, 174 insertions, 1 deletions
diff --git a/src/files.c b/src/files.c
index 551d878..94153ac 100644
--- a/src/files.c
+++ b/src/files.c
@@ -1,6 +1,6 @@
#define _BSD_SOURCE
-//#include <unistd.h>
+#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -533,6 +533,179 @@ int CloseGameDirectory(void *dir)
return -1;
}
+/*
+ Game-specific helper function.
+ */
+static int try_game_directory(char *dir, char *file)
+{
+ char tmppath[PATH_MAX];
+
+ strncpy(tmppath, dir, PATH_MAX-32);
+ tmppath[PATH_MAX-32] = 0;
+ strcat(tmppath, file);
+
+ return access(tmppath, R_OK) == 0;
+}
+
+/*
+ Game-specific helper function.
+ */
+static int check_game_directory(char *dir)
+{
+ if (!dir || !*dir) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/avp_huds")) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/avp_huds/alien.rif")) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/avp_rifs")) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/avp_rifs/temple.rif")) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/fastfile")) {
+ return 0;
+ }
+
+ if (!try_game_directory(dir, "/fastfile/ffinfo.txt")) {
+ return 0;
+ }
+
+ return 1;
+}
+
+/*
+ Game-specific initialization
+ */
+void InitGameDirectories(char *argv0)
+{
+ extern char *SecondTex_Directory;
+ extern char *SecondSoundDir;
+
+ char tmppath[PATH_MAX];
+ char *homedir, *gamedir, *localdir, *tmp;
+ char *path;
+ size_t len, copylen;
+
+ SecondTex_Directory = "graphics/";
+ SecondSoundDir = "sound/";
+
+ homedir = getenv("HOME");
+ if (homedir == NULL)
+ homedir = ".";
+ localdir = (char *)malloc(strlen(homedir)+10);
+ strcpy(localdir, homedir);
+ strcat(localdir, "/");
+ strcat(localdir, ".avp");
+
+ tmp = NULL;
+
+ /*
+ 1. $AVP_DATA overrides all
+ 2. executable path from argv[0]
+ 3. realpath of executable path from argv[0]
+ 4. $PATH
+ 5. current directory
+ */
+
+ /* 1. $AVP_DATA */
+ gamedir = getenv("AVP_DATA");
+
+ /* $AVP_DATA overrides all, so no check */
+
+ if (gamedir == NULL) {
+ /* 2. executable path from argv[0] */
+ tmp = strdup(argv0);
+
+ if (tmp == NULL) {
+ /* ... */
+ fprintf(stderr, "InitGameDirectories failure\n");
+ exit(EXIT_FAILURE);
+ }
+
+ gamedir = strrchr(tmp, '/');
+
+ if (gamedir) {
+ *gamedir = 0;
+ gamedir = tmp;
+
+ if (!check_game_directory(gamedir)) {
+ gamedir = NULL;
+ }
+ }
+ }
+
+ if (gamedir == NULL) {
+ /* 3. realpath of executable path from argv[0] */
+
+ assert(tmp != NULL);
+
+ gamedir = realpath(tmp, tmppath);
+
+ if (!check_game_directory(gamedir)) {
+ gamedir = NULL;
+ }
+ }
+
+ if (gamedir == NULL) {
+ /* 4. $PATH */
+ path = getenv("PATH");
+ if (path) {
+ while (*path) {
+ len = strcspn(path, ":");
+
+ copylen = min(len, PATH_MAX-1);
+
+ strncpy(tmppath, path, copylen);
+ tmppath[copylen] = 0;
+
+ if (check_game_directory(tmppath)) {
+ gamedir = tmppath;
+ break;
+ }
+
+ path += len;
+ path += strspn(path, ":");
+ }
+ }
+ }
+
+ if (gamedir == NULL) {
+ /* 5. current directory */
+ gamedir = ".";
+ }
+
+ assert(gamedir != NULL);
+
+ /* last chance sanity check */
+ if (!check_game_directory(gamedir)) {
+ fprintf(stderr, "Unable to find the AvP gamedata.\n");
+ fprintf(stderr, "The directory last examined was: %s\n", gamedir);
+ fprintf(stderr, "Has the game been installed and\n");
+ fprintf(stderr, "are all game files lowercase?\n");
+ exit(EXIT_FAILURE);
+ }
+
+ SetGameDirectories(localdir, gamedir);
+
+ free(localdir);
+ if (tmp) {
+ free(tmp);
+ }
+
+ /* delete some log files */
+ DeleteGameFile("dx_error.log");
+}
+
#ifdef FILES_DRIVER
int main(int argc, char *argv[])
{