X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=eda87185ec74990bc6ac9efbee0b648a93d82436;hb=7ecf99096c7c23909ada034e05d971464f66552f;hp=9d6e0cc9c9e461cd3e55d33f9ffb8e88a8e04e4f;hpb=297ee9b33dab3bca9433befc81d7c7ce91450dd0;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 9d6e0cc9..eda87185 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -441,7 +441,7 @@ unsigned int get_random_number(int nr, int max) /* system info functions */ /* ------------------------------------------------------------------------- */ -#if !defined(PLATFORM_MSDOS) +#if !defined(PLATFORM_MSDOS) && !defined(PLATFORM_ANDROID) static char *get_corrected_real_name(char *real_name) { char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1); @@ -835,9 +835,13 @@ void GetOptions(char *argv[], void (*print_usage_function)(void)) options.debug = FALSE; options.debug_x11_sync = FALSE; +#if 1 + options.verbose = TRUE; +#else #if !defined(PLATFORM_UNIX) if (*options_left == NULL) /* no options given -- enable verbose mode */ options.verbose = TRUE; +#endif #endif while (*options_left) @@ -1043,8 +1047,11 @@ void Error(int mode, char *format, ...) char *process_name = ""; #if defined(PLATFORM_ANDROID) - android_log_prio = (mode & ERR_WARN ? ANDROID_LOG_WARN : - mode & ERR_EXIT ? ANDROID_LOG_FATAL : ANDROID_LOG_INFO); + android_log_prio = (mode & ERR_DEBUG ? ANDROID_LOG_DEBUG : + mode & ERR_INFO ? ANDROID_LOG_INFO : + mode & ERR_WARN ? ANDROID_LOG_WARN : + mode & ERR_EXIT ? ANDROID_LOG_FATAL : + ANDROID_LOG_UNKNOWN); #endif #if 1 @@ -1446,7 +1453,9 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode) { KSYM_End, "XK_End", "end" }, { KSYM_Page_Up, "XK_Page_Up", "page up" }, { KSYM_Page_Down, "XK_Page_Down", "page down" }, - { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */ + + { KSYM_Menu, "XK_Menu", "menu" }, /* menu key */ + { KSYM_Back, "XK_Back", "back" }, /* back key */ /* ASCII 0x20 to 0x40 keys (except numbers) */ { KSYM_space, "XK_space", "space" }, @@ -1894,12 +1903,110 @@ void dumpList(ListNode *node_first) /* ------------------------------------------------------------------------- */ -/* functions for reading directories */ +/* functions for file handling */ /* ------------------------------------------------------------------------- */ -struct Directory *openDirectory(char *dir_name) +File *openFile(char *filename, char *mode) +{ + File *file = checked_calloc(sizeof(File)); + + file->file = fopen(filename, mode); + + if (file->file != NULL) + { + file->filename = getStringCopy(filename); + + return file; + } + +#if defined(PLATFORM_ANDROID) + file->asset_file = SDL_RWFromFile(filename, mode); + + if (file->asset_file != NULL) + { + file->file_is_asset = TRUE; + file->filename = getStringCopy(filename); + + return file; + } +#endif + + checked_free(file); + + return NULL; +} + +int closeFile(File *file) { - struct Directory *dir = checked_calloc(sizeof(struct Directory)); + if (file == NULL) + return -1; + + int result; + +#if defined(PLATFORM_ANDROID) + if (file->asset_file) + result = SDL_RWclose(file->asset_file); +#endif + + if (file->file) + result = fclose(file->file); + + checked_free(file->filename); + checked_free(file); + + return result; +} + +int checkEndOfFile(File *file) +{ +#if defined(PLATFORM_ANDROID) + if (file->file_is_asset) + return file->end_of_file; +#endif + + return feof(file->file); +} + +char *getStringFromFile(File *file, char *line, int size) +{ +#if defined(PLATFORM_ANDROID) + if (file->file_is_asset) + { + if (file->end_of_file) + return NULL; + + char *line_ptr = line; + int num_bytes_read = 0; + + while (num_bytes_read < size - 1 && + SDL_RWread(file->asset_file, line_ptr, 1, 1) == 1 && + *line_ptr++ != '\n') + num_bytes_read++; + + *line_ptr = '\0'; + + if (strlen(line) == 0) + { + file->end_of_file = TRUE; + + return NULL; + } + + return line; + } +#endif + + return fgets(line, size, file->file); +} + + +/* ------------------------------------------------------------------------- */ +/* functions for directory handling */ +/* ------------------------------------------------------------------------- */ + +Directory *openDirectory(char *dir_name) +{ + Directory *dir = checked_calloc(sizeof(Directory)); dir->dir = opendir(dir_name); @@ -1931,7 +2038,7 @@ struct Directory *openDirectory(char *dir_name) return NULL; } -int closeDirectory(struct Directory *dir) +int closeDirectory(Directory *dir) { if (dir == NULL) return -1; @@ -1955,7 +2062,7 @@ int closeDirectory(struct Directory *dir) return result; } -struct DirectoryEntry *readDirectory(struct Directory *dir) +DirectoryEntry *readDirectory(Directory *dir) { if (dir->dir_entry) freeDirectoryEntry(dir->dir_entry); @@ -1967,14 +2074,12 @@ struct DirectoryEntry *readDirectory(struct Directory *dir) { char line[MAX_LINE_LEN]; char *line_ptr = line; - char *last_line_ptr = line_ptr; int num_bytes_read = 0; - while (num_bytes_read < MAX_LINE_LEN && + while (num_bytes_read < MAX_LINE_LEN - 1 && SDL_RWread(dir->asset_toc_file, line_ptr, 1, 1) == 1 && *line_ptr != '\n') { - last_line_ptr = line_ptr; line_ptr++; num_bytes_read++; } @@ -1984,10 +2089,10 @@ struct DirectoryEntry *readDirectory(struct Directory *dir) if (strlen(line) == 0) return NULL; - dir->dir_entry = checked_calloc(sizeof(struct DirectoryEntry)); + dir->dir_entry = checked_calloc(sizeof(DirectoryEntry)); dir->dir_entry->is_directory = FALSE; - if (line[strlen(line) - 1] = '/') + if (line[strlen(line) - 1] == '/') { dir->dir_entry->is_directory = TRUE; @@ -2006,7 +2111,7 @@ struct DirectoryEntry *readDirectory(struct Directory *dir) if (dir_entry == NULL) return NULL; - dir->dir_entry = checked_calloc(sizeof(struct DirectoryEntry)); + dir->dir_entry = checked_calloc(sizeof(DirectoryEntry)); dir->dir_entry->basename = getStringCopy(dir_entry->d_name); dir->dir_entry->filename = getPath2(dir->filename, dir_entry->d_name); @@ -2015,12 +2120,18 @@ struct DirectoryEntry *readDirectory(struct Directory *dir) dir->dir_entry->is_directory = (stat(dir->dir_entry->filename, &file_status) == 0 && - (file_status.st_mode & S_IFMT) != S_IFDIR); + (file_status.st_mode & S_IFMT) == S_IFDIR); + +#if 0 + Error(ERR_INFO, "::: '%s' is directory: %d", + dir->dir_entry->basename, + dir->dir_entry->is_directory); +#endif return dir->dir_entry; } -void freeDirectoryEntry(struct DirectoryEntry *dir_entry) +void freeDirectoryEntry(DirectoryEntry *dir_entry) { if (dir_entry == NULL) return; @@ -2035,23 +2146,53 @@ void freeDirectoryEntry(struct DirectoryEntry *dir_entry) /* functions for checking files and filenames */ /* ------------------------------------------------------------------------- */ +boolean directoryExists(char *dir_name) +{ + if (dir_name == NULL) + return FALSE; + + boolean success = (access(dir_name, F_OK) == 0); + +#if defined(PLATFORM_ANDROID) + if (!success) + { + // this might be an asset directory; check by trying to open toc file + char *asset_toc_filename = getPath2(dir_name, ASSET_TOC_BASENAME); + SDL_RWops *file = SDL_RWFromFile(asset_toc_filename, MODE_READ); + + checked_free(asset_toc_filename); + + success = (file != NULL); + + if (success) + SDL_RWclose(file); + } +#endif + + return success; +} + boolean fileExists(char *filename) { if (filename == NULL) return FALSE; + boolean success = (access(filename, F_OK) == 0); + #if defined(PLATFORM_ANDROID) - // workaround: check if file exists by opening and closing it - SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ); - boolean success = (file != NULL); + if (!success) + { + // this might be an asset file; check by trying to open it + SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ); - if (success) - SDL_RWclose(file); + success = (file != NULL); - return success; -#else - return (access(filename, F_OK) == 0); + if (success) + SDL_RWclose(file); + } #endif + + return success; } boolean fileHasPrefix(char *basename, char *prefix) @@ -2102,23 +2243,38 @@ boolean FileIsGraphic(char *filename) { char *basename = getBaseNamePtr(filename); +#if defined(TARGET_SDL) + return (!fileHasSuffix(basename, "txt") && + !fileHasSuffix(basename, "conf")); +#else return fileHasSuffix(basename, "pcx"); +#endif } boolean FileIsSound(char *filename) { char *basename = getBaseNamePtr(filename); +#if defined(TARGET_SDL) + return (!fileHasSuffix(basename, "txt") && + !fileHasSuffix(basename, "conf")); +#else return fileHasSuffix(basename, "wav"); +#endif } boolean FileIsMusic(char *filename) { char *basename = getBaseNamePtr(filename); +#if defined(TARGET_SDL) + return (!fileHasSuffix(basename, "txt") && + !fileHasSuffix(basename, "conf")); +#else if (FileIsSound(basename)) return TRUE; +#if 0 #if defined(TARGET_SDL) if ((fileHasPrefix(basename, "mod") && !fileHasSuffix(basename, "txt")) || fileHasSuffix(basename, "mod") || @@ -2130,9 +2286,11 @@ boolean FileIsMusic(char *filename) fileHasSuffix(basename, "mp3") || fileHasSuffix(basename, "ogg")) return TRUE; +#endif #endif return FALSE; +#endif } boolean FileIsArtworkType(char *basename, int type) @@ -2390,10 +2548,13 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, } list_pos = 0; + for (i = 0; config_list[i].token != NULL; i++) { int len_config_token = strlen(config_list[i].token); +#if 0 int len_config_value = strlen(config_list[i].value); +#endif boolean is_file_entry = TRUE; for (j = 0; suffix_list[j].token != NULL; j++) @@ -2408,6 +2569,7 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, config_list[i].value); is_file_entry = FALSE; + break; } } @@ -2425,6 +2587,7 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, if (list_pos >= num_file_list_entries) break; +#if 0 /* simple sanity check if this is really a file definition */ if (!strEqual(&config_list[i].value[len_config_value - 4], ".pcx") && !strEqual(&config_list[i].value[len_config_value - 4], ".wav") && @@ -2434,6 +2597,7 @@ struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list, config_list[i].token, config_list[i].value); Error(ERR_EXIT, "This seems to be no valid definition -- please fix"); } +#endif file_list[list_pos].token = config_list[i].token; file_list[list_pos].default_filename = config_list[i].value; @@ -3224,6 +3388,7 @@ static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info, if (strEqual(file_list_entry->filename, UNDEFINED_FILENAME)) { deleteArtworkListEntry(artwork_info, listnode); + return; } @@ -3239,6 +3404,8 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info) artwork_info->num_dynamic_file_list_entries; int i; + print_timestamp_init("ReloadCustomArtworkList"); + for (i = 0; i < num_file_list_entries; i++) LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[i], &file_list[i]); @@ -3247,6 +3414,8 @@ void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info) LoadCustomArtwork(artwork_info, &artwork_info->dynamic_artwork_list[i], &dynamic_file_list[i]); + print_timestamp_done("ReloadCustomArtworkList"); + #if 0 dumpList(artwork_info->content_list); #endif @@ -3344,8 +3513,11 @@ void NotifyUserAboutErrorFile() #if DEBUG -#define DEBUG_NUM_TIMESTAMPS 5 -#define DEBUG_TIME_IN_MICROSECONDS 0 +#define DEBUG_PRINT_INIT_TIMESTAMPS TRUE +#define DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH 10 + +#define DEBUG_NUM_TIMESTAMPS 10 +#define DEBUG_TIME_IN_MICROSECONDS 0 #if DEBUG_TIME_IN_MICROSECONDS static double Counter_Microseconds() @@ -3409,7 +3581,11 @@ void debug_print_timestamp(int counter_nr, char *message) counter[counter_nr][1] = counter[counter_nr][0]; if (message) +#if 1 + Error(ERR_DEBUG, "%s%s%s %.3f %s", +#else printf("%s%s%s %.3f %s\n", +#endif debug_print_timestamp_get_padding(counter_nr * indent_size), message, debug_print_timestamp_get_padding(padding_size - strlen(message)), @@ -3433,4 +3609,69 @@ void debug_print_parent_only(char *format, ...) printf("\n"); } } + +void print_timestamp_ext(char *message, char *mode) +{ +#if DEBUG_PRINT_INIT_TIMESTAMPS + static char *debug_message = NULL; + static char *last_message = NULL; + static int counter_nr = 0; + int max_depth = DEBUG_PRINT_INIT_TIMESTAMPS_DEPTH; + + checked_free(debug_message); + debug_message = getStringCat3(mode, " ", message); + + if (strEqual(mode, "INIT")) + { + debug_print_timestamp(counter_nr, NULL); + + if (counter_nr + 1 < max_depth) + debug_print_timestamp(counter_nr, debug_message); + + counter_nr++; + + debug_print_timestamp(counter_nr, NULL); + } + else if (strEqual(mode, "DONE")) + { + counter_nr--; + + if (counter_nr + 1 < max_depth || + (counter_nr == 0 && max_depth == 1)) + { + last_message = message; + + if (counter_nr == 0 && max_depth == 1) + { + checked_free(debug_message); + debug_message = getStringCat3("TIME", " ", message); + } + + debug_print_timestamp(counter_nr, debug_message); + } + } + else if (!strEqual(mode, "TIME") || + !strEqual(message, last_message)) + { + if (counter_nr < max_depth) + debug_print_timestamp(counter_nr, debug_message); + } #endif +} + +void print_timestamp_init(char *message) +{ + print_timestamp_ext(message, "INIT"); +} + +void print_timestamp_time(char *message) +{ + print_timestamp_ext(message, "TIME"); +} + +void print_timestamp_done(char *message) +{ + print_timestamp_ext(message, "DONE"); +} + +#endif /* DEBUG */