From ea933dd51bfca7ddcc6b75465476cce57afd1682 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 31 Mar 2003 02:09:43 +0200 Subject: [PATCH] rnd-20030331-1-src --- src/Makefile | 1 + src/conftime.h | 2 +- src/files.c | 32 +++++++---- src/init.c | 7 --- src/libgame/macosx.h | 24 ++++++++ src/libgame/misc.c | 125 ++++++++++++++++++++++++++++-------------- src/libgame/misc.h | 6 +- src/libgame/setup.c | 31 +++++++++-- src/libgame/setup.h | 1 + src/libgame/system.c | 58 ++++++++------------ src/libgame/system.h | 22 +++----- src/libgame/types.h | 3 + src/libgame/windows.h | 41 ++++++++++++++ src/main.c | 8 ++- src/main.h | 10 +++- 15 files changed, 253 insertions(+), 118 deletions(-) create mode 100644 src/libgame/macosx.h create mode 100644 src/libgame/windows.h diff --git a/src/Makefile b/src/Makefile index fe9f33e0..595f5996 100644 --- a/src/Makefile +++ b/src/Makefile @@ -64,6 +64,7 @@ TARGET = allegro endif ifeq ($(PLATFORM),cross-win32) +EXTRA_LDFLAGS = -lshfolder PROGNAME = ../$(PROGBASE).exe TARGET = sdl endif diff --git a/src/conftime.h b/src/conftime.h index ed9b5fa2..896228b6 100644 --- a/src/conftime.h +++ b/src/conftime.h @@ -1 +1 @@ -#define COMPILE_DATE_STRING "[2003-03-27 00:55]" +#define COMPILE_DATE_STRING "[2003-03-31 02:04]" diff --git a/src/files.c b/src/files.c index 436decce..15264d2d 100644 --- a/src/files.c +++ b/src/files.c @@ -1515,11 +1515,26 @@ static struct TokenInfo options_setup_tokens[] = { TYPE_BOOLEAN, &soi.verbose, "options.verbose" } }; +static char *get_corrected_login_name(char *login_name) +{ + /* needed because player name must be a fixed length string */ + char *login_name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1); + + strncpy(login_name_new, login_name, MAX_PLAYER_NAME_LEN); + login_name_new[MAX_PLAYER_NAME_LEN] = '\0'; + + if (strlen(login_name) > MAX_PLAYER_NAME_LEN) /* name has been cut */ + if (strchr(login_name_new, ' ')) + *strchr(login_name_new, ' ') = '\0'; + + return login_name_new; +} + static void setSetupInfoToDefaults(struct SetupInfo *si) { int i; - si->player_name = getStringCopy(getLoginName()); + si->player_name = get_corrected_login_name(getLoginName()); si->sound = TRUE; si->sound_loops = TRUE; @@ -1660,6 +1675,8 @@ void LoadSetup() if (setup_file_list) { + char *player_name_new; + checkSetupFileListIdentifier(setup_file_list, getCookie("SETUP")); decodeSetupFileList(setup_file_list); @@ -1668,16 +1685,9 @@ void LoadSetup() freeSetupFileList(setup_file_list); /* needed to work around problems with fixed length strings */ - if (strlen(setup.player_name) > MAX_PLAYER_NAME_LEN) - setup.player_name[MAX_PLAYER_NAME_LEN] = '\0'; - else if (strlen(setup.player_name) < MAX_PLAYER_NAME_LEN) - { - char *new_name = checked_malloc(MAX_PLAYER_NAME_LEN + 1); - - strcpy(new_name, setup.player_name); - free(setup.player_name); - setup.player_name = new_name; - } + player_name_new = get_corrected_login_name(setup.player_name); + free(setup.player_name); + setup.player_name = player_name_new; } else Error(ERR_WARN, "using default setup values"); diff --git a/src/init.c b/src/init.c index 46f27d80..80073129 100644 --- a/src/init.c +++ b/src/init.c @@ -24,7 +24,6 @@ #include "network.h" #include "netserv.h" #include "cartoons.h" -#include "config.h" #include "conf_e2g.c" /* include auto-generated data structure definitions */ #include "conf_esg.c" /* include auto-generated data structure definitions */ @@ -2770,12 +2769,6 @@ void OpenAll() exit(0); /* never reached */ } - InitProgramInfo(UNIX_USERDATA_DIRECTORY, - PROGRAM_TITLE_STRING, getWindowTitleString(), - ICON_TITLE_STRING, X11_ICON_FILENAME, X11_ICONMASK_FILENAME, - MSDOS_POINTER_FILENAME, - COOKIE_PREFIX, FILENAME_PREFIX, GAME_VERSION_ACTUAL); - InitSetup(); InitPlayerInfo(); diff --git a/src/libgame/macosx.h b/src/libgame/macosx.h new file mode 100644 index 00000000..7cccf571 --- /dev/null +++ b/src/libgame/macosx.h @@ -0,0 +1,24 @@ +/*********************************************************** +* Artsoft Retro-Game Library * +*----------------------------------------------------------* +* (c) 1994-2003 Artsoft Entertainment * +* Holger Schemel * +* Detmolder Strasse 189 * +* 33604 Bielefeld * +* Germany * +* e-mail: info@artsoft.org * +*----------------------------------------------------------* +* macosx.h * +***********************************************************/ + +#ifndef MACOSX_H +#define MACOSX_H + + +/* some symbols are already defined on Mac OS X */ +#define Delay Delay_internal +#define DrawLine DrawLine_internal +#define DrawText DrawText_internal +#define GetPixel GetPixel_internal + +#endif /* MACOSX_H */ diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 2750b627..cffa180b 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -332,13 +332,53 @@ unsigned int get_random_number(int nr, unsigned int max) /* system info functions */ /* ------------------------------------------------------------------------- */ +static char *get_corrected_real_name(char *real_name) +{ + char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1); + char *from_ptr = real_name; + char *to_ptr = real_name_new; + + if (strchr(real_name, 'ß') == NULL) /* name does not contain 'ß' */ + { + strncpy(real_name_new, real_name, MAX_USERNAME_LEN); + real_name_new[MAX_USERNAME_LEN] = '\0'; + + return real_name_new; + } + + /* the user's real name may contain a 'ß' character (german sharp s), + which has no equivalent in upper case letters (which our fonts use) */ + while (*from_ptr && (long)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1) + { + if (*from_ptr != 'ß') + *to_ptr++ = *from_ptr++; + else + { + from_ptr++; + *to_ptr++ = 's'; + *to_ptr++ = 's'; + } + } + + *to_ptr = '\0'; + + return real_name_new; +} + char *getLoginName() { -#if defined(PLATFORM_WIN32) - return ANONYMOUS_NAME; -#else static char *login_name = NULL; +#if defined(PLATFORM_WIN32) + if (login_name == NULL) + { + unsigned long buffer_size = MAX_USERNAME_LEN + 1; + login_name = checked_malloc(buffer_size); + + if (GetUserName(login_name, &buffer_size) == 0) + strcpy(login_name, ANONYMOUS_NAME); + } +#else if (login_name == NULL) { struct passwd *pwd; @@ -348,70 +388,73 @@ char *getLoginName() else login_name = getStringCopy(pwd->pw_name); } +#endif return login_name; -#endif } char *getRealName() { -#if defined(PLATFORM_UNIX) - struct passwd *pwd; + static char *real_name = NULL; - if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0) - return ANONYMOUS_NAME; - else +#if defined(PLATFORM_WIN32) + if (real_name == NULL) { - static char real_name[1024]; - char *from_ptr = pwd->pw_gecos, *to_ptr = real_name; - - if (strchr(pwd->pw_gecos, 'ß') == NULL) - return pwd->pw_gecos; + static char buffer[MAX_USERNAME_LEN + 1]; + unsigned long buffer_size = MAX_USERNAME_LEN + 1; - /* the user's real name contains a 'ß' character (german sharp s), - which has no equivalent in upper case letters (which our fonts use) */ - while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2) - { - if (*from_ptr != 'ß') - *to_ptr++ = *from_ptr++; - else - { - from_ptr++; - *to_ptr++ = 's'; - *to_ptr++ = 's'; - } - } - *to_ptr = '\0'; + if (GetUserName(buffer, &buffer_size) != 0) + real_name = get_corrected_real_name(buffer); + else + real_name = ANONYMOUS_NAME; + } +#elif defined(PLATFORM_UNIX) + if (real_name == NULL) + { + struct passwd *pwd; - return real_name; + if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0) + real_name = get_corrected_real_name(pwd->pw_gecos); + else + real_name = ANONYMOUS_NAME; } -#else /* !PLATFORM_UNIX */ - return ANONYMOUS_NAME; +#else + real_name = ANONYMOUS_NAME; #endif + + return real_name; } char *getHomeDir() { -#if defined(PLATFORM_UNIX) - static char *home_dir = NULL; + static char *dir = NULL; - if (home_dir == NULL) +#if defined(PLATFORM_WIN32) + if (dir == NULL) + { + dir = checked_malloc(MAX_PATH + 1); + + if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir))) + strcpy(dir, "."); + } +#elif defined(PLATFORM_UNIX) + if (dir == NULL) { - if ((home_dir = getenv("HOME")) == NULL) + if ((dir = getenv("HOME")) == NULL) { struct passwd *pwd; - if ((pwd = getpwuid(getuid())) == NULL) - home_dir = "."; + if ((pwd = getpwuid(getuid())) != NULL) + dir = getStringCopy(pwd->pw_dir); else - home_dir = getStringCopy(pwd->pw_dir); + dir = "."; } } - - return home_dir; #else - return "."; + dir = "."; #endif + + return dir; } diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 6b05533a..2a727278 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -57,7 +57,11 @@ /* values for general file handling stuff */ #define MAX_FILENAME_LEN 256 -#define MAX_LINE_LEN 1000 +#define MAX_LINE_LEN 1024 + +/* values for general username handling stuff */ +#define MAX_USERNAME_LEN 1024 + void fprintf_line(FILE *, char *, int); void printf_line(char *, int); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index dcf94aaa..68dd7434 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -170,7 +170,7 @@ static char *getTapeDir(char *level_subdir) static char *getScoreDir(char *level_subdir) { static char *score_dir = NULL; - char *data_dir = options.rw_base_directory; + char *data_dir = getCommonDataDir(); char *score_subdir = SCORES_DIRECTORY; if (score_dir) @@ -610,6 +610,7 @@ void InitTapeDirectory(char *level_subdir) void InitScoreDirectory(char *level_subdir) { + createDirectory(getCommonDataDir(), "common data", PERMS_PUBLIC); createDirectory(getScoreDir(NULL), "main score", PERMS_PUBLIC); createDirectory(getScoreDir(level_subdir), "level score", PERMS_PUBLIC); } @@ -885,15 +886,33 @@ char *getUserDataDir(void) { static char *userdata_dir = NULL; - if (!userdata_dir) + if (userdata_dir == NULL) + userdata_dir = getPath2(getHomeDir(), program.userdata_directory); + + return userdata_dir; +} + +char *getCommonDataDir(void) +{ + static char *common_data_dir = NULL; + +#if defined(PLATFORM_WIN32) + if (common_data_dir == NULL) { - char *home_dir = getHomeDir(); - char *data_dir = program.userdata_directory; + char *dir = checked_malloc(MAX_PATH + 1); - userdata_dir = getPath2(home_dir, data_dir); + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, dir)) + && strcmp(dir, "") != 0) /* empty for Windows 95/98 */ + common_data_dir = getPath2(dir, program.userdata_directory); + else + common_data_dir = options.rw_base_directory; } +#else + if (common_data_dir == NULL) + common_data_dir = options.rw_base_directory; +#endif - return userdata_dir; + return common_data_dir; } char *getSetupDir() diff --git a/src/libgame/setup.h b/src/libgame/setup.h index e5dc50d8..618e7cb0 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -195,6 +195,7 @@ void sortTreeInfo(TreeInfo **, int (*compare_function)(const void *, const void *)); char *getUserDataDir(void); +char *getCommonDataDir(void); char *getSetupDir(void); void createDirectory(char *, char *, int); void InitUserDataDirectory(void); diff --git a/src/libgame/system.c b/src/libgame/system.c index 0131c133..1e628241 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -66,10 +66,31 @@ int FrameCounter = 0; /* init/close functions */ /* ========================================================================= */ -void InitCommandName(char *argv0) +void InitProgramInfo(char *argv0, + char *userdata_directory, char *program_title, + char *window_title, char *icon_title, + char *x11_icon_filename, char *x11_iconmask_filename, + char *msdos_pointer_filename, + char *cookie_prefix, char *filename_prefix, + int program_version) { program.command_basename = (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0); + + program.userdata_directory = userdata_directory; + program.program_title = program_title; + program.window_title = window_title; + program.icon_title = icon_title; + program.x11_icon_filename = x11_icon_filename; + program.x11_iconmask_filename = x11_iconmask_filename; + program.msdos_pointer_filename = msdos_pointer_filename; + + program.cookie_prefix = cookie_prefix; + program.filename_prefix = filename_prefix; + + program.version_major = VERSION_MAJOR(program_version); + program.version_minor = VERSION_MINOR(program_version); + program.version_patch = VERSION_PATCH(program_version); } void InitExitFunction(void (*exit_function)(int)) @@ -90,13 +111,6 @@ void InitPlatformDependantStuff(void) { #if defined(PLATFORM_MSDOS) _fmode = O_BINARY; -#endif - -#if !defined(PLATFORM_UNIX) - program.userdata_directory = "userdata"; -#endif - -#if defined(PLATFORM_MSDOS) initErrorFile(); #endif @@ -113,34 +127,6 @@ void ClosePlatformDependantStuff(void) #endif } -void InitProgramInfo(char *unix_userdata_directory, char *program_title, - char *window_title, char *icon_title, - char *x11_icon_filename, char *x11_iconmask_filename, - char *msdos_pointer_filename, - char *cookie_prefix, char *filename_prefix, - int program_version) -{ -#if defined(PLATFORM_UNIX) - program.userdata_directory = unix_userdata_directory; -#else - program.userdata_directory = "userdata"; -#endif - - program.program_title = program_title; - program.window_title = window_title; - program.icon_title = icon_title; - program.x11_icon_filename = x11_icon_filename; - program.x11_iconmask_filename = x11_iconmask_filename; - program.msdos_pointer_filename = msdos_pointer_filename; - - program.cookie_prefix = cookie_prefix; - program.filename_prefix = filename_prefix; - - program.version_major = VERSION_MAJOR(program_version); - program.version_minor = VERSION_MINOR(program_version); - program.version_patch = VERSION_PATCH(program_version); -} - void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize, int real_sx, int real_sy, int full_sxsize, int full_sysize) diff --git a/src/libgame/system.h b/src/libgame/system.h index bba4a305..2abb3cdd 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -17,7 +17,12 @@ #include "platform.h" #include "types.h" -#if defined(PLATFORM_MSDOS) + +#if defined(PLATFORM_MACOSX) +#include "macosx.h" +#elif defined(PLATFORM_WIN32) +#include "windows.h" +#elif defined(PLATFORM_MSDOS) #include "msdos.h" #endif @@ -27,14 +32,6 @@ #include "x11.h" #endif -#if defined(PLATFORM_MACOSX) -/* some symbols are already defined on Mac OS X */ -#define Delay Delay_internal -#define DrawLine DrawLine_internal -#define DrawText DrawText_internal -#define GetPixel GetPixel_internal -#endif - /* the additional 'b' is needed for Win32 to open files in binary mode */ #define MODE_READ "rb" @@ -632,14 +629,13 @@ extern int FrameCounter; /* function definitions */ -void InitCommandName(char *); +void InitProgramInfo(char *, char *, char *, char *, char *, char *, char *, + char *, char *, char *, int); + void InitExitFunction(void (*exit_function)(int)); void InitPlatformDependantStuff(void); void ClosePlatformDependantStuff(void); -void InitProgramInfo(char *, char *, char *, char *, char *, char *, char *, - char *, char *, int); - void InitGfxFieldInfo(int, int, int, int, int, int, int, int); void InitGfxDoor1Info(int, int, int, int); void InitGfxDoor2Info(int, int, int, int); diff --git a/src/libgame/types.h b/src/libgame/types.h index 1c527d09..c8ab0efc 100644 --- a/src/libgame/types.h +++ b/src/libgame/types.h @@ -20,7 +20,10 @@ #include typedef unsigned char boolean; + +#if !defined(PLATFORM_WIN32) typedef unsigned char byte; +#endif #ifndef FALSE #define FALSE 0 diff --git a/src/libgame/windows.h b/src/libgame/windows.h new file mode 100644 index 00000000..d96d4b71 --- /dev/null +++ b/src/libgame/windows.h @@ -0,0 +1,41 @@ +/*********************************************************** +* Artsoft Retro-Game Library * +*----------------------------------------------------------* +* (c) 1994-2003 Artsoft Entertainment * +* Holger Schemel * +* Detmolder Strasse 189 * +* 33604 Bielefeld * +* Germany * +* e-mail: info@artsoft.org * +*----------------------------------------------------------* +* windows.h * +***********************************************************/ + +#ifndef WINDOWS_H +#define WINDOWS_H + +#include + + +/* some symbols are already defined on Windows */ +#define CreateBitmap CreateBitmap_internal +#define GetPixel GetPixel_internal +#define CloseWindow CloseWindow_internal +#define FloodFill FloodFill_internal + +#ifdef LoadImage +#undef LoadImage +#define LoadImage LoadImage_internal +#endif + +#ifdef PlaySound +#undef PlaySound +#define PlaySound PlaySound_internal +#endif + +#ifdef DrawText +#undef DrawText +#define DrawText DrawText_internal +#endif + +#endif /* WINDOWS_H */ diff --git a/src/main.c b/src/main.c index 4fd5ead5..bcd5dce7 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include "init.h" #include "game.h" #include "events.h" +#include "config.h" #if 0 GC tile_clip_gc; @@ -3035,7 +3036,12 @@ struct FontInfo font_info[NUM_FONTS + 1] = int main(int argc, char *argv[]) { - InitCommandName(argv[0]); + InitProgramInfo(argv[0], USERDATA_DIRECTORY, + PROGRAM_TITLE_STRING, getWindowTitleString(), + ICON_TITLE_STRING, X11_ICON_FILENAME, X11_ICONMASK_FILENAME, + MSDOS_POINTER_FILENAME, + COOKIE_PREFIX, FILENAME_PREFIX, GAME_VERSION_ACTUAL); + InitExitFunction(CloseAllAndExit); InitPlatformDependantStuff(); diff --git a/src/main.h b/src/main.h index b75fc447..3922fb29 100644 --- a/src/main.h +++ b/src/main.h @@ -858,10 +858,18 @@ #define WINDOW_TITLE_STRING PROGRAM_TITLE_STRING " " PROGRAM_IDENT_STRING #define WINDOW_SUBTITLE_STRING PROGRAM_RIGHTS_STRING " " PROGRAM_AUTHOR_STRING #define ICON_TITLE_STRING PROGRAM_TITLE_STRING -#define UNIX_USERDATA_DIRECTORY ".rocksndiamonds" #define COOKIE_PREFIX "ROCKSNDIAMONDS" #define FILENAME_PREFIX "Rocks" +#define USERDATA_DIR_STRING "Userdata" +#if defined(PLATFORM_UNIX) +#define USERDATA_DIRECTORY ".rocksndiamonds" +#elif defined(PLATFORM_WIN32) +#define USERDATA_DIRECTORY PROGRAM_TITLE_STRING " " USERDATA_DIR_STRING +#else +#define USERDATA_DIRECTORY USERDATA_DIR_STRING +#endif + #define X11_ICON_FILENAME "rocks_icon.xbm" #define X11_ICONMASK_FILENAME "rocks_iconmask.xbm" #define MSDOS_POINTER_FILENAME "mouse.pcx" -- 2.34.1