X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=910a7e95ef1d18a725549daad837bbf26a3c9ded;hb=c38b8938950533926b72af227c38513fe665a8f6;hp=8fe3200d3c4a28b22675eeccd81a9fc00f7450da;hpb=2d603d06ca862f3ca0721b66f62da188faf866c4;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 8fe3200d..910a7e95 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -1,7 +1,7 @@ /*********************************************************** * Artsoft Retro-Game Library * *----------------------------------------------------------* -* (c) 1994-2001 Artsoft Entertainment * +* (c) 1994-2002 Artsoft Entertainment * * Holger Schemel * * Detmolder Strasse 189 * * 33604 Bielefeld * @@ -29,6 +29,7 @@ #include "misc.h" #include "setup.h" #include "random.h" +#include "text.h" #if defined(PLATFORM_MSDOS) @@ -306,12 +307,19 @@ char *getLoginName() #if defined(PLATFORM_WIN32) return ANONYMOUS_NAME; #else - struct passwd *pwd; + static char *login_name = NULL; - if ((pwd = getpwuid(getuid())) == NULL) - return ANONYMOUS_NAME; - else - return pwd->pw_name; + if (login_name == NULL) + { + struct passwd *pwd; + + if ((pwd = getpwuid(getuid())) == NULL) + login_name = ANONYMOUS_NAME; + else + login_name = getStringCopy(pwd->pw_name); + } + + return login_name; #endif } @@ -357,16 +365,16 @@ char *getHomeDir() #if defined(PLATFORM_UNIX) static char *home_dir = NULL; - if (!home_dir) + if (home_dir == NULL) { - if (!(home_dir = getenv("HOME"))) + if ((home_dir = getenv("HOME")) == NULL) { struct passwd *pwd; - if ((pwd = getpwuid(getuid()))) - home_dir = pwd->pw_dir; - else + if ((pwd = getpwuid(getuid())) == NULL) home_dir = "."; + else + home_dir = getStringCopy(pwd->pw_dir); } } @@ -395,6 +403,14 @@ char *getPath3(char *path1, char *path2, char *path3) return complete_path; } +static char *getStringCat2(char *s1, char *s2) +{ + char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1); + + sprintf(complete_string, "%s%s", s1, s2); + return complete_string; +} + char *getStringCopy(char *s) { char *s_copy; @@ -628,7 +644,7 @@ void Error(int mode, char *format, ...) if (mode & ERR_WARN && !options.verbose) return; -#if !defined(PLATFORM_UNIX) +#if defined(PLATFORM_MSDOS) newline = "\r\n"; if ((error = openErrorFile()) == NULL) @@ -1205,6 +1221,86 @@ char getCharFromKey(Key key) } +/* ========================================================================= */ +/* functions for generic lists */ +/* ========================================================================= */ + +ListNode *newListNode() +{ + return checked_calloc(sizeof(ListNode)); +} + +void addNodeToList(ListNode **node_first, char *key, void *content) +{ + ListNode *node_new = newListNode(); + +#if 0 + printf("LIST: adding node with key '%s'\n", key); +#endif + + node_new->key = getStringCopy(key); + node_new->content = content; + node_new->next = *node_first; + *node_first = node_new; +} + +void deleteNodeFromList(ListNode **node_first, char *key, + void (*destructor_function)(void *)) +{ + if (node_first == NULL || *node_first == NULL) + return; + +#if 0 + printf("[CHECKING LIST KEY '%s' == '%s']\n", + (*node_first)->key, key); +#endif + + if (strcmp((*node_first)->key, key) == 0) + { +#if 0 + printf("[DELETING LIST ENTRY]\n"); +#endif + + free((*node_first)->key); + if (destructor_function) + destructor_function((*node_first)->content); + *node_first = (*node_first)->next; + } + else + deleteNodeFromList(&(*node_first)->next, key, destructor_function); +} + +ListNode *getNodeFromKey(ListNode *node_first, char *key) +{ + if (node_first == NULL) + return NULL; + + if (strcmp(node_first->key, key) == 0) + return node_first; + else + return getNodeFromKey(node_first->next, key); +} + +int getNumNodes(ListNode *node_first) +{ + return (node_first ? 1 + getNumNodes(node_first->next) : 0); +} + +void dumpList(ListNode *node_first) +{ + ListNode *node = node_first; + + while (node) + { + printf("['%s' (%d)]\n", node->key, + ((struct ListNodeInfo *)node->content)->num_references); + node = node->next; + } + + printf("[%d nodes]\n", getNumNodes(node_first)); +} + + /* ========================================================================= */ /* functions for checking filenames */ /* ========================================================================= */ @@ -1256,46 +1352,413 @@ boolean FileIsArtworkType(char *basename, int type) return FALSE; } - /* ========================================================================= */ -/* functions only needed for non-Unix (non-command-line) systems */ +/* functions for loading artwork configuration information */ /* ========================================================================= */ -#if !defined(PLATFORM_UNIX) +struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, + struct ConfigInfo *suffix_list, + int num_file_list_entries) +{ + struct FileInfo *file_list; + int num_suffix_list_entries = 0; + int list_pos = 0; + int i, j; -#define ERROR_FILENAME "error.out" + file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo)); -void initErrorFile() + for (i=0; suffix_list[i].token != NULL; i++) + num_suffix_list_entries++; + + /* always start with reliable default values */ + for (i=0; i 0) + { + int parameter_array_size = num_suffix_list_entries * sizeof(int); + + file_list[i].default_parameter = checked_calloc(parameter_array_size); + file_list[i].parameter = checked_calloc(parameter_array_size); + + for (j=0; j 0) + list_pos++; + + if (list_pos > num_file_list_entries - 1) + break; + + /* simple sanity check if this is really a file definition */ + if (strcmp(&config_list[i].value[len_config_value - 4], ".pcx") != 0 && + strcmp(&config_list[i].value[len_config_value - 4], ".wav") != 0 && + strcmp(config_list[i].value, UNDEFINED_FILENAME) != 0) + { + Error(ERR_RETURN, "Configuration directive '%s' -> '%s':", + config_list[i].token, config_list[i].value); + Error(ERR_EXIT, "This seems to be no valid definition -- please fix"); + } + + file_list[list_pos].token = config_list[i].token; + file_list[list_pos].default_filename = config_list[i].value; + } + } + + if (list_pos != num_file_list_entries - 1) + Error(ERR_EXIT, "inconsistant config list information -- please fix"); + + return file_list; +} + +static void LoadArtworkConfig(struct ArtworkListInfo *artwork_info) { - char *filename; + struct FileInfo *file_list = artwork_info->file_list; + struct ConfigInfo *suffix_list = artwork_info->suffix_list; + int num_file_list_entries = artwork_info->num_file_list_entries; + int num_suffix_list_entries = artwork_info->num_suffix_list_entries; + char *filename = getCustomArtworkConfigFilename(artwork_info->type); + struct SetupFileList *setup_file_list; + int i, j; - InitUserDataDirectory(); +#if 0 + printf("GOT CUSTOM ARTWORK CONFIG FILE '%s'\n", filename); +#endif - filename = getPath2(getUserDataDir(), ERROR_FILENAME); - unlink(filename); - free(filename); + /* always start with reliable default values */ + for (i=0; i '%s'\n", file_list[i].filename); + else + printf("-> UNDEFINED [-> '%s']\n", file_list[i].default_filename); + } +#endif + } } -FILE *openErrorFile() +static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info, + struct ListNodeInfo **listnode) +{ + if (*listnode) + { + char *filename = (*listnode)->source_filename; + +#if 0 + printf("[decrementing reference counter of artwork '%s']\n", filename); +#endif + + if (--(*listnode)->num_references <= 0) + { +#if 0 + printf("[deleting artwork '%s']\n", filename); +#endif + + deleteNodeFromList(&artwork_info->content_list, filename, + artwork_info->free_artwork); + } + + *listnode = NULL; + } +} + +static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info, + struct ListNodeInfo **listnode, + char *basename) +{ + char *init_text[] = + { "", + "Loading graphics:", + "Loading sounds:", + "Loading music:" + }; + + ListNode *node; + char *filename = getCustomArtworkFilename(basename, artwork_info->type); + + if (filename == NULL) + { + Error(ERR_WARN, "cannot find artwork file '%s'", basename); + return; + } + + /* check if the old and the new artwork file are the same */ + if (*listnode && strcmp((*listnode)->source_filename, filename) == 0) + { + /* The old and new artwork are the same (have the same filename and path). + This usually means that this artwork does not exist in this artwork set + and a fallback to the existing artwork is done. */ + +#if 0 + printf("[artwork '%s' already exists (same list entry)]\n", filename); +#endif + + return; + } + + /* delete existing artwork file entry */ + deleteArtworkListEntry(artwork_info, listnode); + + /* check if the new artwork file already exists in the list of artworks */ + if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL) + { +#if 0 + printf("[artwork '%s' already exists (other list entry)]\n", filename); +#endif + + *listnode = (struct ListNodeInfo *)node->content; + (*listnode)->num_references++; + + return; + } + + DrawInitText(init_text[artwork_info->type], 120, FC_GREEN); + DrawInitText(basename, 150, FC_YELLOW); + + if ((*listnode = artwork_info->load_artwork(filename)) != NULL) + { +#if 0 + printf("[adding new artwork '%s']\n", filename); +#endif + + (*listnode)->num_references = 1; + addNodeToList(&artwork_info->content_list, (*listnode)->source_filename, + *listnode); + } +} + +static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info, + struct ListNodeInfo **listnode, + char *basename) { - char *filename; - FILE *error_file; +#if 0 + char *filename = getCustomArtworkFilename(basename, artwork_info->type); +#endif - filename = getPath2(getUserDataDir(), ERROR_FILENAME); - error_file = fopen(filename, MODE_APPEND); - free(filename); +#if 0 + printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename); +#endif + + if (strcmp(basename, UNDEFINED_FILENAME) == 0) + { + deleteArtworkListEntry(artwork_info, listnode); + return; + } - return error_file; +#if 0 + if (filename == NULL) + { + Error(ERR_WARN, "cannot find artwork file '%s'", basename); + return; + } + + replaceArtworkListEntry(artwork_info, listnode, filename); +#else + replaceArtworkListEntry(artwork_info, listnode, basename); +#endif } -void dumpErrorFile() +static void LoadArtworkToList(struct ArtworkListInfo *artwork_info, + char *basename, int list_pos) { - char *filename; - FILE *error_file; + if (artwork_info->artwork_list == NULL || + list_pos >= artwork_info->num_file_list_entries) + return; + +#if 0 + printf("loading artwork '%s' ... [%d]\n", + basename, getNumNodes(artwork_info->content_list)); +#endif + + LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos], + basename); + +#if 0 + printf("loading artwork '%s' done [%d]\n", + basename, getNumNodes(artwork_info->content_list)); +#endif +} - filename = getPath2(getUserDataDir(), ERROR_FILENAME); - error_file = fopen(filename, MODE_READ); - free(filename); +void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info) +{ +#if 0 + static struct + { + char *text; + boolean do_it; + } + draw_init[] = + { + { "", FALSE }, + { "Loading graphics:", TRUE }, + { "Loading sounds:", TRUE }, + { "Loading music:", TRUE } + }; +#endif + + int num_file_list_entries = artwork_info->num_file_list_entries; + struct FileInfo *file_list = artwork_info->file_list; + int i; + + LoadArtworkConfig(artwork_info); + +#if 0 + if (draw_init[artwork_info->type].do_it) + DrawInitText(draw_init[artwork_info->type].text, 120, FC_GREEN); +#endif + +#if 0 + printf("DEBUG: reloading %d artwork files ...\n", num_file_list_entries); +#endif + + for(i=0; itype].do_it) + DrawInitText(file_list[i].token, 150, FC_YELLOW); +#endif + + LoadArtworkToList(artwork_info, file_list[i].filename, i); + +#if 0 + printf("DEBUG: loading artwork file '%s'...\n", file_list[i].filename); +#endif + } + +#if 0 + draw_init[artwork_info->type].do_it = FALSE; +#endif + + /* + printf("list size == %d\n", getNumNodes(artwork_info->content_list)); + */ + +#if 0 + dumpList(artwork_info->content_list); +#endif +} + +void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info) +{ + int i; + + if (artwork_info->artwork_list == NULL) + return; + +#if 0 + printf("%s: FREEING ARTWORK ...\n", + IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT"); +#endif + + for(i=0; inum_file_list_entries; i++) + deleteArtworkListEntry(artwork_info, &artwork_info->artwork_list[i]); + +#if 0 + printf("%s: FREEING ARTWORK -- DONE\n", + IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT"); +#endif + + free(artwork_info->artwork_list); + + artwork_info->artwork_list = NULL; + artwork_info->num_file_list_entries = 0; +} + + +/* ========================================================================= */ +/* functions only needed for non-Unix (non-command-line) systems */ +/* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */ +/* ========================================================================= */ + +#if defined(PLATFORM_MSDOS) + +#define ERROR_FILENAME "stderr.txt" + +void initErrorFile() +{ + unlink(ERROR_FILENAME); +} + +FILE *openErrorFile() +{ + return fopen(ERROR_FILENAME, MODE_APPEND); +} + +void dumpErrorFile() +{ + FILE *error_file = fopen(ERROR_FILENAME, MODE_READ); if (error_file != NULL) {