From 42655813dca31f55b576dd61c2082984b6af3ee1 Mon Sep 17 00:00:00 2001 From: theoddone33 Date: Thu, 20 Feb 2003 17:41:07 +0000 Subject: [PATCH] Userdir patch from Taylor Richards --- include/cfile.h | 6 ++++++ include/osapi.h | 5 +++++ include/osregistry.h | 4 +++- src/cfile/cfile.cpp | 12 +++++++++++- src/cfile/cfilesystem.cpp | 20 ++++++++++++++++++++ src/freespace2/freespace.cpp | 8 ++++++++ src/freespace2/unixmain.cpp | 15 +++++++-------- src/osapi/os_unix.cpp | 22 ++++++++++++++++++++++ src/osapi/osregistry-unix.cpp | 28 ++++++++++++++++++---------- 9 files changed, 100 insertions(+), 20 deletions(-) diff --git a/include/cfile.h b/include/cfile.h index cfc7182..4bdda99 100644 --- a/include/cfile.h +++ b/include/cfile.h @@ -15,6 +15,9 @@ * * * $Log$ + * Revision 1.3 2003/02/20 17:41:07 theoddone33 + * Userdir patch from Taylor Richards + * * Revision 1.2 2002/06/09 04:41:12 relnev * added copyright header * @@ -418,6 +421,9 @@ extern int (*Get_file_list_filter)(char *filename); // cfile directory. valid after cfile_init() returns successfully #define CFILE_ROOT_DIRECTORY_LEN 256 extern char Cfile_root_dir[CFILE_ROOT_DIRECTORY_LEN]; +#ifdef PLAT_UNIX +extern char Cfile_user_dir[CFILE_ROOT_DIRECTORY_LEN]; +#endif //================= LOW-LEVEL FUNCTIONS ================== // Call this once at the beginning of the program diff --git a/include/osapi.h b/include/osapi.h index 2136dbb..5c5f641 100644 --- a/include/osapi.h +++ b/include/osapi.h @@ -37,6 +37,11 @@ extern int Os_debugger_running; // initialization/shutdown functions ----------------------------------------------- +#ifdef PLAT_UNIX +extern const char *detect_home(void); +void default_registry(); +#endif + // If app_name is NULL or ommited, then TITLE is used // for the app name, which is where registry keys are stored. void os_init(char * wclass, char * title, char *app_name=NULL, char *version_string=NULL ); diff --git a/include/osregistry.h b/include/osregistry.h index edff0a2..1c67731 100644 --- a/include/osregistry.h +++ b/include/osregistry.h @@ -20,7 +20,9 @@ extern char *Osreg_company_name; extern char *Osreg_class_name; extern char *Osreg_app_name; extern char *Osreg_title; - +#ifdef PLAT_UNIX +extern char *Osreg_user_dir; +#endif // ------------------------------------------------------------------------------------------------------------ // REGISTRY FUNCTIONS diff --git a/src/cfile/cfile.cpp b/src/cfile/cfile.cpp index d63022e..feca69f 100644 --- a/src/cfile/cfile.cpp +++ b/src/cfile/cfile.cpp @@ -15,6 +15,9 @@ * Utilities for operating on files * * $Log$ + * Revision 1.8 2003/02/20 17:41:07 theoddone33 + * Userdir patch from Taylor Richards + * * Revision 1.7 2002/12/30 03:23:29 relnev * disable root dir check * @@ -208,7 +211,11 @@ #include "cfilesystem.h" #include "cfilearchive.h" #include "osapi.h" +#include "osregistry.h" // for Osreg_user_dir +#ifdef PLAT_UNIX +char Cfile_user_dir[CFILE_ROOT_DIRECTORY_LEN] = ""; +#endif char Cfile_root_dir[CFILE_ROOT_DIRECTORY_LEN] = ""; // During cfile_init, verify that Pathtypes[n].index == n for each item @@ -415,6 +422,9 @@ int cfile_init(char *exe_dir, char *cdrom_dir) // set root directory strncpy(Cfile_root_dir, buf, CFILE_ROOT_DIRECTORY_LEN-1); +#ifdef PLAT_UNIX + snprintf(Cfile_user_dir, MAX_PATH, "%s/%s/", detect_home(), Osreg_user_dir); +#endif for ( i = 0; i < MAX_CFILE_BLOCKS; i++ ) { Cfile_block_list[i].type = CFILE_BLOCK_UNUSED; } @@ -751,7 +761,7 @@ void cf_create_directory( int dir_type ) cf_create_default_path_string( longname, dir_tree[i], NULL ); #ifdef PLAT_UNIX - if ( _mkdir(longname, 0777)==0 ) { + if ( _mkdir(longname, 0700)==0 ) { mprintf(( "CFILE: Created new directory '%s'\n", longname )); } #else diff --git a/src/cfile/cfilesystem.cpp b/src/cfile/cfilesystem.cpp index 08d03b9..3e1be7b 100644 --- a/src/cfile/cfilesystem.cpp +++ b/src/cfile/cfilesystem.cpp @@ -19,6 +19,9 @@ * all those locations, inherently enforcing precedence orders. * * $Log$ + * Revision 1.8 2003/02/20 17:41:07 theoddone33 + * Userdir patch from Taylor Richards + * * Revision 1.7 2002/06/22 23:57:39 relnev * remove writable strings. * @@ -465,6 +468,23 @@ void cf_build_root_list(char *cdrom_dir) cf_root *root; +#ifdef PLAT_UNIX + // ================================================================ + // use users HOME directory as default for loading and saving files + root = cf_create_root(); + strcpy( root->path, Cfile_user_dir ); + + // do we already have a slash? as in the case of a root directory install + if(strlen(root->path) && (root->path[strlen(root->path)-1] != '/')){ + strcat(root->path, "/"); // put trailing backslash on for easier path construction + } + root->roottype = CF_ROOTTYPE_PATH; + + //====================================================== + // Next, check any VP files under the current directory. + cf_build_pack_list(root); +#endif + //====================================================== // First, check the current directory. // strcpy( root->path, "d:\\projects\\freespace\\" ); diff --git a/src/freespace2/freespace.cpp b/src/freespace2/freespace.cpp index 518909b..4f583fd 100644 --- a/src/freespace2/freespace.cpp +++ b/src/freespace2/freespace.cpp @@ -15,6 +15,9 @@ * Freespace main body * * $Log$ + * Revision 1.26 2003/02/20 17:41:07 theoddone33 + * Userdir patch from Taylor Richards + * * Revision 1.25 2003/01/30 19:54:10 relnev * ini config option for the frames per second counter (Taylor Richards) * @@ -2175,6 +2178,11 @@ void game_init() // verify that he has a valid weapons.tbl verify_weapons_tbl(); +#ifdef PLAT_UNIX + // setup the default osreg values if they don't exist + default_registry(); +#endif + // Output version numbers to registry for auto patching purposes os_config_write_uint(NOX("Version"), NOX("Major"), FS_VERSION_MAJOR); os_config_write_uint(NOX("Version"), NOX("Minor"), FS_VERSION_MINOR); diff --git a/src/freespace2/unixmain.cpp b/src/freespace2/unixmain.cpp index d5b6dd2..8044f34 100644 --- a/src/freespace2/unixmain.cpp +++ b/src/freespace2/unixmain.cpp @@ -1,20 +1,19 @@ +#include + #include "pstypes.h" #include "osregistry.h" +#include "osapi.h" int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow); void vm_dump(); int main(int argc, char **argv) { - /* set some sane defaults since we don't have a laucher... */ - if (os_config_read_string(NULL, NOX("Videocard"), NULL) == NULL) - os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)")); - - if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL) - os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan")); + char userdir[MAX_PATH]; - if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL) - os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow")); + // create user game directory + snprintf(userdir, sizeof(userdir), "%s/%s", detect_home(), Osreg_user_dir); + _mkdir(userdir, 0700); char *argptr = NULL; int i; diff --git a/src/osapi/os_unix.cpp b/src/osapi/os_unix.cpp index dea9075..6acea68 100644 --- a/src/osapi/os_unix.cpp +++ b/src/osapi/os_unix.cpp @@ -15,6 +15,9 @@ * Low level Windows code * * $Log$ + * Revision 1.12 2003/02/20 17:41:07 theoddone33 + * Userdir patch from Taylor Richards + * * Revision 1.11 2002/07/28 21:45:30 theoddone33 * Add ctrl-z to iconify window * @@ -170,6 +173,25 @@ void os_deinit(); // initialization/shutdown functions ----------------------------------------------- +// detect users home directory +const char *detect_home(void) +{ + return (getenv("HOME")); +} + +void default_registry() +{ + /* set some sane defaults since we don't have a laucher... */ + if (os_config_read_string(NULL, NOX("Videocard"), NULL) == NULL) + os_config_write_string(NULL, NOX("Videocard"), NOX("OpenGL (640x480)")); + + if (os_config_read_string(NULL, NOX("NetworkConnection"), NULL) == NULL) + os_config_write_string(NULL, NOX("NetworkConnection"), NOX("lan")); + + if (os_config_read_string(NULL, NOX("ConnectionSpeed"), NULL) == NULL) + os_config_write_string(NULL, NOX("ConnectionSpeed"), NOX("Slow")); +} + // If app_name is NULL or ommited, then TITLE is used // for the app name, which is where registry keys are stored. void os_init(char * wclass, char * title, char *app_name, char *version_string ) diff --git a/src/osapi/osregistry-unix.cpp b/src/osapi/osregistry-unix.cpp index 87a0df9..19ef9cd 100644 --- a/src/osapi/osregistry-unix.cpp +++ b/src/osapi/osregistry-unix.cpp @@ -1,5 +1,6 @@ #include "pstypes.h" #include "osregistry.h" +#include "cfile.h" #undef malloc #undef free #undef strdup @@ -14,6 +15,7 @@ char *Osreg_class_name = "Freespace2Class"; #if defined(FS2_DEMO) char *Osreg_app_name = "FreeSpace2Demo"; char *Osreg_title = "Freespace 2 Demo"; +char *Osreg_user_dir = ".freespace_demo"; #define PROFILE_NAME "FreeSpace2Demo.ini" #elif defined(OEM_BUILD) char *Osreg_app_name = "FreeSpace2OEM"; @@ -22,6 +24,7 @@ char *Osreg_title = "Freespace 2 OEM"; #else char *Osreg_app_name = "FreeSpace2"; char *Osreg_title = "Freespace 2"; +char *Osreg_user_dir = ".freespace2"; #define PROFILE_NAME "FreeSpace2.ini" #endif @@ -48,7 +51,7 @@ typedef struct Profile struct Section *sections; } Profile; -static char *read_line_from_file(FILE *fp) +static char *read_line_from_file(CFILE *fp) { char *buf, *buf_start; int buflen, len, eol; @@ -63,7 +66,7 @@ static char *read_line_from_file(FILE *fp) return NULL; } - if (fgets(buf_start, 80, fp) == NULL) { + if (cfgets(buf_start, 80, fp) == NULL) { if (buf_start == buf) { free(buf); return NULL; @@ -132,7 +135,7 @@ static char *trim_string(char *str) static Profile *profile_read(char *file) { - FILE *fp = fopen(file, "r"); + CFILE *fp = cfopen(file, "rt", CFILE_NORMAL, CF_TYPE_ROOT); if (fp == NULL) return NULL; @@ -202,7 +205,7 @@ static Profile *profile_read(char *file) free(str); } - fclose(fp); + cfclose(fp); return profile; } @@ -336,31 +339,36 @@ static char *profile_get_value(Profile *profile, char *section, char *key) static void profile_save(Profile *profile, char *file) { - FILE *fp; + CFILE *fp; + + char tmp[MAX_PATH] = ""; + char tmp2[MAX_PATH] = ""; if (profile == NULL) return; - fp = fopen(file, "w"); + fp = cfopen(file, "wt", CFILE_NORMAL, CF_TYPE_ROOT); if (fp == NULL) return; Section *sp = profile->sections; while (sp != NULL) { - fprintf(fp, "[%s]\n", sp->name); + sprintf(tmp, NOX("[%s]\n"), sp->name); + cfputs(tmp, fp); KeyValue *kvp = sp->pairs; while (kvp != NULL) { - fprintf(fp, "%s=%s\n", kvp->key, kvp->value); + sprintf(tmp2, NOX("%s=%s\n"), kvp->key, kvp->value); + cfputs(tmp2, fp); kvp = kvp->next; } - fprintf(fp, "\n"); + cfwrite_char('\n', fp); sp = sp->next; } - fclose(fp); + cfclose(fp); } static char tmp_string_data[1024]; -- 2.39.2