From 23c0d3b873ddaf35a2cd2cf99eaa3597d339b138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0imek?= Date: Sat, 28 May 2022 09:38:16 +0200 Subject: [PATCH 1/9] Replace CopyFileA with a cross-platform function (#20) Co-authored-by: Alexander Batalov --- src/file_utils.cc | 15 ++++++++++----- src/file_utils.h | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/file_utils.cc b/src/file_utils.cc index 725eb62..dd48204 100644 --- a/src/file_utils.cc +++ b/src/file_utils.cc @@ -6,8 +6,7 @@ #include #include -#define WIN32_LEAN_AND_MEAN -#include +#include // 0x452740 int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) @@ -50,7 +49,7 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) return -1; } } else { - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } return 0; @@ -73,7 +72,7 @@ int fileCopyCompressed(const char* existingFilePath, const char* newFilePath) // Source file is already gzipped, there is no need to do anything // besides copying. fclose(inStream); - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } else { gzFile outStream = gzopen(newFilePath, "wb"); if (outStream == NULL) { @@ -136,8 +135,14 @@ int _gzdecompress_file(const char* existingFilePath, const char* newFilePath) gzclose(gzstream); fclose(stream); } else { - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } return 0; } + +void fileCopy(const char* existingFilePath, const char* newFilePath) +{ + std::error_code ec; + std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), ec); +} diff --git a/src/file_utils.h b/src/file_utils.h index 10f626a..fd886f7 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -4,5 +4,6 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath); int fileCopyCompressed(const char* existingFilePath, const char* newFilePath); int _gzdecompress_file(const char* existingFilePath, const char* newFilePath); +void fileCopy(const char* existingFilePath, const char* newFilePath); #endif /* FILE_UTILS_H */ From e1cf71eac7cf552bd81ab894afdfe207f27117fd Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 11:29:45 +0300 Subject: [PATCH 2/9] Add overwrite param (#20) --- src/file_utils.cc | 15 ++++++++++----- src/file_utils.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/file_utils.cc b/src/file_utils.cc index dd48204..7edba4b 100644 --- a/src/file_utils.cc +++ b/src/file_utils.cc @@ -49,7 +49,7 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) return -1; } } else { - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } return 0; @@ -72,7 +72,7 @@ int fileCopyCompressed(const char* existingFilePath, const char* newFilePath) // Source file is already gzipped, there is no need to do anything // besides copying. fclose(inStream); - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } else { gzFile outStream = gzopen(newFilePath, "wb"); if (outStream == NULL) { @@ -135,14 +135,19 @@ int _gzdecompress_file(const char* existingFilePath, const char* newFilePath) gzclose(gzstream); fclose(stream); } else { - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } return 0; } -void fileCopy(const char* existingFilePath, const char* newFilePath) +// Modelled as replacement for `CopyFileA`, but `overwrite` is the opposite to +// `bFailIfExists` param. Update callers accordingly. +void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite) { std::error_code ec; - std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), ec); + std::filesystem::copy_options options = overwrite + ? std::filesystem::copy_options::overwrite_existing + : std::filesystem::copy_options::none; + std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), options, ec); } diff --git a/src/file_utils.h b/src/file_utils.h index fd886f7..cdac4d8 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -4,6 +4,6 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath); int fileCopyCompressed(const char* existingFilePath, const char* newFilePath); int _gzdecompress_file(const char* existingFilePath, const char* newFilePath); -void fileCopy(const char* existingFilePath, const char* newFilePath); +void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite); #endif /* FILE_UTILS_H */ From 4bac8297394610c9b02a21d387c6a1b9d45fd00a Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 11:57:32 +0300 Subject: [PATCH 3/9] Add string functions compatibility layer See #17 --- CMakeLists.txt | 2 ++ src/art.cc | 13 +++++++------ src/character_editor.cc | 35 ++++++++++++++++++----------------- src/combat_ai.cc | 5 +++-- src/config.cc | 3 ++- src/db.cc | 5 +++-- src/debug.cc | 3 ++- src/dfile.cc | 4 +++- src/dialog.cc | 3 ++- src/dictionary.cc | 4 +++- src/export.cc | 11 ++++++----- src/game.cc | 11 ++++++----- src/game_mouse.cc | 3 ++- src/game_movie.cc | 3 ++- src/game_sound.cc | 13 +++++++------ src/interpreter.cc | 5 +++-- src/map.cc | 5 +++-- src/message.cc | 5 +++-- src/movie_effect.cc | 7 ++++--- src/nevs.cc | 3 ++- src/platform_compat.cc | 28 ++++++++++++++++++++++++++++ src/platform_compat.h | 13 +++++++++++++ src/sound_effects_list.cc | 5 +++-- src/string_parsers.cc | 13 +++++++------ src/tile.cc | 3 ++- src/window.cc | 25 +++++++++++++------------ src/world_map.cc | 25 +++++++++++++------------ src/xfile.cc | 3 ++- 28 files changed, 164 insertions(+), 94 deletions(-) create mode 100644 src/platform_compat.cc create mode 100644 src/platform_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d977423..5411e6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -235,6 +235,8 @@ add_executable(${EXECUTABLE_NAME} WIN32 target_sources(${EXECUTABLE_NAME} PUBLIC "src/fps_limiter.cc" "src/fps_limiter.h" + "src/platform_compat.cc" + "src/platform_compat.h" "src/sfall_config.cc" "src/sfall_config.h" ) diff --git a/src/art.cc b/src/art.cc index 5096a96..a6fcd5a 100644 --- a/src/art.cc +++ b/src/art.cc @@ -6,6 +6,7 @@ #include "game_config.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include @@ -108,7 +109,7 @@ int artInit() } char* language; - if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_LANGUAGE_KEY, &language) && stricmp(language, ENGLISH) != 0) { + if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_LANGUAGE_KEY, &language) && compat_stricmp(language, ENGLISH) != 0) { strcpy(gArtLanguage, language); gArtLanguageInitialized = true; } @@ -155,16 +156,16 @@ int artInit() ptr = gArtListDescriptions[1].fileNames; for (i = 0; i < gArtListDescriptions[1].fileNamesLength; i++) { - if (stricmp(ptr, "hmjmps") == 0) { + if (compat_stricmp(ptr, "hmjmps") == 0) { _art_vault_person_nums[DUDE_NATIVE_LOOK_JUMPSUIT][GENDER_MALE] = i; - } else if (stricmp(ptr, "hfjmps") == 0) { + } else if (compat_stricmp(ptr, "hfjmps") == 0) { _art_vault_person_nums[DUDE_NATIVE_LOOK_JUMPSUIT][GENDER_FEMALE] = i; } - if (stricmp(ptr, "hmwarr") == 0) { + if (compat_stricmp(ptr, "hmwarr") == 0) { _art_vault_person_nums[DUDE_NATIVE_LOOK_TRIBAL][GENDER_MALE] = i; _art_vault_guy_num = i; - } else if (stricmp(ptr, "hfprim") == 0) { + } else if (compat_stricmp(ptr, "hfprim") == 0) { _art_vault_person_nums[DUDE_NATIVE_LOOK_TRIBAL][GENDER_FEMALE] = i; } @@ -202,7 +203,7 @@ int artInit() ptr = gArtListDescriptions[4].fileNames; for (i = 0; i < gArtListDescriptions[4].fileNamesLength; i++) { - if (stricmp(ptr, "grid001.frm") == 0) { + if (compat_stricmp(ptr, "grid001.frm") == 0) { _art_mapper_blank_tile = i; } } diff --git a/src/character_editor.cc b/src/character_editor.cc index 2d176b5..779f000 100644 --- a/src/character_editor.cc +++ b/src/character_editor.cc @@ -20,6 +20,7 @@ #include "object.h" #include "palette.h" #include "perk.h" +#include "platform_compat.h" #include "proto.h" #include "scripts.h" #include "skill.h" @@ -679,7 +680,7 @@ int _editor_design(bool isCreationMode) continue; } - if (stricmp(critterGetName(gDude), "None") == 0) { + if (compat_stricmp(critterGetName(gDude), "None") == 0) { soundPlayFile("iisxxxx1"); // Warning: You haven't changed your player @@ -1946,7 +1947,7 @@ void editorRenderPerks() // 0x434498 int _kills_list_comp(const KillInfo* a, const KillInfo* b) { - return stricmp(a->name, b->name); + return compat_stricmp(a->name, b->name); } // 0x4344A4 @@ -2520,7 +2521,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_ARMOR_CLASS), t, 10); + compat_itoa(critterGetStat(gDude, STAT_ARMOR_CLASS), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, color); // Action Points @@ -2536,7 +2537,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_MAXIMUM_ACTION_POINTS), t, 10); + compat_itoa(critterGetStat(gDude, STAT_MAXIMUM_ACTION_POINTS), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, color); // Carry Weight @@ -2552,7 +2553,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_CARRY_WEIGHT), t, 10); + compat_itoa(critterGetStat(gDude, STAT_CARRY_WEIGHT), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, critterIsEncumbered(gDude) ? _colorTable[31744] : color); // Melee Damage @@ -2568,7 +2569,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_MELEE_DAMAGE), t, 10); + compat_itoa(critterGetStat(gDude, STAT_MELEE_DAMAGE), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, color); // Damage Resistance @@ -2632,7 +2633,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_SEQUENCE), t, 10); + compat_itoa(critterGetStat(gDude, STAT_SEQUENCE), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, color); // Healing Rate @@ -2648,7 +2649,7 @@ void editorRenderSecondaryStats() sprintf(t, "%s", messageListItemText); fontDrawText(characterEditorWindowBuf + 640 * y + 194, t, 640, 640, color); - itoa(critterGetStat(gDude, STAT_HEALING_RATE), t, 10); + compat_itoa(critterGetStat(gDude, STAT_HEALING_RATE), t, 10); fontDrawText(characterEditorWindowBuf + 640 * y + 288, t, 640, 640, color); // Critical Chance @@ -3694,7 +3695,7 @@ int _OptionWindow() // already exists sprintf(dest, "%s %s", - strupr(v236), + compat_strupr(v236), getmsg(&editorMessageList, &editorMessageListItem, 609)); char v240[512]; @@ -3717,7 +3718,7 @@ int _OptionWindow() if (characterPrintToFile(dest) == 0) { sprintf(dest, "%s%s", - strupr(v236), + compat_strupr(v236), getmsg(&editorMessageList, &editorMessageListItem, 607)); showDialogBox(dest, NULL, 0, 169, 126, _colorTable[992], NULL, _colorTable[992], 0); } else { @@ -3726,7 +3727,7 @@ int _OptionWindow() sprintf(dest, "%s%s%s", getmsg(&editorMessageList, &editorMessageListItem, 611), - strupr(v236), + compat_strupr(v236), "!"); showDialogBox(dest, NULL, 0, 169, 126, _colorTable[32328], NULL, _colorTable[992], 0x01); } @@ -3813,7 +3814,7 @@ int _OptionWindow() if (characterFileExists(title)) { sprintf(title, "%s %s", - strupr(fileName), + compat_strupr(fileName), getmsg(&editorMessageList, &editorMessageListItem, 609)); char line2[512]; @@ -3838,7 +3839,7 @@ int _OptionWindow() sprintf(title, "%s%s%s", getmsg(&editorMessageList, &editorMessageListItem, 611), - strupr(fileName), + compat_strupr(fileName), "!"); showDialogBox(title, NULL, 0, 169, 126, _colorTable[32328], NULL, _colorTable[32328], 1); } @@ -4131,7 +4132,7 @@ int characterPrintToFile(const char* fileName) sprintf(title1, " %s: %s (%s)", getmsg(&editorMessageList, &editorMessageListItem, 125), - itoa(gGameGlobalVars[GVAR_PLAYER_REPUTATION], title2, 10), + compat_itoa(gGameGlobalVars[GVAR_PLAYER_REPUTATION], title2, 10), getmsg(&editorMessageList, &editorMessageListItem, reputationDescription->name)); fileWriteString(title1, stream); fileWriteString("\n", stream); @@ -4489,7 +4490,7 @@ char* _itostndn(int value, char* dest) int v18 = value / v16[index]; if (v18 > 0 || v3) { char temp[64]; // TODO: Size is probably wrong. - itoa(v18, temp, 10); + compat_itoa(v18, temp, 10); strcat(dest, temp); v3 = true; @@ -5102,7 +5103,7 @@ void editorRenderKarma() GenericReputationEntry* reputationDescription = &(gGenericReputationEntries[reputation]); char reputationValue[32]; - itoa(gGameGlobalVars[GVAR_PLAYER_REPUTATION], reputationValue, 10); + compat_itoa(gGameGlobalVars[GVAR_PLAYER_REPUTATION], reputationValue, 10); sprintf(formattedText, "%s: %s (%s)", @@ -6490,7 +6491,7 @@ bool editorDrawKillsEntry(const char* name, int kills) color = _colorTable[992]; } - itoa(kills, killsString, 10); + compat_itoa(kills, killsString, 10); int v6 = fontGetStringWidth(killsString); // TODO: Check. diff --git a/src/combat_ai.cc b/src/combat_ai.cc index 2c17431..3d42f9b 100644 --- a/src/combat_ai.cc +++ b/src/combat_ai.cc @@ -17,6 +17,7 @@ #include "map.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "random.h" @@ -199,7 +200,7 @@ void _parse_hurt_str(char* str, int* valuePtr) *valuePtr = 0; - str = strlwr(str); + str = compat_strlwr(str); while (*str) { v5 = strspn(str, " "); str += v5; @@ -234,7 +235,7 @@ int _cai_match_str_to_list(const char* str, const char** list, int count, int* v { *valuePtr = -1; for (int index = 0; index < count; index++) { - if (stricmp(str, list[index]) == 0) { + if (compat_stricmp(str, list[index]) == 0) { *valuePtr = index; } } diff --git a/src/config.cc b/src/config.cc index 7a14978..dbbc52c 100644 --- a/src/config.cc +++ b/src/config.cc @@ -2,6 +2,7 @@ #include "db.h" #include "memory.h" +#include "platform_compat.h" #include #include @@ -243,7 +244,7 @@ bool configGetIntList(Config* config, const char* sectionKey, const char* key, i bool configSetInt(Config* config, const char* sectionKey, const char* key, int value) { char stringValue[20]; - itoa(value, stringValue, 10); + compat_itoa(value, stringValue, 10); return configSetString(config, sectionKey, key, stringValue); } diff --git a/src/db.cc b/src/db.cc index c7a511f..7964cad 100644 --- a/src/db.cc +++ b/src/db.cc @@ -1,5 +1,6 @@ #include "db.h" +#include "platform_compat.h" #include "xfile.h" #include @@ -620,7 +621,7 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a int fileNamesLength = xlist->fileNamesLength; for (int index = 0; index < fileNamesLength - 1; index++) { - if (stricmp(xlist->fileNames[index], xlist->fileNames[index + 1]) == 0) { + if (compat_stricmp(xlist->fileNames[index], xlist->fileNames[index + 1]) == 0) { char* temp = xlist->fileNames[index + 1]; memmove(&(xlist->fileNames[index + 1]), &(xlist->fileNames[index + 2]), sizeof(*xlist->fileNames) * (xlist->fileNamesLength - index - 1)); xlist->fileNames[xlist->fileNamesLength - 1] = temp; @@ -730,5 +731,5 @@ void _db_enable_hash_table_() // 0x4C68E8 int _db_list_compare(const void* p1, const void* p2) { - return stricmp(*(const char**)p1, *(const char**)p2); + return compat_stricmp(*(const char**)p1, *(const char**)p2); } diff --git a/src/debug.cc b/src/debug.cc index 6e7f2a2..bb0ba39 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -1,6 +1,7 @@ #include "debug.h" #include "memory.h" +#include "platform_compat.h" #include "window_manager_private.h" #include @@ -82,7 +83,7 @@ void _debug_register_env() } strcpy(copy, type); - strlwr(copy); + compat_strlwr(copy); if (strcmp(copy, "mono") == 0) { // NOTE: Uninline. diff --git a/src/dfile.cc b/src/dfile.cc index 90a6b21..2332c46 100644 --- a/src/dfile.cc +++ b/src/dfile.cc @@ -1,5 +1,7 @@ #include "dfile.h" +#include "platform_compat.h" + #include #include @@ -591,7 +593,7 @@ int dbaseFindEntryByFilePath(const void* a1, const void* a2) const char* filePath = (const char*)a1; DBaseEntry* entry = (DBaseEntry*)a2; - return stricmp(filePath, entry->path); + return compat_stricmp(filePath, entry->path); } // 0x4E5D9C diff --git a/src/dialog.cc b/src/dialog.cc index 50dd167..db4a0cd 100644 --- a/src/dialog.cc +++ b/src/dialog.cc @@ -3,6 +3,7 @@ #include "core.h" #include "memory_manager.h" #include "movie.h" +#include "platform_compat.h" #include "text_font.h" #include "widget.h" #include "window_manager.h" @@ -450,7 +451,7 @@ int _dialogGotoReply(const char* a1) ptr = &(_dialog[_tods]); for (i = 0; i < ptr->field_8; i++) { v5 = &(ptr->field_4[i]); - if (v5->field_4 != NULL && stricmp(v5->field_4, a1) == 0) { + if (v5->field_4 != NULL && compat_stricmp(v5->field_4, a1) == 0) { ptr->field_10 = i; return 0; } diff --git a/src/dictionary.cc b/src/dictionary.cc index 2cb9252..f49ba03 100644 --- a/src/dictionary.cc +++ b/src/dictionary.cc @@ -1,5 +1,7 @@ #include "dictionary.h" +#include "platform_compat.h" + #include #include #include @@ -143,7 +145,7 @@ int dictionaryFindIndexForKey(Dictionary* dictionary, const char* key, int* inde while (r >= l) { mid = (l + r) / 2; - cmp = stricmp(key, dictionary->entries[mid].key); + cmp = compat_stricmp(key, dictionary->entries[mid].key); if (cmp == 0) { break; } diff --git a/src/export.cc b/src/export.cc index e40644e..2d2ce98 100644 --- a/src/export.cc +++ b/src/export.cc @@ -2,6 +2,7 @@ #include "interpreter_lib.h" #include "memory_manager.h" +#include "platform_compat.h" #include #include @@ -38,7 +39,7 @@ ExternalProcedure* externalProcedureFind(const char* identifier) ExternalProcedure* externalProcedure = &(gExternalProcedures[v1]); if (externalProcedure->program != NULL) { - if (stricmp(externalProcedure->name, identifier) == 0) { + if (compat_stricmp(externalProcedure->name, identifier) == 0) { return externalProcedure; } } @@ -51,7 +52,7 @@ ExternalProcedure* externalProcedureFind(const char* identifier) externalProcedure = &(gExternalProcedures[v1]); if (externalProcedure->program != NULL) { - if (stricmp(externalProcedure->name, identifier) == 0) { + if (compat_stricmp(externalProcedure->name, identifier) == 0) { return externalProcedure; } } @@ -95,7 +96,7 @@ ExternalVariable* externalVariableFind(const char* identifier) unsigned int v2 = v1; ExternalVariable* exportedVariable = &(gExternalVariables[v1]); - if (stricmp(exportedVariable->name, identifier) == 0) { + if (compat_stricmp(exportedVariable->name, identifier) == 0) { return exportedVariable; } @@ -111,7 +112,7 @@ ExternalVariable* externalVariableFind(const char* identifier) } exportedVariable = &(gExternalVariables[v1]); - if (stricmp(exportedVariable->name, identifier) == 0) { + if (compat_stricmp(exportedVariable->name, identifier) == 0) { return exportedVariable; } } while (v1 != v2); @@ -200,7 +201,7 @@ int externalVariableCreate(Program* program, const char* identifier) ExternalVariable* exportedVariable = externalVariableFind(identifier); if (exportedVariable != NULL) { - if (stricmp(exportedVariable->programName, programName) != 0) { + if (compat_stricmp(exportedVariable->programName, programName) != 0) { return 1; } diff --git a/src/game.cc b/src/game.cc index 4647d27..a023a35 100644 --- a/src/game.cc +++ b/src/game.cc @@ -39,6 +39,7 @@ #include "party_member.h" #include "perk.h" #include "pipboy.h" +#include "platform_compat.h" #include "proto.h" #include "queue.h" #include "random.h" @@ -137,13 +138,13 @@ int gameInitWithOptions(const char* windowTitle, bool isMapper, int font, int a4 char* language; if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_LANGUAGE_KEY, &language)) { - if (stricmp(language, FRENCH) == 0) { + if (compat_stricmp(language, FRENCH) == 0) { keyboardSetLayout(KEYBOARD_LAYOUT_FRENCH); - } else if (stricmp(language, GERMAN) == 0) { + } else if (compat_stricmp(language, GERMAN) == 0) { keyboardSetLayout(KEYBOARD_LAYOUT_GERMAN); - } else if (stricmp(language, ITALIAN) == 0) { + } else if (compat_stricmp(language, ITALIAN) == 0) { keyboardSetLayout(KEYBOARD_LAYOUT_ITALIAN); - } else if (stricmp(language, SPANISH) == 0) { + } else if (compat_stricmp(language, SPANISH) == 0) { keyboardSetLayout(KEYBOARD_LAYOUT_SPANISH); } } @@ -1222,7 +1223,7 @@ void showSplash() char path[64]; char* language; - if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_LANGUAGE_KEY, &language) && stricmp(language, ENGLISH) != 0) { + if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_LANGUAGE_KEY, &language) && compat_stricmp(language, ENGLISH) != 0) { sprintf(path, "art\\%s\\splash\\", language); } else { sprintf(path, "art\\splash\\"); diff --git a/src/game_mouse.cc b/src/game_mouse.cc index 853ae5f..3181e4d 100644 --- a/src/game_mouse.cc +++ b/src/game_mouse.cc @@ -13,6 +13,7 @@ #include "item.h" #include "map.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "skilldex.h" @@ -2193,7 +2194,7 @@ int _gmouse_3d_move_to(int x, int y, int elevation, Rect* a4) char* executable; configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_EXECUTABLE_KEY, &executable); - if (stricmp(executable, "mapper") == 0) { + if (compat_stricmp(executable, "mapper") == 0) { if (tileRoofIsVisible()) { if ((gDude->flags & OBJECT_HIDDEN) == 0) { y1 = -83; diff --git a/src/game_movie.cc b/src/game_movie.cc index dd86da6..afefaf1 100644 --- a/src/game_movie.cc +++ b/src/game_movie.cc @@ -11,6 +11,7 @@ #include "movie.h" #include "movie_effect.h" #include "palette.h" +#include "platform_compat.h" #include "text_font.h" #include "widget.h" #include "window_manager.h" @@ -148,7 +149,7 @@ int gameMoviePlay(int movie, int flags) int movieFileSize; bool movieFound = false; - if (stricmp(language, ENGLISH) != 0) { + if (compat_stricmp(language, ENGLISH) != 0) { sprintf(movieFilePath, "art\\%s\\cuts\\%s", language, gMovieFileNames[movie]); movieFound = dbGetFileSize(movieFilePath, &movieFileSize) == 0; } diff --git a/src/game_sound.cc b/src/game_sound.cc index 461a661..937c11d 100644 --- a/src/game_sound.cc +++ b/src/game_sound.cc @@ -13,6 +13,7 @@ #include "memory.h" #include "movie.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include "queue.h" #include "random.h" @@ -1308,7 +1309,7 @@ char* sfxBuildCharName(Object* a1, int anim, int extra) } sprintf(_sfx_file_name, "%s%c%c", v7, v8, v9); - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } @@ -1317,7 +1318,7 @@ char* sfxBuildCharName(Object* a1, int anim, int extra) char* gameSoundBuildAmbientSoundEffectName(const char* a1) { sprintf(_sfx_file_name, "A%6s%1d", a1, 1); - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } @@ -1326,7 +1327,7 @@ char* gameSoundBuildAmbientSoundEffectName(const char* a1) char* gameSoundBuildInterfaceName(const char* a1) { sprintf(_sfx_file_name, "N%6s%1d", a1, 1); - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } @@ -1402,7 +1403,7 @@ char* sfxBuildWeaponName(int effectType, Object* weapon, int hitMode, Object* ta } sprintf(_sfx_file_name, "W%c%c%1d%cXX%1d", effectTypeCode, weaponSoundCode, v6, materialCode, 1); - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } @@ -1414,7 +1415,7 @@ char* sfxBuildSceneryName(int actionType, int action, const char* name) char actionCode = _snd_lookup_scenery_action[action]; sprintf(_sfx_file_name, "S%c%c%4s%1d", actionTypeCode, actionCode, name, 1); - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } @@ -1437,7 +1438,7 @@ char* sfxBuildOpenName(Object* object, int action) protoGetProto(object->pid, &proto); sprintf(_sfx_file_name, "I%cCNTNR%c", _snd_lookup_scenery_action[action], proto->item.field_80); } - strupr(_sfx_file_name); + compat_strupr(_sfx_file_name); return _sfx_file_name; } diff --git a/src/interpreter.cc b/src/interpreter.cc index 0bc49b7..ecce1ab 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -6,6 +6,7 @@ #include "export.h" #include "interpreter_lib.h" #include "memory_manager.h" +#include "platform_compat.h" #include #include @@ -2967,7 +2968,7 @@ void opLookupStringProc(Program* program) for (int index = 1; index < procedureCount; index++) { int offset = stackReadInt32(procedurePtr, 0); const char* procedureName = programGetIdentifier(program, offset); - if (stricmp(procedureName, procedureNameToLookup) == 0) { + if (compat_stricmp(procedureName, procedureNameToLookup) == 0) { programStackPushInt32(program, index); programStackPushInt16(program, VALUE_TYPE_INT); return; @@ -3309,7 +3310,7 @@ int programFindProcedure(Program* program, const char* name) unsigned char* ptr = program->procedures + 4; for (int index = 0; index < procedureCount; index++) { int identifierOffset = stackReadInt32(ptr, offsetof(Procedure, field_0)); - if (stricmp((char*)(program->identifiers + identifierOffset), name) == 0) { + if (compat_stricmp((char*)(program->identifiers + identifierOffset), name) == 0) { return index; } diff --git a/src/map.cc b/src/map.cc index bbf92c7..e45a4c8 100644 --- a/src/map.cc +++ b/src/map.cc @@ -23,6 +23,7 @@ #include "object.h" #include "palette.h" #include "pipboy.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "queue.h" @@ -255,7 +256,7 @@ void _map_init() { char* executable; configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, "executable", &executable); - if (stricmp(executable, "mapper") == 0) { + if (compat_stricmp(executable, "mapper") == 0) { _map_scroll_refresh = isoWindowRefreshRectMapper; } @@ -696,7 +697,7 @@ int mapLoadByName(char* fileName) { int rc; - strupr(fileName); + compat_strupr(fileName); rc = -1; diff --git a/src/message.cc b/src/message.cc index c0f6260..e15e5f7 100644 --- a/src/message.cc +++ b/src/message.cc @@ -3,6 +3,7 @@ #include "debug.h" #include "game_config.h" #include "memory.h" +#include "platform_compat.h" #include "random.h" #include @@ -82,7 +83,7 @@ int badwordsInit() break; } - strupr(gBadwords[index]); + compat_strupr(gBadwords[index]); gBadwordsLengths[index] = len; } @@ -523,7 +524,7 @@ bool messageListFilterBadwords(MessageList* messageList) for (int index = 0; index < messageList->entries_num; index++) { MessageListItem* item = &(messageList->entries[index]); strcpy(_bad_copy, item->text); - strupr(_bad_copy); + compat_strupr(_bad_copy); for (int badwordIndex = 0; badwordIndex < gBadwordsCount; badwordIndex++) { // I don't quite understand the loop below. It has no stop diff --git a/src/movie_effect.cc b/src/movie_effect.cc index b041bd7..830243f 100644 --- a/src/movie_effect.cc +++ b/src/movie_effect.cc @@ -5,6 +5,7 @@ #include "memory.h" #include "movie.h" #include "palette.h" +#include "platform_compat.h" #include #include @@ -127,7 +128,7 @@ int movieEffectsLoad(const char* filePath) int movieEffectsCreated = 0; for (int index = 0; index < movieEffectsLength; index++) { char section[20]; - itoa(movieEffectFrameList[index], section, 10); + compat_itoa(movieEffectFrameList[index], section, 10); char* fadeTypeString; if (!configGetString(&config, section, "fade_type", &fadeTypeString)) { @@ -135,9 +136,9 @@ int movieEffectsLoad(const char* filePath) } int fadeType = MOVIE_EFFECT_TYPE_NONE; - if (stricmp(fadeTypeString, "in") == 0) { + if (compat_stricmp(fadeTypeString, "in") == 0) { fadeType = MOVIE_EFFECT_TYPE_FADE_IN; - } else if (stricmp(fadeTypeString, "out") == 0) { + } else if (compat_stricmp(fadeTypeString, "out") == 0) { fadeType = MOVIE_EFFECT_TYPE_FADE_OUT; } diff --git a/src/nevs.cc b/src/nevs.cc index 4fd5afe..7fa9256 100644 --- a/src/nevs.cc +++ b/src/nevs.cc @@ -2,6 +2,7 @@ #include "debug.h" #include "memory_manager.h" +#include "platform_compat.h" #include #include @@ -81,7 +82,7 @@ Nevs* _nevs_find(const char* a1) for (int index = 0; index < NEVS_COUNT; index++) { Nevs* nevs = &(_nevs[index]); - if (nevs->field_0 != 0 && stricmp(nevs->field_4, a1) == 0) { + if (nevs->field_0 != 0 && compat_stricmp(nevs->field_4, a1) == 0) { return nevs; } } diff --git a/src/platform_compat.cc b/src/platform_compat.cc new file mode 100644 index 0000000..67adaf4 --- /dev/null +++ b/src/platform_compat.cc @@ -0,0 +1,28 @@ +#include "platform_compat.h" + +#include + +int compat_stricmp(const char* string1, const char* string2) +{ + return SDL_strcasecmp(string1, string2); +} + +int compat_strnicmp(const char* string1, const char* string2, size_t size) +{ + return SDL_strncasecmp(string1, string2, size); +} + +char* compat_strupr(char* string) +{ + return SDL_strupr(string); +} + +char* compat_strlwr(char* string) +{ + return SDL_strlwr(string); +} + +char* compat_itoa(int value, char* buffer, int radix) +{ + return SDL_itoa(value, buffer, radix); +} diff --git a/src/platform_compat.h b/src/platform_compat.h new file mode 100644 index 0000000..b13b3f6 --- /dev/null +++ b/src/platform_compat.h @@ -0,0 +1,13 @@ +#ifndef PLATFORM_COMPAT_H +#define PLATFORM_COMPAT_H + +// TODO: This is compatibility cross-platform layer. Designed to have minimal +// impact on the codebase. Remove once it's no longer needed. + +int compat_stricmp(const char* string1, const char* string2); +int compat_strnicmp(const char* string1, const char* string2, size_t size); +char* compat_strupr(char* string); +char* compat_strlwr(char* string); +char* compat_itoa(int value, char* buffer, int radix); + +#endif /* PLATFORM_COMPAT_H */ diff --git a/src/sound_effects_list.cc b/src/sound_effects_list.cc index e2962b0..3d6d0e6 100644 --- a/src/sound_effects_list.cc +++ b/src/sound_effects_list.cc @@ -3,6 +3,7 @@ #include "db.h" #include "debug.h" #include "memory.h" +#include "platform_compat.h" #include "sound_decoder.h" #include @@ -164,7 +165,7 @@ void soundEffectsListExit() // 0x4A9C28 int soundEffectsListGetTag(char* name, int* tagPtr) { - if (strnicmp(gSoundEffectsListPath, name, gSoundEffectsListPathLength) != 0) { + if (compat_strnicmp(gSoundEffectsListPath, name, gSoundEffectsListPathLength) != 0) { return SFXL_ERR; } @@ -441,7 +442,7 @@ int soundEffectsListCompareByName(const void* a1, const void* a2) SoundEffectsListEntry* v1 = (SoundEffectsListEntry*)a1; SoundEffectsListEntry* v2 = (SoundEffectsListEntry*)a2; - return stricmp(v1->name, v2->name); + return compat_stricmp(v1->name, v2->name); } // read via xfile diff --git a/src/string_parsers.cc b/src/string_parsers.cc index db07833..6ee786d 100644 --- a/src/string_parsers.cc +++ b/src/string_parsers.cc @@ -1,6 +1,7 @@ #include "string_parsers.h" #include "debug.h" +#include "platform_compat.h" #include #include @@ -19,7 +20,7 @@ int strParseInt(char** stringPtr, int* valuePtr) str = *stringPtr; - strlwr(str); + compat_strlwr(str); v1 = strspn(str, " "); str += v1; @@ -63,7 +64,7 @@ int strParseStrFromList(char** stringPtr, int* valuePtr, const char** stringList str = *stringPtr; - strlwr(str); + compat_strlwr(str); v1 = strspn(str, " "); str += v1; @@ -84,7 +85,7 @@ int strParseStrFromList(char** stringPtr, int* valuePtr, const char** stringList } for (i = 0; i < stringListLength; i++) { - if (stricmp(str, stringList[i]) == 0) { + if (compat_stricmp(str, stringList[i]) == 0) { break; } } @@ -119,7 +120,7 @@ int strParseStrFromFunc(char** stringPtr, int* valuePtr, StringParserCallback* c str = *stringPtr; - strlwr(str); + compat_strlwr(str); v1 = strspn(str, " "); str += v1; @@ -174,7 +175,7 @@ int strParseIntWithKey(char** stringPtr, const char* key, int* valuePtr, const c return -1; } - strlwr(str); + compat_strlwr(str); if (*str == ',') { str++; @@ -225,7 +226,7 @@ int strParseKeyValue(char** stringPtr, char* key, int* valuePtr, const char* del return -1; } - strlwr(str); + compat_strlwr(str); if (*str == ',') { str++; diff --git a/src/tile.cc b/src/tile.cc index 37d8fae..7721d28 100644 --- a/src/tile.cc +++ b/src/tile.cc @@ -10,6 +10,7 @@ #include "light.h" #include "map.h" #include "object.h" +#include "platform_compat.h" #include #include @@ -391,7 +392,7 @@ int tileInit(TileData** a1, int squareGridWidth, int squareGridHeight, int hexGr char* executable; configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_EXECUTABLE_KEY, &executable); - if (stricmp(executable, "mapper") == 0) { + if (compat_stricmp(executable, "mapper") == 0) { gTileWindowRefreshElevationProc = tileRefreshMapper; } diff --git a/src/window.cc b/src/window.cc index 040755e..d75f398 100644 --- a/src/window.cc +++ b/src/window.cc @@ -6,6 +6,7 @@ #include "memory_manager.h" #include "mouse_manager.h" #include "movie.h" +#include "platform_compat.h" #include "text_font.h" #include "widget.h" #include "window_manager.h" @@ -162,7 +163,7 @@ int _selectWindow(const char* windowName) { if (gCurrentManagedWindowIndex != -1) { ManagedWindow* managedWindow = &(gManagedWindows[gCurrentManagedWindowIndex]); - if (stricmp(managedWindow->name, windowName) == 0) { + if (compat_stricmp(managedWindow->name, windowName) == 0) { return gCurrentManagedWindowIndex; } } @@ -171,7 +172,7 @@ int _selectWindow(const char* windowName) for (index = 0; index < MANAGED_WINDOW_COUNT; index++) { ManagedWindow* managedWindow = &(gManagedWindows[index]); if (managedWindow->window != -1) { - if (stricmp(managedWindow->name, windowName) == 0) { + if (compat_stricmp(managedWindow->name, windowName) == 0) { break; } } @@ -666,7 +667,7 @@ bool _windowDeleteButton(const char* buttonName) for (int index = 0; index < managedWindow->buttonsLength; index++) { ManagedButton* managedButton = &(managedWindow->buttons[index]); - if (stricmp(managedButton->name, buttonName) == 0) { + if (compat_stricmp(managedButton->name, buttonName) == 0) { buttonDestroy(managedButton->btn); if (managedButton->field_48 != NULL) { @@ -724,7 +725,7 @@ bool _windowSetButtonFlag(const char* buttonName, int value) for (int index = 0; index < managedWindow->buttonsLength; index++) { ManagedButton* managedButton = &(managedWindow->buttons[index]); - if (stricmp(managedButton->name, buttonName) == 0) { + if (compat_stricmp(managedButton->name, buttonName) == 0) { managedButton->flags |= value; return true; } @@ -747,7 +748,7 @@ bool _windowAddButtonProc(const char* buttonName, Program* program, int a3, int for (int index = 0; index < managedWindow->buttonsLength; index++) { ManagedButton* managedButton = &(managedWindow->buttons[index]); - if (stricmp(managedButton->name, buttonName) == 0) { + if (compat_stricmp(managedButton->name, buttonName) == 0) { managedButton->field_5C = a3; managedButton->field_60 = a4; managedButton->field_54 = a5; @@ -774,7 +775,7 @@ bool _windowAddButtonRightProc(const char* buttonName, Program* program, int a3, for (int index = 0; index < managedWindow->buttonsLength; index++) { ManagedButton* managedButton = &(managedWindow->buttons[index]); - if (stricmp(managedButton->name, buttonName) == 0) { + if (compat_stricmp(managedButton->name, buttonName) == 0) { managedButton->field_68 = a4; managedButton->field_64 = a3; managedButton->program = program; @@ -814,7 +815,7 @@ bool _windowCheckRegionExists(const char* regionName) for (int index = 0; index < managedWindow->regionsLength; index++) { Region* region = managedWindow->regions[index]; if (region != NULL) { - if (stricmp(regionGetName(region), regionName) == 0) { + if (compat_stricmp(regionGetName(region), regionName) == 0) { return true; } } @@ -898,7 +899,7 @@ bool _windowAddRegionProc(const char* regionName, Program* program, int a3, int for (int index = 0; index < managedWindow->regionsLength; index++) { Region* region = managedWindow->regions[index]; if (region != NULL) { - if (stricmp(region->name, regionName) == 0) { + if (compat_stricmp(region->name, regionName) == 0) { region->field_50 = a3; region->field_54 = a4; region->field_48 = a5; @@ -923,7 +924,7 @@ bool _windowAddRegionRightProc(const char* regionName, Program* program, int a3, for (int index = 0; index < managedWindow->regionsLength; index++) { Region* region = managedWindow->regions[index]; if (region != NULL) { - if (stricmp(region->name, regionName) == 0) { + if (compat_stricmp(region->name, regionName) == 0) { region->field_58 = a3; region->field_5C = a4; region->program = program; @@ -943,7 +944,7 @@ bool _windowSetRegionFlag(const char* regionName, int value) for (int index = 0; index < managedWindow->regionsLength; index++) { Region* region = managedWindow->regions[index]; if (region != NULL) { - if (stricmp(region->name, regionName) == 0) { + if (compat_stricmp(region->name, regionName) == 0) { regionAddFlag(region, value); return true; } @@ -971,7 +972,7 @@ bool _windowAddRegionName(const char* regionName) if (index != managedWindow->currentRegionIndex) { Region* other = managedWindow->regions[index]; if (other != NULL) { - if (stricmp(regionGetName(other), regionName) == 0) { + if (compat_stricmp(regionGetName(other), regionName) == 0) { regionDelete(other); managedWindow->regions[index] = NULL; break; @@ -1003,7 +1004,7 @@ bool _windowDeleteRegion(const char* regionName) for (int index = 0; index < managedWindow->regionsLength; index++) { Region* region = managedWindow->regions[index]; if (region != NULL) { - if (stricmp(regionGetName(region), regionName) == 0) { + if (compat_stricmp(regionGetName(region), regionName) == 0) { regionDelete(region); managedWindow->regions[index] = NULL; managedWindow->field_38++; diff --git a/src/world_map.cc b/src/world_map.cc index eaabc32..6a4ada7 100644 --- a/src/world_map.cc +++ b/src/world_map.cc @@ -22,6 +22,7 @@ #include "object.h" #include "party_member.h" #include "perk.h" +#include "platform_compat.h" #include "proto_instance.h" #include "queue.h" #include "random.h" @@ -1306,7 +1307,7 @@ int worldmapConfigLoadEncounterEntry(EncounterEntry* entry, char* string) int _wmParseEncounterSubEncStr(EncounterEntry* encounterEntry, char** stringPtr) { char* string = *stringPtr; - if (strnicmp(string, "enc:", 4) != 0) { + if (compat_strnicmp(string, "enc:", 4) != 0) { return -1; } @@ -1411,7 +1412,7 @@ int _wmParseFindSubEncTypeMatch(char* str, int* valuePtr) { *valuePtr = 0; - if (stricmp(str, "player") == 0) { + if (compat_stricmp(str, "player") == 0) { *valuePtr = -1; return 0; } @@ -1431,7 +1432,7 @@ int _wmParseFindSubEncTypeMatch(char* str, int* valuePtr) int _wmFindEncBaseTypeMatch(char* str, int* valuePtr) { for (int index = 0; index < _wmMaxEncBaseTypes; index++) { - if (stricmp(_wmEncBaseTypeList[index].name, str) == 0) { + if (compat_stricmp(_wmEncBaseTypeList[index].name, str) == 0) { *valuePtr = index; return 0; } @@ -1700,7 +1701,7 @@ int _wmParseTerrainTypes(Config* config, char* string) worldmapTerrainInfoInit(terrain); } - strlwr(string); + compat_strlwr(string); pch = string; for (int index = 0; index < gTerrainsLength; index++) { @@ -1794,7 +1795,7 @@ int worldmapConfigLoadSubtile(TileInfo* tile, int row, int column, char* string) int worldmapFindEncounterTableByLookupName(char* string, int* valuePtr) { for (int index = 0; index < gEncounterTablesLength; index++) { - if (stricmp(string, gEncounterTables[index].lookupName) == 0) { + if (compat_stricmp(string, gEncounterTables[index].lookupName) == 0) { *valuePtr = index; return 0; } @@ -1812,7 +1813,7 @@ int worldmapFindTerrainByLookupName(char* string, int* valuePtr) { for (int index = 0; index < gTerrainsLength; index++) { Terrain* terrain = &(gTerrains[index]); - if (stricmp(string, terrain->field_0) == 0) { + if (compat_stricmp(string, terrain->field_0) == 0) { *valuePtr = index; return 0; } @@ -1840,7 +1841,7 @@ int _wmParseEncounterItemType(char** stringPtr, ENC_BASE_TYPE_38_48* a2, int* a3 return -1; } - strlwr(string); + compat_strlwr(string); if (*string == ',') { string++; @@ -1983,7 +1984,7 @@ int worldmapConfigParseConditionEntry(char** stringPtr, const char* a2, int* typ return -1; } - strlwr(string); + compat_strlwr(string); if (*string == ',') { string++; @@ -2383,7 +2384,7 @@ int worldmapFindMapByLookupName(char* string, int* valuePtr) { for (int index = 0; index < gMapsLength; index++) { MapInfo* map = &(gMaps[index]); - if (stricmp(string, map->lookupName) == 0) { + if (compat_stricmp(string, map->lookupName) == 0) { *valuePtr = index; return 0; } @@ -2471,7 +2472,7 @@ int _wmMapInit() exit(1); } - strlwr(str); + compat_strlwr(str); strncpy(map->mapFileName, str, 40); if (configGetString(&config, section, "music", &str)) { @@ -2636,7 +2637,7 @@ int mapGetFileName(int map, char* dest) // 0x4BF9BC int mapGetIndexByFileName(char* name) { - strlwr(name); + compat_strlwr(name); char* pch = name; while (*pch != '\0' && *pch != '.') { @@ -6323,7 +6324,7 @@ int worldmapCompareCitiesByName(const void* a1, const void* a2) CityInfo* city1 = &(gCities[v1]); CityInfo* city2 = &(gCities[v2]); - return stricmp(city1->name, city2->name); + return compat_stricmp(city1->name, city2->name); } // 0x4C5734 diff --git a/src/xfile.cc b/src/xfile.cc index ce3edeb..324766b 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -1,6 +1,7 @@ #include "xfile.h" #include "file_find.h" +#include "platform_compat.h" #include #include @@ -464,7 +465,7 @@ bool xbaseOpen(const char* path) XBase* curr = gXbaseHead; XBase* prev = NULL; while (curr != NULL) { - if (stricmp(path, curr->path) == 0) { + if (compat_stricmp(path, curr->path) == 0) { break; } From b1216967c036ab46728514c9dea2af63d9e413f2 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 12:34:49 +0300 Subject: [PATCH 4/9] Replace MAX_PATH with COMPAT_MAX_PATH --- src/art.cc | 9 +++---- src/art.h | 6 ++--- src/audio_file.cc | 6 ++--- src/automap.cc | 9 ++++--- src/character_editor.cc | 4 +-- src/character_editor.h | 3 ++- src/character_selector.cc | 5 ++-- src/combat.cc | 3 ++- src/combat_ai.cc | 2 +- src/credits.cc | 3 ++- src/critter.cc | 3 ++- src/dbox.cc | 7 +++--- src/dfile.h | 7 +++--- src/electronic_registration.cc | 3 ++- src/endgame.cc | 6 ++--- src/endgame.h | 6 ++--- src/game.cc | 6 ++--- src/game_config.cc | 3 ++- src/game_movie.cc | 5 ++-- src/game_movie.h | 6 ++--- src/game_sound.cc | 45 +++++++++++++++++----------------- src/game_sound.h | 3 ++- src/interface.cc | 3 ++- src/inventory.cc | 3 ++- src/item.cc | 3 ++- src/lips.cc | 3 ++- src/loadsave.cc | 24 +++++++++--------- src/loadsave.h | 14 +++++------ src/main.cc | 3 ++- src/map.cc | 9 +++---- src/map.h | 6 ++--- src/message.cc | 2 +- src/movie.cc | 3 ++- src/movie_effect.cc | 2 +- src/options.cc | 5 ++-- src/perk.cc | 3 ++- src/pipboy.cc | 3 ++- src/platform_compat.h | 6 +++++ src/proto.cc | 12 ++++----- src/proto.h | 6 ++--- src/scripts.cc | 11 +++++---- src/sfall_config.cc | 4 ++- src/skill.cc | 3 ++- src/skilldex.cc | 3 ++- src/sound_effects_list.cc | 2 +- src/stat.cc | 3 ++- src/text_font.cc | 6 ++--- src/trait.cc | 3 ++- src/window.cc | 2 +- src/world_map.cc | 4 +-- src/xfile.cc | 15 ++++++------ src/xfile.h | 4 ++- 52 files changed, 166 insertions(+), 154 deletions(-) diff --git a/src/art.cc b/src/art.cc index a6fcd5a..fc4ab4d 100644 --- a/src/art.cc +++ b/src/art.cc @@ -6,7 +6,6 @@ #include "game_config.h" #include "memory.h" #include "object.h" -#include "platform_compat.h" #include "proto.h" #include @@ -75,7 +74,7 @@ char gArtLanguage[32]; Cache gArtCache; // 0x56C9E4 -char _art_name[MAX_PATH]; +char _art_name[COMPAT_MAX_PATH]; // head_info // 0x56CAE8 @@ -92,7 +91,7 @@ int* gArtCritterFidShoudRunData; // 0x418840 int artInit() { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; int i; File* stream; char str[200]; @@ -897,7 +896,7 @@ int artCacheGetFileSizeImpl(int fid, int* sizePtr) char* str; char* ptr; int result; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; bool loaded; int fileSize; @@ -955,7 +954,7 @@ int artCacheReadDataImpl(int fid, int* sizePtr, unsigned char* data) char* str; char* ptr; int result; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; bool loaded; v4 = -1; diff --git a/src/art.h b/src/art.h index c537fdb..38258f8 100644 --- a/src/art.h +++ b/src/art.h @@ -5,11 +5,9 @@ #include "db.h" #include "heap.h" #include "obj_types.h" +#include "platform_compat.h" #include "proto_types.h" -#define WIN32_LEAN_AND_MEAN -#include - typedef enum Head { HEAD_INVALID, HEAD_MARCUS, @@ -136,7 +134,7 @@ extern int _art_mapper_blank_tile; extern char gArtLanguage[32]; extern Cache gArtCache; -extern char _art_name[MAX_PATH]; +extern char _art_name[COMPAT_MAX_PATH]; extern HeadDescription* gHeadDescriptions; extern int* _anon_alias; extern int* gArtCritterFidShoudRunData; diff --git a/src/audio_file.cc b/src/audio_file.cc index 64a85b4..d9001de 100644 --- a/src/audio_file.cc +++ b/src/audio_file.cc @@ -2,6 +2,7 @@ #include "debug.h" #include "memory_manager.h" +#include "platform_compat.h" #include "sound.h" #include @@ -9,9 +10,6 @@ #include #include -#define WIN32_LEAN_AND_MEAN -#include - // 0x5108C0 AudioFileIsCompressedProc* _queryCompressedFunc_2 = _defaultCompressionFunc__; @@ -41,7 +39,7 @@ int audioFileSoundDecoderReadHandler(int fileHandle, void* buffer, unsigned int // 0x41A88C int audioFileOpen(const char* fname, int flags, ...) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, fname); int compression; diff --git a/src/automap.cc b/src/automap.cc index b2bcf76..6df1686 100644 --- a/src/automap.cc +++ b/src/automap.cc @@ -15,6 +15,7 @@ #include "map.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "text_font.h" #include "window_manager.h" @@ -232,7 +233,7 @@ void automapExit() { char* masterPatchesPath; if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_MASTER_PATCHES_KEY, &masterPatchesPath)) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s\\%s", masterPatchesPath, "MAPS", AUTOMAP_DB); remove(path); } @@ -864,7 +865,7 @@ int automapLoadEntry(int map, int elevation) { gAutomapEntry.compressedData = NULL; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s", "MAPS", AUTOMAP_DB); bool success = true; @@ -1039,7 +1040,7 @@ int automapCreate() gAutomapHeader.dataSize = 1925; memcpy(gAutomapHeader.offsets, _defam, sizeof(_defam)); - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s", "MAPS", AUTOMAP_DB); File* stream = fileOpen(path, "wb"); @@ -1094,7 +1095,7 @@ int _copy_file_data(File* stream1, File* stream2, int length) // 0x41CE74 int automapGetHeader(AutomapHeader** automapHeaderPtr) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s", "MAPS", AUTOMAP_DB); File* stream = fileOpen(path, "rb"); diff --git a/src/character_editor.cc b/src/character_editor.cc index 779f000..021f5f7 100644 --- a/src/character_editor.cc +++ b/src/character_editor.cc @@ -988,7 +988,7 @@ int characterEditorWindowInit() int i; int v1; int v3; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; int fid; char* str; int len; @@ -3745,7 +3745,7 @@ int _OptionWindow() } } else if (keyCode == 501 || keyCode == KEY_UPPERCASE_L || keyCode == KEY_LOWERCASE_L) { // LOAD - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; path[0] = '\0'; strcat(path, "*."); strcat(path, "GCD"); diff --git a/src/character_editor.h b/src/character_editor.h index b863e47..a562caa 100644 --- a/src/character_editor.h +++ b/src/character_editor.h @@ -11,7 +11,8 @@ #include "stat_defs.h" #include "trait_defs.h" -#define DIALOG_PICKER_NUM_OPTIONS max(PERK_COUNT, TRAIT_COUNT) +// TODO: Should be MAX(PERK_COUNT, TRAIT_COUNT). +#define DIALOG_PICKER_NUM_OPTIONS PERK_COUNT #define TOWN_REPUTATION_COUNT 19 #define ADDICTION_REPUTATION_COUNT 8 diff --git a/src/character_selector.cc b/src/character_selector.cc index 233ae3d..3ad27ab 100644 --- a/src/character_selector.cc +++ b/src/character_selector.cc @@ -14,6 +14,7 @@ #include "object.h" #include "options.h" #include "palette.h" +#include "platform_compat.h" #include "proto.h" #include "skill.h" #include "stat.h" @@ -613,7 +614,7 @@ void characterSelectorWindowFree() // 0x4A7D58 bool characterSelectorWindowRefresh() { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s.gcd", gPremadeCharacterDescriptions[gCurrentPremadeCharacter].fileName); if (_proto_dude_init(path) == -1) { debugPrint("\n ** Error in dude init! **\n"); @@ -919,7 +920,7 @@ bool characterSelectorWindowRenderBio() int oldFont = fontGetCurrent(); fontSetCurrent(101); - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s.bio", gPremadeCharacterDescriptions[gCurrentPremadeCharacter].fileName); File* stream = fileOpen(path, "rt"); diff --git a/src/combat.cc b/src/combat.cc index 5f5eaa9..6d4102c 100644 --- a/src/combat.cc +++ b/src/combat.cc @@ -24,6 +24,7 @@ #include "object.h" #include "perk.h" #include "pipboy.h" +#include "platform_compat.h" #include "proto.h" #include "queue.h" #include "random.h" @@ -1864,7 +1865,7 @@ Attack _explosion_ctd; int combatInit() { int max_action_points; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; _combat_turn_running = 0; _combatNumTurns = 0; diff --git a/src/combat_ai.cc b/src/combat_ai.cc index 3d42f9b..e32d9ab 100644 --- a/src/combat_ai.cc +++ b/src/combat_ai.cc @@ -3252,7 +3252,7 @@ int aiMessageListInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "combatai.msg"); if (!messageListLoad(&gCombatAiMessageList, path)) { diff --git a/src/credits.cc b/src/credits.cc index 34e4270..6ea671a 100644 --- a/src/credits.cc +++ b/src/credits.cc @@ -10,6 +10,7 @@ #include "memory.h" #include "message.h" #include "palette.h" +#include "platform_compat.h" #include "sound.h" #include "text_font.h" #include "window_manager.h" @@ -52,7 +53,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle) soundContinueAll(); - char localizedPath[MAX_PATH]; + char localizedPath[COMPAT_MAX_PATH]; if (_message_make_path(localizedPath, filePath)) { gCreditsFile = fileOpen(localizedPath, "rt"); if (gCreditsFile != NULL) { diff --git a/src/critter.cc b/src/critter.cc index 968e5e2..f042c7b 100644 --- a/src/critter.cc +++ b/src/critter.cc @@ -14,6 +14,7 @@ #include "memory.h" #include "object.h" #include "party_member.h" +#include "platform_compat.h" #include "proto.h" #include "queue.h" #include "random.h" @@ -124,7 +125,7 @@ int critterInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%sscrname.msg", asc_5186C8); if (!messageListLoad(&gCritterMessageList, path)) { diff --git a/src/dbox.cc b/src/dbox.cc index 2e0b2c7..c4c315a 100644 --- a/src/dbox.cc +++ b/src/dbox.cc @@ -8,6 +8,7 @@ #include "game.h" #include "game_sound.h" #include "message.h" +#include "platform_compat.h" #include "text_font.h" #include "window_manager.h" #include "word_wrap.h" @@ -205,7 +206,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "DBOX.MSG"); if (!messageListLoad(&messageList, path)) { @@ -303,7 +304,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "DBOX.MSG"); if (!messageListLoad(&messageList, path)) { @@ -492,7 +493,7 @@ int _save_file_dialog(char* a1, char** fileList, char* fileName, int fileListLen return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "DBOX.MSG"); if (!messageListLoad(&messageList, path)) { diff --git a/src/dfile.h b/src/dfile.h index 54e5b2d..64d313b 100644 --- a/src/dfile.h +++ b/src/dfile.h @@ -1,8 +1,7 @@ #ifndef DFILE_H #define DFILE_H -#define WIN32_LEAN_AND_MEAN -#include +#include "platform_compat.h" #include #include @@ -116,13 +115,13 @@ typedef struct DFile { typedef struct DFileFindData { // The name of file that was found during previous search. - char fileName[MAX_PATH]; + char fileName[COMPAT_MAX_PATH]; // The pattern to search. // // This value is set automatically when [dbaseFindFirstEntry] succeeds so // that subsequent calls to [dbaseFindNextEntry] know what to look for. - char pattern[MAX_PATH]; + char pattern[COMPAT_MAX_PATH]; // The index of entry that was found during previous search. // diff --git a/src/electronic_registration.cc b/src/electronic_registration.cc index b81f311..b296000 100644 --- a/src/electronic_registration.cc +++ b/src/electronic_registration.cc @@ -1,6 +1,7 @@ #include "electronic_registration.h" #include "game_config.h" +#include "platform_compat.h" #define WIN32_LEAN_AND_MEAN #include @@ -11,7 +12,7 @@ void runElectronicRegistration() int timesRun = 0; configGetInt(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_TIMES_RUN_KEY, ×Run); if (timesRun > 0 && timesRun < 5) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (GetModuleFileNameA(NULL, path, sizeof(path)) != 0) { char* pch = strrchr(path, '\\'); if (pch == NULL) { diff --git a/src/endgame.cc b/src/endgame.cc index cde8413..8d81c60 100644 --- a/src/endgame.cc +++ b/src/endgame.cc @@ -87,7 +87,7 @@ char gEndgameDeathEndingFileName[40]; bool gEndgameEndingVoiceOverSpeechLoaded; // 0x570ABC -char gEndgameEndingSubtitlesLocalizedPath[MAX_PATH]; +char gEndgameEndingSubtitlesLocalizedPath[COMPAT_MAX_PATH]; // The flag used to denote voice over speech for current slide has ended. // @@ -580,7 +580,7 @@ void endgameEndingSlideshowWindowFree() // 0x4401A0 void endgameEndingVoiceOverInit(const char* fileBaseName) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; // NOTE: Uninline. endgameEndingVoiceOverFree(); @@ -666,7 +666,7 @@ void endgameEndingLoadPalette(int type, int id) } if (strlen(fileName) <= 8) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s.pal", "art\\intrface", fileName); colorPaletteLoad(path); } diff --git a/src/endgame.h b/src/endgame.h index b3a2bdb..b7ae1c7 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -1,9 +1,7 @@ #ifndef ENDGAME_H #define ENDGAME_H -// Provides [MAX_PATH]. -#define WIN32_LEAN_AND_MEAN -#include +#include "platform_compat.h" typedef enum EndgameDeathEndingReason { // Dude died. @@ -46,7 +44,7 @@ extern int gEndgameDeathEndingsLength; extern char gEndgameDeathEndingFileName[40]; extern bool gEndgameEndingVoiceOverSpeechLoaded; -extern char gEndgameEndingSubtitlesLocalizedPath[MAX_PATH]; +extern char gEndgameEndingSubtitlesLocalizedPath[COMPAT_MAX_PATH]; extern bool gEndgameEndingSpeechEnded; extern EndgameEnding* gEndgameEndings; extern char** gEndgameEndingSubtitles; diff --git a/src/game.cc b/src/game.cc index a023a35..eeeb359 100644 --- a/src/game.cc +++ b/src/game.cc @@ -111,7 +111,7 @@ int _critter_db_handle; // 0x442580 int gameInitWithOptions(const char* windowTitle, bool isMapper, int font, int a4, int argc, char** argv) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (gameMemoryInit() == -1) { return -1; @@ -725,7 +725,7 @@ int gameHandleKey(int eventCode, bool isInCombatMode) MessageList messageList; if (messageListInit(&messageList)) { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "editor.msg"); if (messageListLoad(&messageList, path)) { @@ -1162,7 +1162,7 @@ int gameDbInit() char* main_file_name; char* patch_file_name; int patch_index; - char filename[MAX_PATH]; + char filename[COMPAT_MAX_PATH]; hashing = 0; main_file_name = NULL; diff --git a/src/game_config.cc b/src/game_config.cc index be5041d..b671b4e 100644 --- a/src/game_config.cc +++ b/src/game_config.cc @@ -1,6 +1,7 @@ #include "game_config.h" #include "main.h" +#include "platform_compat.h" #include #include @@ -19,7 +20,7 @@ Config gGameConfig; // probably means it's size is 264 bytes. // // 0x58E978 -char gGameConfigFilePath[FILENAME_MAX]; +char gGameConfigFilePath[COMPAT_MAX_PATH]; // Inits main game config. // diff --git a/src/game_movie.cc b/src/game_movie.cc index afefaf1..8dd2ca3 100644 --- a/src/game_movie.cc +++ b/src/game_movie.cc @@ -11,7 +11,6 @@ #include "movie.h" #include "movie_effect.h" #include "palette.h" -#include "platform_compat.h" #include "text_font.h" #include "widget.h" #include "window_manager.h" @@ -77,7 +76,7 @@ bool gGameMovieFaded = false; unsigned char gGameMoviesSeen[MOVIE_COUNT]; // 0x596C89 -char gGameMovieSubtitlesFilePath[MAX_PATH]; +char gGameMovieSubtitlesFilePath[COMPAT_MAX_PATH]; // gmovie_init // 0x44E5C0 @@ -145,7 +144,7 @@ int gameMoviePlay(int movie, int flags) return -1; } - char movieFilePath[MAX_PATH]; + char movieFilePath[COMPAT_MAX_PATH]; int movieFileSize; bool movieFound = false; diff --git a/src/game_movie.h b/src/game_movie.h index fb574ec..7ca8cb0 100644 --- a/src/game_movie.h +++ b/src/game_movie.h @@ -2,9 +2,7 @@ #define GAME_MOVIE_H #include "db.h" - -#define WIN32_LEAN_AND_MEAN -#include +#include "platform_compat.h" typedef enum GameMovieFlags { GAME_MOVIE_FADE_IN = 0x01, @@ -41,7 +39,7 @@ extern char* gMoviePaletteFilePaths[MOVIE_COUNT]; extern bool gGameMovieIsPlaying; extern bool gGameMovieFaded; -extern char gGameMovieSubtitlesFilePath[MAX_PATH]; +extern char gGameMovieSubtitlesFilePath[COMPAT_MAX_PATH]; extern unsigned char gGameMoviesSeen[MOVIE_COUNT]; int gameMoviesInit(); diff --git a/src/game_sound.cc b/src/game_sound.cc index 937c11d..47eab43 100644 --- a/src/game_sound.cc +++ b/src/game_sound.cc @@ -13,7 +13,6 @@ #include "memory.h" #include "movie.h" #include "object.h" -#include "platform_compat.h" #include "proto.h" #include "queue.h" #include "random.h" @@ -125,7 +124,7 @@ int _detectDevices = -1; int _lastTime_1 = 0; // 0x596EB0 -char _background_fname_copied[MAX_PATH]; +char _background_fname_copied[COMPAT_MAX_PATH]; // 0x596FB5 char _sfx_file_name[13]; @@ -614,7 +613,7 @@ int backgroundSoundLoad(const char* fileName, int a2, int a3, int a4) return -1; } - char path[MAX_PATH + 1]; + char path[COMPAT_MAX_PATH + 1]; if (a3 == 13) { rc = gameSoundFindBackgroundSoundPath(path, fileName); } else if (a3 == 14) { @@ -838,7 +837,7 @@ int speechGetDuration() // 0x450CA0 int speechLoad(const char* fname, int a2, int a3, int a4) { - char path[MAX_PATH + 1]; + char path[COMPAT_MAX_PATH + 1]; if (!gGameSoundInitialized) { return -1; @@ -1064,7 +1063,7 @@ Sound* soundEffectLoad(const char* name, Object* object) ++_gsound_active_effect_counter; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s%s", _sound_sfx_path, name, ".ACM"); if (soundLoad(sound, path) == 0) { @@ -1675,7 +1674,7 @@ int _gsound_background_allocate(Sound** soundPtr, int a2, int a3) int gameSoundFindBackgroundSoundPathWithCopy(char* dest, const char* src) { size_t len = strlen(src) + strlen(".ACM"); - if (strlen(_sound_music_path1) + len > MAX_PATH || strlen(_sound_music_path2) + len > MAX_PATH) { + if (strlen(_sound_music_path1) + len > COMPAT_MAX_PATH || strlen(_sound_music_path2) + len > COMPAT_MAX_PATH) { if (gGameSoundDebugEnabled) { debugPrint("Full background path too long.\n"); } @@ -1687,11 +1686,11 @@ int gameSoundFindBackgroundSoundPathWithCopy(char* dest, const char* src) debugPrint(" finding background sound "); } - char outPath[MAX_PATH]; + char outPath[COMPAT_MAX_PATH]; sprintf(outPath, "%s%s%s", _sound_music_path1, src, ".ACM"); if (_gsound_file_exists_f(outPath)) { - strncpy(dest, outPath, MAX_PATH); - dest[MAX_PATH] = '\0'; + strncpy(dest, outPath, COMPAT_MAX_PATH); + dest[COMPAT_MAX_PATH] = '\0'; return 0; } @@ -1701,7 +1700,7 @@ int gameSoundFindBackgroundSoundPathWithCopy(char* dest, const char* src) gameSoundDeleteOldMusicFile(); - char inPath[MAX_PATH]; + char inPath[COMPAT_MAX_PATH]; sprintf(inPath, "%s%s%s", _sound_music_path2, src, ".ACM"); FILE* inStream = fopen(inPath, "rb"); @@ -1764,8 +1763,8 @@ int gameSoundFindBackgroundSoundPathWithCopy(char* dest, const char* src) strcpy(_background_fname_copied, src); - strncpy(dest, outPath, MAX_PATH); - dest[MAX_PATH] = '\0'; + strncpy(dest, outPath, COMPAT_MAX_PATH); + dest[COMPAT_MAX_PATH] = '\0'; return 0; } @@ -1773,11 +1772,11 @@ int gameSoundFindBackgroundSoundPathWithCopy(char* dest, const char* src) // 0x451E2C int gameSoundFindBackgroundSoundPath(char* dest, const char* src) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; int len; len = strlen(src) + strlen(".ACM"); - if (strlen(_sound_music_path1) + len > MAX_PATH || strlen(_sound_music_path2) + len > MAX_PATH) { + if (strlen(_sound_music_path1) + len > COMPAT_MAX_PATH || strlen(_sound_music_path2) + len > COMPAT_MAX_PATH) { if (gGameSoundDebugEnabled) { debugPrint("Full background path too long.\n"); } @@ -1791,8 +1790,8 @@ int gameSoundFindBackgroundSoundPath(char* dest, const char* src) sprintf(path, "%s%s%s", _sound_music_path1, src, ".ACM"); if (_gsound_file_exists_f(path)) { - strncpy(dest, path, MAX_PATH); - dest[MAX_PATH] = '\0'; + strncpy(dest, path, COMPAT_MAX_PATH); + dest[COMPAT_MAX_PATH] = '\0'; return 0; } @@ -1802,8 +1801,8 @@ int gameSoundFindBackgroundSoundPath(char* dest, const char* src) sprintf(path, "%s%s%s", _sound_music_path2, src, ".ACM"); if (_gsound_file_exists_f(path)) { - strncpy(dest, path, MAX_PATH); - dest[MAX_PATH] = '\0'; + strncpy(dest, path, COMPAT_MAX_PATH); + dest[COMPAT_MAX_PATH] = '\0'; return 0; } @@ -1817,9 +1816,9 @@ int gameSoundFindBackgroundSoundPath(char* dest, const char* src) // 0x451F94 int gameSoundFindSpeechSoundPath(char* dest, const char* src) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; - if (strlen(_sound_speech_path) + strlen(".acm") > MAX_PATH) { + if (strlen(_sound_speech_path) + strlen(".acm") > COMPAT_MAX_PATH) { if (gGameSoundDebugEnabled) { // FIXME: The message is wrong (notes background path, but here // we're dealing with speech path). @@ -1845,8 +1844,8 @@ int gameSoundFindSpeechSoundPath(char* dest, const char* src) return -1; } - strncpy(dest, path, MAX_PATH); - dest[MAX_PATH] = '\0'; + strncpy(dest, path, COMPAT_MAX_PATH); + dest[COMPAT_MAX_PATH] = '\0'; return 0; } @@ -1856,7 +1855,7 @@ int gameSoundFindSpeechSoundPath(char* dest, const char* src) void gameSoundDeleteOldMusicFile() { if (_background_fname_copied[0] != '\0') { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s%s", "sound\\music\\", _background_fname_copied, ".ACM"); if (remove(path)) { if (gGameSoundDebugEnabled) { diff --git a/src/game_sound.h b/src/game_sound.h index bb23ac8..00be6c8 100644 --- a/src/game_sound.h +++ b/src/game_sound.h @@ -2,6 +2,7 @@ #define GAME_SOUND_H #include "obj_types.h" +#include "platform_compat.h" #include "sound.h" #define WIN32_LEAN_AND_MEAN @@ -71,7 +72,7 @@ extern int gSoundEffectsVolume; extern int _detectDevices; extern int _lastTime_1; -extern char _background_fname_copied[MAX_PATH]; +extern char _background_fname_copied[COMPAT_MAX_PATH]; extern char _sfx_file_name[13]; extern char gBackgroundSoundFileName[270]; diff --git a/src/interface.cc b/src/interface.cc index 337b450..baf62fa 100644 --- a/src/interface.cc +++ b/src/interface.cc @@ -18,6 +18,7 @@ #include "item.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "proto_types.h" @@ -2318,7 +2319,7 @@ int indicatorBarInit() rc = -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "intrface.msg"); if (rc != -1) { diff --git a/src/inventory.cc b/src/inventory.cc index 6c670fc..5dd1393 100644 --- a/src/inventory.cc +++ b/src/inventory.cc @@ -23,6 +23,7 @@ #include "map.h" #include "object.h" #include "perk.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "random.h" @@ -356,7 +357,7 @@ void _inven_reset_dude() // 0x46E73C int inventoryMessageListInit() { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (!messageListInit(&gInventoryMessageList)) return -1; diff --git a/src/item.cc b/src/item.cc index b0fdbc3..9bcb921 100644 --- a/src/item.cc +++ b/src/item.cc @@ -14,6 +14,7 @@ #include "memory.h" #include "object.h" #include "perk.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "queue.h" @@ -110,7 +111,7 @@ int itemsInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "item.msg"); if (!messageListLoad(&gItemsMessageList, path)) { diff --git a/src/lips.cc b/src/lips.cc index fa8b2a0..fb3657f 100644 --- a/src/lips.cc +++ b/src/lips.cc @@ -5,6 +5,7 @@ #include "debug.h" #include "game_sound.h" #include "memory.h" +#include "platform_compat.h" #include "sound.h" #include @@ -393,7 +394,7 @@ int _lips_make_speech() gLipsData.field_14 = NULL; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; char* v1 = _lips_fix_string(gLipsData.field_50, sizeof(gLipsData.field_50)); sprintf(path, "%s%s\\%s.%s", "SOUND\\SPEECH\\", _lips_subdir_name, v1, "ACM"); diff --git a/src/loadsave.cc b/src/loadsave.cc index baab366..9cec59a 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -190,22 +190,22 @@ unsigned char* gLoadSaveFrmData[LOAD_SAVE_FRM_COUNT]; unsigned char* _snapshot; // 0x6142F0 -char _str2[MAX_PATH]; +char _str2[COMPAT_MAX_PATH]; // 0x6143F4 -char _str0[MAX_PATH]; +char _str0[COMPAT_MAX_PATH]; // 0x6144F8 -char _str1[MAX_PATH]; +char _str1[COMPAT_MAX_PATH]; // 0x6145FC -char _str[MAX_PATH]; +char _str[COMPAT_MAX_PATH]; // 0x614700 unsigned char* gLoadSaveWindowBuffer; // 0x614704 -char _gmpath[MAX_PATH]; +char _gmpath[COMPAT_MAX_PATH]; // 0x614808 File* _flptr; @@ -289,7 +289,7 @@ int lsgSaveGame(int mode) return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "LSGAME.MSG"); if (!messageListLoad(&gLoadSaveMessageList, path)) { return -1; @@ -687,7 +687,7 @@ int lsgLoadGame(int mode) return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s", asc_5186C8, "LSGAME.MSG"); if (!messageListLoad(&gLoadSaveMessageList, path)) { return -1; @@ -2132,7 +2132,7 @@ int _GameMap2Slot(File* stream) continue; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (_proto_list_str(pid, path) != 0) { continue; } @@ -2268,7 +2268,7 @@ int _SlotMap2Game(File* stream) for (int index = 1; index < gPartyMemberDescriptionsLength; index += 1) { int pid = gPartyMemberPids[index]; if (pid != -2) { - char protoPath[MAX_PATH]; + char protoPath[COMPAT_MAX_PATH]; if (_proto_list_str(pid, protoPath) == 0) { const char* basePath = pid >> 24 == OBJ_TYPE_CRITTER ? "PROTO\\CRITTERS" @@ -2429,7 +2429,7 @@ out: // 0x48000C void lsgInit() { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\", "MAPS"); _MapDirErase(path, "SAV"); } @@ -2437,7 +2437,7 @@ void lsgInit() // 0x480040 int _MapDirErase(const char* relativePath, const char* extension) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s*.%s", relativePath, extension); char** fileList; @@ -2454,7 +2454,7 @@ int _MapDirErase(const char* relativePath, const char* extension) // 0x4800C8 int _MapDirEraseFile_(const char* a1, const char* a2) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s%s", _patches, a1, a2); if (remove(path) != 0) { diff --git a/src/loadsave.h b/src/loadsave.h index 112742b..747bf99 100644 --- a/src/loadsave.h +++ b/src/loadsave.h @@ -5,9 +5,7 @@ #include "db.h" #include "geometry.h" #include "message.h" - -#define WIN32_LEAN_AND_MEAN -#include +#include "platform_compat.h" #define LOAD_SAVE_DESCRIPTION_LENGTH (30) #define LOAD_SAVE_HANDLER_COUNT (27) @@ -103,12 +101,12 @@ extern int _dbleclkcntr; extern int gLoadSaveWindow; extern unsigned char* gLoadSaveFrmData[LOAD_SAVE_FRM_COUNT]; extern unsigned char* _snapshot; -extern char _str2[MAX_PATH]; -extern char _str0[MAX_PATH]; -extern char _str1[MAX_PATH]; -extern char _str[MAX_PATH]; +extern char _str2[COMPAT_MAX_PATH]; +extern char _str0[COMPAT_MAX_PATH]; +extern char _str1[COMPAT_MAX_PATH]; +extern char _str[COMPAT_MAX_PATH]; extern unsigned char* gLoadSaveWindowBuffer; -extern char _gmpath[MAX_PATH]; +extern char _gmpath[COMPAT_MAX_PATH]; extern File* _flptr; extern int _ls_error_code; extern int gLoadSaveWindowOldFont; diff --git a/src/main.cc b/src/main.cc index 8b0badf..964dd94 100644 --- a/src/main.cc +++ b/src/main.cc @@ -20,6 +20,7 @@ #include "object.h" #include "options.h" #include "palette.h" +#include "platform_compat.h" #include "random.h" #include "scripts.h" #include "sfall_config.h" @@ -510,7 +511,7 @@ int _mainDeathGrabTextFile(const char* fileName, char* dest) language = _aEnglish_2; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "text\\%s\\cuts\\%s%s", language, p + 1, ".TXT"); File* stream = fileOpen(path, "rt"); diff --git a/src/map.cc b/src/map.cc index e45a4c8..21350b2 100644 --- a/src/map.cc +++ b/src/map.cc @@ -23,7 +23,6 @@ #include "object.h" #include "palette.h" #include "pipboy.h" -#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "queue.h" @@ -131,7 +130,7 @@ char _scratchStr[40]; // Last map file name. // // 0x631E78 -char _map_path[MAX_PATH]; +char _map_path[COMPAT_MAX_PATH]; // iso_init // 0x481CA0 @@ -261,7 +260,7 @@ void _map_init() } if (messageListInit(&gMapMessageList)) { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%smap.msg", asc_5186C8); if (!messageListLoad(&gMapMessageList, path)) { @@ -876,7 +875,7 @@ int mapLoad(File* stream) gMapHeader.field_34 = mapGetIndexByFileName(gMapHeader.name); if ((gMapHeader.flags & 1) == 0) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "maps\\%s", gMapHeader.name); char* extension = strstr(path, ".MAP"); @@ -1443,7 +1442,7 @@ int _map_save_in_game(bool a1) // 0x483E28 void mapMakeMapsDirectory() { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; char* masterPatchesPath; if (configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_MASTER_PATCHES_KEY, &masterPatchesPath)) { diff --git a/src/map.h b/src/map.h index cc9ca6f..b658d5b 100644 --- a/src/map.h +++ b/src/map.h @@ -6,9 +6,7 @@ #include "geometry.h" #include "map_defs.h" #include "message.h" - -#define WIN32_LEAN_AND_MEAN -#include +#include "platform_compat.h" #define ORIGINAL_ISO_WINDOW_WIDTH 640 #define ORIGINAL_ISO_WINDOW_HEIGHT 380 @@ -91,7 +89,7 @@ extern MapHeader gMapHeader; extern TileData* _square[ELEVATION_COUNT]; extern int gIsoWindow; extern char _scratchStr[40]; -extern char _map_path[MAX_PATH]; +extern char _map_path[COMPAT_MAX_PATH]; int isoInit(); void isoReset(); diff --git a/src/message.cc b/src/message.cc index e15e5f7..f620a2b 100644 --- a/src/message.cc +++ b/src/message.cc @@ -167,7 +167,7 @@ bool messageListFree(MessageList* messageList) bool messageListLoad(MessageList* messageList, const char* path) { char* language; - char localized_path[FILENAME_MAX]; + char localized_path[COMPAT_MAX_PATH]; File* file_ptr; char num[1024]; char audio[1024]; diff --git a/src/movie.cc b/src/movie.cc index d7477b7..f577cd8 100644 --- a/src/movie.cc +++ b/src/movie.cc @@ -8,6 +8,7 @@ #include "memory_manager.h" #include "movie_effect.h" #include "movie_lib.h" +#include "platform_compat.h" #include "sound.h" #include "text_font.h" #include "window_manager.h" @@ -521,7 +522,7 @@ void movieLoadSubtitles(char* filePath) filePath = gMovieBuildSubtitleFilePathProc(filePath); } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, filePath); debugPrint("Opening subtitle file %s\n", path); diff --git a/src/movie_effect.cc b/src/movie_effect.cc index 830243f..807310e 100644 --- a/src/movie_effect.cc +++ b/src/movie_effect.cc @@ -93,7 +93,7 @@ int movieEffectsLoad(const char* filePath) int rc = -1; - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; strcpy(path, filePath); char* pch = strrchr(path, '.'); diff --git a/src/options.cc b/src/options.cc index 24c2bb3..7819dfc 100644 --- a/src/options.cc +++ b/src/options.cc @@ -14,6 +14,7 @@ #include "grayscale.h" #include "loadsave.h" #include "memory.h" +#include "platform_compat.h" #include "scripts.h" #include "text_font.h" #include "text_object.h" @@ -453,7 +454,7 @@ int optionsWindowInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "options.msg"); if (!messageListLoad(&gOptionsMessageList, path)) { return -1; @@ -633,7 +634,7 @@ int showPause(bool a1) return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "options.msg"); if (!messageListLoad(&gOptionsMessageList, path)) { // FIXME: Leaking graphics. diff --git a/src/perk.cc b/src/perk.cc index c50aad4..57414a1 100644 --- a/src/perk.cc +++ b/src/perk.cc @@ -6,6 +6,7 @@ #include "memory.h" #include "object.h" #include "party_member.h" +#include "platform_compat.h" #include "skill.h" #include "stat.h" @@ -164,7 +165,7 @@ int perksInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "perk.msg"); if (!messageListLoad(&gPerksMessageList, path)) { diff --git a/src/pipboy.cc b/src/pipboy.cc index 1dbb0a1..bd5eac3 100644 --- a/src/pipboy.cc +++ b/src/pipboy.cc @@ -19,6 +19,7 @@ #include "map.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "queue.h" #include "random.h" #include "scripts.h" @@ -315,7 +316,7 @@ int pipboyWindowInit(bool forceRest) return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "pipboy.msg"); if (!(messageListLoad(&gPipboyMessageList, path))) { diff --git a/src/platform_compat.h b/src/platform_compat.h index b13b3f6..6c268f9 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -4,6 +4,12 @@ // TODO: This is compatibility cross-platform layer. Designed to have minimal // impact on the codebase. Remove once it's no longer needed. +// A naive cross-platform MAX_PATH/PATH_MAX/MAX_PATH drop-in replacement. +// +// TODO: Remove when we migrate to use std::filesystem::path or std::string to +// represent paths across the codebase. +#define COMPAT_MAX_PATH 260 + int compat_stricmp(const char* string1, const char* string2); int compat_strnicmp(const char* string1, const char* string2, size_t size); char* compat_strupr(char* string); diff --git a/src/proto.cc b/src/proto.cc index 972bf5a..46bab71 100644 --- a/src/proto.cc +++ b/src/proto.cc @@ -33,7 +33,7 @@ char _aDrugStatSpecia[] = "Drug Stat (Special)"; char _aNone_1[] = "None"; // 0x51C18C -char _cd_path_base[MAX_PATH]; +char _cd_path_base[COMPAT_MAX_PATH]; // 0x51C290 ProtoList _protoLists[11] = { @@ -173,8 +173,8 @@ char** _critter_stats_list; // 0x49E758 int _proto_list_str(int pid, char* proto_path) { - char path[MAX_PATH]; - char str[MAX_PATH]; + char path[COMPAT_MAX_PATH]; + char str[COMPAT_MAX_PATH]; char* pch; File* stream; int i; @@ -1037,7 +1037,7 @@ int protoInit() char* master_patches; int len; MessageListItem messageListItem; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; int i; if (!configGetString(&gGameConfig, GAME_CONFIG_SYSTEM_KEY, GAME_CONFIG_MASTER_PATCHES_KEY, &master_patches)) { @@ -1215,7 +1215,7 @@ int _proto_header_load() ptr->length = 0; ptr->max_entries_num = 1; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, _cd_path_base); strcat(path, _proto_path_base); strcat(path, artGetObjectTypeName(index)); @@ -1654,7 +1654,7 @@ int _proto_save_pid(int pid) // 0x4A1C3C int _proto_load_pid(int pid, Proto** protoPtr) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, _cd_path_base); strcat(path, "proto\\"); diff --git a/src/proto.h b/src/proto.h index 1609bc2..e757336 100644 --- a/src/proto.h +++ b/src/proto.h @@ -5,13 +5,11 @@ #include "message.h" #include "obj_types.h" #include "perk_defs.h" +#include "platform_compat.h" #include "proto_types.h" #include "skill_defs.h" #include "stat_defs.h" -#define WIN32_LEAN_AND_MEAN -#include - typedef enum ItemDataMember { ITEM_DATA_MEMBER_PID = 0, ITEM_DATA_MEMBER_NAME = 1, @@ -97,7 +95,7 @@ typedef enum PrototypeMessage { extern char _aProto_0[]; -extern char _cd_path_base[MAX_PATH]; +extern char _cd_path_base[COMPAT_MAX_PATH]; extern ProtoList _protoLists[11]; extern const size_t _proto_sizes[11]; extern int _protos_been_initialized; diff --git a/src/scripts.cc b/src/scripts.cc index dd183a3..cafade4 100644 --- a/src/scripts.cc +++ b/src/scripts.cc @@ -16,6 +16,7 @@ #include "game_movie.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "proto.h" #include "proto_instance.h" #include "queue.h" @@ -596,7 +597,7 @@ int scriptSetActionBeingUsed(int sid, int value) // 0x4A3B74 Program* scriptsCreateProgramByName(const char* name) { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, _cd_path_base); strcat(path, gScriptsBasePath); @@ -1291,7 +1292,7 @@ bool scriptHasProc(int sid, int proc) // 0x4A4D50 int scriptsLoadScriptsList() { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", "scripts\\", "scripts.lst"); File* stream = fileOpen(path, "rt"); @@ -1477,7 +1478,7 @@ int _scr_reset() int _scr_game_init() { int i; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (!messageListInit(&gScrMessageList)) { debugPrint("\nError initing script message file!"); @@ -1666,7 +1667,7 @@ int _scr_header_load() { _num_script_indexes = 0; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; strcpy(path, _cd_path_base); strcat(path, gScriptsBasePath); strcat(path, "scripts.lst"); @@ -2590,7 +2591,7 @@ int scriptsGetMessageList(int a1, MessageList** messageListPtr) *pch = '\0'; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "dialog\\%s.msg", scriptName); if (!messageListLoad(messageList, path)) { diff --git a/src/sfall_config.cc b/src/sfall_config.cc index fac4faa..5122495 100644 --- a/src/sfall_config.cc +++ b/src/sfall_config.cc @@ -1,5 +1,7 @@ #include "sfall_config.h" +#include "platform_compat.h" + #include #include @@ -21,7 +23,7 @@ bool sfallConfigInit(int argc, char** argv) // Initialize defaults. configSetString(&gSfallConfig, SFALL_CONFIG_MISC_KEY, SFALL_CONFIG_STARTING_MAP_KEY, ""); - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; char* executable = argv[0]; char* ch = strrchr(executable, '\\'); if (ch != NULL) { diff --git a/src/skill.cc b/src/skill.cc index 44c5611..ba01e2f 100644 --- a/src/skill.cc +++ b/src/skill.cc @@ -15,6 +15,7 @@ #include "party_member.h" #include "perk.h" #include "pipboy.h" +#include "platform_compat.h" #include "proto.h" #include "random.h" #include "scripts.h" @@ -96,7 +97,7 @@ int skillsInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "skill.msg"); if (!messageListLoad(&gSkillsMessageList, path)) { diff --git a/src/skilldex.cc b/src/skilldex.cc index 3c18c84..28604e7 100644 --- a/src/skilldex.cc +++ b/src/skilldex.cc @@ -12,6 +12,7 @@ #include "map.h" #include "memory.h" #include "object.h" +#include "platform_compat.h" #include "skill.h" #include "text_font.h" #include "window_manager.h" @@ -122,7 +123,7 @@ int skilldexWindowInit() return -1; } - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "skilldex.msg"); if (!messageListLoad(&gSkilldexMessageList, path)) { diff --git a/src/sound_effects_list.cc b/src/sound_effects_list.cc index 3d6d0e6..8d28162 100644 --- a/src/sound_effects_list.cc +++ b/src/sound_effects_list.cc @@ -50,7 +50,7 @@ bool soundEffectsListIsValidTag(int a1) // 0x4A98F4 int soundEffectsListInit(const char* soundEffectsPath, int a2, int debugLevel) { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; // TODO: What for? // memcpy(path, byte_4A97E0, 0xFF); diff --git a/src/stat.cc b/src/stat.cc index 87f434f..84bde62 100644 --- a/src/stat.cc +++ b/src/stat.cc @@ -11,6 +11,7 @@ #include "memory.h" #include "object.h" #include "perk.h" +#include "platform_compat.h" #include "proto.h" #include "random.h" #include "scripts.h" @@ -92,7 +93,7 @@ int statsInit() return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "stat.msg"); if (!messageListLoad(&gStatsMessageList, path)) { diff --git a/src/text_font.cc b/src/text_font.cc index 54efe4a..e9facec 100644 --- a/src/text_font.cc +++ b/src/text_font.cc @@ -3,13 +3,11 @@ #include "color.h" #include "db.h" #include "memory.h" +#include "platform_compat.h" #include #include -#define WIN32_LEAN_AND_MEAN -#include - // 0x4D5530 FontManager gTextFontManager = { 0, @@ -112,7 +110,7 @@ int textFontLoad(int font) { int rc = -1; - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "font%d.fon", font); // NOTE: Original code is slightly different. It uses deep nesting and diff --git a/src/trait.cc b/src/trait.cc index 43eb1c8..d833315 100644 --- a/src/trait.cc +++ b/src/trait.cc @@ -2,6 +2,7 @@ #include "game.h" #include "object.h" +#include "platform_compat.h" #include "skill.h" #include "stat.h" @@ -42,7 +43,7 @@ int traitsInit() return -1; } - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s%s", asc_5186C8, "trait.msg"); if (!messageListLoad(&gTraitsMessageList, path)) { diff --git a/src/window.cc b/src/window.cc index d75f398..0db6aff 100644 --- a/src/window.cc +++ b/src/window.cc @@ -498,7 +498,7 @@ void _removeProgramReferences_3(Program* program) // 0x4B9190 void _initWindow(int resolution, int a2) { - char err[MAX_PATH]; + char err[COMPAT_MAX_PATH]; int rc; int i, j; diff --git a/src/world_map.cc b/src/world_map.cc index 6a4ada7..7809233 100644 --- a/src/world_map.cc +++ b/src/world_map.cc @@ -612,7 +612,7 @@ int gEncounterTablesLength; // 0x4BC89C int worldmapInit() { - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; if (_wmGenDataInit() == -1) { return -1; @@ -4028,7 +4028,7 @@ int _wmGrabTileWalkMask(int tile) return -1; } - char path[MAX_PATH]; + char path[COMPAT_MAX_PATH]; sprintf(path, "data\\%s.msk", tileInfo->walkMaskName); File* stream = fileOpen(path, "rb"); diff --git a/src/xfile.cc b/src/xfile.cc index 324766b..e4dd5c3 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -1,7 +1,6 @@ #include "xfile.h" #include "file_find.h" -#include "platform_compat.h" #include #include @@ -60,7 +59,7 @@ XFile* xfileOpen(const char* filePath, const char* mode) char dir[_MAX_DIR]; _splitpath(filePath, drive, dir, NULL, NULL); - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') { // [filePath] is an absolute path. Attempt to open as plain stream. stream->file = fopen(filePath, mode); @@ -505,8 +504,8 @@ bool xbaseOpen(const char* path) return true; } - char workingDirectory[FILENAME_MAX]; - if (getcwd(workingDirectory, FILENAME_MAX) == NULL) { + char workingDirectory[COMPAT_MAX_PATH]; + if (getcwd(workingDirectory, COMPAT_MAX_PATH) == NULL) { // FIXME: Leaking xbase and path. return false; } @@ -600,7 +599,7 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList dbaseFindClose(xbase->dbase, &dbaseFindData); } } else { - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; sprintf(path, "%s\\%s", xbase->path, pattern); if (fileFindFirst(path, &directoryFileFindData)) { @@ -704,8 +703,8 @@ void xlistFree(XList* xlist) // 0x4DFFAC int xbaseMakeDirectory(const char* filePath) { - char workingDirectory[FILENAME_MAX]; - if (getcwd(workingDirectory, FILENAME_MAX) == NULL) { + char workingDirectory[COMPAT_MAX_PATH]; + if (getcwd(workingDirectory, COMPAT_MAX_PATH) == NULL) { return -1; } @@ -713,7 +712,7 @@ int xbaseMakeDirectory(const char* filePath) char dir[_MAX_DIR]; _splitpath(filePath, drive, dir, NULL, NULL); - char path[FILENAME_MAX]; + char path[COMPAT_MAX_PATH]; if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') { // [filePath] is an absolute path. strcpy(path, filePath); diff --git a/src/xfile.h b/src/xfile.h index 50bfd35..c63ebfb 100644 --- a/src/xfile.h +++ b/src/xfile.h @@ -3,6 +3,8 @@ #include "dfile.h" +#include "platform_compat.h" + #include #include @@ -51,7 +53,7 @@ typedef enum XFileEnumerationEntryType { } XFileEnumerationEntryType; typedef struct XListEnumerationContext { - char name[FILENAME_MAX]; + char name[COMPAT_MAX_PATH]; unsigned char type; XList* xlist; } XListEnumerationContext; From bf9b865efd1da1651ba76bb9d432cb28438fe096 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 12:48:26 +0300 Subject: [PATCH 5/9] Add missing include --- src/fps_limiter.cc | 2 +- src/fps_limiter.h | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fps_limiter.cc b/src/fps_limiter.cc index e91040d..5c1897a 100644 --- a/src/fps_limiter.cc +++ b/src/fps_limiter.cc @@ -2,7 +2,7 @@ #include -FpsLimiter::FpsLimiter(size_t fps) +FpsLimiter::FpsLimiter(std::size_t fps) : _fps(fps) , _ticks(0) { diff --git a/src/fps_limiter.h b/src/fps_limiter.h index 1667d28..9617154 100644 --- a/src/fps_limiter.h +++ b/src/fps_limiter.h @@ -1,15 +1,17 @@ #ifndef FPS_LIMITER_H #define FPS_LIMITER_H +#include + class FpsLimiter { public: - FpsLimiter(size_t fps = 60); + FpsLimiter(std::size_t fps = 60); void mark(); void throttle() const; private: - const size_t _fps; - size_t _ticks; + const std::size_t _fps; + std::size_t _ticks; }; #endif /* FPS_LIMITER_H */ From 451e73293b23cd3868ab6c91e59d7349570a4ecf Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 12:49:55 +0300 Subject: [PATCH 6/9] Add missing include --- src/platform_compat.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/platform_compat.h b/src/platform_compat.h index 6c268f9..8154913 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -1,6 +1,8 @@ #ifndef PLATFORM_COMPAT_H #define PLATFORM_COMPAT_H +#include + // TODO: This is compatibility cross-platform layer. Designed to have minimal // impact on the codebase. Remove once it's no longer needed. From 6ae1afc57ce4425a6d1956c9ab185e850b4f306a Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 14:01:52 +0300 Subject: [PATCH 7/9] Make file find utils cross-platform See #17 --- src/file_find.cc | 18 +++++-------- src/file_find.h | 30 ++++++++++++++++++--- src/xfile.cc | 42 +++++------------------------ third_party/fpattern/CMakeLists.txt | 6 +++++ 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/src/file_find.cc b/src/file_find.cc index 517c340..59c3f13 100644 --- a/src/file_find.cc +++ b/src/file_find.cc @@ -1,14 +1,16 @@ #include "file_find.h" +#include + // 0x4E6380 bool fileFindFirst(const char* path, DirectoryFileFindData* findData) { -#if defined(_MSC_VER) +#if defined(_WIN32) findData->hFind = FindFirstFileA(path, &(findData->ffd)); if (findData->hFind == INVALID_HANDLE_VALUE) { return false; } -#elif defined(__WATCOMC__) +#else findData->dir = opendir(path); if (findData->dir == NULL) { return false; @@ -19,8 +21,6 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData) closedir(findData->dir); return false; } -#else -#error Not implemented #endif return true; @@ -29,18 +29,16 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData) // 0x4E63A8 bool fileFindNext(DirectoryFileFindData* findData) { -#if defined(_MSC_VER) +#if defined(_WIN32) if (!FindNextFileA(findData->hFind, &(findData->ffd))) { return false; } -#elif defined(__WATCOMC__) +#else findData->entry = readdir(findData->dir); if (findData->entry == NULL) { closedir(findData->dir); return false; } -#else -#error Not implemented #endif return true; @@ -51,12 +49,10 @@ bool findFindClose(DirectoryFileFindData* findData) { #if defined(_MSC_VER) FindClose(findData->hFind); -#elif defined(__WATCOMC__) +#else if (closedir(findData->dir) != 0) { return false; } -#else -#error Not implemented #endif return true; diff --git a/src/file_find.h b/src/file_find.h index 0dbb552..90e497e 100644 --- a/src/file_find.h +++ b/src/file_find.h @@ -1,8 +1,12 @@ #ifndef FILE_FIND_H #define FILE_FIND_H +#if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include +#else +#include +#endif // NOTE: This structure is significantly different from what was in the // original code. Watcom provides opendir/readdir/closedir implementations, @@ -24,14 +28,12 @@ // original implementation for Watcom (not tested). I'm not sure it will work // in other compilers, so for now just stick with the error. typedef struct DirectoryFileFindData { -#if defined(_MSC_VER) +#if defined(_WIN32) HANDLE hFind; WIN32_FIND_DATAA ffd; -#elif defined(__WATCOMC__) +#else DIR* dir; struct dirent* entry; -#else -#error Not implemented #endif } DirectoryFileFindData; @@ -39,4 +41,24 @@ bool fileFindFirst(const char* path, DirectoryFileFindData* findData); bool fileFindNext(DirectoryFileFindData* findData); bool findFindClose(DirectoryFileFindData* findData); +static inline bool fileFindIsDirectory(DirectoryFileFindData* findData) +{ +#if defined(_WIN32) + return (findData->ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; +#elif defined(__WATCOMC__) + return (findData->entry->d_attr & _A_SUBDIR) != 0; +#else + return findData->entry->d_type == DT_DIR; +#endif +} + +static inline char* fileFindGetName(DirectoryFileFindData* findData) +{ +#if defined(_WIN32) + return findData->ffd.cFileName; +#else + return findData->entry->d_name; +#endif +} + #endif /* FILE_FIND_H */ diff --git a/src/xfile.cc b/src/xfile.cc index e4dd5c3..ed667ae 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -549,18 +549,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') { if (fileFindFirst(pattern, &directoryFileFindData)) { do { - bool isDirectory; - char* entryName; - -#if defined(_MSC_VER) - isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - entryName = directoryFileFindData.ffd.cFileName; -#elif defined(__WATCOMC__) - isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0; - entryName = directoryFileFindData.entry->d_name; -#else -#error Not implemented -#endif + bool isDirectory = fileFindIsDirectory(&directoryFileFindData); + char* entryName = fileFindGetName(&directoryFileFindData); if (isDirectory) { if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) { @@ -604,18 +594,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList if (fileFindFirst(path, &directoryFileFindData)) { do { - bool isDirectory; - char* entryName; - -#if defined(_MSC_VER) - isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - entryName = directoryFileFindData.ffd.cFileName; -#elif defined(__WATCOMC__) - isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0; - entryName = directoryFileFindData.entry->d_name; -#else -#error Not implemented -#endif + bool isDirectory = fileFindIsDirectory(&directoryFileFindData); + char* entryName = fileFindGetName(&directoryFileFindData); if (isDirectory) { if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) { @@ -642,18 +622,8 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList _splitpath(pattern, drive, dir, fileName, extension); if (fileFindFirst(pattern, &directoryFileFindData)) { do { - bool isDirectory; - char* entryName; - -#if defined(_MSC_VER) - isDirectory = (directoryFileFindData.ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - entryName = directoryFileFindData.ffd.cFileName; -#elif defined(__WATCOMC__) - isDirectory = (directoryFileFindData.entry->d_attr & _A_SUBDIR) != 0; - entryName = directoryFileFindData.entry->d_name; -#else -#error Not implemented -#endif + bool isDirectory = fileFindIsDirectory(&directoryFileFindData); + char* entryName = fileFindGetName(&directoryFileFindData); if (isDirectory) { if (strcmp(entryName, "..") == 0 || strcmp(entryName, ".") == 0) { diff --git a/third_party/fpattern/CMakeLists.txt b/third_party/fpattern/CMakeLists.txt index 43f4153..d809812 100644 --- a/third_party/fpattern/CMakeLists.txt +++ b/third_party/fpattern/CMakeLists.txt @@ -20,5 +20,11 @@ add_library(fpattern STATIC "${fpattern_SOURCE_DIR}/fpattern.h" ) +if(APPLE) + target_compile_definitions(fpattern PRIVATE + "-Dunix" + ) +endif() + set(FPATTERN_LIBRARY "fpattern" PARENT_SCOPE) set(FPATTERN_INCLUDE_DIR "${fpattern_SOURCE_DIR}" PARENT_SCOPE) From 17dc61b782ab935df1a3bc963df310b84c8acd03 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 14:08:00 +0300 Subject: [PATCH 8/9] Make randomGetSeed cross-platform See #17 --- src/random.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/random.cc b/src/random.cc index 6d059af..b9ed226 100644 --- a/src/random.cc +++ b/src/random.cc @@ -6,11 +6,15 @@ #include #include +#if defined(_WIN32) // clang-format off #define WIN32_LEAN_AND_MEAN #include #include // clang-format on +#else +#include +#endif // 0x50D4BA const double dbl_50D4BA = 36.42; @@ -208,7 +212,13 @@ void randomSeedPrerandomInternal(int seed) // 0x4A3258 unsigned int randomGetSeed() { +#if defined(_WIN32) return timeGetTime(); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_usec / 1000; +#endif } // 0x4A3264 From 14ce3994ce793b17741f8f79aa113751092ac85c Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 14:45:48 +0300 Subject: [PATCH 9/9] Replace min/max macro See #17 --- src/automap.cc | 4 ++- src/autorun.h | 1 + src/color.cc | 4 ++- src/dbox.cc | 4 ++- src/file_find.h | 1 + src/game_sound.h | 1 + src/inventory.cc | 6 ++-- src/loadsave.cc | 4 ++- src/options.cc | 56 ++++++++++++++++++----------------- src/sound.cc | 4 ++- src/stat.cc | 8 +++-- src/win32.h | 1 + src/window_manager.cc | 10 ++++--- src/window_manager.h | 1 + src/window_manager_private.cc | 4 ++- 15 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/automap.cc b/src/automap.cc index 6df1686..6542970 100644 --- a/src/automap.cc +++ b/src/automap.cc @@ -22,6 +22,8 @@ #include #include +#include + // 0x41ADE0 const int _defam[AUTOMAP_MAP_COUNT][ELEVATION_COUNT] = { { -1, -1, -1 }, @@ -1070,7 +1072,7 @@ int _copy_file_data(File* stream1, File* stream2, int length) // NOTE: Original code is slightly different, but does the same thing. while (length != 0) { - int chunkLength = min(length, 0xFFFF); + int chunkLength = std::min(length, 0xFFFF); if (fileRead(buffer, chunkLength, 1, stream1) != 1) { break; diff --git a/src/autorun.h b/src/autorun.h index ba84d7e..46ecd03 100644 --- a/src/autorun.h +++ b/src/autorun.h @@ -2,6 +2,7 @@ #define AUTORUN_H #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include extern HANDLE gInterplayGenericAutorunMutex; diff --git a/src/color.cc b/src/color.cc index a98b8a2..bac5c2d 100644 --- a/src/color.cc +++ b/src/color.cc @@ -5,6 +5,8 @@ #include #include +#include + // 0x50F930 char _aColor_cNoError[] = "color.c: No errors\n"; @@ -552,7 +554,7 @@ void colorSetBrightness(double value) for (int i = 0; i < 64; i++) { double value = pow(i, gBrightness); - _currentGammaTable[i] = (unsigned char)min(max(value, 0.0), 63.0); + _currentGammaTable[i] = (unsigned char)std::clamp(value, 0.0, 63.0); } _setSystemPalette(_systemCmap); diff --git a/src/dbox.cc b/src/dbox.cc index c4c315a..6600724 100644 --- a/src/dbox.cc +++ b/src/dbox.cc @@ -16,6 +16,8 @@ #include #include +#include + // 0x5108C8 const int gDialogBoxBackgroundFrmIds[DIALOG_TYPE_COUNT] = { 218, // MEDIALOG.FRM - Medium generic dialog box @@ -107,7 +109,7 @@ int showDialogBox(const char* title, const char** body, int bodyLength, int x, i int linesCount = 0; for (int index = 0; index < bodyLength; index++) { // NOTE: Calls [fontGetStringWidth] twice because of [max] macro. - maximumLineWidth = max(fontGetStringWidth(body[index]), maximumLineWidth); + maximumLineWidth = std::max(fontGetStringWidth(body[index]), maximumLineWidth); linesCount++; } diff --git a/src/file_find.h b/src/file_find.h index 90e497e..2bba6ed 100644 --- a/src/file_find.h +++ b/src/file_find.h @@ -3,6 +3,7 @@ #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include #else #include diff --git a/src/game_sound.h b/src/game_sound.h index 00be6c8..0bbc598 100644 --- a/src/game_sound.h +++ b/src/game_sound.h @@ -6,6 +6,7 @@ #include "sound.h" #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include typedef enum WeaponSoundEffect { diff --git a/src/inventory.cc b/src/inventory.cc index 5dd1393..8d3c5e5 100644 --- a/src/inventory.cc +++ b/src/inventory.cc @@ -38,6 +38,8 @@ #include #include +#include + #define INVENTORY_LARGE_SLOT_WIDTH 90 #define INVENTORY_LARGE_SLOT_HEIGHT 61 @@ -1381,7 +1383,7 @@ void _display_body(int fid, int inventoryWindowType) unsigned char* frameData = artGetFrameData(art, frame, rotation); int framePitch = artGetWidth(art, frame, rotation); - int frameWidth = min(framePitch, INVENTORY_BODY_VIEW_WIDTH); + int frameWidth = std::min(framePitch, INVENTORY_BODY_VIEW_WIDTH); int frameHeight = artGetHeight(art, frame, rotation); if (frameHeight > INVENTORY_BODY_VIEW_HEIGHT) { @@ -3633,7 +3635,7 @@ int inventoryOpenLooting(Object* a1, Object* a2) if (!isCaughtStealing) { if (stealingXp > 0) { if (!objectIsPartyMember(a2)) { - stealingXp = min(300 - skillGetValue(a1, SKILL_STEAL), stealingXp); + stealingXp = std::min(300 - skillGetValue(a1, SKILL_STEAL), stealingXp); debugPrint("\n[[[%d]]]", 300 - skillGetValue(a1, SKILL_STEAL)); // You gain %d experience points for successfully using your Steal skill. diff --git a/src/loadsave.cc b/src/loadsave.cc index 9cec59a..a67d93f 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -45,6 +45,8 @@ #include #include +#include + #define LS_WINDOW_WIDTH 640 #define LS_WINDOW_HEIGHT 480 @@ -2389,7 +2391,7 @@ int _copy_file(const char* a1, const char* a2) } while (length != 0) { - chunk_length = min(length, 0xFFFF); + chunk_length = std::min(length, 0xFFFF); if (fileRead(buf, chunk_length, 1, stream1) != 1) { break; diff --git a/src/options.cc b/src/options.cc index 7819dfc..3e6d983 100644 --- a/src/options.cc +++ b/src/options.cc @@ -25,6 +25,8 @@ #include #include +#include + #define PREFERENCES_WINDOW_WIDTH 640 #define PREFERENCES_WINDOW_HEIGHT 480 @@ -919,32 +921,32 @@ void preferencesSetDefaults(bool a1) // 0x4931F8 void _JustUpdate_() { - gPreferencesGameDifficulty1 = min(max(gPreferencesGameDifficulty1, 0), 2); - gPreferencesCombatDifficulty1 = min(max(gPreferencesCombatDifficulty1, 0), 2); - gPreferencesViolenceLevel1 = min(max(gPreferencesViolenceLevel1, 0), 3); - gPreferencesTargetHighlight1 = min(max(gPreferencesTargetHighlight1, 0), 2); - gPreferencesCombatMessages1 = min(max(gPreferencesCombatMessages1, 0), 1); - gPreferencesCombatLooks1 = min(max(gPreferencesCombatLooks1, 0), 1); - gPreferencesCombatTaunts1 = min(max(gPreferencesCombatTaunts1, 0), 1); - gPreferencesLanguageFilter1 = min(max(gPreferencesLanguageFilter1, 0), 1); - gPreferencesRunning1 = min(max(gPreferencesRunning1, 0), 1); - gPreferencesSubtitles1 = min(max(gPreferencesSubtitles1, 0), 1); - gPreferencesItemHighlight1 = min(max(gPreferencesItemHighlight1, 0), 1); - gPreferencesCombatSpeed1 = min(max(gPreferencesCombatSpeed1, 0), 50); - gPreferencesPlayerSpeedup1 = min(max(gPreferencesPlayerSpeedup1, 0), 1); - gPreferencesTextBaseDelay1 = min(max(gPreferencesTextBaseDelay1, 1.0), 6.0); - gPreferencesMasterVolume1 = min(max(gPreferencesMasterVolume1, 0), VOLUME_MAX); - gPreferencesMusicVolume1 = min(max(gPreferencesMusicVolume1, 0), VOLUME_MAX); - gPreferencesSoundEffectsVolume1 = min(max(gPreferencesSoundEffectsVolume1, 0), VOLUME_MAX); - gPreferencesSpeechVolume1 = min(max(gPreferencesSpeechVolume1, 0), VOLUME_MAX); - gPreferencesBrightness1 = min(max(gPreferencesBrightness1, 1.0), 1.17999267578125); - gPreferencesMouseSensitivity1 = min(max(gPreferencesMouseSensitivity1, 1.0), 2.5); + gPreferencesGameDifficulty1 = std::clamp(gPreferencesGameDifficulty1, 0, 2); + gPreferencesCombatDifficulty1 = std::clamp(gPreferencesCombatDifficulty1, 0, 2); + gPreferencesViolenceLevel1 = std::clamp(gPreferencesViolenceLevel1, 0, 3); + gPreferencesTargetHighlight1 = std::clamp(gPreferencesTargetHighlight1, 0, 2); + gPreferencesCombatMessages1 = std::clamp(gPreferencesCombatMessages1, 0, 1); + gPreferencesCombatLooks1 = std::clamp(gPreferencesCombatLooks1, 0, 1); + gPreferencesCombatTaunts1 = std::clamp(gPreferencesCombatTaunts1, 0, 1); + gPreferencesLanguageFilter1 = std::clamp(gPreferencesLanguageFilter1, 0, 1); + gPreferencesRunning1 = std::clamp(gPreferencesRunning1, 0, 1); + gPreferencesSubtitles1 = std::clamp(gPreferencesSubtitles1, 0, 1); + gPreferencesItemHighlight1 = std::clamp(gPreferencesItemHighlight1, 0, 1); + gPreferencesCombatSpeed1 = std::clamp(gPreferencesCombatSpeed1, 0, 50); + gPreferencesPlayerSpeedup1 = std::clamp(gPreferencesPlayerSpeedup1, 0, 1); + gPreferencesTextBaseDelay1 = std::clamp(gPreferencesTextBaseDelay1, 6.0, 10.0); + gPreferencesMasterVolume1 = std::clamp(gPreferencesMasterVolume1, 0, VOLUME_MAX); + gPreferencesMusicVolume1 = std::clamp(gPreferencesMusicVolume1, 0, VOLUME_MAX); + gPreferencesSoundEffectsVolume1 = std::clamp(gPreferencesSoundEffectsVolume1, 0, VOLUME_MAX); + gPreferencesSpeechVolume1 = std::clamp(gPreferencesSpeechVolume1, 0, VOLUME_MAX); + gPreferencesBrightness1 = std::clamp(gPreferencesBrightness1, 1.0, 1.17999267578125); + gPreferencesMouseSensitivity1 = std::clamp(gPreferencesMouseSensitivity1, 1.0, 2.5); textObjectsSetBaseDelay(gPreferencesTextBaseDelay1); gameMouseLoadItemHighlight(); double textLineDelay = (gPreferencesTextBaseDelay1 + (-1.0)) * 0.2 * 2.0; - textLineDelay = min(max(textLineDelay, 0.0), 2.0); + textLineDelay = std::clamp(textLineDelay, 0.0, 2.0); textObjectsSetLineDelay(textLineDelay); aiMessageListReloadIfNeeded(); @@ -1065,7 +1067,7 @@ void _UpdateThing(int index) case PREF_COMBAT_SPEED: if (1) { double value = *meta->valuePtr; - value = min(max(value, 0.0), 50.0); + value = std::clamp(value, 0.0, 50.0); int x = (int)((value - meta->minValue) * 219.0 / (meta->maxValue - meta->minValue) + 384.0); blitBufferToBufferTrans(gPreferencesWindowFrmData[PREFERENCES_WINDOW_FRM_KNOB_OFF], 21, 12, 21, gPreferencesWindowBuffer + 640 * meta->knobY + x, 640); @@ -1073,13 +1075,13 @@ void _UpdateThing(int index) break; case PREF_TEXT_BASE_DELAY: if (1) { - gPreferencesTextBaseDelay1 = min(max(gPreferencesTextBaseDelay1, 1.0), 6.0); + gPreferencesTextBaseDelay1 = std::clamp(gPreferencesTextBaseDelay1, 1.0, 6.0); int x = (int)((6.0 - gPreferencesTextBaseDelay1) * 43.8 + 384.0); blitBufferToBufferTrans(gPreferencesWindowFrmData[PREFERENCES_WINDOW_FRM_KNOB_OFF], 21, 12, 21, gPreferencesWindowBuffer + 640 * meta->knobY + x, 640); double value = (gPreferencesTextBaseDelay1 - 1.0) * 0.2 * 2.0; - value = min(max(value, 0.0), 2.0); + value = std::clamp(value, 0.0, 2.0); textObjectsSetBaseDelay(gPreferencesTextBaseDelay1); textObjectsSetLineDelay(value); @@ -1091,7 +1093,7 @@ void _UpdateThing(int index) case PREF_SPEECH_VOLUME: if (1) { double value = *meta->valuePtr; - value = min(max(value, meta->minValue), meta->maxValue); + value = std::clamp(value, meta->minValue, meta->maxValue); int x = (int)((value - meta->minValue) * 219.0 / (meta->maxValue - meta->minValue) + 384.0); blitBufferToBufferTrans(gPreferencesWindowFrmData[PREFERENCES_WINDOW_FRM_KNOB_OFF], 21, 12, 21, gPreferencesWindowBuffer + 640 * meta->knobY + x, 640); @@ -1114,7 +1116,7 @@ void _UpdateThing(int index) break; case PREF_BRIGHTNESS: if (1) { - gPreferencesBrightness1 = min(max(gPreferencesBrightness1, 1.0), 1.17999267578125); + gPreferencesBrightness1 = std::clamp(gPreferencesBrightness1, 1.0, 1.17999267578125); int x = (int)((gPreferencesBrightness1 - meta->minValue) * (219.0 / (meta->maxValue - meta->minValue)) + 384.0); blitBufferToBufferTrans(gPreferencesWindowFrmData[PREFERENCES_WINDOW_FRM_KNOB_OFF], 21, 12, 21, gPreferencesWindowBuffer + 640 * meta->knobY + x, 640); @@ -1124,7 +1126,7 @@ void _UpdateThing(int index) break; case PREF_MOUSE_SENSITIVIY: if (1) { - gPreferencesMouseSensitivity1 = min(max(gPreferencesMouseSensitivity1, 1.0), 2.5); + gPreferencesMouseSensitivity1 = std::clamp(gPreferencesMouseSensitivity1, 1.0, 2.5); int x = (int)((gPreferencesMouseSensitivity1 - meta->minValue) * (219.0 / (meta->maxValue - meta->minValue)) + 384.0); blitBufferToBufferTrans(gPreferencesWindowFrmData[PREFERENCES_WINDOW_FRM_KNOB_OFF], 21, 12, 21, gPreferencesWindowBuffer + 640 * meta->knobY + x, 640); diff --git a/src/sound.cc b/src/sound.cc index 56f3fdc..00ec98f 100644 --- a/src/sound.cc +++ b/src/sound.cc @@ -9,6 +9,8 @@ #include #include +#include + // 0x51D478 STRUCT_51D478* _fadeHead = NULL; @@ -1069,7 +1071,7 @@ int _soundVolumeHMItoDirectSound(int volume) if (volume != 0) { normalizedVolume = -1000.0 * log2(32767.0 / volume); - normalizedVolume = max(min(normalizedVolume, 0.0), -10000.0); + normalizedVolume = std::clamp(normalizedVolume, -10000.0, 0.0); } else { normalizedVolume = -10000.0; } diff --git a/src/stat.cc b/src/stat.cc index 84bde62..f08ca84 100644 --- a/src/stat.cc +++ b/src/stat.cc @@ -21,6 +21,8 @@ #include +#include + // 0x51D53C StatDescription gStatDescriptions[STAT_COUNT] = { { NULL, NULL, 0, PRIMARY_STAT_MIN, PRIMARY_STAT_MAX, 5 }, @@ -345,7 +347,7 @@ int critterGetStat(Object* critter, int stat) } } - value = min(max(value, gStatDescriptions[stat].minimumValue), gStatDescriptions[stat].maximumValue); + value = std::clamp(value, gStatDescriptions[stat].minimumValue, gStatDescriptions[stat].maximumValue); } else { switch (stat) { case STAT_CURRENT_HIT_POINTS: @@ -546,10 +548,10 @@ void critterUpdateDerivedStats(Object* critter) data->baseStats[STAT_MAXIMUM_HIT_POINTS] = critterGetBaseStatWithTraitModifier(critter, STAT_STRENGTH) + critterGetBaseStatWithTraitModifier(critter, STAT_ENDURANCE) * 2 + 15; data->baseStats[STAT_MAXIMUM_ACTION_POINTS] = agility / 2 + 5; data->baseStats[STAT_ARMOR_CLASS] = agility; - data->baseStats[STAT_MELEE_DAMAGE] = max(strength - 5, 1); + data->baseStats[STAT_MELEE_DAMAGE] = std::max(strength - 5, 1); data->baseStats[STAT_CARRY_WEIGHT] = 25 * strength + 25; data->baseStats[STAT_SEQUENCE] = 2 * perception; - data->baseStats[STAT_HEALING_RATE] = max(endurance / 3, 1); + data->baseStats[STAT_HEALING_RATE] = std::max(endurance / 3, 1); data->baseStats[STAT_CRITICAL_CHANCE] = luck; data->baseStats[STAT_BETTER_CRITICALS] = 0; data->baseStats[STAT_RADIATION_RESISTANCE] = 2 * endurance; diff --git a/src/win32.h b/src/win32.h index 076d34a..45056cd 100644 --- a/src/win32.h +++ b/src/win32.h @@ -2,6 +2,7 @@ #define WIN32_H #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include #include diff --git a/src/window_manager.cc b/src/window_manager.cc index 47c2b5f..e4d55b5 100644 --- a/src/window_manager.cc +++ b/src/window_manager.cc @@ -12,6 +12,8 @@ #include +#include + // 0x50FA30 char _path_patches[] = ""; @@ -781,10 +783,10 @@ void _GNW_win_refresh(Window* window, Rect* rect, unsigned char* a3) v26->next = NULL; - v26->rect.left = max(window->rect.left, rect->left); - v26->rect.top = max(window->rect.top, rect->top); - v26->rect.right = min(window->rect.right, rect->right); - v26->rect.bottom = min(window->rect.bottom, rect->bottom); + v26->rect.left = std::max(window->rect.left, rect->left); + v26->rect.top = std::max(window->rect.top, rect->top); + v26->rect.right = std::min(window->rect.right, rect->right); + v26->rect.bottom = std::min(window->rect.bottom, rect->bottom); if (v26->rect.right >= v26->rect.left && v26->rect.bottom >= v26->rect.top) { if (a3) { diff --git a/src/window_manager.h b/src/window_manager.h index e1da1ec..0504dfd 100644 --- a/src/window_manager.h +++ b/src/window_manager.h @@ -6,6 +6,7 @@ #include #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include #define MAX_WINDOW_COUNT (50) diff --git a/src/window_manager_private.cc b/src/window_manager_private.cc index 3fd493b..6e2a423 100644 --- a/src/window_manager_private.cc +++ b/src/window_manager_private.cc @@ -8,6 +8,8 @@ #include #include +#include + // 0x51E414 int _wd = -1; @@ -210,7 +212,7 @@ int _calc_max_field_chars_wcursor(int a1, int a2) internal_free(str); - return max(len1, len2) + 1; + return std::max(len1, len2) + 1; } // 0x4DD3EC