diff --git a/src/audio/audio.c b/src/audio/audio.c index d94dea542a..64548460e2 100644 --- a/src/audio/audio.c +++ b/src/audio/audio.c @@ -84,7 +84,7 @@ BOOL CALLBACK dsound_enum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription, LPCS int CALLBACK audio_timer_callback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2); int sound_fill_buffer(LPDIRECTSOUNDBUFFER dsbuffer, char* src, DWORD size); void sound_channel_free(HMMIO* hmmio, HGLOBAL* hmem); -LPVOID map_file(LPCSTR lpFileName, DWORD dwCreationDisposition, DWORD dwNumberOfBytesToMap); +LPVOID map_file(LPCWSTR lpFileName, DWORD dwCreationDisposition, DWORD dwNumberOfBytesToMap); int unmap_file(LPCVOID base); void audio_init(int i) @@ -693,7 +693,9 @@ int map_sound_effects(const char* filename) if (RCT2_GLOBAL(RCT2_ADDRESS_SOUND_EFFECTS_MAPPING, LPVOID)) { return 0; } else { - RCT2_GLOBAL(RCT2_ADDRESS_SOUND_EFFECTS_MAPPING, LPVOID) = map_file(filename, 0, 0); + wchar_t *wcFilename = utf8_to_widechar(filename); + RCT2_GLOBAL(RCT2_ADDRESS_SOUND_EFFECTS_MAPPING, LPVOID) = map_file(wcFilename, 0, 0); + free(wcFilename); return RCT2_GLOBAL(RCT2_ADDRESS_SOUND_EFFECTS_MAPPING, LPVOID) != 0; } } @@ -1296,7 +1298,7 @@ MMRESULT mmio_seek(HMMIO* hmmio, LPMMCKINFO mmckinfo1, LPMMCKINFO mmckinfo2, int * * rct2: 0x004067F9 */ -LPVOID map_file(LPCSTR lpFileName, DWORD dwCreationDisposition, DWORD dwNumberOfBytesToMap) +LPVOID map_file(LPCWSTR lpFileName, DWORD dwCreationDisposition, DWORD dwNumberOfBytesToMap) { DWORD dwDesiredAccess; DWORD dwDesiredAccessmap; @@ -1317,9 +1319,9 @@ LPVOID map_file(LPCSTR lpFileName, DWORD dwCreationDisposition, DWORD dwNumberOf dwDesiredAccessmap = FILE_MAP_READ; dwCreationDisposition = OPEN_EXISTING; } - filehandle = CreateFileA(lpFileName, dwDesiredAccess, 0, 0, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, 0); + filehandle = CreateFileW(lpFileName, dwDesiredAccess, 0, 0, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, 0); if (filehandle != INVALID_HANDLE_VALUE) { - filemaphandle = CreateFileMappingA(filehandle, 0, flProtect, 0, dwNumberOfBytesToMap, 0); + filemaphandle = CreateFileMappingW(filehandle, 0, flProtect, 0, dwNumberOfBytesToMap, 0); CloseHandle(filehandle); if (filemaphandle) { address = MapViewOfFile(filemaphandle, dwDesiredAccessmap, 0, 0, dwNumberOfBytesToMap); @@ -1750,11 +1752,11 @@ void audio_init1() for(int m = 0; m < countof(ride_music_info_list); m++) { rct_ride_music_info* ride_music_info = ride_music_info_list[m]; - const char* path = get_file_path(ride_music_info->pathid); - FILE *file = fopen(path, "rb"); + const utf8* path = get_file_path(ride_music_info->pathid); + SDL_RWops *file = SDL_RWFromFile(path, "rb"); if (file != NULL) { - fread(&RCT2_GLOBAL(0x009AF47E, uint32), 4, 1, file); - fclose(file); + SDL_RWread(file, &RCT2_GLOBAL(0x009AF47E, uint32), 4, 1); + SDL_RWclose(file); RCT2_GLOBAL(0x014241BC, uint32) = 0; if (RCT2_GLOBAL(0x009AF47E, uint32) == 0x78787878) { ride_music_info->length = 0; diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index 9a3d2c6da5..909347c73b 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -90,7 +90,7 @@ bool Source_Sample::LoadWAV(const char* filename) log_verbose("Source_Sample::LoadWAV(%s)", filename); Unload(); - SDL_RWops* rw = platform_sdl_rwfromfile(filename, "rb"); + SDL_RWops* rw = SDL_RWFromFile(filename, "rb"); if (rw == NULL) { log_verbose("Error loading %s", filename); return false; @@ -114,12 +114,12 @@ bool Source_Sample::LoadWAV(const char* filename) return true; } -bool Source_Sample::LoadCSS1(const char* filename, unsigned int offset) +bool Source_Sample::LoadCSS1(const char *filename, unsigned int offset) { log_verbose("Source_Sample::LoadCSS1(%s, %d)", filename, offset); Unload(); - SDL_RWops* rw = platform_sdl_rwfromfile(filename, "rb"); + SDL_RWops* rw = SDL_RWFromFile(filename, "rb"); if (rw == NULL) { log_verbose("Unable to load %s", filename); return false; @@ -851,9 +851,9 @@ void* Mixer_Play_Music(int pathid, int loop, int streaming) return 0; } if (streaming) { - const char* filename = get_file_path(pathid); + const utf8 *filename = get_file_path(pathid); - SDL_RWops* rw = platform_sdl_rwfromfile(filename, "rb"); + SDL_RWops* rw = SDL_RWFromFile(filename, "rb"); if (rw == NULL) { return 0; } diff --git a/src/cmdline_sprite.c b/src/cmdline_sprite.c index 3d1c8497dd..af9a9ea022 100644 --- a/src/cmdline_sprite.c +++ b/src/cmdline_sprite.c @@ -49,16 +49,16 @@ void sprite_entries_make_relative() spriteFileEntries[i].offset -= (int)spriteFileData; } -bool sprite_file_open(const char *path) +bool sprite_file_open(const utf8 *path) { - FILE *file; + SDL_RWops *file; - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) return false; - if (fread(&spriteFileHeader, sizeof(rct_sprite_file_header), 1, file) != 1) { - fclose(file); + if (SDL_RWread(file, &spriteFileHeader, sizeof(rct_sprite_file_header), 1) != 1) { + SDL_RWclose(file); return false; } @@ -66,32 +66,32 @@ bool sprite_file_open(const char *path) int entryTableSize = spriteFileHeader.num_entries * sizeof(rct_g1_element); spriteFileEntries = malloc(entryTableSize); - if (fread(spriteFileEntries, entryTableSize, 1, file) != 1) { - fclose(file); + if (SDL_RWread(file, spriteFileEntries, entryTableSize, 1) != 1) { + SDL_RWclose(file); return false; } spriteFileData = malloc(spriteFileHeader.total_size); - if (fread(spriteFileData, spriteFileHeader.total_size, 1, file) != 1) { - fclose(file); + if (SDL_RWread(file, spriteFileData, spriteFileHeader.total_size, 1) != 1) { + SDL_RWclose(file); return false; } sprite_entries_make_absolute(); } - fclose(file); + SDL_RWclose(file); return true; } bool sprite_file_save(const char *path) { - FILE *file = fopen(path, "wb"); + SDL_RWops *file = SDL_RWFromFile(path, "wb"); if (file == NULL) return false; - if (fwrite(&spriteFileHeader, sizeof(rct_sprite_file_header), 1, file) != 1) { - fclose(file); + if (SDL_RWwrite(file, &spriteFileHeader, sizeof(rct_sprite_file_header), 1) != 1) { + SDL_RWclose(file); return false; } @@ -100,21 +100,21 @@ bool sprite_file_save(const char *path) int entryTableSize = spriteFileHeader.num_entries * sizeof(rct_g1_element); - if (fwrite(spriteFileEntries, entryTableSize, 1, file) != 1) { + if (SDL_RWwrite(file, spriteFileEntries, entryTableSize, 1) != 1) { sprite_entries_make_absolute(); - fclose(file); + SDL_RWclose(file); return false; } else { sprite_entries_make_absolute(); } - if (fwrite(spriteFileData, spriteFileHeader.total_size, 1, file) != 1) { - fclose(file); + if (SDL_RWwrite(file, spriteFileData, spriteFileHeader.total_size, 1) != 1) { + SDL_RWclose(file); return false; } } - fclose(file); + SDL_RWclose(file); return true; } @@ -591,7 +591,7 @@ int cmdline_for_sprite(const char **argv, int argc) bool silent = (argc >= 4 && strcmp(argv[3], "silent") == 0); bool fileExists = true; - FILE *file; + SDL_RWops *file; spriteFileHeader.num_entries = 0; spriteFileHeader.total_size = 0; @@ -606,9 +606,9 @@ int cmdline_for_sprite(const char **argv, int argc) imagePath[resourceLength - 1] = 0; sprintf(imagePath, "%s%c%d.png", imagePath, platform_get_path_separator(), i); - file = fopen(imagePath, "r"); + file = SDL_RWFromFile(imagePath, "r"); if (file != NULL) { - fclose(file); + SDL_RWclose(file); rct_g1_element spriteElement; uint8 *buffer; int bufferLength; diff --git a/src/config.c b/src/config.c index 0a95bb5679..7194ebf11e 100644 --- a/src/config.c +++ b/src/config.c @@ -262,16 +262,39 @@ title_sequences_configuration gConfigTitleSequences; bool config_open(const utf8string path); bool config_save(const utf8string path); static void config_read_properties(config_section_definition **currentSection, const_utf8string line); -static void config_save_property_value(FILE *file, uint8 type, value_union *value); +static void config_save_property_value(SDL_RWops *file, uint8 type, value_union *value); static bool config_read_enum(void *dest, int destSize, const utf8 *key, int keySize, config_enum_definition *enumDefinitions); -static void config_write_enum(FILE *file, uint8 type, value_union *value, config_enum_definition *enumDefinitions); +static void config_write_enum(SDL_RWops *file, uint8 type, value_union *value, config_enum_definition *enumDefinitions); -static int utf8_read(utf8 **outch); static void utf8_skip_whitespace(utf8 **outch); static void utf8_skip_non_whitespace(utf8 **outch); void config_apply_to_old_addresses(); +static int rwopsreadc(SDL_RWops *file) +{ + int c = 0; + if (SDL_RWread(file, &c, 1, 1) != 1) + c = EOF; + return c; +} + +static void rwopswritec(SDL_RWops *file, char c) +{ + SDL_RWwrite(file, &c, 1, 1); +} + +static void rwopsprintf(SDL_RWops *file, const char *format, ...) +{ + va_list args; + va_start(args, format); + + char buffer[64]; + vsprintf(buffer, format, args); + + va_end(args); +} + void config_set_defaults() { int i, j; @@ -344,14 +367,14 @@ bool config_save_default() bool config_open(const utf8string path) { - FILE *file; + SDL_RWops *file; uint8 *lineBuffer; size_t lineBufferCapacity; size_t lineLength; int c; config_section_definition *currentSection; - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) return false; @@ -361,11 +384,11 @@ bool config_open(const utf8string path) lineLength = 0; // Skim UTF-8 byte order mark - fread(lineBuffer, 3, 1, file); + SDL_RWread(file, lineBuffer, 3, 1); if (!utf8_is_bom(lineBuffer)) - fseek(file, 0, SEEK_SET); + SDL_RWseek(file, 0, RW_SEEK_SET); - while ((c = fgetc(file)) != EOF) { + while ((c = rwopsreadc(file)) != EOF) { if (c == '\n' || c == '\r') { lineBuffer[lineLength++] = 0; config_read_properties(¤tSection, (const_utf8string)lineBuffer); @@ -386,17 +409,17 @@ bool config_open(const utf8string path) } free(lineBuffer); - fclose(file); + SDL_RWclose(file); return true; } bool config_save(const utf8string path) { - FILE *file; + SDL_RWops *file; int i, j; value_union *value; - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { log_error("Unable to write to config file."); return false; @@ -405,67 +428,67 @@ bool config_save(const utf8string path) for (i = 0; i < countof(_sectionDefinitions); i++) { config_section_definition *section = &_sectionDefinitions[i]; - fputc('[', file); - fwrite(section->section_name, strlen(section->section_name), 1, file); - fputc(']', file); - fputc('\n', file); + rwopswritec(file, '['); + SDL_RWwrite(file, section->section_name, strlen(section->section_name), 1); + rwopswritec(file, ']'); + rwopswritec(file, '\n'); for (j = 0; j < section->property_definitions_count; j++) { config_property_definition *property = §ion->property_definitions[j]; - fwrite(property->property_name, strlen(property->property_name), 1, file); - fwrite(" = ", 3, 1, file); + SDL_RWwrite(file, property->property_name, strlen(property->property_name), 1); + SDL_RWwrite(file, " = ", 3, 1); value = (value_union*)((size_t)section->base_address + (size_t)property->offset); if (property->enum_definitions != NULL) config_write_enum(file, property->type, value, property->enum_definitions); else config_save_property_value(file, property->type, value); - fputc('\n', file); + rwopswritec(file, '\n'); } - fputc('\n', file); + rwopswritec(file, '\n'); } - fclose(file); + SDL_RWclose(file); return true; } -static void config_save_property_value(FILE *file, uint8 type, value_union *value) +static void config_save_property_value(SDL_RWops *file, uint8 type, value_union *value) { switch (type) { case CONFIG_VALUE_TYPE_BOOLEAN: - if (value->value_boolean) fwrite("true", 4, 1, file); - else fwrite("false", 5, 1, file); + if (value->value_boolean) SDL_RWwrite(file, "true", 4, 1); + else SDL_RWwrite(file, "false", 5, 1); break; case CONFIG_VALUE_TYPE_UINT8: - fprintf(file, "%u", value->value_uint8); + rwopsprintf(file, "%u", value->value_uint8); break; case CONFIG_VALUE_TYPE_UINT16: - fprintf(file, "%u", value->value_uint16); + rwopsprintf(file, "%u", value->value_uint16); break; case CONFIG_VALUE_TYPE_UINT32: - fprintf(file, "%u", value->value_uint32); + rwopsprintf(file, "%u", value->value_uint32); break; case CONFIG_VALUE_TYPE_SINT8: - fprintf(file, "%d", value->value_sint8); + rwopsprintf(file, "%d", value->value_sint8); break; case CONFIG_VALUE_TYPE_SINT16: - fprintf(file, "%d", value->value_sint16); + rwopsprintf(file, "%d", value->value_sint16); break; case CONFIG_VALUE_TYPE_SINT32: - fprintf(file, "%d", value->value_sint32); + rwopsprintf(file, "%d", value->value_sint32); break; case CONFIG_VALUE_TYPE_FLOAT: - fprintf(file, "%.3f", value->value_float); + rwopsprintf(file, "%.3f", value->value_float); break; case CONFIG_VALUE_TYPE_DOUBLE: - fprintf(file, "%.6f", value->value_double); + rwopsprintf(file, "%.6f", value->value_double); break; case CONFIG_VALUE_TYPE_STRING: - fputc('"', file); + rwopswritec(file, '"'); if (value->value_string != NULL) - fwrite(value->value_string, strlen(value->value_string), 1, file); - fputc('"', file); + SDL_RWwrite(file, value->value_string, strlen(value->value_string), 1); + rwopswritec(file, '"'); break; } } @@ -480,8 +503,7 @@ bool config_get_section(const utf8string line, const utf8 **sectionName, int *se if (*ch != '[') return false; *sectionName = ++ch; - while (*ch != 0) { - c = utf8_read(&ch); + while ((c = utf8_get_next(ch, &ch)) != 0) { if (c == '#') return false; if (c == '[') return false; if (c == ' ') break; @@ -504,8 +526,7 @@ bool config_get_property_name_value(const utf8string line, utf8 **propertyName, if (*ch == 0) return false; *propertyName = ch; - while (*ch != 0) { - c = utf8_read(&ch); + while ((c = utf8_get_next(ch, &ch)) != 0) { if (isspace(c) || c == '=') { *propertyNameSize = ch - *propertyName - 1; break; @@ -529,14 +550,13 @@ bool config_get_property_name_value(const utf8string line, utf8 **propertyName, } *value = ch; - while (*ch != 0) { - c = utf8_read(&ch); + while ((c = utf8_get_next(ch, &ch)) != 0) { if (isspace(c) || c == '#') { if (!quotes) break; } lastC = c; } - *valueSize = ch - *value; + *valueSize = ch - *value - 1; if (quotes) (*valueSize)--; return true; } @@ -654,12 +674,12 @@ static bool config_read_enum(void *dest, int destSize, const utf8 *key, int keyS return false; } -static void config_write_enum(FILE *file, uint8 type, value_union *value, config_enum_definition *enumDefinitions) +static void config_write_enum(SDL_RWops *file, uint8 type, value_union *value, config_enum_definition *enumDefinitions) { uint32 enumValue = (value->value_uint32) & ((1 << (_configValueTypeSize[type] * 8)) - 1); while (enumDefinitions->key != NULL) { if (enumDefinitions->value.value_uint32 == enumValue) { - fwrite(enumDefinitions->key, strlen(enumDefinitions->key), 1, file); + SDL_RWwrite(file, enumDefinitions->key, strlen(enumDefinitions->key), 1); return; } enumDefinitions++; @@ -692,7 +712,7 @@ static void utf8_skip_whitespace(utf8 **outch) utf8 *ch; while (**outch != 0) { ch = *outch; - if (!isspace(utf8_read(outch))) { + if (!isspace(utf8_get_next(*outch, outch))) { *outch = ch; break; } @@ -702,7 +722,7 @@ static void utf8_skip_whitespace(utf8 **outch) static void utf8_skip_non_whitespace(utf8 **outch) { while (**outch != 0) { - if (isspace(utf8_read(outch))) + if (isspace(utf8_get_next(*outch, outch))) break; } } @@ -988,7 +1008,7 @@ void config_reset_shortcut_keys() memcpy(gShortcutKeys, _defaultShortcutKeys, sizeof(gShortcutKeys)); } -void config_shortcut_keys_get_path(char *outPath) +void config_shortcut_keys_get_path(utf8 *outPath) { platform_get_user_directory(outPath, NULL); strcat(outPath, "hotkeys.cfg"); @@ -996,22 +1016,22 @@ void config_shortcut_keys_get_path(char *outPath) bool config_shortcut_keys_load() { - char path[MAX_PATH]; - FILE *file; + utf8 path[MAX_PATH]; + SDL_RWops *file; bool result; uint16 version; config_shortcut_keys_get_path(path); - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file != NULL) { - result = fread(&version, sizeof(version), 1, file) == 1; + result = SDL_RWread(file, &version, sizeof(version), 1) == 1; if (result && version == SHORTCUT_FILE_VERSION) { - result = fread(gShortcutKeys, sizeof(gShortcutKeys), 1, file) == 1; + result = SDL_RWread(file, gShortcutKeys, sizeof(gShortcutKeys), 1) == 1; } else { result = false; } - fclose(file); + SDL_RWclose(file); } else { result = false; } @@ -1023,19 +1043,19 @@ bool config_shortcut_keys_save() { const uint16 version = SHORTCUT_FILE_VERSION; - char path[MAX_PATH]; - FILE *file; + utf8 path[MAX_PATH]; + SDL_RWops *file; bool result; config_shortcut_keys_get_path(path); - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file != NULL) { - result = fwrite(&version, sizeof(version), 1, file) == 1; + result = SDL_RWwrite(file, &version, sizeof(version), 1) == 1; if (result) { - result = fwrite(gShortcutKeys, sizeof(gShortcutKeys), 1, file) == 1; + result = SDL_RWwrite(file, gShortcutKeys, sizeof(gShortcutKeys), 1) == 1; } - fclose(file); + SDL_RWclose(file); } else { result = false; } @@ -1190,14 +1210,14 @@ bool themes_save_preset(int preset) bool themes_open(const_utf8string path) { - FILE *file; + SDL_RWops *file; uint8 *lineBuffer; size_t lineBufferCapacity; size_t lineLength; int c, preset; theme_section_definition *currentSection; - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) return false; @@ -1229,11 +1249,11 @@ bool themes_open(const_utf8string path) lineLength = 0; // Skim UTF-8 byte order mark - fread(lineBuffer, 3, 1, file); + SDL_RWread(file, lineBuffer, 3, 1); if (!(lineBuffer[0] == 0xEF && lineBuffer[1] == 0xBB && lineBuffer[2] == 0xBF)) - fseek(file, 0, SEEK_SET); + SDL_RWseek(file, 0, SEEK_SET); - while ((c = fgetc(file)) != EOF) { + while ((c = rwopsreadc(file)) != EOF) { if (c == '\n' || c == '\r') { lineBuffer[lineLength++] = 0; themes_read_properties(&gConfigThemes.presets[preset], ¤tSection, (utf8string)lineBuffer); @@ -1255,17 +1275,17 @@ bool themes_open(const_utf8string path) } free(lineBuffer); - fclose(file); + SDL_RWclose(file); return true; } static bool themes_save(const_utf8string path, int preset) { - FILE *file; + SDL_RWops *file; int i, j; value_union *value; - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { log_error("Unable to write to theme file."); return false; @@ -1275,16 +1295,16 @@ static bool themes_save(const_utf8string path, int preset) for (i = 1; i < countof(_themeSectionDefinitions); i++) { theme_section_definition *section = &_themeSectionDefinitions[i]; - fputc('[', file); - fwrite(section->section_name, strlen(section->section_name), 1, file); - fputc(']', file); - fputc('\n', file); + rwopswritec(file, '['); + SDL_RWwrite(file, section->section_name, strlen(section->section_name), 1); + rwopswritec(file, ']'); + rwopswritec(file, '\n'); for (j = 0; j < section->property_definitions_count; j++) { theme_property_definition *property = §ion->property_definitions[j]; - fwrite(property->property_name, strlen(property->property_name), 1, file); - fwrite(" = ", 3, 1, file); + SDL_RWwrite(file, property->property_name, strlen(property->property_name), 1); + SDL_RWwrite(file, " = ", 3, 1); value = (value_union*)((size_t)&gConfigThemes.presets[preset] + (size_t)section->offset + (size_t)property->offset); @@ -1292,24 +1312,24 @@ static bool themes_save(const_utf8string path, int preset) config_write_enum(file, property->type, value, property->enum_definitions); else config_save_property_value(file, property->type, value); - fputc('\n', file); - } - fputc('\n', file); + rwopswritec(file, '\n'); } + rwopswritec(file, '\n'); + } for (i = 0; i < (int)gNumThemeWindows; i++) { theme_section_definition *section = &_themeSectionDefinitions[0]; - fputc('[', file); - fwrite(gThemeWindowDefinitions[i].section_name, strlen(gThemeWindowDefinitions[i].section_name), 1, file); - fputc(']', file); - fputc('\n', file); + rwopswritec(file, '['); + SDL_RWwrite(file, gThemeWindowDefinitions[i].section_name, strlen(gThemeWindowDefinitions[i].section_name), 1); + rwopswritec(file, ']'); + rwopswritec(file, '\n'); for (j = 0; j < section->property_definitions_count; j++) { theme_property_definition *property = §ion->property_definitions[j]; - fwrite(property->property_name, strlen(property->property_name), 1, file); - fwrite(" = ", 3, 1, file); + SDL_RWwrite(file, property->property_name, strlen(property->property_name), 1); + SDL_RWwrite(file, " = ", 3, 1); value = (value_union*)((size_t)gConfigThemes.presets[preset].windows + (size_t)(sizeof(theme_window) * i) + (size_t)property->offset); @@ -1317,11 +1337,11 @@ static bool themes_save(const_utf8string path, int preset) config_write_enum(file, property->type, value, property->enum_definitions); else config_save_property_value(file, property->type, value); - fputc('\n', file); - } + rwopswritec(file, '\n'); + } } - fclose(file); + SDL_RWclose(file); return true; } @@ -1336,8 +1356,7 @@ static void themes_read_properties(theme_preset *theme, theme_section_definition int sectionNameSize; if (config_get_section(ch, §ionName, §ionNameSize)) *currentSection = themes_get_section_def((utf8string)sectionName, sectionNameSize); - } - else { + } else { if (*currentSection != NULL) { utf8 *propertyName, *value; int propertyNameSize, valueSize; @@ -1500,7 +1519,7 @@ static void title_sequence_open(const char *path, const char *customName) { utf8 titlePath[MAX_PATH], scriptPath[MAX_PATH]; file_info fileInfo; - FILE *file; + SDL_RWops *file; int fileEnumHandle, i, preset; char parts[3 * 128], *token, *part1, *part2; char separator = platform_get_path_separator(); @@ -1571,8 +1590,8 @@ static void title_sequence_open(const char *path, const char *customName) platform_enumerate_files_end(fileEnumHandle); // Load the script file - file = fopen(scriptPath, "r"); - + file = SDL_RWFromFile(scriptPath, "r"); + sint64 fileSize = SDL_RWsize(file); do { title_script_get_line(file, parts); @@ -1619,14 +1638,14 @@ static void title_sequence_open(const char *path, const char *customName) gConfigTitleSequences.presets[preset].commands = realloc(gConfigTitleSequences.presets[preset].commands, sizeof(title_command) * (size_t)gConfigTitleSequences.presets[preset].num_commands); gConfigTitleSequences.presets[preset].commands[gConfigTitleSequences.presets[preset].num_commands - 1] = command; } - } while (!feof(file)); - fclose(file); + } while (SDL_RWtell(file) < fileSize); + SDL_RWclose(file); } void title_sequence_save_preset_script(int preset) { utf8 path[MAX_PATH]; - FILE *file; + SDL_RWops *file; int i; char separator = platform_get_path_separator(); @@ -1636,7 +1655,7 @@ void title_sequence_save_preset_script(int preset) strncat(path, &separator, 1); strcat(path, "script.txt"); - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { log_error("Unable to write to script file."); return; @@ -1647,36 +1666,36 @@ void title_sequence_save_preset_script(int preset) switch (command->command) { case TITLE_SCRIPT_LOAD: if (command->saveIndex == 0xFF) - fprintf(file, "LOAD \r\n"); + rwopsprintf(file, "LOAD \r\n"); else - fprintf(file, "LOAD %s\r\n", gConfigTitleSequences.presets[preset].saves[command->saveIndex]); + rwopsprintf(file, "LOAD %s\r\n", gConfigTitleSequences.presets[preset].saves[command->saveIndex]); break; case TITLE_SCRIPT_LOCATION: - fprintf(file, "LOCATION %i %i\r\n", command->x, command->y); + rwopsprintf(file, "LOCATION %i %i\r\n", command->x, command->y); break; case TITLE_SCRIPT_ROTATE: - fprintf(file, "ROTATE %i\r\n", command->rotations); + rwopsprintf(file, "ROTATE %i\r\n", command->rotations); break; case TITLE_SCRIPT_ZOOM: - fprintf(file, "ZOOM %i\r\n", command->zoom); + rwopsprintf(file, "ZOOM %i\r\n", command->zoom); break; case TITLE_SCRIPT_SPEED: - fprintf(file, "SPEED %i\r\n", command->speed); + rwopsprintf(file, "SPEED %i\r\n", command->speed); break; case TITLE_SCRIPT_WAIT: - fprintf(file, "WAIT %i\r\n\r\n", command->seconds); + rwopsprintf(file, "WAIT %i\r\n\r\n", command->seconds); break; case TITLE_SCRIPT_RESTART: - fprintf(file, "RESTART\r\n"); + rwopsprintf(file, "RESTART\r\n"); break; case TITLE_SCRIPT_END: - fprintf(file, "END\r\n"); + rwopsprintf(file, "END\r\n"); break; } } - fclose(file); + SDL_RWclose(file); } diff --git a/src/drawing/sprite.c b/src/drawing/sprite.c index 8fee10a0c7..c343ee1ebf 100644 --- a/src/drawing/sprite.c +++ b/src/drawing/sprite.c @@ -39,25 +39,25 @@ int gfx_load_g1() { log_verbose("loading g1 graphics"); - FILE *file; + SDL_RWops *file; rct_g1_header header; unsigned int i; - file = fopen(get_file_path(PATH_ID_G1), "rb"); + file = SDL_RWFromFile(get_file_path(PATH_ID_G1), "rb"); if (file != NULL) { - if (fread(&header, 8, 1, file) == 1) { + if (SDL_RWread(file, &header, 8, 1) == 1) { // number of elements is stored in g1.dat, but because the entry headers are static, this can't be variable until // made into a dynamic array header.num_entries = 29294; // Read element headers - fread(g1Elements, header.num_entries * sizeof(rct_g1_element), 1, file); + SDL_RWread(file, g1Elements, header.num_entries * sizeof(rct_g1_element), 1); // Read element data _g1Buffer = rct2_malloc(header.total_size); - fread(_g1Buffer, header.total_size, 1, file); + SDL_RWread(file, _g1Buffer, header.total_size, 1); - fclose(file); + SDL_RWclose(file); // Fix entry data offsets for (i = 0; i < header.num_entries; i++) @@ -66,7 +66,7 @@ int gfx_load_g1() // Successful return 1; } - fclose(file); + SDL_RWclose(file); } // Unsuccessful @@ -78,23 +78,23 @@ int gfx_load_g2() { log_verbose("loading g2 graphics"); - FILE *file; + SDL_RWops *file; unsigned int i; char path[MAX_PATH]; sprintf(path, "%s%cdata%cg2.dat", gExePath, platform_get_path_separator(), platform_get_path_separator()); - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file != NULL) { - if (fread(&g2.header, 8, 1, file) == 1) { + if (SDL_RWread(file, &g2.header, 8, 1) == 1) { // Read element headers g2.elements = malloc(g2.header.num_entries * sizeof(rct_g1_element)); - fread(g2.elements, g2.header.num_entries * sizeof(rct_g1_element), 1, file); + SDL_RWread(file, g2.elements, g2.header.num_entries * sizeof(rct_g1_element), 1); // Read element data g2.data = malloc(g2.header.total_size); - fread(g2.data, g2.header.total_size, 1, file); + SDL_RWread(file, g2.data, g2.header.total_size, 1); - fclose(file); + SDL_RWclose(file); // Fix entry data offsets for (i = 0; i < g2.header.num_entries; i++) @@ -103,7 +103,7 @@ int gfx_load_g2() // Successful return 1; } - fclose(file); + SDL_RWclose(file); } // Unsuccessful diff --git a/src/editor.c b/src/editor.c index d13386693c..e71753072d 100644 --- a/src/editor.c +++ b/src/editor.c @@ -330,7 +330,7 @@ static int editor_read_s6(const char *path) log_verbose("loading landscape, %s", path); - rw = platform_sdl_rwfromfile(path, "rb"); + rw = SDL_RWFromFile(path, "rb"); if (rw != NULL) { if (!sawyercoding_validate_checksum(rw)) { SDL_RWclose(rw); diff --git a/src/game.c b/src/game.c index ba6c818676..265b34d0eb 100644 --- a/src/game.c +++ b/src/game.c @@ -853,7 +853,7 @@ int game_load_save(const char *path) strcpy(gScenarioSaveName, path_get_filename(path)); path_remove_extension(gScenarioSaveName); - SDL_RWops* rw = platform_sdl_rwfromfile(path, "rb"); + SDL_RWops* rw = SDL_RWFromFile(path, "rb"); if (rw == NULL) { log_error("unable to open %s", path); RCT2_GLOBAL(RCT2_ADDRESS_ERROR_TYPE, uint8) = 255; @@ -1006,7 +1006,7 @@ void save_game() strcat(path, gScenarioSaveName); strcat(path, ".sv6"); - SDL_RWops* rw = platform_sdl_rwfromfile(path, "wb+"); + SDL_RWops* rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { scenario_save(rw, 0x80000000); log_verbose("Saved to %s", gScenarioSaveName); @@ -1038,7 +1038,7 @@ void game_autosave() platform_file_copy(path, backupPath, true); } - SDL_RWops* rw = platform_sdl_rwfromfile(path, "wb+"); + SDL_RWops* rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { scenario_save(rw, 0x80000000); SDL_RWclose(rw); diff --git a/src/interface/screenshot.c b/src/interface/screenshot.c index 14a9d83b72..f5f3c7201b 100644 --- a/src/interface/screenshot.c +++ b/src/interface/screenshot.c @@ -143,7 +143,7 @@ int screenshot_dump_bmp() int i, y, index, width, height, stride; char *buffer, path[MAX_PATH], *row; - FILE *fp; + SDL_RWops *fp; unsigned int bytesWritten; // Get a free screenshot path @@ -151,15 +151,14 @@ int screenshot_dump_bmp() return -1; // Open binary file for writing - if ((fp = fopen(path, "wb")) == NULL){ + if ((fp = SDL_RWFromFile(path, "wb")) == NULL){ return -1; } // Allocate buffer buffer = malloc(0xFFFF); if (buffer == NULL) { - //CloseHandle(hFile); - fclose(fp); + SDL_RWclose(fp); return -1; } @@ -174,9 +173,9 @@ int screenshot_dump_bmp() header.bfSize = height * stride + 1038; header.bfOffBits = 1038; - bytesWritten = fwrite(&header, sizeof(BitmapFileHeader), 1, fp); + bytesWritten = SDL_RWwrite(fp, &header, sizeof(BitmapFileHeader), 1); if (bytesWritten != 1) { - fclose(fp); + SDL_RWclose(fp); free(buffer); } @@ -191,9 +190,9 @@ int screenshot_dump_bmp() info.biYPelsPerMeter = 2520; info.biClrUsed = 246; - bytesWritten=fwrite(&info, sizeof(BitmapInfoHeader), 1, fp); + bytesWritten = SDL_RWwrite(fp, &info, sizeof(BitmapInfoHeader), 1); if (bytesWritten != 1) { - fclose(fp); + SDL_RWclose(fp); free(buffer); } @@ -205,9 +204,9 @@ int screenshot_dump_bmp() buffer[i * 4 + 2] = RCT2_ADDRESS(0x01424680, uint8)[i * 4 + 2]; } - bytesWritten = fwrite(buffer, sizeof(char), 246*4, fp); + bytesWritten = SDL_RWwrite(fp, buffer, sizeof(char), 246 * 4); if (bytesWritten != 246*4){ - fclose(fp); + SDL_RWclose(fp); free(buffer); } @@ -219,14 +218,14 @@ int screenshot_dump_bmp() memset(buffer, 0, stride); memcpy(buffer, row, dpi->width); - bytesWritten=fwrite(buffer, sizeof(char), stride, fp); + bytesWritten = SDL_RWwrite(fp, buffer, sizeof(char), stride); if (bytesWritten != stride){ - fclose(fp); + SDL_RWclose(fp); free(buffer); } } - fclose(fp); + SDL_RWclose(fp); free(buffer); return index; @@ -289,7 +288,14 @@ int screenshot_dump_png() log_error("Unable to save screenshot, %u: %s", lodepng_error_text(error)); index = -1; } else { - lodepng_save_file(png, pngSize, path); + SDL_RWops *file = SDL_RWFromFile(path, "wb"); + if (file == NULL) { + log_error("Unable to save screenshot, %s", SDL_GetError()); + index = -1; + } else { + SDL_RWwrite(file, png, pngSize, 1); + SDL_RWclose(file); + } } free(png); @@ -327,11 +333,13 @@ bool screenshot_write_png(rct_drawpixelinfo *dpi, const char *path) free(png); return false; } else { - error = lodepng_save_file(png, pngSize, path); - if (error != 0) { + SDL_RWops *file = SDL_RWFromFile(path, "wb"); + if (file == NULL) { free(png); return false; } + SDL_RWwrite(file, png, pngSize, 1); + SDL_RWclose(file); } free(png); diff --git a/src/localisation/language.c b/src/localisation/language.c index eec31cb815..72a24b0b36 100644 --- a/src/localisation/language.c +++ b/src/localisation/language.c @@ -98,7 +98,7 @@ const utf8 BlackLeftArrowString[] = { 0xC2, 0x8E, 0xE2, 0x97, 0x80, 0x00 }; const utf8 BlackRightArrowString[] = { 0xC2, 0x8E, 0xE2, 0x96, 0xB6, 0x00 }; const utf8 CheckBoxMarkString[] = { 0xE2, 0x9C, 0x93, 0x00 }; -static int language_open_file(const char *filename, language_data *language); +static int language_open_file(const utf8 *filename, language_data *language); static void language_close(language_data *language); void utf8_remove_format_codes(utf8 *text) @@ -200,21 +200,21 @@ void language_close_all() * colon and before the new line will be saved as the string. Tokens are written with inside curly braces {TOKEN}. * Use # at the beginning of a line to leave a comment. */ -static int language_open_file(const char *filename, language_data *language) +static int language_open_file(const utf8 *filename, language_data *language) { assert(filename != NULL); assert(language != NULL); - FILE *f = fopen(filename, "rb"); + SDL_RWops *f = SDL_RWFromFile(filename, "rb"); if (f == NULL) return 0; - fseek(f, 0, SEEK_END); - language->string_data_size = ftell(f) + 1; + SDL_RWseek(f, 0, RW_SEEK_END); + language->string_data_size = (size_t)(SDL_RWtell(f) + 1); language->string_data = calloc(1, language->string_data_size); - fseek(f, 0, SEEK_SET); - fread(language->string_data, language->string_data_size, 1, f); - fclose(f); + SDL_RWseek(f, 0, RW_SEEK_SET); + SDL_RWread(f, language->string_data, language->string_data_size, 1); + SDL_RWclose(f); language->strings = calloc(STR_COUNT, sizeof(char*)); diff --git a/src/object.c b/src/object.c index 2d44166568..4fcfadef1a 100644 --- a/src/object.c +++ b/src/object.c @@ -33,20 +33,20 @@ #include "scenario.h" #include "rct1.h" -int object_load_entry(const char *path, rct_object_entry *outEntry) +int object_load_entry(const utf8 *path, rct_object_entry *outEntry) { - FILE *file; + SDL_RWops *file; - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) return 0; - if (fread(outEntry, sizeof(rct_object_entry), 1, file) != 1) { - fclose(file); + if (SDL_RWread(file, outEntry, sizeof(rct_object_entry), 1) != 1) { + SDL_RWclose(file); return 0; } - fclose(file); + SDL_RWclose(file); return 1; } @@ -61,7 +61,7 @@ int object_load_file(int groupIndex, const rct_object_entry *entry, int* chunkSi log_verbose("loading object, %s", path); - rw = platform_sdl_rwfromfile(path, "rb"); + rw = SDL_RWFromFile(path, "rb"); if (rw == NULL) return 0; @@ -315,7 +315,7 @@ int object_load_packed(SDL_RWops* rw) } // Actually write the object to the file - SDL_RWops* rw_out = platform_sdl_rwfromfile(path, "wb"); + SDL_RWops* rw_out = SDL_RWFromFile(path, "wb"); if (rw_out != NULL){ uint8 result = write_object_file(rw_out, &entry); @@ -1514,7 +1514,7 @@ int object_get_scenario_text(rct_object_entry *entry) subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), objectPath); rct_object_entry openedEntry; - SDL_RWops* rw = platform_sdl_rwfromfile(path, "rb"); + SDL_RWops* rw = SDL_RWFromFile(path, "rb"); if (rw != NULL) { SDL_RWread(rw, &openedEntry, sizeof(rct_object_entry), 1); if (object_entry_compare(&openedEntry, entry)) { diff --git a/src/object.h b/src/object.h index 0294c98865..ad7d7c66bd 100644 --- a/src/object.h +++ b/src/object.h @@ -92,7 +92,7 @@ typedef struct { extern rct_object_entry_group object_entry_groups[]; -int object_load_entry(const char *path, rct_object_entry *outEntry); +int object_load_entry(const utf8 *path, rct_object_entry *outEntry); void object_list_load(); void set_load_objects_fail_reason(); int object_read_and_load_entries(SDL_RWops* rw); diff --git a/src/object_list.c b/src/object_list.c index 12409ecb0a..1e96973ee5 100644 --- a/src/object_list.c +++ b/src/object_list.c @@ -94,7 +94,7 @@ static void load_object_filter(rct_object_entry* entry, uint8* chunk, rct_object static rct_object_filters *_installedObjectFilters = NULL; -static void get_plugin_path(char *outPath) +static void get_plugin_path(utf8 *outPath) { platform_get_user_directory(outPath, NULL); strcat(outPath, "plugin.dat"); @@ -347,20 +347,20 @@ void object_list_load() static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int fileDateModifiedChecksum) { char path[MAX_PATH]; - FILE *file; + SDL_RWops *file; rct_plugin_header pluginHeader; uint32 filterVersion = 0; log_verbose("loading object list cache (plugin.dat)"); get_plugin_path(path); - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) { log_verbose("Unable to load %s", path); return 0; } - if (fread(&pluginHeader, sizeof(rct_plugin_header), 1, file) == 1) { + if (SDL_RWread(file, &pluginHeader, sizeof(rct_plugin_header), 1) == 1) { // Check if object repository has changed in anyway if ( pluginHeader.total_files == totalFiles && @@ -375,20 +375,20 @@ static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int file // Read installed object list RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*) = rct2_malloc(pluginHeader.object_list_size); - if (fread(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*), pluginHeader.object_list_size, 1, file) == 1) { + if (SDL_RWread(file, RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, void*), pluginHeader.object_list_size, 1) == 1) { RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32) = pluginHeader.object_list_no_items; if (pluginHeader.object_list_no_items != (pluginHeader.total_files & 0xFFFFFF)) log_error("Potential mismatch in file numbers. Possible corrupt file. Consider deleting plugin.dat."); - if (fread(&filterVersion, sizeof(filterVersion), 1, file) == 1) { + if (SDL_RWread(file, &filterVersion, sizeof(filterVersion), 1) == 1) { if (filterVersion == FILTER_VERSION) { if (_installedObjectFilters) free(_installedObjectFilters); _installedObjectFilters = malloc(sizeof(rct_object_filters) * pluginHeader.object_list_no_items); - if (fread(_installedObjectFilters, sizeof(rct_object_filters) * pluginHeader.object_list_no_items, 1, file) == 1) { + if (SDL_RWread(file, _installedObjectFilters, sizeof(rct_object_filters) * pluginHeader.object_list_no_items, 1) == 1) { - fclose(file); + SDL_RWclose(file); reset_loaded_objects(); object_list_examine(); return 1; @@ -411,11 +411,11 @@ static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int file log_info("Objects files have been updated... updating object list cache"); } - fclose(file); + SDL_RWclose(file); return 0; } - fclose(file); + SDL_RWclose(file); log_error("loading object list cache failed"); return 0; @@ -423,8 +423,8 @@ static int object_list_cache_load(int totalFiles, uint64 totalFileSize, int file static int object_list_cache_save(int fileCount, uint64 totalFileSize, int fileDateModifiedChecksum, int currentItemOffset) { - char path[MAX_PATH]; - FILE *file; + utf8 path[MAX_PATH]; + SDL_RWops *file; rct_plugin_header pluginHeader; uint32 filterVersion = FILTER_VERSION; @@ -437,17 +437,17 @@ static int object_list_cache_save(int fileCount, uint64 totalFileSize, int fileD pluginHeader.object_list_no_items = RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32); get_plugin_path(path); - file = fopen(path,"wb"); + file = SDL_RWFromFile(path,"wb"); if (file == NULL) { log_error("Failed to save %s", path); return 0; } - fwrite(&pluginHeader, sizeof(rct_plugin_header), 1, file); - fwrite(RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*), pluginHeader.object_list_size, 1, file); - fwrite(&filterVersion, sizeof(filterVersion), 1, file); - fwrite(_installedObjectFilters, sizeof(rct_object_filters) * RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32), 1, file); - fclose(file); + SDL_RWwrite(file, &pluginHeader, sizeof(rct_plugin_header), 1); + SDL_RWwrite(file, RCT2_GLOBAL(RCT2_ADDRESS_INSTALLED_OBJECT_LIST, uint8*), pluginHeader.object_list_size, 1); + SDL_RWwrite(file, &filterVersion, sizeof(filterVersion), 1); + SDL_RWwrite(file, _installedObjectFilters, sizeof(rct_object_filters) * RCT2_GLOBAL(RCT2_ADDRESS_OBJECT_LIST_NO_ITEMS, uint32), 1); + SDL_RWclose(file); return 1; } diff --git a/src/platform/platform.h b/src/platform/platform.h index 5d62464e63..55ef8a5461 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -89,7 +89,6 @@ void platform_process_messages(); int platform_scancode_to_rct_keycode(int sdl_key); void platform_start_text_input(utf8 *buffer, int max_length); void platform_stop_text_input(); -SDL_RWops* platform_sdl_rwfromfile(const utf8* filename, const char* mode); // Platform specific definitions char platform_get_path_separator(); diff --git a/src/platform/shared.c b/src/platform/shared.c index 4e6572e199..a80dffaa05 100644 --- a/src/platform/shared.c +++ b/src/platform/shared.c @@ -685,13 +685,6 @@ void platform_stop_text_input() gTextInput = NULL; } -SDL_RWops* platform_sdl_rwfromfile(const char* filename, const char* mode) -{ - utf8 utf8filename[512]; - win1252_to_utf8(utf8filename, filename, sizeof(utf8filename)); - return SDL_RWFromFile(utf8filename, mode); -} - static void platform_unload_cursors() { for (int i = 0; i < CURSOR_COUNT; i++) diff --git a/src/rct2.c b/src/rct2.c index 534ec5c337..d432f5d692 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -290,12 +290,12 @@ int check_file_paths() */ int check_file_path(int pathId) { - const char * path = get_file_path(pathId); - HANDLE file = CreateFile(path, FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL); + const utf8* path = get_file_path(pathId); + SDL_RWops *file = SDL_RWFromFile(path, "rb"); switch (pathId) { case PATH_ID_G1: - if (file == INVALID_HANDLE_VALUE) { + if (file == NULL) { // A data file is missing from the installation directory. The original implementation // asks for a CD-ROM path at this point and stores it in cdrom_path @ 0x9AA318. // The file_on_cdrom[pathId] @ 0x009AA0B flag is set to 1 as well. @@ -309,18 +309,18 @@ int check_file_path(int pathId) break; case PATH_ID_CUSTOM1: - if (file != INVALID_HANDLE_VALUE) - ride_music_info_list[36]->length = SetFilePointer(file, 0, 0, FILE_END); // Store file size in music_custom1_size @ 0x009AF164 + if (file != NULL) + ride_music_info_list[36]->length = (uint32)SDL_RWsize(file); // Store file size in music_custom1_size @ 0x009AF164 break; case PATH_ID_CUSTOM2: - if (file != INVALID_HANDLE_VALUE) - ride_music_info_list[37]->length = SetFilePointer(file, 0, 0, FILE_END); // Store file size in music_custom2_size @ 0x009AF16E + if (file != NULL) + ride_music_info_list[37]->length = (uint32)SDL_RWsize(file); // Store file size in music_custom2_size @ 0x009AF16E break; } - if (file != INVALID_HANDLE_VALUE) - CloseHandle(file); + if (file != NULL) + SDL_RWclose(file); return 1; } @@ -399,9 +399,9 @@ void rct2_endupdate() * * rct2: 0x00674E6C */ -const char *get_file_path(int pathId) +const utf8 *get_file_path(int pathId) { - static char path[MAX_PATH]; // get_file_path_buffer @ 0x009E3605 + static utf8 path[MAX_PATH]; // get_file_path_buffer @ 0x009E3605 // The original implementation checks if the file is on CD-ROM here (file_on_cdrom[pathId] @ 0x009AA0B1). // If so, the CD-ROM path (cdrom_path @ 0x9AA318) is used instead. This has been removed for now for diff --git a/src/ride/track.c b/src/ride/track.c index da08a9d92e..6994cb371d 100644 --- a/src/ride/track.c +++ b/src/ride/track.c @@ -277,33 +277,36 @@ static void track_list_query_directory(int *outTotalFiles){ platform_enumerate_files_end(enumFileHandle); } -static int track_list_cache_save(int fileCount, uint8* track_list_cache, uint32 track_list_size){ +static int track_list_cache_save(int fileCount, uint8* track_list_cache, uint32 track_list_size) +{ char path[MAX_PATH]; - FILE *file; + SDL_RWops *file; log_verbose("saving track list cache (tracks.idx)"); get_track_idx_path(path); - file = fopen(path, "wb"); + + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { log_error("Failed to save %s", path); return 0; } - fwrite(&fileCount, sizeof(int), 1, file); - fwrite(track_list_cache, track_list_size, 1, file); + SDL_RWwrite(file, &fileCount, sizeof(int), 1); + SDL_RWwrite(file, track_list_cache, track_list_size, 1); uint8 last_entry = 0xFE; - fwrite(&last_entry, 1, 1, file); - fclose(file); + SDL_RWwrite(file, &last_entry, 1, 1); + SDL_RWclose(file); return 1; } -static uint8* track_list_cache_load(int totalFiles){ +static uint8* track_list_cache_load(int totalFiles) +{ char path[MAX_PATH]; - FILE *file; + SDL_RWops *file; log_verbose("loading track list cache (tracks.idx)"); get_track_idx_path(path); - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) { log_error("Failed to load %s", path); return 0; @@ -312,11 +315,11 @@ static uint8* track_list_cache_load(int totalFiles){ uint8* track_list_cache; uint32 fileCount; // Remove 4 for the file count variable - long track_list_size = fsize(file) - 4; + long track_list_size = (long)SDL_RWsize(file) - 4; if (track_list_size < 0) return 0; - fread(&fileCount, 4, 1, file); + SDL_RWread(file, &fileCount, 4, 1); if (fileCount != totalFiles){ log_verbose("Track file count is different."); @@ -324,7 +327,7 @@ static uint8* track_list_cache_load(int totalFiles){ } track_list_cache = malloc(track_list_size); - fread(track_list_cache, track_list_size, 1, file); + SDL_RWread(file, track_list_cache, track_list_size, 1); return track_list_cache; } @@ -500,15 +503,15 @@ uint8 td4_track_has_boosters(rct_track_td6* track_design, uint8* track_elements) */ rct_track_td6* load_track_design(const char *path) { - FILE *fp; - long fpLength; + SDL_RWops *fp; + int fpLength; char *fpBuffer, *decoded, *src; int i, decodedLength; uint8* edi; RCT2_GLOBAL(0x009AAC54, uint8) = 1; - fp = fopen(path, "rb"); + fp = SDL_RWFromFile(path, "rb"); if (fp == NULL) return 0; @@ -521,10 +524,10 @@ rct_track_td6* load_track_design(const char *path) *default_name++ = '\0'; // Read whole file into a buffer - fpLength = fsize(fp); + fpLength = (int)SDL_RWsize(fp); fpBuffer = malloc(fpLength); - fread(fpBuffer, fpLength, 1, fp); - fclose(fp); + SDL_RWread(fp, fpBuffer, fpLength, 1); + SDL_RWclose(fp); // Validate the checksum // Not the same checksum algorithm as scenarios and saved games @@ -2944,25 +2947,26 @@ int ride_to_td6(uint8 rideIndex){ /* rct2: 0x006771DC but not really its branched from that * quite far. */ -int save_track_to_file(rct_track_td6* track_design, char* path){ +int save_track_to_file(rct_track_td6* track_design, char* path) +{ window_close_construction_windows(); uint8* track_file = malloc(0x8000); int length = sawyercoding_encode_td6((char*)track_design, track_file, 0x609F); - FILE *file; + SDL_RWops *file; log_verbose("saving track %s", path); - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { free(track_file); log_error("Failed to save %s", path); return 0; } - fwrite(track_file, length, 1, file); - fclose(file); + SDL_RWwrite(file, track_file, length, 1); + SDL_RWclose(file); free(track_file); return 1; diff --git a/src/scenario.c b/src/scenario.c index 5671b7cb90..82baeddefb 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -63,7 +63,7 @@ int scenario_load_basic(const char *path, rct_s6_header *header, rct_s6_info *in log_verbose("loading scenario details, %s", path); - rw = platform_sdl_rwfromfile(path, "rb"); + rw = SDL_RWFromFile(path, "rb"); if (rw != NULL) { // Read first chunk sawyercoding_read_chunk(rw, (uint8*)header); @@ -108,7 +108,7 @@ int scenario_load(const char *path) rct_s6_header *s6Header = (rct_s6_header*)0x009E34E4; rct_s6_info *s6Info = (rct_s6_info*)0x0141F570; - rw = platform_sdl_rwfromfile(path, "rb"); + rw = SDL_RWFromFile(path, "rb"); if (rw != NULL) { if (!sawyercoding_validate_checksum(rw) && !gConfigGeneral.allow_loading_with_incorrect_checksum) { SDL_RWclose(rw); diff --git a/src/scenario_list.c b/src/scenario_list.c index b924cbf0a5..adb08858e6 100644 --- a/src/scenario_list.c +++ b/src/scenario_list.c @@ -154,7 +154,7 @@ static int scenario_list_sort_compare(const void *a, const void *b) /** * Gets the path for the scenario scores path. */ -static void scenario_scores_get_path(char *outPath) +static void scenario_scores_get_path(utf8 *outPath) { platform_get_user_directory(outPath, NULL); strcat(outPath, "scores.dat"); @@ -166,7 +166,7 @@ static void scenario_scores_get_path(char *outPath) */ static int scenario_scores_load() { - FILE *file; + SDL_RWops *file; char scoresPath[MAX_PATH]; scenario_scores_get_path(scoresPath); @@ -180,9 +180,9 @@ static int scenario_scores_load() // Try and load the scores file // First check user folder and then fallback to install directory - file = fopen(scoresPath, "rb"); + file = SDL_RWFromFile(scoresPath, "rb"); if (file == NULL) { - file = fopen(get_file_path(PATH_ID_SCORES), "rb"); + file = SDL_RWFromFile(get_file_path(PATH_ID_SCORES), "rb"); if (file == NULL) { log_error("Unable to load scenario scores."); return 0; @@ -191,8 +191,8 @@ static int scenario_scores_load() // Load header rct_scenario_scores_header header; - if (fread(&header, 16, 1, file) != 1) { - fclose(file); + if (SDL_RWread(file, &header, 16, 1) != 1) { + SDL_RWclose(file); log_error("Invalid header in scenario scores file."); return 0; } @@ -202,13 +202,13 @@ static int scenario_scores_load() int scenarioListBufferSize = gScenarioListCount * sizeof(rct_scenario_basic); gScenarioListCapacity = gScenarioListCount; gScenarioList = malloc(scenarioListBufferSize); - if (fread(gScenarioList, scenarioListBufferSize, 1, file) == 1) { - fclose(file); + if (SDL_RWread(file, gScenarioList, scenarioListBufferSize, 1) == 1) { + SDL_RWclose(file); return 1; } // Unable to load scores, free scenario list - fclose(file); + SDL_RWclose(file); gScenarioListCount = 0; gScenarioListCapacity = 0; free(gScenarioList); @@ -222,12 +222,12 @@ static int scenario_scores_load() */ int scenario_scores_save() { - FILE *file; - char scoresPath[MAX_PATH]; + SDL_RWops *file; + utf8 scoresPath[MAX_PATH]; scenario_scores_get_path(scoresPath); - file = fopen(scoresPath, "wb"); + file = SDL_RWFromFile(scoresPath, "wb"); if (file == NULL) { log_error("Unable to save scenario scores."); return 0; @@ -236,10 +236,10 @@ int scenario_scores_save() rct_scenario_scores_header header; header.scenario_count = gScenarioListCount; - fwrite(&header, sizeof(header), 1, file); + SDL_RWwrite(file, &header, sizeof(header), 1); if (gScenarioListCount > 0) - fwrite(gScenarioList, gScenarioListCount * sizeof(rct_scenario_basic), 1, file); + SDL_RWwrite(file, gScenarioList, gScenarioListCount * sizeof(rct_scenario_basic), 1); - fclose(file); + SDL_RWclose(file); return 1; } \ No newline at end of file diff --git a/src/title.c b/src/title.c index 0fc0201a72..f91f5a53ea 100644 --- a/src/title.c +++ b/src/title.c @@ -170,7 +170,7 @@ static int title_load_park(const char *path) int successfulLoad; if (_strcmpi(path_get_extension(path), ".sv6") == 0) { - SDL_RWops* rw = platform_sdl_rwfromfile(path, "rb"); + SDL_RWops* rw = SDL_RWFromFile(path, "rb"); if (rw != NULL) { successfulLoad = game_load_sv6(rw); SDL_RWclose(rw); @@ -522,7 +522,7 @@ static uint8 *generate_random_script() #pragma region Load script.txt -void title_script_get_line(FILE *file, char *parts) +void title_script_get_line(SDL_RWops *file, char *parts) { int i, c, part, cindex, whitespace, comment, load; @@ -535,7 +535,10 @@ void title_script_get_line(FILE *file, char *parts) comment = 0; load = 0; for (; part < 3;) { - c = fgetc(file); + c = 0; + if (SDL_RWread(file, &c, 1, 1) != 1) + c = EOF; + if (c == '\n' || c == '\r' || c == EOF) { parts[part * 128 + cindex] = 0; return; @@ -567,15 +570,16 @@ void title_script_get_line(FILE *file, char *parts) static uint8 *title_script_load() { - FILE *file; + SDL_RWops *file; char parts[3 * 128], *token, *part1, *part2, *src; - char path[MAX_PATH]; - char filePath[] = "data/title/script.txt"; + utf8 path[MAX_PATH]; + utf8 filePath[] = "data/title/script.txt"; sprintf(path, "%s%c%s", gExePath, platform_get_path_separator(), filePath); log_verbose("loading title script, %s", path); - file = fopen(path, "r"); + file = SDL_RWFromFile(path, "r"); + sint64 fileSize = SDL_RWsize(file); if (file == NULL) { log_error("unable to load title script"); return NULL; @@ -583,7 +587,7 @@ static uint8 *title_script_load() uint8 *binaryScript = (uint8*)malloc(1024 * 8); if (binaryScript == NULL) { - fclose(file); + SDL_RWclose(file); log_error("unable to allocate memory for script"); return NULL; @@ -623,8 +627,8 @@ static uint8 *title_script_load() return NULL; } } - } while (!feof(file)); - fclose(file); + } while (SDL_RWtell(file) < fileSize); + SDL_RWclose(file); *scriptPtr++ = TITLE_SCRIPT_RESTART; diff --git a/src/title.h b/src/title.h index a07ee2e59b..234adb6fe7 100644 --- a/src/title.h +++ b/src/title.h @@ -21,6 +21,8 @@ #ifndef _TITLE_H_ #define _TITLE_H_ +#include + enum { TITLE_SCRIPT_WAIT, TITLE_SCRIPT_LOADMM, @@ -43,7 +45,7 @@ extern sint32 gTitleScriptSkipLoad; void title_load(); void title_update(); void title_skip_from_beginning(); -void title_script_get_line(FILE *file, char *parts); +void title_script_get_line(SDL_RWops *file, char *parts); bool title_refresh_sequence(); void DrawOpenRCT2(int x, int y); diff --git a/src/util/util.c b/src/util/util.c index 72420b55b2..d5e8c39b4d 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -19,6 +19,7 @@ *****************************************************************************/ #include "util.h" +#include int squaredmetres_to_squaredfeet(int squaredMetres) { @@ -41,7 +42,7 @@ int mph_to_kmph(int mph) return (mph * 1648) / 1024; } -bool filename_valid_characters(const char *filename) +bool filename_valid_characters(const utf8 *filename) { for (int i = 0; filename[i] != '\0'; i++) { if (filename[i] == '\\' || filename[i] == '/' || filename[i] == ':' || filename[i] == '?' || @@ -51,7 +52,7 @@ bool filename_valid_characters(const char *filename) return true; } -const char *path_get_filename(const char *path) +const char *path_get_filename(const utf8 *path) { const char *result, *ch; @@ -66,7 +67,7 @@ const char *path_get_filename(const char *path) return result; } -const char *path_get_extension(const char *path) +const char *path_get_extension(const utf8 *path) { const char *extension = NULL; const char *ch = path; @@ -81,7 +82,7 @@ const char *path_get_extension(const char *path) return extension; } -void path_set_extension(char *path, const char *newExtension) +void path_set_extension(utf8 *path, const utf8 *newExtension) { char *extension = NULL; char *ch = path; @@ -100,7 +101,7 @@ void path_set_extension(char *path, const char *newExtension) strcpy(extension, newExtension); } -void path_remove_extension(char *path) +void path_remove_extension(utf8 *path) { char *ch = path + strlen(path); for (--ch; ch >= path; --ch) { @@ -111,38 +112,26 @@ void path_remove_extension(char *path) } } -long fsize(FILE *fp) +bool readentirefile(const utf8 *path, void **outBuffer, int *outLength) { - long originalPosition, size; - - originalPosition = ftell(fp); - fseek(fp, 0, SEEK_END); - size = ftell(fp); - fseek(fp, originalPosition, SEEK_SET); - - return size; -} - -bool readentirefile(const char *path, void **outBuffer, long *outLength) -{ - FILE *fp; - long fpLength; + SDL_RWops *fp; + int fpLength; void *fpBuffer; // Open file - fp = fopen(path, "rb"); + fp = SDL_RWFromFile(path, "rb"); if (fp == NULL) return 0; // Get length - fseek(fp, 0, SEEK_END); - fpLength = ftell(fp); - rewind(fp); + SDL_RWseek(fp, 0, RW_SEEK_END); + fpLength = (int)SDL_RWtell(fp); + SDL_RWseek(fp, 0, RW_SEEK_SET); // Read whole file into a buffer fpBuffer = malloc(fpLength); - fread(fpBuffer, fpLength, 1, fp); - fclose(fp); + SDL_RWread(fp, fpBuffer, fpLength, 1); + SDL_RWclose(fp); *outBuffer = fpBuffer; *outLength = fpLength; @@ -190,7 +179,7 @@ int strcicmp(char const *a, char const *b) bool utf8_is_bom(const char *str) { - return str[0] == 0xEF && str[1] == 0xBB && str[2] == 0xBF; + return str[0] == (char)0xEF && str[1] == (char)0xBB && str[2] == (char)0xBF; } bool str_is_null_or_empty(const char *str) diff --git a/src/util/util.h b/src/util/util.h index 2723404a2a..c0e75feb61 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -27,14 +27,13 @@ int squaredmetres_to_squaredfeet(int squaredMetres); int metres_to_feet(int metres); int mph_to_kmph(int mph); -bool filename_valid_characters(const char *filename); +bool filename_valid_characters(const utf8 *filename); -const char *path_get_filename(const char *path); -const char *path_get_extension(const char *path); -void path_set_extension(char *path, const char *newExtension); -void path_remove_extension(char *path); -long fsize(FILE *fp); -bool readentirefile(const char *path, void **outBuffer, long *outLength); +const char *path_get_filename(const utf8 *path); +const char *path_get_extension(const utf8 *path); +void path_set_extension(utf8 *path, const utf8 *newExtension); +void path_remove_extension(utf8 *path); +bool readentirefile(const utf8 *path, void **outBuffer, int *outLength); int bitscanforward(int source); int bitcount(int source); diff --git a/src/windows/changelog.c b/src/windows/changelog.c index a10d57fffd..5cfc1147da 100644 --- a/src/windows/changelog.c +++ b/src/windows/changelog.c @@ -192,7 +192,7 @@ static void window_changelog_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, static bool window_changelog_read_file() { window_changelog_dispose_file(); - char path[MAX_PATH]; + utf8 path[MAX_PATH]; sprintf(path, "%s%cchangelog.txt", gExePath, platform_get_path_separator()); if (!readentirefile(path, &_changelogText, &_changelogTextSize)) { log_error("Unable to read changelog.txt"); diff --git a/src/windows/editor_bottom_toolbar.c b/src/windows/editor_bottom_toolbar.c index e6d7614c7e..62a55efa6d 100644 --- a/src/windows/editor_bottom_toolbar.c +++ b/src/windows/editor_bottom_toolbar.c @@ -377,7 +377,7 @@ void window_editor_bottom_toolbar_jump_forward_to_save_scenario() // Save the scenario parkFlagsBackup = RCT2_GLOBAL(RCT2_ADDRESS_PARK_FLAGS, uint32); RCT2_GLOBAL(RCT2_ADDRESS_PARK_FLAGS, uint32) &= ~PARK_FLAGS_18; - SDL_RWops* rw = platform_sdl_rwfromfile(path, "wb+"); + SDL_RWops* rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { success = scenario_save(rw, gConfigGeneral.save_plugin_data ? 3 : 2); SDL_RWclose(rw); diff --git a/src/windows/loadsave.c b/src/windows/loadsave.c index 786cb694ef..a164f8af22 100644 --- a/src/windows/loadsave.c +++ b/src/windows/loadsave.c @@ -767,7 +767,7 @@ static void window_loadsave_select(rct_window *w, const char *path) } break; case (LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME) : - rw = platform_sdl_rwfromfile(path, "wb+"); + rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { int success = scenario_save(rw, gConfigGeneral.save_plugin_data ? 1 : 0); SDL_RWclose(rw); @@ -799,7 +799,7 @@ static void window_loadsave_select(rct_window *w, const char *path) } break; case (LOADSAVETYPE_SAVE | LOADSAVETYPE_LANDSCAPE) : - rw = platform_sdl_rwfromfile(path, "wb+"); + rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { int success = scenario_save(rw, gConfigGeneral.save_plugin_data ? 3 : 2); SDL_RWclose(rw); @@ -819,7 +819,7 @@ static void window_loadsave_select(rct_window *w, const char *path) int parkFlagsBackup = RCT2_GLOBAL(RCT2_ADDRESS_PARK_FLAGS, uint32); RCT2_GLOBAL(RCT2_ADDRESS_PARK_FLAGS, uint32) &= ~PARK_FLAGS_18; s6Info->var_000 = 255; - rw = platform_sdl_rwfromfile(path, "wb+"); + rw = SDL_RWFromFile(path, "wb+"); int success = 0; if (rw != NULL) { success = scenario_save(rw, gConfigGeneral.save_plugin_data ? 3 : 2); diff --git a/src/windows/server_list.c b/src/windows/server_list.c index 113992677a..8d109d1d3a 100644 --- a/src/windows/server_list.c +++ b/src/windows/server_list.c @@ -382,7 +382,7 @@ static void server_list_update_player_name() } } -static char *freadstralloc(FILE *file) +static char *freadstralloc(SDL_RWops *file) { int capacity = 64; char *buffer = malloc(capacity); @@ -390,8 +390,8 @@ static char *freadstralloc(FILE *file) int length = 0; int c; for (;;) { - c = fgetc(file); - if (c == EOF) break; + c = 0; + if (SDL_RWread(file, &c, 1, 1) != 1) break; if (c == 0) break; if (length > capacity) { @@ -410,12 +410,12 @@ static char *freadstralloc(FILE *file) static void server_list_load_saved_servers() { utf8 path[MAX_PATH]; - FILE *file; + SDL_RWops *file; platform_get_user_directory(path, NULL); strcat(path, "servers.cfg"); - file = fopen(path, "rb"); + file = SDL_RWFromFile(path, "rb"); if (file == NULL) { return; } @@ -423,7 +423,7 @@ static void server_list_load_saved_servers() dispose_saved_server_list(); // Read number of saved servers - fread(&_numSavedServers, sizeof(uint32), 1, file); + SDL_RWread(file, &_numSavedServers, sizeof(uint32), 1); _savedServers = malloc(_numSavedServers * sizeof(saved_server)); // Load each saved server @@ -435,36 +435,36 @@ static void server_list_load_saved_servers() serverInfo->description = freadstralloc(file); } - fclose(file); + SDL_RWclose(file); } static void server_list_save_saved_servers() { utf8 path[MAX_PATH]; - FILE *file; + SDL_RWops *file; platform_get_user_directory(path, NULL); strcat(path, "servers.cfg"); - file = fopen(path, "wb"); + file = SDL_RWFromFile(path, "wb"); if (file == NULL) { log_error("Unable to save servers."); return; } // Write number of saved servers - fwrite(&_numSavedServers, sizeof(uint32), 1, file); + SDL_RWwrite(file, &_numSavedServers, sizeof(uint32), 1); // Write each saved server for (int i = 0; i < _numSavedServers; i++) { saved_server *serverInfo = &_savedServers[i]; - fwrite(serverInfo->address, strlen(serverInfo->address) + 1, 1, file); - fwrite(serverInfo->name, strlen(serverInfo->name) + 1, 1, file); - fwrite(serverInfo->description, strlen(serverInfo->description) + 1, 1, file); + SDL_RWwrite(file, serverInfo->address, strlen(serverInfo->address) + 1, 1); + SDL_RWwrite(file, serverInfo->name, strlen(serverInfo->name) + 1, 1); + SDL_RWwrite(file, serverInfo->description, strlen(serverInfo->description) + 1, 1); } - fclose(file); + SDL_RWclose(file); } static void dispose_saved_server_list()