From 32a879ca58c048a3d09380d6d74d242ffe7bffd2 Mon Sep 17 00:00:00 2001 From: Gabriel Ravier Date: Mon, 29 Jun 2020 00:14:30 +0200 Subject: [PATCH 1/9] src: Removed MAX_PATH and made the path/string handling better in general (ported over from supportPathsAboveFilenameMax) Signed-off-by: Gabriel Ravier --- CMakeLists.txt | 8 +++++ src/ArmsItem.cpp | 28 ++++++++++----- src/Attributes.h | 10 ++++-- src/Back.cpp | 6 ++-- src/Backends/Misc.h | 2 +- src/Backends/Platform/GLFW3.cpp | 2 +- src/Backends/Platform/Null.cpp | 2 +- src/Backends/Platform/SDL2.cpp | 5 +-- src/Backends/Platform/WiiU.cpp | 10 +++--- src/Config.cpp | 7 ++-- src/Draw.cpp | 27 +++++++++++---- src/Ending.cpp | 13 +++++-- src/Game.cpp | 10 ++++-- src/Generic.cpp | 33 +++++++++--------- src/GenericLoad.cpp | 8 +++-- src/Helpers/Asprintf.cpp | 18 ++++++++++ src/Helpers/Asprintf.h | 15 ++++++++ src/Helpers/FopenFormatted.cpp | 23 +++++++++++++ src/Helpers/FopenFormatted.h | 5 +++ src/Helpers/Strdup.cpp | 20 +++++++++++ src/Helpers/Strdup.h | 16 +++++++++ src/Helpers/Vasprintf.cpp | 32 +++++++++++++++++ src/Helpers/Vasprintf.h | 16 +++++++++ src/Main.cpp | 61 +++++++++++++++++++-------------- src/Main.h | 4 +-- src/Map.cpp | 12 ++----- src/MycParam.cpp | 12 +++---- src/NpChar.cpp | 6 ++-- src/Profile.cpp | 32 +++++------------ src/SelStage.cpp | 16 +++++++-- src/Stage.cpp | 12 +++++-- src/TextScr.cpp | 43 +++++++++++++++++------ src/TextScr.h | 4 +-- src/WindowsWrapper.h | 5 +-- 34 files changed, 370 insertions(+), 153 deletions(-) create mode 100644 src/Helpers/Asprintf.cpp create mode 100644 src/Helpers/Asprintf.h create mode 100644 src/Helpers/FopenFormatted.cpp create mode 100644 src/Helpers/FopenFormatted.h create mode 100644 src/Helpers/Strdup.cpp create mode 100644 src/Helpers/Strdup.h create mode 100644 src/Helpers/Vasprintf.cpp create mode 100644 src/Helpers/Vasprintf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index df7c78d3..578f595b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,14 @@ add_executable(CSE2 WIN32 "src/Backends/Controller.h" "src/Backends/Misc.h" "src/Backends/Rendering.h" + "src/Helpers/Asprintf.cpp" + "src/Helpers/Asprintf.h" + "src/Helpers/Vasprintf.cpp" + "src/Helpers/Vasprintf.h" + "src/Helpers/FopenFormatted.cpp" + "src/Helpers/FopenFormatted.h" + "src/Helpers/Strdup.cpp" + "src/Helpers/Strdup.h" ) set(RESOURCES diff --git a/src/ArmsItem.cpp b/src/ArmsItem.cpp index 9c39d1f9..dd25c6d1 100644 --- a/src/ArmsItem.cpp +++ b/src/ArmsItem.cpp @@ -1,5 +1,6 @@ #include "ArmsItem.h" +#include #include #include "WindowsWrapper.h" @@ -414,12 +415,13 @@ void PutCampObject(void) int CampLoop(void) { - char old_script_path[MAX_PATH]; - RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; + int ret = enum_ESCRETURN_continue; // Save the current script path (to restore it when we get out of the inventory) - GetTextScriptPath(old_script_path); + char *old_script_path = GetTextScriptPath(); + if (old_script_path == NULL) + return enum_ESCRETURN_exit; // Load the inventory script LoadTextScript2("ArmsItem.tsc"); @@ -450,10 +452,12 @@ int CampLoop(void) switch (Call_Escape()) { case enum_ESCRETURN_exit: - return enum_ESCRETURN_exit; // Quit game + free(old_script_path); + return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - return enum_ESCRETURN_restart; // Go to game intro + free(old_script_path); + return enum_ESCRETURN_restart; } } @@ -463,10 +467,12 @@ int CampLoop(void) switch (TextScriptProc()) { case enum_ESCRETURN_exit: - return enum_ESCRETURN_exit; // Quit game + free(old_script_path); + return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - return enum_ESCRETURN_restart; // Go to game intro + free(old_script_path); + return enum_ESCRETURN_restart; } // Get currently displayed image @@ -494,13 +500,17 @@ int CampLoop(void) } if (!Flip_SystemTask()) - return enum_ESCRETURN_exit; // Quit game + { + free(old_script_path); + return enum_ESCRETURN_exit; + } } // Resume original script LoadTextScript_Stage(old_script_path); gArmsEnergyX = 32; // Displays weapon rotation animation in case the weapon was changed - return enum_ESCRETURN_continue; // Go to game + free(old_script_path); + return enum_ESCRETURN_continue; } BOOL CheckItem(long a) diff --git a/src/Attributes.h b/src/Attributes.h index 35fcd26f..f62ca618 100644 --- a/src/Attributes.h +++ b/src/Attributes.h @@ -12,16 +12,22 @@ #ifdef __GNUC__ +#define ATTRIBUTE_FORMAT(archetype, formatArgumentIndex, firstToCheck) __attribute__((format(archetype, formatArgumentIndex, firstToCheck))) +#define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define ATTRIBUTE_MALLOC __attribute__((malloc)) +#define ATTRIBUTE_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) #define ATTRIBUTE_HOT __attribute__((hot)) -#define ATTRIBUTE_OPTIMIZE(optString) __attribute__((optimize(optString))) #define LIKELY(condition) __builtin_expect((condition), 1) #define UNLIKELY(condition) __builtin_expect((condition), 0) #define PREFETCH(address, isWrite, locality) __builtin_prefetch((address), (isWrite), (locality)) #else +#define ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) +#define ATTRIBUTE_WARN_UNUSED_RESULT +#define ATTRIBUTE_MALLOC +#define ATTRIBUTE_NONNULL #define ATTRIBUTE_HOT -#define ATTRIBUTE_OPTIMIZE(optString) #define LIKELY(condition) condition #define UNLIKELY(condition) condition #define PREFETCH(address, isWrite, locality) diff --git a/src/Back.cpp b/src/Back.cpp index dab38a48..97f00350 100644 --- a/src/Back.cpp +++ b/src/Back.cpp @@ -13,6 +13,7 @@ #include "Main.h" #include "Map.h" #include "Stage.h" +#include "Helpers/FopenFormatted.h" BACK gBack; int gWaterY; @@ -25,10 +26,7 @@ BOOL InitBack(const char *fName, int type) color_black = GetCortBoxColor(RGB(0, 0, 0x10)); // Get width and height - char path[MAX_PATH]; - sprintf(path, "%s/%s.pbm", gDataPath, fName); - - FILE *fp = fopen(path, "rb"); + FILE *fp = fopenFormatted("rb", "%s/%s.pbm", gDataPath, fName); if (fp == NULL) return FALSE; diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index 103ff98c..25fbdf60 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -86,7 +86,7 @@ enum bool Backend_Init(void); void Backend_Deinit(void); void Backend_PostWindowCreation(void); -bool Backend_GetBasePath(char *string_buffer); +bool Backend_GetBasePath(char **string_buffer); void Backend_HideMouse(void); void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index e36c3a4e..23ed0d17 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -181,7 +181,7 @@ void Backend_PostWindowCreation(void) glfwSetWindowSizeCallback(window, WindowSizeCallback); } -bool Backend_GetBasePath(char *string_buffer) +bool Backend_GetBasePath(char **string_buffer) { (void)string_buffer; diff --git a/src/Backends/Platform/Null.cpp b/src/Backends/Platform/Null.cpp index c4005d9b..bf4ab2f2 100644 --- a/src/Backends/Platform/Null.cpp +++ b/src/Backends/Platform/Null.cpp @@ -15,7 +15,7 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char *string_buffer) +bool Backend_GetBasePath(char **string_buffer) { (void)string_buffer; diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 1408b605..775f265c 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -13,6 +13,7 @@ #include "../../Organya.h" #include "../../Profile.h" #include "../../Resource.h" +#include "../../Helpers/Strdup.h" #define DO_KEY(SDL_KEY, BACKEND_KEY) \ case SDL_KEY: \ @@ -84,7 +85,7 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char *string_buffer) +bool Backend_GetBasePath(char **string_buffer) { #ifdef _WIN32 // SDL_GetBasePath returns a UTF-8 string, but Windows' fopen uses (extended?) ASCII. @@ -100,7 +101,7 @@ bool Backend_GetBasePath(char *string_buffer) // Trim the trailing '/' size_t base_path_length = strlen(base_path); base_path[base_path_length - 1] = '\0'; - strcpy(string_buffer, base_path); + *string_buffer = strdup(base_path); SDL_free(base_path); return true; diff --git a/src/Backends/Platform/WiiU.cpp b/src/Backends/Platform/WiiU.cpp index 578e1df3..14e5f54d 100644 --- a/src/Backends/Platform/WiiU.cpp +++ b/src/Backends/Platform/WiiU.cpp @@ -12,6 +12,8 @@ #include #include +#include "../../Helpers/Asprintf.h" + static unsigned long ticks_per_second; bool Backend_Init(void) @@ -54,15 +56,13 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char *string_buffer) +bool Backend_GetBasePath(char **string_buffer) { - strcpy(string_buffer, WHBGetSdCardMountPath()); #ifdef JAPANESE - strcat(string_buffer, "/CSE2-portable-jp"); + *string_buffer = asprintf("%s/CSE-portable-jp", WHBGetSdCardMountPath()); #else - strcat(string_buffer, "/CSE2-portable-en"); + *string_buffer = asprintf("%s/CSE-portable-en", WHBGetSdCardMountPath()); #endif - return true; } diff --git a/src/Config.cpp b/src/Config.cpp index e2afcb95..a0a5a585 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -7,6 +7,7 @@ #include "Config.h" #include "File.h" #include "Main.h" +#include "Helpers/FopenFormatted.h" const char* const gConfigName = "Config.dat"; const char* const gProof = "DOUKUTSU20041206"; @@ -16,12 +17,8 @@ BOOL LoadConfigData(CONFIG *conf) // Clear old configuration data memset(conf, 0, sizeof(CONFIG)); - // Get path - char path[MAX_PATH]; - sprintf(path, "%s/%s", gModulePath, gConfigName); - // Open file - FILE *fp = fopen(path, "rb"); + FILE *fp = fopenFormatted("rb", "%s/%s", gModulePath, gConfigName); if (fp == NULL) return FALSE; diff --git a/src/Draw.cpp b/src/Draw.cpp index 7673d59b..61804bc9 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -18,6 +18,7 @@ #include "MapName.h" #include "Resource.h" #include "TextScr.h" +#include "Helpers/Asprintf.h" typedef enum SurfaceType { @@ -250,12 +251,15 @@ BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL MakeSurface_File(const char *name, SurfaceID surf_no) { - char path[MAX_PATH]; - sprintf(path, "%s/%s.pbm", gDataPath, name); + char *path; + + if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0) + return FALSE; if (!IsEnableBitmap(path)) { ErrorLog(path, 0); + free(path); return FALSE; } @@ -266,12 +270,14 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no) #endif { ErrorLog("surface no", surf_no); + free(path); return FALSE; } if (surf[surf_no] != NULL) { ErrorLog("existing", surf_no); + free(path); return FALSE; } @@ -281,8 +287,10 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no) if (image_buffer == NULL) { ErrorLog(path, 1); + free(path); return FALSE; } + free(path); surf[surf_no] = RenderBackend_CreateSurface(width * mag, height * mag, false); @@ -341,12 +349,14 @@ BOOL ReloadBitmap_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) { - char path[MAX_PATH]; - sprintf(path, "%s/%s.pbm", gDataPath, name); + char *path; + if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0) + return FALSE; if (!IsEnableBitmap(path)) { ErrorLog(path, 0); + free(path); return FALSE; } @@ -357,6 +367,7 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) #endif { ErrorLog("surface no", surf_no); + free(path); return FALSE; } @@ -366,8 +377,10 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) if (image_buffer == NULL) { ErrorLog(path, 1); + free(path); return FALSE; } + free(path); if (!ScaleAndUploadSurface(image_buffer, width, height, surf_no)) { @@ -378,7 +391,6 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) FreeBitmap(image_buffer); surface_metadata[surf_no].type = SURFACE_SOURCE_FILE; strcpy(surface_metadata[surf_no].name, name); - return TRUE; } @@ -647,8 +659,9 @@ void InitTextObject(const char *name) { (void)name; // Unused in this branch - char path[MAX_PATH]; - sprintf(path, "%s/Font/font", gDataPath); + char *path; + if (asprintf(&path, "%s/Font/font", gDataPath) < 0) + return; // Get font size unsigned int width, height; diff --git a/src/Ending.cpp b/src/Ending.cpp index 1a4532ff..73c75edd 100644 --- a/src/Ending.cpp +++ b/src/Ending.cpp @@ -16,6 +16,7 @@ #include "Organya.h" #include "Stage.h" #include "TextScr.h" +#include "Helpers/Asprintf.h" struct CREDIT { @@ -216,7 +217,6 @@ void ReleaseCreditScript(void) BOOL StartCreditScript(void) { FILE *fp; - char path[MAX_PATH]; // Clear previously existing credits data if (Credit.pData != NULL) @@ -226,18 +226,27 @@ BOOL StartCreditScript(void) } // Open file - sprintf(path, "%s/%s", gDataPath, credit_script); + char *path; + if (asprintf(&path, "%s/%s", gDataPath, credit_script) < 0) + return FALSE; Credit.size = GetFileSizeLong(path); if (Credit.size == -1) + { + free(path); return FALSE; + } // Allocate buffer data Credit.pData = (char*)malloc(Credit.size); if (Credit.pData == NULL) + { + free(path); return FALSE; + } fp = fopen(path, "rb"); + free(path); if (fp == NULL) { free(Credit.pData); diff --git a/src/Game.cpp b/src/Game.cpp index 3a0608aa..1a613ca4 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -44,6 +44,7 @@ #include "Star.h" #include "TextScr.h" #include "ValueView.h" +#include "Helpers/Asprintf.h" int g_GameFlags; int gCounter; @@ -701,10 +702,13 @@ BOOL Game(void) PlaySoundObject(7, -1); - char path[MAX_PATH]; - sprintf(path, "%s/npc.tbl", gDataPath); + char *path; + if (asprintf(&path, "%s/npc.tbl", gDataPath) < 0) + return FALSE; - if (!LoadNpcTable(path)) + BOOL load_npc_table_succeeded = LoadNpcTable(path); + free(path); + if (!load_npc_table_succeeded) { #ifdef JAPANESE Backend_ShowMessageBox("エラー", "NPCテーブルが読めない"); diff --git a/src/Generic.cpp b/src/Generic.cpp index 5ef2a272..753f7bfa 100644 --- a/src/Generic.cpp +++ b/src/Generic.cpp @@ -2,11 +2,14 @@ #include #include +#include #include #include "WindowsWrapper.h" #include "Main.h" +#include "Helpers/Asprintf.h" +#include "Helpers/FopenFormatted.h" void GetCompileDate(int *year, int *month, int *day) { @@ -49,19 +52,18 @@ BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4) void DeleteLog(void) { - char path[MAX_PATH]; + char *path; + + if (asprintf(&path, "%s/debug.txt", gModulePath) < 0) + return; - sprintf(path, "%s/debug.txt", gModulePath); remove(path); + free(path); } BOOL WriteLog(const char *string, int value1, int value2, int value3) { - char path[MAX_PATH]; - FILE *fp; - - sprintf(path, "%s/debug.txt", gModulePath); - fp = fopen(path, "a+"); + FILE *fp = fopenFormatted("a+", "%s/debug.txt", gModulePath); if (fp == NULL) return FALSE; @@ -73,11 +75,7 @@ BOOL WriteLog(const char *string, int value1, int value2, int value3) BOOL IsKeyFile(const char *name) { - char path[MAX_PATH]; - - sprintf(path, "%s/%s", gModulePath, name); - - FILE *file = fopen(path, "rb"); + FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, name); if (file == NULL) return FALSE; @@ -86,6 +84,7 @@ BOOL IsKeyFile(const char *name) return TRUE; } +// Some code uses this as CheckFileExists, checking if the return value is -1 long GetFileSizeLong(const char *path) { long len; @@ -105,15 +104,15 @@ long GetFileSizeLong(const char *path) BOOL ErrorLog(const char *string, int value) { - char path[MAX_PATH]; - FILE *fp; - - sprintf(path, "%s/%s", gModulePath, "error.log"); + char *path; + if (asprintf(&path, "%s/%s", gModulePath, "error.log") < 0) + return FALSE; if (GetFileSizeLong(path) > 0x19000) // Purge the error log if it gets too big, I guess remove(path); - fp = fopen(path, "a+"); + FILE *fp = fopen(path, "a+"); + free(path); if (fp == NULL) return FALSE; diff --git a/src/GenericLoad.cpp b/src/GenericLoad.cpp index 9f177091..29343894 100644 --- a/src/GenericLoad.cpp +++ b/src/GenericLoad.cpp @@ -293,8 +293,10 @@ BOOL LoadGenericData(void) pt_size += MakePixToneObject(&gPtpTable[137], 1, 6); pt_size += MakePixToneObject(&gPtpTable[138], 1, 7); - char str[0x40]; - sprintf(str, "PixTone = %d byte", pt_size); - // There must have been some kind of console print function here or something + /* + * char str[0x40]; + * sprintf(str, "PixTone = %d byte", pt_size); + * // There must have been some kind of console print function here or something + */ return TRUE; } diff --git a/src/Helpers/Asprintf.cpp b/src/Helpers/Asprintf.cpp new file mode 100644 index 00000000..6d8cfb54 --- /dev/null +++ b/src/Helpers/Asprintf.cpp @@ -0,0 +1,18 @@ +#ifndef _GNU_SOURCE + +#include "Asprintf.h" +#include "Vasprintf.h" +#include "../Attributes.h" +#include + +ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **result_string, const char *format_string, ...) +{ + // Handle variadic arguments and redirect to vasprintf + va_list arguments_local; + va_start(arguments_local, format_string); + int retVal = vasprintf(result_string, format_string, arguments_local); + va_end(arguments_local); // Destroy arguments_local + return retVal; +} + +#endif diff --git a/src/Helpers/Asprintf.h b/src/Helpers/Asprintf.h new file mode 100644 index 00000000..c1337abc --- /dev/null +++ b/src/Helpers/Asprintf.h @@ -0,0 +1,15 @@ +#pragma once + +// If defines asprintf for us, use its definition +#ifdef _GNU_SOURCE + +#include + +#else + +#include "../Attributes.h" +#define asprintf Portable_asprintf + +ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **resultString, const char *formatString, ...); + +#endif diff --git a/src/Helpers/FopenFormatted.cpp b/src/Helpers/FopenFormatted.cpp new file mode 100644 index 00000000..ded92eea --- /dev/null +++ b/src/Helpers/FopenFormatted.cpp @@ -0,0 +1,23 @@ +#include "FopenFormatted.h" +#include "../Attributes.h" +#include +#include +#include + +ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *format_string, ...) +{ + // Handle variadic arguments and redirect to vasprintf + va_list arguments_local; + va_start(arguments_local, format_string); + char *path; + int vasprintf_retval = vasprintf(&path, format_string, arguments_local); + va_end(arguments_local); // Destroy arguments_local + + if (vasprintf_retval < 0) + return NULL; + + FILE *file = fopen(path, mode); + free(path); + + return file; +} diff --git a/src/Helpers/FopenFormatted.h b/src/Helpers/FopenFormatted.h new file mode 100644 index 00000000..e13c1726 --- /dev/null +++ b/src/Helpers/FopenFormatted.h @@ -0,0 +1,5 @@ +#pragma once +#include "../Attributes.h" +#include + +ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *formatString, ...); diff --git a/src/Helpers/Strdup.cpp b/src/Helpers/Strdup.cpp new file mode 100644 index 00000000..84f9f478 --- /dev/null +++ b/src/Helpers/Strdup.cpp @@ -0,0 +1,20 @@ +#include +#if !(_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L) + +#include "Strdup.h" +#include "../Attributes.h" +#include +#include + +ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL((1)) char *strdup(const char *duplicated_string) +{ + size_t duplicatedStringLength = strlen(duplicated_string) + sizeof(char); // Length of the whole string, plus the terminator + char *returnedString = (char *)malloc(duplicatedStringLength); // Memory for the new string is obtained with malloc. malloc also sets errno to ENOMEM on failure, which is exactly what we want for conformance + + if (returnedString) // If the result of malloc was NULL, allocation failed copying into it would be UB + memcpy(returnedString, duplicated_string, duplicatedStringLength); // Copy the entire duplicated string (including the null terminator) + + return returnedString; // Returns a pointer to the duplicated string on success, NULL if insufficient memory was available +} + +#endif diff --git a/src/Helpers/Strdup.h b/src/Helpers/Strdup.h new file mode 100644 index 00000000..08111b6a --- /dev/null +++ b/src/Helpers/Strdup.h @@ -0,0 +1,16 @@ +#pragma once +#include // Probably the smallest header that will include on systems with it + +// If defines strdup for us, use its definition +#if (_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L) + +#include + +#else + +#include "../Attributes.h" +#define strdup Portable_strdup + +ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL(1) char *strdup(const char *duplicatedString); + +#endif diff --git a/src/Helpers/Vasprintf.cpp b/src/Helpers/Vasprintf.cpp new file mode 100644 index 00000000..ba5babfd --- /dev/null +++ b/src/Helpers/Vasprintf.cpp @@ -0,0 +1,32 @@ +#ifndef _GNU_SOURCE + +#include "Vasprintf.h" +#include "../Attributes.h" +#include +#include +#include +#include + +ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **result_string, const char *format_string, va_list arguments) +{ + const int error_return_value = -1; // According to the man page, this is returned on error + va_list arguments_local; + va_copy(arguments_local, arguments); // Copy the arguments so we can actually use them + + int size = vsnprintf(NULL, 0, format_string, arguments_local); // Get the amount of bytes we need for the output buffer + + va_end(arguments_local); // Destroy arguments_local + + if (size < 0) // If an output error is encountered, vsnprintf returns a negative value + return error_return_value; + + *result_string = (char *)malloc(size + 1); // Allocate enough space for the string (need + 1 for the null terminator) + + if (*result_string == NULL) // On error, malloc returns NULL + return error_return_value; + + // We know we have enough space, so we can use vsprintf safely + return vsprintf(*result_string, format_string, arguments); // vsprintf returns the amount of characters that got printed. This is what we're supposed to return +} + +#endif diff --git a/src/Helpers/Vasprintf.h b/src/Helpers/Vasprintf.h new file mode 100644 index 00000000..7af89984 --- /dev/null +++ b/src/Helpers/Vasprintf.h @@ -0,0 +1,16 @@ +#pragma once + +// If defines asprintf for us, use its definition +#ifdef _GNU_SOURCE + +#include + +#else + +#include "../Attributes.h" +#include +#define vasprintf Portable_vasprintf + +ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **resultString, const char *formatString, va_list arguments); + +#endif diff --git a/src/Main.cpp b/src/Main.cpp index 7ecc3e91..61e861a1 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -24,9 +24,11 @@ #include "Resource.h" #include "Sound.h" #include "Triangle.h" +#include "Helpers/Asprintf.h" +#include "Helpers/Strdup.h" -char gModulePath[MAX_PATH]; -char gDataPath[MAX_PATH]; +char *gModulePath; +char *gDataPath; BOOL bFullscreen; BOOL gbUseJoystick = FALSE; @@ -82,6 +84,24 @@ void PutFramePerSecound(void) } } +static int main_out_backend_deinit(int return_value) +{ + Backend_Deinit(); + return return_value; +} + +static int main_out_free_module_path(int return_value) +{ + free(gModulePath); + return main_out_backend_deinit(return_value); +} + +static int main_out_free_data_path(int return_value) +{ + free(gDataPath); + return main_out_free_module_path(return_value); +} + // TODO - Inaccurate stack frame int main(int argc, char *argv[]) { @@ -93,10 +113,15 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; // Get executable's path - if (!Backend_GetBasePath(gModulePath)) + if (!Backend_GetBasePath(&gModulePath)) { // Fall back on argv[0] if the backend cannot provide a path - strcpy(gModulePath, argv[0]); + gModulePath = strdup(argv[0]); + if (!gModulePath) + { + Backend_PrintError("Could not get executable path (not enough memory)"); + return main_out_backend_deinit(EXIT_FAILURE); + } for (size_t i = strlen(gModulePath);; --i) { @@ -109,8 +134,8 @@ int main(int argc, char *argv[]) } // Get path of the data folder - strcpy(gDataPath, gModulePath); - strcat(gDataPath, "/data"); + if (asprintf(&gDataPath, "%s/data", gModulePath) < 0) + return main_out_free_module_path(EXIT_FAILURE); CONFIG conf; if (!LoadConfigData(&conf)) @@ -223,18 +248,12 @@ int main(int argc, char *argv[]) if (conf.display_mode == 1) { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0)) - { - Backend_Deinit(); - return EXIT_FAILURE; - } + return main_out_free_data_path(EXIT_FAILURE); } else { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1)) - { - Backend_Deinit(); - return EXIT_FAILURE; - } + return main_out_free_data_path(EXIT_FAILURE); } #else // Doesn't handle StartDirectDraw failing @@ -255,10 +274,7 @@ int main(int argc, char *argv[]) #ifdef FIX_BUGS if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2)) - { - Backend_Deinit(); - return EXIT_FAILURE; - } + return main_out_free_data_path(EXIT_FAILURE); #else // Doesn't handle StartDirectDraw failing StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2); @@ -326,10 +342,7 @@ int main(int argc, char *argv[]) // Draw to screen if (!Flip_SystemTask()) - { - Backend_Deinit(); - return EXIT_SUCCESS; - } + return main_out_free_data_path(EXIT_SUCCESS); // Initialize sound InitDirectSound(); @@ -353,9 +366,7 @@ int main(int argc, char *argv[]) EndDirectSound(); EndDirectDraw(); - Backend_Deinit(); - - return EXIT_SUCCESS; + return main_out_free_data_path(EXIT_SUCCESS); } void InactiveWindow(void) diff --git a/src/Main.h b/src/Main.h index 62d618bd..6c49da91 100644 --- a/src/Main.h +++ b/src/Main.h @@ -2,8 +2,8 @@ #include "WindowsWrapper.h" -extern char gModulePath[MAX_PATH]; -extern char gDataPath[MAX_PATH]; +extern char *gModulePath; +extern char *gDataPath; extern BOOL bFullscreen; extern BOOL gbUseJoystick; diff --git a/src/Map.cpp b/src/Map.cpp index 6cd6ac3e..05bf4357 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -12,6 +12,7 @@ #include "File.h" #include "Main.h" #include "NpChar.h" +#include "Helpers/FopenFormatted.h" #define PXM_BUFFER_SIZE 0x4B000 @@ -29,13 +30,9 @@ BOOL LoadMapData2(const char *path_map) { FILE *fp; char check[3]; - char path[MAX_PATH]; - - // Get path - sprintf(path, "%s/%s", gDataPath, path_map); // Open file - fp = fopen(path, "rb"); + fp = fopenFormatted("rb", "%s/%s", gDataPath, path_map); if (fp == NULL) return FALSE; @@ -69,12 +66,9 @@ BOOL LoadMapData2(const char *path_map) BOOL LoadAttributeData(const char *path_atrb) { FILE *fp; - char path[MAX_PATH]; // Open file - sprintf(path, "%s/%s", gDataPath, path_atrb); - - fp = fopen(path, "rb"); + fp = fopenFormatted("rb", "%s/%s", gDataPath, path_atrb); if (fp == NULL) return FALSE; diff --git a/src/MycParam.cpp b/src/MycParam.cpp index fe15772e..fd88f661 100644 --- a/src/MycParam.cpp +++ b/src/MycParam.cpp @@ -16,6 +16,8 @@ #include "Sound.h" #include "TextScr.h" #include "ValueView.h" +#include "Helpers/Asprintf.h" +#include "Helpers/FopenFormatted.h" ARMS_LEVEL gArmsLevelTable[14] = { @@ -436,14 +438,15 @@ BOOL SaveTimeCounter(void) unsigned char p[4]; REC rec; FILE *fp; - char path[MAX_PATH]; + char *path; // Quit if player doesn't have the Nikumaru Counter if (!(gMC.equip & EQUIP_NIKUMARU_COUNTER)) return TRUE; // Get last time - sprintf(path, "%s/290.rec", gModulePath); + if (asprintf(&path, "%s/290.rec", gModulePath) < 0) + return FALSE; fp = fopen(path, "rb"); if (fp != NULL) @@ -508,12 +511,9 @@ int LoadTimeCounter(void) unsigned char p[4]; REC rec; FILE *fp; - char path[MAX_PATH]; // Open file - sprintf(path, "%s/290.rec", gModulePath); - - fp = fopen(path, "rb"); + fp = fopenFormatted("rb", "%s/290.rec", gModulePath); if (fp == NULL) return 0; diff --git a/src/NpChar.cpp b/src/NpChar.cpp index d5d7b261..244006f5 100644 --- a/src/NpChar.cpp +++ b/src/NpChar.cpp @@ -17,6 +17,7 @@ #include "NpcTbl.h" #include "Sound.h" #include "ValueView.h" +#include "Helpers/FopenFormatted.h" NPCHAR gNPC[NPC_MAX]; int gCurlyShoot_wait; @@ -59,10 +60,7 @@ BOOL LoadEvent(const char *path_event) char code[4]; EVENT eve; - char path[MAX_PATH]; - sprintf(path, "%s/%s", gDataPath, path_event); - - fp = fopen(path, "rb"); + fp = fopenFormatted("rb", "%s/%s", gDataPath, path_event); if (fp == NULL) return FALSE; diff --git a/src/Profile.cpp b/src/Profile.cpp index 7d0c9cfa..b7fce47d 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -22,16 +22,14 @@ #include "Stage.h" #include "Star.h" #include "ValueView.h" +#include "Helpers/FopenFormatted.h" const char* const gDefaultName = "Profile.dat"; const char* const gProfileCode = "Do041220"; BOOL IsProfile(void) { - char path[MAX_PATH]; - sprintf(path, "%s/%s", gModulePath, gDefaultName); - - FILE *file = fopen(path, "rb"); + FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName); if (file == NULL) return FALSE; @@ -41,24 +39,15 @@ BOOL IsProfile(void) BOOL SaveProfile(const char *name) { - FILE *fp; - PROFILE profile; const char *FLAG = "FLAG"; - char path[MAX_PATH]; - - // Get path - if (name != NULL) - sprintf(path, "%s/%s", gModulePath, name); - else - sprintf(path, "%s/%s", gModulePath, gDefaultName); - // Open file - fp = fopen(path, "wb"); + FILE *fp = fopenFormatted("wb", "%s/%s", gModulePath, ((name != NULL) ? name : gDefaultName)); if (fp == NULL) return FALSE; // Set up profile + PROFILE profile; memset(&profile, 0, sizeof(PROFILE)); memcpy(profile.code, gProfileCode, sizeof(profile.code)); memcpy(profile.FLAG, FLAG, sizeof(profile.FLAG)); @@ -124,16 +113,13 @@ BOOL LoadProfile(const char *name) { FILE *fp; PROFILE profile; - char path[MAX_PATH]; - - // Get path - if (name != NULL) - sprintf(path, "%s", name); - else - sprintf(path, "%s/%s", gModulePath, gDefaultName); // Open file - fp = fopen(path, "rb"); + if (name != NULL) + fp = fopenFormatted("rb", "%s", name); + else + fp = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName); + if (fp == NULL) return FALSE; diff --git a/src/SelStage.cpp b/src/SelStage.cpp index 1202ac01..377b84d0 100644 --- a/src/SelStage.cpp +++ b/src/SelStage.cpp @@ -1,5 +1,6 @@ #include "SelStage.h" +#include #include #include "WindowsWrapper.h" @@ -156,13 +157,13 @@ void PutStageSelectObject(void) int StageSelectLoop(int *p_event) { - char old_script_path[MAX_PATH]; - RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; gSelectedStage = 0; BackupSurface(SURFACE_ID_SCREEN_GRAB, &grcFull); - GetTextScriptPath(old_script_path); + char *old_script_path = GetTextScriptPath(); + if (!old_script_path) + return enum_ESCRETURN_exit; LoadTextScript2("StageSelect.tsc"); gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66; StartTextScript(gPermitStage[gSelectedStage].index + 1000); @@ -176,9 +177,11 @@ int StageSelectLoop(int *p_event) switch (Call_Escape()) { case enum_ESCRETURN_exit: + free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: + free(old_script_path); return enum_ESCRETURN_restart; } } @@ -188,9 +191,11 @@ int StageSelectLoop(int *p_event) switch (TextScriptProc()) { case enum_ESCRETURN_exit: + free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: + free(old_script_path); return enum_ESCRETURN_restart; } @@ -212,6 +217,7 @@ int StageSelectLoop(int *p_event) { StopTextScript(); LoadTextScript_Stage(old_script_path); + free(old_script_path); *p_event = 0; return enum_ESCRETURN_continue; } @@ -219,10 +225,14 @@ int StageSelectLoop(int *p_event) PutFramePerSecound(); if (!Flip_SystemTask()) + { + free(old_script_path); return enum_ESCRETURN_exit; + } } LoadTextScript_Stage(old_script_path); + free(old_script_path); *p_event = gPermitStage[gSelectedStage].event; return enum_ESCRETURN_continue; } diff --git a/src/Stage.cpp b/src/Stage.cpp index 21cafa53..15c094e9 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -131,8 +131,6 @@ const STAGE_TABLE gTMT[95] = { BOOL TransferStage(int no, int w, int x, int y) { - char path[MAX_PATH]; - char path_dir[20]; BOOL bError; // Move character @@ -141,7 +139,15 @@ BOOL TransferStage(int no, int w, int x, int y) bError = FALSE; // Get path - strcpy(path_dir, "Stage"); + char path_dir[sizeof("Stage")] = "Stage"; // Can only be either "Stage" or "Npc", and sizeof("Stage") > sizeof("Npc") + /* + * The size of path should be size of the format string (without the operands) + size of first operand + size of second operand - 2 (no null terminator needed for the first two operands, and sizeof will include the size for the null terminators here) + * The size of the format string is sizeof("/.pxa") because that's the biggest raw format (omitting the operands) used here + * Size of first operand is sizeof(path_dir) because that's always the first operand + * Size of second operand is sizeof(gTMT[no].parts) because all the operands used here (gTMT[no].parts, gTMT[no].map, gTMT[no].npc, gTMT[no].boss) are of the same size + * sprintf(path, "%s", gTMT[no].back) is irrelevant here because sizeof(gTMT[no].back) is the same as the size of all the operands discussed for the size of the second operand, so gTMT[no].back always fits into the size allocated for the second operand alone + */ + char path[sizeof("/.pxa") + sizeof(path_dir) + sizeof(gTMT[no].parts) - 2]; // Load tileset sprintf(path, "%s/Prt%s", path_dir, gTMT[no].parts); diff --git a/src/TextScr.cpp b/src/TextScr.cpp index 8c9bada9..660cea3c 100644 --- a/src/TextScr.cpp +++ b/src/TextScr.cpp @@ -33,6 +33,8 @@ #include "SelStage.h" #include "Sound.h" #include "Stage.h" +#include "Helpers/Asprintf.h" +#include "Helpers/Strdup.h" // This limits the size of a .tsc script to 0x5000 bytes (the game will crash above this) #define TSC_BUFFER_SIZE 0x5000 @@ -124,17 +126,22 @@ void EncryptionBinaryData2(unsigned char *pData, long size) BOOL LoadTextScript2(const char *name) { FILE *fp; - char path[MAX_PATH]; + char *path; // Get path - sprintf(path, "%s/%s", gDataPath, name); + if (asprintf(&path, "%s/%s", gDataPath, name) < 0) + return FALSE; gTS.size = GetFileSizeLong(path); if (gTS.size == -1) + { + free(path); return FALSE; + } // Open file fp = fopen(path, "rb"); + free(path); if (fp == NULL) return FALSE; @@ -144,7 +151,9 @@ BOOL LoadTextScript2(const char *name) fclose(fp); // Set path - strcpy(gTS.path, name); + if (gTS.path != NULL) + free(gTS.path); + gTS.path = strdup(name); // Decrypt data EncryptionBinaryData2((unsigned char*)gTS.data, gTS.size); @@ -156,18 +165,23 @@ BOOL LoadTextScript2(const char *name) BOOL LoadTextScript_Stage(const char *name) { FILE *fp; - char path[MAX_PATH]; long head_size; long body_size; // Open Head.tsc - sprintf(path, "%s/%s", gDataPath, "Head.tsc"); + char *path; + if (asprintf(&path, "%s/%s", gDataPath, "Head.tsc") < 0) + return FALSE; head_size = GetFileSizeLong(path); if (head_size == -1) + { + free(path); return FALSE; + } fp = fopen(path, "rb"); + free(path); if (fp == NULL) return FALSE; @@ -178,13 +192,18 @@ BOOL LoadTextScript_Stage(const char *name) fclose(fp); // Open stage's .tsc - sprintf(path, "%s/%s", gDataPath, name); + if (asprintf(&path, "%s/%s", gDataPath, name) < 0) + return FALSE; body_size = GetFileSizeLong(path); if (body_size == -1) + { + free(path); return FALSE; + } fp = fopen(path, "rb"); + free(path); if (fp == NULL) return FALSE; @@ -196,15 +215,17 @@ BOOL LoadTextScript_Stage(const char *name) // Set parameters gTS.size = head_size + body_size; - strcpy(gTS.path, name); + if (gTS.path != NULL) + free(gTS.path); + gTS.path = strdup(name); return TRUE; } // Get current path -void GetTextScriptPath(char *path) +char *GetTextScriptPath() { - strcpy(path, gTS.path); + return strdup(gTS.path); } // Get 4 digit number from TSC data @@ -1281,11 +1302,13 @@ int TextScriptProc(void) } else { - char str_0[0x40]; + // The size of str_0 is the size of the format string (includes null terminator so that's the space we'll be using for ours) + the 3 chars from the format string #ifdef JAPANESE + char str_0[sizeof("不明のコード:<") + (sizeof(char) * 3)]; sprintf(str_0, "不明のコード:<%c%c%c", gTS.data[gTS.p_read + 1], gTS.data[gTS.p_read + 2], gTS.data[gTS.p_read + 3]); Backend_ShowMessageBox("エラー", str_0); #else + char str_0[sizeof("Unknown code:<%c%c%c") + (sizeof(char) * 3)]; sprintf(str_0, "Unknown code:<%c%c%c", gTS.data[gTS.p_read + 1], gTS.data[gTS.p_read + 2], gTS.data[gTS.p_read + 3]); Backend_ShowMessageBox("Error", str_0); #endif diff --git a/src/TextScr.h b/src/TextScr.h index eb87f61f..c3ded179 100644 --- a/src/TextScr.h +++ b/src/TextScr.h @@ -5,7 +5,7 @@ typedef struct TEXT_SCRIPT { // Path (reload when exit teleporter menu/inventory) - char path[MAX_PATH]; + char *path; // Script buffer long size; @@ -62,7 +62,7 @@ void EndTextScript(void); void EncryptionBinaryData2(unsigned char *pData, long size); BOOL LoadTextScript2(const char *name); BOOL LoadTextScript_Stage(const char *name); -void GetTextScriptPath(char *path); +char *GetTextScriptPath(); BOOL StartTextScript(int no); void StopTextScript(void); void PutTextScript(void); diff --git a/src/WindowsWrapper.h b/src/WindowsWrapper.h index 58eda2c1..1db7be95 100644 --- a/src/WindowsWrapper.h +++ b/src/WindowsWrapper.h @@ -4,10 +4,9 @@ #define WIN32_LEAN_AND_MEAN #include #undef FindResource +#undef MAX_PATH // Explicitly undefine MAX_PATH to avoid accidental usage of it #else -#include - #define RGB(r,g,b) ((r) | ((g) << 8) | ((b) << 16)) typedef bool BOOL; @@ -23,6 +22,4 @@ struct RECT long bottom; }; -#define MAX_PATH FILENAME_MAX - #endif From 564d42dbd23a369d7d575b176e93fc0721fb407d Mon Sep 17 00:00:00 2001 From: Gabriel Ravier Date: Mon, 29 Jun 2020 16:13:14 +0200 Subject: [PATCH 2/9] src: Change from using asprintf-based code to using std::string-based code Signed-off-by: Gabriel Ravier --- CMakeLists.txt | 8 ------ src/ArmsItem.cpp | 15 ++-------- src/Back.cpp | 3 +- src/Backends/Misc.h | 3 +- src/Backends/Platform/GLFW3.cpp | 2 +- src/Backends/Platform/Null.cpp | 3 +- src/Backends/Platform/SDL2.cpp | 7 +++-- src/Backends/Platform/WiiU.cpp | 6 ++-- src/Config.cpp | 3 +- src/Draw.cpp | 41 +++++++------------------- src/Ending.cpp | 17 ++--------- src/Game.cpp | 9 ++---- src/Generic.cpp | 27 +++++------------ src/Helpers/Asprintf.cpp | 18 ------------ src/Helpers/Asprintf.h | 15 ---------- src/Helpers/FopenFormatted.cpp | 23 --------------- src/Helpers/FopenFormatted.h | 5 ---- src/Helpers/Strdup.cpp | 20 ------------- src/Helpers/Strdup.h | 16 ----------- src/Helpers/Vasprintf.cpp | 32 --------------------- src/Helpers/Vasprintf.h | 16 ----------- src/Main.cpp | 48 +++++++------------------------ src/Main.h | 5 ++-- src/Map.cpp | 8 ++---- src/MycParam.cpp | 14 +++------ src/NpChar.cpp | 4 +-- src/Profile.cpp | 11 ++++--- src/SelStage.cpp | 17 ++--------- src/Stage.cpp | 45 ++++++++++------------------- src/TextScr.cpp | 51 +++++++++------------------------ src/TextScr.h | 5 ++-- 31 files changed, 99 insertions(+), 398 deletions(-) delete mode 100644 src/Helpers/Asprintf.cpp delete mode 100644 src/Helpers/Asprintf.h delete mode 100644 src/Helpers/FopenFormatted.cpp delete mode 100644 src/Helpers/FopenFormatted.h delete mode 100644 src/Helpers/Strdup.cpp delete mode 100644 src/Helpers/Strdup.h delete mode 100644 src/Helpers/Vasprintf.cpp delete mode 100644 src/Helpers/Vasprintf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 578f595b..df7c78d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,14 +171,6 @@ add_executable(CSE2 WIN32 "src/Backends/Controller.h" "src/Backends/Misc.h" "src/Backends/Rendering.h" - "src/Helpers/Asprintf.cpp" - "src/Helpers/Asprintf.h" - "src/Helpers/Vasprintf.cpp" - "src/Helpers/Vasprintf.h" - "src/Helpers/FopenFormatted.cpp" - "src/Helpers/FopenFormatted.h" - "src/Helpers/Strdup.cpp" - "src/Helpers/Strdup.h" ) set(RESOURCES diff --git a/src/ArmsItem.cpp b/src/ArmsItem.cpp index dd25c6d1..a1062f00 100644 --- a/src/ArmsItem.cpp +++ b/src/ArmsItem.cpp @@ -416,12 +416,9 @@ void PutCampObject(void) int CampLoop(void) { RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; - int ret = enum_ESCRETURN_continue; // Save the current script path (to restore it when we get out of the inventory) - char *old_script_path = GetTextScriptPath(); - if (old_script_path == NULL) - return enum_ESCRETURN_exit; + std::string old_script_path = GetTextScriptPath(); // Load the inventory script LoadTextScript2("ArmsItem.tsc"); @@ -452,11 +449,9 @@ int CampLoop(void) switch (Call_Escape()) { case enum_ESCRETURN_exit: - free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - free(old_script_path); return enum_ESCRETURN_restart; } } @@ -467,11 +462,9 @@ int CampLoop(void) switch (TextScriptProc()) { case enum_ESCRETURN_exit: - free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - free(old_script_path); return enum_ESCRETURN_restart; } @@ -500,16 +493,12 @@ int CampLoop(void) } if (!Flip_SystemTask()) - { - free(old_script_path); return enum_ESCRETURN_exit; - } } // Resume original script - LoadTextScript_Stage(old_script_path); + LoadTextScript_Stage(old_script_path.c_str()); gArmsEnergyX = 32; // Displays weapon rotation animation in case the weapon was changed - free(old_script_path); return enum_ESCRETURN_continue; } diff --git a/src/Back.cpp b/src/Back.cpp index 97f00350..09f9bbaf 100644 --- a/src/Back.cpp +++ b/src/Back.cpp @@ -13,7 +13,6 @@ #include "Main.h" #include "Map.h" #include "Stage.h" -#include "Helpers/FopenFormatted.h" BACK gBack; int gWaterY; @@ -26,7 +25,7 @@ BOOL InitBack(const char *fName, int type) color_black = GetCortBoxColor(RGB(0, 0, 0x10)); // Get width and height - FILE *fp = fopenFormatted("rb", "%s/%s.pbm", gDataPath, fName); + FILE *fp = fopen((gDataPath + '/' + fName + ".pbm").c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index 25fbdf60..9fcddc26 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -1,6 +1,7 @@ #pragma once #include "../Attributes.h" +#include enum { @@ -86,7 +87,7 @@ enum bool Backend_Init(void); void Backend_Deinit(void); void Backend_PostWindowCreation(void); -bool Backend_GetBasePath(char **string_buffer); +bool Backend_GetBasePath(std::string *string_buffer); void Backend_HideMouse(void); void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index 23ed0d17..83fd83f2 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -181,7 +181,7 @@ void Backend_PostWindowCreation(void) glfwSetWindowSizeCallback(window, WindowSizeCallback); } -bool Backend_GetBasePath(char **string_buffer) +bool Backend_GetBasePath(std::string *string_buffer) { (void)string_buffer; diff --git a/src/Backends/Platform/Null.cpp b/src/Backends/Platform/Null.cpp index bf4ab2f2..a3988631 100644 --- a/src/Backends/Platform/Null.cpp +++ b/src/Backends/Platform/Null.cpp @@ -1,4 +1,5 @@ #include "../Misc.h" +#include bool Backend_Init(void) { @@ -15,7 +16,7 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char **string_buffer) +bool Backend_GetBasePath(std::string *string_buffer) { (void)string_buffer; diff --git a/src/Backends/Platform/SDL2.cpp b/src/Backends/Platform/SDL2.cpp index 775f265c..1d9e2484 100644 --- a/src/Backends/Platform/SDL2.cpp +++ b/src/Backends/Platform/SDL2.cpp @@ -13,7 +13,6 @@ #include "../../Organya.h" #include "../../Profile.h" #include "../../Resource.h" -#include "../../Helpers/Strdup.h" #define DO_KEY(SDL_KEY, BACKEND_KEY) \ case SDL_KEY: \ @@ -85,13 +84,15 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char **string_buffer) +bool Backend_GetBasePath(std::string *string_buffer) { #ifdef _WIN32 // SDL_GetBasePath returns a UTF-8 string, but Windows' fopen uses (extended?) ASCII. // This is apparently a problem for accented characters, as they will make fopen fail. // So, instead, we rely on argv[0], as that will put the accented characters in a // format Windows will understand. + (void)string_buffer; + return false; #else char *base_path = SDL_GetBasePath(); @@ -101,7 +102,7 @@ bool Backend_GetBasePath(char **string_buffer) // Trim the trailing '/' size_t base_path_length = strlen(base_path); base_path[base_path_length - 1] = '\0'; - *string_buffer = strdup(base_path); + *string_buffer = base_path; SDL_free(base_path); return true; diff --git a/src/Backends/Platform/WiiU.cpp b/src/Backends/Platform/WiiU.cpp index 14e5f54d..0c89684d 100644 --- a/src/Backends/Platform/WiiU.cpp +++ b/src/Backends/Platform/WiiU.cpp @@ -56,12 +56,12 @@ void Backend_PostWindowCreation(void) } -bool Backend_GetBasePath(char **string_buffer) +bool Backend_GetBasePath(std::string *string_buffer) { #ifdef JAPANESE - *string_buffer = asprintf("%s/CSE-portable-jp", WHBGetSdCardMountPath()); + *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-jp"; #else - *string_buffer = asprintf("%s/CSE-portable-en", WHBGetSdCardMountPath()); + *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-en"; #endif return true; } diff --git a/src/Config.cpp b/src/Config.cpp index a0a5a585..3b4f4443 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -7,7 +7,6 @@ #include "Config.h" #include "File.h" #include "Main.h" -#include "Helpers/FopenFormatted.h" const char* const gConfigName = "Config.dat"; const char* const gProof = "DOUKUTSU20041206"; @@ -18,7 +17,7 @@ BOOL LoadConfigData(CONFIG *conf) memset(conf, 0, sizeof(CONFIG)); // Open file - FILE *fp = fopenFormatted("rb", "%s/%s", gModulePath, gConfigName); + FILE *fp = fopen((gModulePath + '/' + gConfigName).c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Draw.cpp b/src/Draw.cpp index 61804bc9..212f87c2 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -18,7 +18,6 @@ #include "MapName.h" #include "Resource.h" #include "TextScr.h" -#include "Helpers/Asprintf.h" typedef enum SurfaceType { @@ -251,15 +250,11 @@ BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL MakeSurface_File(const char *name, SurfaceID surf_no) { - char *path; + std::string path = gDataPath + '/' + name + ".pbm"; - if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0) - return FALSE; - - if (!IsEnableBitmap(path)) + if (!IsEnableBitmap(path.c_str())) { - ErrorLog(path, 0); - free(path); + ErrorLog(path.c_str(), 0); return FALSE; } @@ -270,27 +265,23 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no) #endif { ErrorLog("surface no", surf_no); - free(path); return FALSE; } if (surf[surf_no] != NULL) { ErrorLog("existing", surf_no); - free(path); return FALSE; } unsigned int width, height; - unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height); + unsigned char *image_buffer = DecodeBitmapFromFile(path.c_str(), &width, &height); if (image_buffer == NULL) { - ErrorLog(path, 1); - free(path); + ErrorLog(path.c_str(), 1); return FALSE; } - free(path); surf[surf_no] = RenderBackend_CreateSurface(width * mag, height * mag, false); @@ -349,14 +340,11 @@ BOOL ReloadBitmap_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) { - char *path; - if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0) - return FALSE; + std::string path = gDataPath + '/' + name + ".pbm"; - if (!IsEnableBitmap(path)) + if (!IsEnableBitmap(path.c_str())) { - ErrorLog(path, 0); - free(path); + ErrorLog(path.c_str(), 0); return FALSE; } @@ -367,20 +355,17 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) #endif { ErrorLog("surface no", surf_no); - free(path); return FALSE; } unsigned int width, height; - unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height); + unsigned char *image_buffer = DecodeBitmapFromFile(path.c_str(), &width, &height); if (image_buffer == NULL) { - ErrorLog(path, 1); - free(path); + ErrorLog(path.c_str(), 1); return FALSE; } - free(path); if (!ScaleAndUploadSurface(image_buffer, width, height, surf_no)) { @@ -659,10 +644,6 @@ void InitTextObject(const char *name) { (void)name; // Unused in this branch - char *path; - if (asprintf(&path, "%s/Font/font", gDataPath) < 0) - return; - // Get font size unsigned int width, height; @@ -679,7 +660,7 @@ void InitTextObject(const char *name) break; } - font = LoadFont(path, width, height); + font = LoadFont((gDataPath + "/Font/font").c_str(), width, height); } void PutText(int x, int y, const char *text, unsigned long color) diff --git a/src/Ending.cpp b/src/Ending.cpp index 73c75edd..cc1cfc50 100644 --- a/src/Ending.cpp +++ b/src/Ending.cpp @@ -16,7 +16,6 @@ #include "Organya.h" #include "Stage.h" #include "TextScr.h" -#include "Helpers/Asprintf.h" struct CREDIT { @@ -226,27 +225,17 @@ BOOL StartCreditScript(void) } // Open file - char *path; - if (asprintf(&path, "%s/%s", gDataPath, credit_script) < 0) - return FALSE; - - Credit.size = GetFileSizeLong(path); + std::string path = gDataPath + '/' + credit_script; + Credit.size = GetFileSizeLong(path.c_str()); if (Credit.size == -1) - { - free(path); return FALSE; - } // Allocate buffer data Credit.pData = (char*)malloc(Credit.size); if (Credit.pData == NULL) - { - free(path); return FALSE; - } - fp = fopen(path, "rb"); - free(path); + fp = fopen(path.c_str(), "rb"); if (fp == NULL) { free(Credit.pData); diff --git a/src/Game.cpp b/src/Game.cpp index 1a613ca4..63c9acc5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -44,7 +44,6 @@ #include "Star.h" #include "TextScr.h" #include "ValueView.h" -#include "Helpers/Asprintf.h" int g_GameFlags; int gCounter; @@ -702,13 +701,9 @@ BOOL Game(void) PlaySoundObject(7, -1); - char *path; - if (asprintf(&path, "%s/npc.tbl", gDataPath) < 0) - return FALSE; + std::string path = gDataPath + "/npc.tbl"; - BOOL load_npc_table_succeeded = LoadNpcTable(path); - free(path); - if (!load_npc_table_succeeded) + if (!LoadNpcTable(path.c_str())) { #ifdef JAPANESE Backend_ShowMessageBox("エラー", "NPCテーブルが読めない"); diff --git a/src/Generic.cpp b/src/Generic.cpp index 753f7bfa..66223b6e 100644 --- a/src/Generic.cpp +++ b/src/Generic.cpp @@ -8,8 +8,6 @@ #include "WindowsWrapper.h" #include "Main.h" -#include "Helpers/Asprintf.h" -#include "Helpers/FopenFormatted.h" void GetCompileDate(int *year, int *month, int *day) { @@ -52,19 +50,12 @@ BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4) void DeleteLog(void) { - char *path; - - if (asprintf(&path, "%s/debug.txt", gModulePath) < 0) - return; - - remove(path); - free(path); + remove((gModulePath + "/debug.txt").c_str()); } BOOL WriteLog(const char *string, int value1, int value2, int value3) { - FILE *fp = fopenFormatted("a+", "%s/debug.txt", gModulePath); - + FILE *fp = fopen((gModulePath + "/debug.txt").c_str(), "a+"); if (fp == NULL) return FALSE; @@ -75,8 +66,7 @@ BOOL WriteLog(const char *string, int value1, int value2, int value3) BOOL IsKeyFile(const char *name) { - FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, name); - + FILE *file = fopen((gModulePath + '/' + name).c_str(), "rb"); if (file == NULL) return FALSE; @@ -104,15 +94,12 @@ long GetFileSizeLong(const char *path) BOOL ErrorLog(const char *string, int value) { - char *path; - if (asprintf(&path, "%s/%s", gModulePath, "error.log") < 0) - return FALSE; + std::string path = gModulePath + "/error.log"; - if (GetFileSizeLong(path) > 0x19000) // Purge the error log if it gets too big, I guess - remove(path); + if (GetFileSizeLong(path.c_str()) > 0x19000) // Purge the error log if it gets too big, I guess + remove(path.c_str()); - FILE *fp = fopen(path, "a+"); - free(path); + FILE *fp = fopen(path.c_str(), "a+"); if (fp == NULL) return FALSE; diff --git a/src/Helpers/Asprintf.cpp b/src/Helpers/Asprintf.cpp deleted file mode 100644 index 6d8cfb54..00000000 --- a/src/Helpers/Asprintf.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _GNU_SOURCE - -#include "Asprintf.h" -#include "Vasprintf.h" -#include "../Attributes.h" -#include - -ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **result_string, const char *format_string, ...) -{ - // Handle variadic arguments and redirect to vasprintf - va_list arguments_local; - va_start(arguments_local, format_string); - int retVal = vasprintf(result_string, format_string, arguments_local); - va_end(arguments_local); // Destroy arguments_local - return retVal; -} - -#endif diff --git a/src/Helpers/Asprintf.h b/src/Helpers/Asprintf.h deleted file mode 100644 index c1337abc..00000000 --- a/src/Helpers/Asprintf.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -// If defines asprintf for us, use its definition -#ifdef _GNU_SOURCE - -#include - -#else - -#include "../Attributes.h" -#define asprintf Portable_asprintf - -ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **resultString, const char *formatString, ...); - -#endif diff --git a/src/Helpers/FopenFormatted.cpp b/src/Helpers/FopenFormatted.cpp deleted file mode 100644 index ded92eea..00000000 --- a/src/Helpers/FopenFormatted.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "FopenFormatted.h" -#include "../Attributes.h" -#include -#include -#include - -ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *format_string, ...) -{ - // Handle variadic arguments and redirect to vasprintf - va_list arguments_local; - va_start(arguments_local, format_string); - char *path; - int vasprintf_retval = vasprintf(&path, format_string, arguments_local); - va_end(arguments_local); // Destroy arguments_local - - if (vasprintf_retval < 0) - return NULL; - - FILE *file = fopen(path, mode); - free(path); - - return file; -} diff --git a/src/Helpers/FopenFormatted.h b/src/Helpers/FopenFormatted.h deleted file mode 100644 index e13c1726..00000000 --- a/src/Helpers/FopenFormatted.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include "../Attributes.h" -#include - -ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *formatString, ...); diff --git a/src/Helpers/Strdup.cpp b/src/Helpers/Strdup.cpp deleted file mode 100644 index 84f9f478..00000000 --- a/src/Helpers/Strdup.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#if !(_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L) - -#include "Strdup.h" -#include "../Attributes.h" -#include -#include - -ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL((1)) char *strdup(const char *duplicated_string) -{ - size_t duplicatedStringLength = strlen(duplicated_string) + sizeof(char); // Length of the whole string, plus the terminator - char *returnedString = (char *)malloc(duplicatedStringLength); // Memory for the new string is obtained with malloc. malloc also sets errno to ENOMEM on failure, which is exactly what we want for conformance - - if (returnedString) // If the result of malloc was NULL, allocation failed copying into it would be UB - memcpy(returnedString, duplicated_string, duplicatedStringLength); // Copy the entire duplicated string (including the null terminator) - - return returnedString; // Returns a pointer to the duplicated string on success, NULL if insufficient memory was available -} - -#endif diff --git a/src/Helpers/Strdup.h b/src/Helpers/Strdup.h deleted file mode 100644 index 08111b6a..00000000 --- a/src/Helpers/Strdup.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include // Probably the smallest header that will include on systems with it - -// If defines strdup for us, use its definition -#if (_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L) - -#include - -#else - -#include "../Attributes.h" -#define strdup Portable_strdup - -ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL(1) char *strdup(const char *duplicatedString); - -#endif diff --git a/src/Helpers/Vasprintf.cpp b/src/Helpers/Vasprintf.cpp deleted file mode 100644 index ba5babfd..00000000 --- a/src/Helpers/Vasprintf.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef _GNU_SOURCE - -#include "Vasprintf.h" -#include "../Attributes.h" -#include -#include -#include -#include - -ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **result_string, const char *format_string, va_list arguments) -{ - const int error_return_value = -1; // According to the man page, this is returned on error - va_list arguments_local; - va_copy(arguments_local, arguments); // Copy the arguments so we can actually use them - - int size = vsnprintf(NULL, 0, format_string, arguments_local); // Get the amount of bytes we need for the output buffer - - va_end(arguments_local); // Destroy arguments_local - - if (size < 0) // If an output error is encountered, vsnprintf returns a negative value - return error_return_value; - - *result_string = (char *)malloc(size + 1); // Allocate enough space for the string (need + 1 for the null terminator) - - if (*result_string == NULL) // On error, malloc returns NULL - return error_return_value; - - // We know we have enough space, so we can use vsprintf safely - return vsprintf(*result_string, format_string, arguments); // vsprintf returns the amount of characters that got printed. This is what we're supposed to return -} - -#endif diff --git a/src/Helpers/Vasprintf.h b/src/Helpers/Vasprintf.h deleted file mode 100644 index 7af89984..00000000 --- a/src/Helpers/Vasprintf.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -// If defines asprintf for us, use its definition -#ifdef _GNU_SOURCE - -#include - -#else - -#include "../Attributes.h" -#include -#define vasprintf Portable_vasprintf - -ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **resultString, const char *formatString, va_list arguments); - -#endif diff --git a/src/Main.cpp b/src/Main.cpp index 61e861a1..7f78cf71 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -24,11 +24,9 @@ #include "Resource.h" #include "Sound.h" #include "Triangle.h" -#include "Helpers/Asprintf.h" -#include "Helpers/Strdup.h" -char *gModulePath; -char *gDataPath; +std::string gModulePath; +std::string gDataPath; BOOL bFullscreen; BOOL gbUseJoystick = FALSE; @@ -84,24 +82,6 @@ void PutFramePerSecound(void) } } -static int main_out_backend_deinit(int return_value) -{ - Backend_Deinit(); - return return_value; -} - -static int main_out_free_module_path(int return_value) -{ - free(gModulePath); - return main_out_backend_deinit(return_value); -} - -static int main_out_free_data_path(int return_value) -{ - free(gDataPath); - return main_out_free_module_path(return_value); -} - // TODO - Inaccurate stack frame int main(int argc, char *argv[]) { @@ -116,26 +96,20 @@ int main(int argc, char *argv[]) if (!Backend_GetBasePath(&gModulePath)) { // Fall back on argv[0] if the backend cannot provide a path - gModulePath = strdup(argv[0]); - if (!gModulePath) - { - Backend_PrintError("Could not get executable path (not enough memory)"); - return main_out_backend_deinit(EXIT_FAILURE); - } + gModulePath = argv[0]; - for (size_t i = strlen(gModulePath);; --i) + for (size_t i = gModulePath.length();; --i) { if (i == 0 || gModulePath[i] == '\\' || gModulePath[i] == '/') { - gModulePath[i] = '\0'; + gModulePath.resize(i); break; } } } // Get path of the data folder - if (asprintf(&gDataPath, "%s/data", gModulePath) < 0) - return main_out_free_module_path(EXIT_FAILURE); + gDataPath = gModulePath + "/data"; CONFIG conf; if (!LoadConfigData(&conf)) @@ -248,12 +222,12 @@ int main(int argc, char *argv[]) if (conf.display_mode == 1) { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0)) - return main_out_free_data_path(EXIT_FAILURE); + return EXIT_FAILURE; } else { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1)) - return main_out_free_data_path(EXIT_FAILURE); + return EXIT_FAILURE; } #else // Doesn't handle StartDirectDraw failing @@ -274,7 +248,7 @@ int main(int argc, char *argv[]) #ifdef FIX_BUGS if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2)) - return main_out_free_data_path(EXIT_FAILURE); + return EXIT_FAILURE; #else // Doesn't handle StartDirectDraw failing StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2); @@ -342,7 +316,7 @@ int main(int argc, char *argv[]) // Draw to screen if (!Flip_SystemTask()) - return main_out_free_data_path(EXIT_SUCCESS); + return EXIT_SUCCESS; // Initialize sound InitDirectSound(); @@ -366,7 +340,7 @@ int main(int argc, char *argv[]) EndDirectSound(); EndDirectDraw(); - return main_out_free_data_path(EXIT_SUCCESS); + return EXIT_SUCCESS; } void InactiveWindow(void) diff --git a/src/Main.h b/src/Main.h index 6c49da91..ace8799a 100644 --- a/src/Main.h +++ b/src/Main.h @@ -1,9 +1,10 @@ #pragma once #include "WindowsWrapper.h" +#include -extern char *gModulePath; -extern char *gDataPath; +extern std::string gModulePath; +extern std::string gDataPath; extern BOOL bFullscreen; extern BOOL gbUseJoystick; diff --git a/src/Map.cpp b/src/Map.cpp index 05bf4357..7ebfa675 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -12,7 +12,6 @@ #include "File.h" #include "Main.h" #include "NpChar.h" -#include "Helpers/FopenFormatted.h" #define PXM_BUFFER_SIZE 0x4B000 @@ -28,11 +27,10 @@ BOOL InitMapData2(void) BOOL LoadMapData2(const char *path_map) { - FILE *fp; char check[3]; // Open file - fp = fopenFormatted("rb", "%s/%s", gDataPath, path_map); + FILE *fp = fopen((gDataPath + '/' + path_map).c_str(), "rb"); if (fp == NULL) return FALSE; @@ -65,10 +63,8 @@ BOOL LoadMapData2(const char *path_map) BOOL LoadAttributeData(const char *path_atrb) { - FILE *fp; - // Open file - fp = fopenFormatted("rb", "%s/%s", gDataPath, path_atrb); + FILE *fp = fopen((gDataPath + '/' + path_atrb).c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/MycParam.cpp b/src/MycParam.cpp index fd88f661..9764f417 100644 --- a/src/MycParam.cpp +++ b/src/MycParam.cpp @@ -16,8 +16,6 @@ #include "Sound.h" #include "TextScr.h" #include "ValueView.h" -#include "Helpers/Asprintf.h" -#include "Helpers/FopenFormatted.h" ARMS_LEVEL gArmsLevelTable[14] = { @@ -437,18 +435,15 @@ BOOL SaveTimeCounter(void) int i; unsigned char p[4]; REC rec; - FILE *fp; - char *path; // Quit if player doesn't have the Nikumaru Counter if (!(gMC.equip & EQUIP_NIKUMARU_COUNTER)) return TRUE; // Get last time - if (asprintf(&path, "%s/290.rec", gModulePath) < 0) - return FALSE; + std::string path = gModulePath + "/290.rec"; - fp = fopen(path, "rb"); + FILE *fp = fopen(path.c_str(), "rb"); if (fp != NULL) { // Read data @@ -488,7 +483,7 @@ BOOL SaveTimeCounter(void) rec.counter[i] = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); } - fp = fopen(path, "wb"); + fp = fopen(path.c_str(), "wb"); if (fp == NULL) return FALSE; @@ -510,10 +505,9 @@ int LoadTimeCounter(void) int i; unsigned char p[4]; REC rec; - FILE *fp; // Open file - fp = fopenFormatted("rb", "%s/290.rec", gModulePath); + FILE *fp = fopen((gModulePath + "/290.rec").c_str(), "rb"); if (fp == NULL) return 0; diff --git a/src/NpChar.cpp b/src/NpChar.cpp index 244006f5..55a01d55 100644 --- a/src/NpChar.cpp +++ b/src/NpChar.cpp @@ -17,7 +17,6 @@ #include "NpcTbl.h" #include "Sound.h" #include "ValueView.h" -#include "Helpers/FopenFormatted.h" NPCHAR gNPC[NPC_MAX]; int gCurlyShoot_wait; @@ -55,12 +54,11 @@ void InitNpChar(void) BOOL LoadEvent(const char *path_event) { int i, n; - FILE *fp; int count; char code[4]; EVENT eve; - fp = fopenFormatted("rb", "%s/%s", gDataPath, path_event); + FILE *fp = fopen((gDataPath + '/' + path_event).c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Profile.cpp b/src/Profile.cpp index b7fce47d..8642df63 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -22,14 +22,13 @@ #include "Stage.h" #include "Star.h" #include "ValueView.h" -#include "Helpers/FopenFormatted.h" const char* const gDefaultName = "Profile.dat"; const char* const gProfileCode = "Do041220"; BOOL IsProfile(void) { - FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName); + FILE *file = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb"); if (file == NULL) return FALSE; @@ -42,7 +41,7 @@ BOOL SaveProfile(const char *name) const char *FLAG = "FLAG"; // Open file - FILE *fp = fopenFormatted("wb", "%s/%s", gModulePath, ((name != NULL) ? name : gDefaultName)); + FILE *fp = fopen((gModulePath + '/' + ((name != NULL) ? name : gDefaultName)).c_str(), "wb"); if (fp == NULL) return FALSE; @@ -111,14 +110,14 @@ BOOL SaveProfile(const char *name) BOOL LoadProfile(const char *name) { - FILE *fp; PROFILE profile; // Open file + FILE *fp; if (name != NULL) - fp = fopenFormatted("rb", "%s", name); + fp = fopen(name, "rb"); else - fp = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName); + fp = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/SelStage.cpp b/src/SelStage.cpp index 377b84d0..4e229ed7 100644 --- a/src/SelStage.cpp +++ b/src/SelStage.cpp @@ -161,9 +161,7 @@ int StageSelectLoop(int *p_event) gSelectedStage = 0; BackupSurface(SURFACE_ID_SCREEN_GRAB, &grcFull); - char *old_script_path = GetTextScriptPath(); - if (!old_script_path) - return enum_ESCRETURN_exit; + std::string old_script_path = GetTextScriptPath(); LoadTextScript2("StageSelect.tsc"); gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66; StartTextScript(gPermitStage[gSelectedStage].index + 1000); @@ -177,11 +175,9 @@ int StageSelectLoop(int *p_event) switch (Call_Escape()) { case enum_ESCRETURN_exit: - free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - free(old_script_path); return enum_ESCRETURN_restart; } } @@ -191,11 +187,9 @@ int StageSelectLoop(int *p_event) switch (TextScriptProc()) { case enum_ESCRETURN_exit: - free(old_script_path); return enum_ESCRETURN_exit; case enum_ESCRETURN_restart: - free(old_script_path); return enum_ESCRETURN_restart; } @@ -216,8 +210,7 @@ int StageSelectLoop(int *p_event) else if (gKeyTrg & gKeyCancel) { StopTextScript(); - LoadTextScript_Stage(old_script_path); - free(old_script_path); + LoadTextScript_Stage(old_script_path.c_str()); *p_event = 0; return enum_ESCRETURN_continue; } @@ -225,14 +218,10 @@ int StageSelectLoop(int *p_event) PutFramePerSecound(); if (!Flip_SystemTask()) - { - free(old_script_path); return enum_ESCRETURN_exit; - } } - LoadTextScript_Stage(old_script_path); - free(old_script_path); + LoadTextScript_Stage(old_script_path.c_str()); *p_event = gPermitStage[gSelectedStage].event; return enum_ESCRETURN_continue; } diff --git a/src/Stage.cpp b/src/Stage.cpp index 15c094e9..4e3bf9fa 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -138,56 +138,41 @@ BOOL TransferStage(int no, int w, int x, int y) bError = FALSE; - // Get path - char path_dir[sizeof("Stage")] = "Stage"; // Can only be either "Stage" or "Npc", and sizeof("Stage") > sizeof("Npc") - /* - * The size of path should be size of the format string (without the operands) + size of first operand + size of second operand - 2 (no null terminator needed for the first two operands, and sizeof will include the size for the null terminators here) - * The size of the format string is sizeof("/.pxa") because that's the biggest raw format (omitting the operands) used here - * Size of first operand is sizeof(path_dir) because that's always the first operand - * Size of second operand is sizeof(gTMT[no].parts) because all the operands used here (gTMT[no].parts, gTMT[no].map, gTMT[no].npc, gTMT[no].boss) are of the same size - * sprintf(path, "%s", gTMT[no].back) is irrelevant here because sizeof(gTMT[no].back) is the same as the size of all the operands discussed for the size of the second operand, so gTMT[no].back always fits into the size allocated for the second operand alone - */ - char path[sizeof("/.pxa") + sizeof(path_dir) + sizeof(gTMT[no].parts) - 2]; - // Load tileset - sprintf(path, "%s/Prt%s", path_dir, gTMT[no].parts); - if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_TILESET)) + std::string path = std::string{"Stage/Prt"} + gTMT[no].parts; + if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_TILESET)) bError = TRUE; - sprintf(path, "%s/%s.pxa", path_dir, gTMT[no].parts); - if (!LoadAttributeData(path)) + path = std::string{"Stage/"} + gTMT[no].parts + ".pxa"; + if (!LoadAttributeData(path.c_str())) bError = TRUE; // Load tilemap - sprintf(path, "%s/%s.pxm", path_dir, gTMT[no].map); - if (!LoadMapData2(path)) + path = std::string{"Stage/"} + gTMT[no].map + ".pxm"; + if (!LoadMapData2(path.c_str())) bError = TRUE; // Load NPCs - sprintf(path, "%s/%s.pxe", path_dir, gTMT[no].map); - if (!LoadEvent(path)) + path = std::string{"Stage/"} + gTMT[no].map + ".pxe"; + if (!LoadEvent(path.c_str())) bError = TRUE; // Load script - sprintf(path, "%s/%s.tsc", path_dir, gTMT[no].map); - if (!LoadTextScript_Stage(path)) + path = std::string{"Stage/"} + gTMT[no].map + ".tsc"; + if (!LoadTextScript_Stage(path.c_str())) bError = TRUE; // Load background - sprintf(path, "%s", gTMT[no].back); - if (!InitBack(path, gTMT[no].bkType)) + if (!InitBack(gTMT[no].back, gTMT[no].bkType)) bError = TRUE; - // Get path - strcpy(path_dir, "Npc"); - // Load NPC sprite sheets - sprintf(path, "%s/Npc%s", path_dir, gTMT[no].npc); - if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_SPRITESET_1)) + path = std::string{"Npc/Npc"} + gTMT[no].npc; + if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_1)) bError = TRUE; - sprintf(path, "%s/Npc%s", path_dir, gTMT[no].boss); - if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_SPRITESET_2)) + path = std::string{"Npc/Npc"} + gTMT[no].boss; + if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_2)) bError = TRUE; if (bError) diff --git a/src/TextScr.cpp b/src/TextScr.cpp index 660cea3c..2c305411 100644 --- a/src/TextScr.cpp +++ b/src/TextScr.cpp @@ -33,8 +33,6 @@ #include "SelStage.h" #include "Sound.h" #include "Stage.h" -#include "Helpers/Asprintf.h" -#include "Helpers/Strdup.h" // This limits the size of a .tsc script to 0x5000 bytes (the game will crash above this) #define TSC_BUFFER_SIZE 0x5000 @@ -126,22 +124,16 @@ void EncryptionBinaryData2(unsigned char *pData, long size) BOOL LoadTextScript2(const char *name) { FILE *fp; - char *path; // Get path - if (asprintf(&path, "%s/%s", gDataPath, name) < 0) - return FALSE; + std::string path = gDataPath + '/' + name; - gTS.size = GetFileSizeLong(path); + gTS.size = GetFileSizeLong(path.c_str()); if (gTS.size == -1) - { - free(path); return FALSE; - } // Open file - fp = fopen(path, "rb"); - free(path); + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; @@ -151,9 +143,7 @@ BOOL LoadTextScript2(const char *name) fclose(fp); // Set path - if (gTS.path != NULL) - free(gTS.path); - gTS.path = strdup(name); + gTS.path = name; // Decrypt data EncryptionBinaryData2((unsigned char*)gTS.data, gTS.size); @@ -169,19 +159,12 @@ BOOL LoadTextScript_Stage(const char *name) long body_size; // Open Head.tsc - char *path; - if (asprintf(&path, "%s/%s", gDataPath, "Head.tsc") < 0) - return FALSE; - - head_size = GetFileSizeLong(path); + std::string path = gDataPath + "/Head.tsc"; + head_size = GetFileSizeLong(path.c_str()); if (head_size == -1) - { - free(path); return FALSE; - } - fp = fopen(path, "rb"); - free(path); + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; @@ -192,18 +175,12 @@ BOOL LoadTextScript_Stage(const char *name) fclose(fp); // Open stage's .tsc - if (asprintf(&path, "%s/%s", gDataPath, name) < 0) - return FALSE; - - body_size = GetFileSizeLong(path); + path = gDataPath + '/' + name; + body_size = GetFileSizeLong(path.c_str()); if (body_size == -1) - { - free(path); return FALSE; - } - fp = fopen(path, "rb"); - free(path); + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; @@ -215,17 +192,15 @@ BOOL LoadTextScript_Stage(const char *name) // Set parameters gTS.size = head_size + body_size; - if (gTS.path != NULL) - free(gTS.path); - gTS.path = strdup(name); + gTS.path = name; return TRUE; } // Get current path -char *GetTextScriptPath() +std::string GetTextScriptPath() { - return strdup(gTS.path); + return gTS.path; } // Get 4 digit number from TSC data diff --git a/src/TextScr.h b/src/TextScr.h index c3ded179..0f74c0fa 100644 --- a/src/TextScr.h +++ b/src/TextScr.h @@ -1,11 +1,12 @@ #pragma once #include "WindowsWrapper.h" +#include typedef struct TEXT_SCRIPT { // Path (reload when exit teleporter menu/inventory) - char *path; + std::string path; // Script buffer long size; @@ -62,7 +63,7 @@ void EndTextScript(void); void EncryptionBinaryData2(unsigned char *pData, long size); BOOL LoadTextScript2(const char *name); BOOL LoadTextScript_Stage(const char *name); -char *GetTextScriptPath(); +std::string GetTextScriptPath(); BOOL StartTextScript(int no); void StopTextScript(void); void PutTextScript(void); From 771b944d1719cefbfeaced882a8d73cf9a556e04 Mon Sep 17 00:00:00 2001 From: Gabriel Ravier Date: Mon, 29 Jun 2020 18:52:04 +0200 Subject: [PATCH 3/9] src: Some cleanup for the MAX_PATH std::string PR Signed-off-by: Gabriel Ravier --- src/ArmsItem.cpp | 3 +-- src/Backends/Platform/WiiU.cpp | 2 -- src/Draw.cpp | 4 ++-- src/Ending.cpp | 2 +- src/Game.cpp | 4 +--- src/Generic.cpp | 3 +-- src/GenericLoad.cpp | 4 ++-- src/Main.cpp | 16 +++++++++++++++- src/MycParam.cpp | 2 +- src/SelStage.cpp | 3 +-- src/Stage.cpp | 2 +- src/TextScr.cpp | 4 ++-- 12 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/ArmsItem.cpp b/src/ArmsItem.cpp index a1062f00..f6dbf214 100644 --- a/src/ArmsItem.cpp +++ b/src/ArmsItem.cpp @@ -1,6 +1,5 @@ #include "ArmsItem.h" -#include #include #include "WindowsWrapper.h" @@ -418,7 +417,7 @@ int CampLoop(void) RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; // Save the current script path (to restore it when we get out of the inventory) - std::string old_script_path = GetTextScriptPath(); + auto old_script_path = GetTextScriptPath(); // Load the inventory script LoadTextScript2("ArmsItem.tsc"); diff --git a/src/Backends/Platform/WiiU.cpp b/src/Backends/Platform/WiiU.cpp index 0c89684d..af727ad4 100644 --- a/src/Backends/Platform/WiiU.cpp +++ b/src/Backends/Platform/WiiU.cpp @@ -12,8 +12,6 @@ #include #include -#include "../../Helpers/Asprintf.h" - static unsigned long ticks_per_second; bool Backend_Init(void) diff --git a/src/Draw.cpp b/src/Draw.cpp index 212f87c2..91333b9b 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -250,7 +250,7 @@ BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL MakeSurface_File(const char *name, SurfaceID surf_no) { - std::string path = gDataPath + '/' + name + ".pbm"; + auto path = gDataPath + '/' + name + ".pbm"; if (!IsEnableBitmap(path.c_str())) { @@ -340,7 +340,7 @@ BOOL ReloadBitmap_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) { - std::string path = gDataPath + '/' + name + ".pbm"; + auto path = gDataPath + '/' + name + ".pbm"; if (!IsEnableBitmap(path.c_str())) { diff --git a/src/Ending.cpp b/src/Ending.cpp index cc1cfc50..9314664e 100644 --- a/src/Ending.cpp +++ b/src/Ending.cpp @@ -225,7 +225,7 @@ BOOL StartCreditScript(void) } // Open file - std::string path = gDataPath + '/' + credit_script; + auto path = gDataPath + '/' + credit_script; Credit.size = GetFileSizeLong(path.c_str()); if (Credit.size == -1) return FALSE; diff --git a/src/Game.cpp b/src/Game.cpp index 63c9acc5..9914e669 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -701,9 +701,7 @@ BOOL Game(void) PlaySoundObject(7, -1); - std::string path = gDataPath + "/npc.tbl"; - - if (!LoadNpcTable(path.c_str())) + if (!LoadNpcTable((gDataPath + "/npc.tbl").c_str())) { #ifdef JAPANESE Backend_ShowMessageBox("エラー", "NPCテーブルが読めない"); diff --git a/src/Generic.cpp b/src/Generic.cpp index 66223b6e..c62e8c57 100644 --- a/src/Generic.cpp +++ b/src/Generic.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include "WindowsWrapper.h" @@ -94,7 +93,7 @@ long GetFileSizeLong(const char *path) BOOL ErrorLog(const char *string, int value) { - std::string path = gModulePath + "/error.log"; + auto path = gModulePath + "/error.log"; if (GetFileSizeLong(path.c_str()) > 0x19000) // Purge the error log if it gets too big, I guess remove(path.c_str()); diff --git a/src/GenericLoad.cpp b/src/GenericLoad.cpp index 29343894..5eaad31d 100644 --- a/src/GenericLoad.cpp +++ b/src/GenericLoad.cpp @@ -294,8 +294,8 @@ BOOL LoadGenericData(void) pt_size += MakePixToneObject(&gPtpTable[138], 1, 7); /* - * char str[0x40]; - * sprintf(str, "PixTone = %d byte", pt_size); + * char str[0x40]; + * sprintf(str, "PixTone = %d byte", pt_size); * // There must have been some kind of console print function here or something */ return TRUE; diff --git a/src/Main.cpp b/src/Main.cpp index 7f78cf71..8bbd7375 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -222,12 +222,18 @@ int main(int argc, char *argv[]) if (conf.display_mode == 1) { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0)) + { + Backend_Deinit(); return EXIT_FAILURE; + } } else { if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1)) + { + Backend_Deinit(); return EXIT_FAILURE; + } } #else // Doesn't handle StartDirectDraw failing @@ -248,7 +254,10 @@ int main(int argc, char *argv[]) #ifdef FIX_BUGS if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2)) + { + Backend_Deinit(); return EXIT_FAILURE; + } #else // Doesn't handle StartDirectDraw failing StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2); @@ -316,7 +325,10 @@ int main(int argc, char *argv[]) // Draw to screen if (!Flip_SystemTask()) - return EXIT_SUCCESS; + { + Backend_Deinit(); + return EXIT_FAILURE; + } // Initialize sound InitDirectSound(); @@ -340,6 +352,8 @@ int main(int argc, char *argv[]) EndDirectSound(); EndDirectDraw(); + Backend_Deinit(); + return EXIT_SUCCESS; } diff --git a/src/MycParam.cpp b/src/MycParam.cpp index 9764f417..8d3119f2 100644 --- a/src/MycParam.cpp +++ b/src/MycParam.cpp @@ -441,7 +441,7 @@ BOOL SaveTimeCounter(void) return TRUE; // Get last time - std::string path = gModulePath + "/290.rec"; + auto path = gModulePath + "/290.rec"; FILE *fp = fopen(path.c_str(), "rb"); if (fp != NULL) diff --git a/src/SelStage.cpp b/src/SelStage.cpp index 4e229ed7..8686fb8e 100644 --- a/src/SelStage.cpp +++ b/src/SelStage.cpp @@ -1,6 +1,5 @@ #include "SelStage.h" -#include #include #include "WindowsWrapper.h" @@ -161,7 +160,7 @@ int StageSelectLoop(int *p_event) gSelectedStage = 0; BackupSurface(SURFACE_ID_SCREEN_GRAB, &grcFull); - std::string old_script_path = GetTextScriptPath(); + auto old_script_path = GetTextScriptPath(); LoadTextScript2("StageSelect.tsc"); gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66; StartTextScript(gPermitStage[gSelectedStage].index + 1000); diff --git a/src/Stage.cpp b/src/Stage.cpp index 4e3bf9fa..c2caf013 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -139,7 +139,7 @@ BOOL TransferStage(int no, int w, int x, int y) bError = FALSE; // Load tileset - std::string path = std::string{"Stage/Prt"} + gTMT[no].parts; + auto path = std::string{"Stage/Prt"} + gTMT[no].parts; if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_TILESET)) bError = TRUE; diff --git a/src/TextScr.cpp b/src/TextScr.cpp index 2c305411..e0fd8150 100644 --- a/src/TextScr.cpp +++ b/src/TextScr.cpp @@ -126,7 +126,7 @@ BOOL LoadTextScript2(const char *name) FILE *fp; // Get path - std::string path = gDataPath + '/' + name; + auto path = gDataPath + '/' + name; gTS.size = GetFileSizeLong(path.c_str()); if (gTS.size == -1) @@ -159,7 +159,7 @@ BOOL LoadTextScript_Stage(const char *name) long body_size; // Open Head.tsc - std::string path = gDataPath + "/Head.tsc"; + auto path = gDataPath + "/Head.tsc"; head_size = GetFileSizeLong(path.c_str()); if (head_size == -1) return FALSE; From 3d7ee30314569795cc595e642dc4aabf542a2f70 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 12:58:59 +0100 Subject: [PATCH 4/9] Clean-up ArmsItem Undoes edits unrelated to the PR, and adds an include --- src/ArmsItem.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ArmsItem.cpp b/src/ArmsItem.cpp index f6dbf214..5a897345 100644 --- a/src/ArmsItem.cpp +++ b/src/ArmsItem.cpp @@ -1,6 +1,7 @@ #include "ArmsItem.h" #include +#include #include "WindowsWrapper.h" @@ -414,10 +415,12 @@ void PutCampObject(void) int CampLoop(void) { + std::string old_script_path; + RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; // Save the current script path (to restore it when we get out of the inventory) - auto old_script_path = GetTextScriptPath(); + old_script_path = GetTextScriptPath(); // Load the inventory script LoadTextScript2("ArmsItem.tsc"); @@ -448,10 +451,10 @@ int CampLoop(void) switch (Call_Escape()) { case enum_ESCRETURN_exit: - return enum_ESCRETURN_exit; + return enum_ESCRETURN_exit; // Quit game case enum_ESCRETURN_restart: - return enum_ESCRETURN_restart; + return enum_ESCRETURN_restart; // Go to game intro } } @@ -461,10 +464,10 @@ int CampLoop(void) switch (TextScriptProc()) { case enum_ESCRETURN_exit: - return enum_ESCRETURN_exit; + return enum_ESCRETURN_exit; // Quit game case enum_ESCRETURN_restart: - return enum_ESCRETURN_restart; + return enum_ESCRETURN_restart; // Go to game intro } // Get currently displayed image @@ -492,13 +495,13 @@ int CampLoop(void) } if (!Flip_SystemTask()) - return enum_ESCRETURN_exit; + return enum_ESCRETURN_exit; // Quit game } // Resume original script LoadTextScript_Stage(old_script_path.c_str()); gArmsEnergyX = 32; // Displays weapon rotation animation in case the weapon was changed - return enum_ESCRETURN_continue; + return enum_ESCRETURN_continue; // Go to game } BOOL CheckItem(long a) From 2970242ff7126d3ea4c1aa04f66ab4679a041763 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 13:00:34 +0100 Subject: [PATCH 5/9] Remove unused attributes --- src/Attributes.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Attributes.h b/src/Attributes.h index f62ca618..a6c318b6 100644 --- a/src/Attributes.h +++ b/src/Attributes.h @@ -12,10 +12,6 @@ #ifdef __GNUC__ -#define ATTRIBUTE_FORMAT(archetype, formatArgumentIndex, firstToCheck) __attribute__((format(archetype, formatArgumentIndex, firstToCheck))) -#define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) -#define ATTRIBUTE_MALLOC __attribute__((malloc)) -#define ATTRIBUTE_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) #define ATTRIBUTE_HOT __attribute__((hot)) #define LIKELY(condition) __builtin_expect((condition), 1) #define UNLIKELY(condition) __builtin_expect((condition), 0) @@ -23,10 +19,6 @@ #else -#define ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) -#define ATTRIBUTE_WARN_UNUSED_RESULT -#define ATTRIBUTE_MALLOC -#define ATTRIBUTE_NONNULL #define ATTRIBUTE_HOT #define LIKELY(condition) condition #define UNLIKELY(condition) condition From 27a1fd900f813796f20c1c9ca569f17eb2ff958a Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 13:48:46 +0100 Subject: [PATCH 6/9] Revert more unnecessary edits --- src/Back.cpp | 5 ++++- src/Backends/Misc.h | 3 ++- src/Backends/Platform/GLFW3.cpp | 1 + src/Backends/Platform/Null.cpp | 1 + src/Backends/Platform/WiiU.cpp | 6 ++++-- src/Config.cpp | 5 ++++- src/Draw.cpp | 10 +++++++--- src/Ending.cpp | 5 ++++- src/Game.cpp | 5 ++++- src/Generic.cpp | 23 +++++++++++++++++----- src/GenericLoad.cpp | 9 ++++----- src/Main.cpp | 3 ++- src/Main.h | 3 ++- src/Map.cpp | 15 ++++++++++++-- src/MycParam.cpp | 13 +++++++++--- src/NpChar.cpp | 6 +++++- src/Profile.cpp | 35 +++++++++++++++++++++++---------- src/SelStage.cpp | 5 ++++- src/Stage.cpp | 26 ++++++++++++++++-------- src/TextScr.cpp | 15 ++++++++------ src/TextScr.h | 5 +++-- 21 files changed, 144 insertions(+), 55 deletions(-) diff --git a/src/Back.cpp b/src/Back.cpp index 09f9bbaf..e011f111 100644 --- a/src/Back.cpp +++ b/src/Back.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "WindowsWrapper.h" @@ -25,7 +26,9 @@ BOOL InitBack(const char *fName, int type) color_black = GetCortBoxColor(RGB(0, 0, 0x10)); // Get width and height - FILE *fp = fopen((gDataPath + '/' + fName + ".pbm").c_str(), "rb"); + std::string path = gDataPath + '/' + fName + ".pbm"; + + FILE *fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index 9fcddc26..1377d88d 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -1,8 +1,9 @@ #pragma once -#include "../Attributes.h" #include +#include "../Attributes.h" + enum { // Based on US QWERTY diff --git a/src/Backends/Platform/GLFW3.cpp b/src/Backends/Platform/GLFW3.cpp index 83fd83f2..9c84e8e0 100644 --- a/src/Backends/Platform/GLFW3.cpp +++ b/src/Backends/Platform/GLFW3.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include diff --git a/src/Backends/Platform/Null.cpp b/src/Backends/Platform/Null.cpp index a3988631..19f3f2a2 100644 --- a/src/Backends/Platform/Null.cpp +++ b/src/Backends/Platform/Null.cpp @@ -1,4 +1,5 @@ #include "../Misc.h" + #include bool Backend_Init(void) diff --git a/src/Backends/Platform/WiiU.cpp b/src/Backends/Platform/WiiU.cpp index af727ad4..28dc8d8e 100644 --- a/src/Backends/Platform/WiiU.cpp +++ b/src/Backends/Platform/WiiU.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -57,10 +58,11 @@ void Backend_PostWindowCreation(void) bool Backend_GetBasePath(std::string *string_buffer) { #ifdef JAPANESE - *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-jp"; + *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE2-portable-jp"; #else - *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-en"; + *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE2-portable-en"; #endif + return true; } diff --git a/src/Config.cpp b/src/Config.cpp index 3b4f4443..52a26456 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -17,7 +18,9 @@ BOOL LoadConfigData(CONFIG *conf) memset(conf, 0, sizeof(CONFIG)); // Open file - FILE *fp = fopen((gModulePath + '/' + gConfigName).c_str(), "rb"); + std::string path = gModulePath + '/' + gConfigName; + + FILE *fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Draw.cpp b/src/Draw.cpp index 91333b9b..e70364ff 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -250,7 +251,7 @@ BOOL MakeSurface_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL MakeSurface_File(const char *name, SurfaceID surf_no) { - auto path = gDataPath + '/' + name + ".pbm"; + std::string path = gDataPath + '/' + name + ".pbm"; if (!IsEnableBitmap(path.c_str())) { @@ -340,7 +341,7 @@ BOOL ReloadBitmap_Resource(const char *name, SurfaceID surf_no) // TODO - Inaccurate stack frame BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) { - auto path = gDataPath + '/' + name + ".pbm"; + std::string path = gDataPath + '/' + name + ".pbm"; if (!IsEnableBitmap(path.c_str())) { @@ -376,6 +377,7 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) FreeBitmap(image_buffer); surface_metadata[surf_no].type = SURFACE_SOURCE_FILE; strcpy(surface_metadata[surf_no].name, name); + return TRUE; } @@ -644,6 +646,8 @@ void InitTextObject(const char *name) { (void)name; // Unused in this branch + std::string path = gDataPath + "/Font/font"; + // Get font size unsigned int width, height; @@ -660,7 +664,7 @@ void InitTextObject(const char *name) break; } - font = LoadFont((gDataPath + "/Font/font").c_str(), width, height); + font = LoadFont(path.c_str(), width, height); } void PutText(int x, int y, const char *text, unsigned long color) diff --git a/src/Ending.cpp b/src/Ending.cpp index 9314664e..130f1836 100644 --- a/src/Ending.cpp +++ b/src/Ending.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -216,6 +217,7 @@ void ReleaseCreditScript(void) BOOL StartCreditScript(void) { FILE *fp; + std::string path; // Clear previously existing credits data if (Credit.pData != NULL) @@ -225,7 +227,8 @@ BOOL StartCreditScript(void) } // Open file - auto path = gDataPath + '/' + credit_script; + path = gDataPath + '/' + credit_script; + Credit.size = GetFileSizeLong(path.c_str()); if (Credit.size == -1) return FALSE; diff --git a/src/Game.cpp b/src/Game.cpp index 9914e669..7dd2e57c 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -701,7 +702,9 @@ BOOL Game(void) PlaySoundObject(7, -1); - if (!LoadNpcTable((gDataPath + "/npc.tbl").c_str())) + std::string path = gDataPath + "/npc.tbl"; + + if (!LoadNpcTable(path.c_str())) { #ifdef JAPANESE Backend_ShowMessageBox("エラー", "NPCテーブルが読めない"); diff --git a/src/Generic.cpp b/src/Generic.cpp index c62e8c57..64dd00d9 100644 --- a/src/Generic.cpp +++ b/src/Generic.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -49,12 +50,18 @@ BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4) void DeleteLog(void) { - remove((gModulePath + "/debug.txt").c_str()); + std::string path = gModulePath + "/debug.txt"; + remove(path.c_str()); } BOOL WriteLog(const char *string, int value1, int value2, int value3) { - FILE *fp = fopen((gModulePath + "/debug.txt").c_str(), "a+"); + std::string path; + FILE *fp; + + path = gModulePath + "/debug.txt"; + fp = fopen(path.c_str(), "a+"); + if (fp == NULL) return FALSE; @@ -65,7 +72,10 @@ BOOL WriteLog(const char *string, int value1, int value2, int value3) BOOL IsKeyFile(const char *name) { - FILE *file = fopen((gModulePath + '/' + name).c_str(), "rb"); + std::string path = gModulePath + '/' + name; + + FILE *file = fopen(path.c_str(), "rb"); + if (file == NULL) return FALSE; @@ -93,12 +103,15 @@ long GetFileSizeLong(const char *path) BOOL ErrorLog(const char *string, int value) { - auto path = gModulePath + "/error.log"; + std::string path; + FILE *fp; + + path = gModulePath + "/error.log"; if (GetFileSizeLong(path.c_str()) > 0x19000) // Purge the error log if it gets too big, I guess remove(path.c_str()); - FILE *fp = fopen(path.c_str(), "a+"); + fp = fopen(path.c_str(), "a+"); if (fp == NULL) return FALSE; diff --git a/src/GenericLoad.cpp b/src/GenericLoad.cpp index 5eaad31d..850c799a 100644 --- a/src/GenericLoad.cpp +++ b/src/GenericLoad.cpp @@ -293,10 +293,9 @@ BOOL LoadGenericData(void) pt_size += MakePixToneObject(&gPtpTable[137], 1, 6); pt_size += MakePixToneObject(&gPtpTable[138], 1, 7); - /* - * char str[0x40]; - * sprintf(str, "PixTone = %d byte", pt_size); - * // There must have been some kind of console print function here or something - */ + // Commented-out, since ints *technically* have an undefined length + // char str[0x40]; + // sprintf(str, "PixTone = %d byte", pt_size); + // There must have been some kind of console print function here or something return TRUE; } diff --git a/src/Main.cpp b/src/Main.cpp index 8bbd7375..e59db1f3 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -327,7 +328,7 @@ int main(int argc, char *argv[]) if (!Flip_SystemTask()) { Backend_Deinit(); - return EXIT_FAILURE; + return EXIT_SUCCESS; } // Initialize sound diff --git a/src/Main.h b/src/Main.h index ace8799a..95753c27 100644 --- a/src/Main.h +++ b/src/Main.h @@ -1,8 +1,9 @@ #pragma once -#include "WindowsWrapper.h" #include +#include "WindowsWrapper.h" + extern std::string gModulePath; extern std::string gDataPath; diff --git a/src/Map.cpp b/src/Map.cpp index 7ebfa675..e4f33cba 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -27,10 +28,15 @@ BOOL InitMapData2(void) BOOL LoadMapData2(const char *path_map) { + FILE *fp; char check[3]; + std::string path; + + // Get path + path = gDataPath + '/' + path_map; // Open file - FILE *fp = fopen((gDataPath + '/' + path_map).c_str(), "rb"); + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; @@ -63,8 +69,13 @@ BOOL LoadMapData2(const char *path_map) BOOL LoadAttributeData(const char *path_atrb) { + FILE *fp; + std::string path; + // Open file - FILE *fp = fopen((gDataPath + '/' + path_atrb).c_str(), "rb"); + path = gDataPath + '/' + path_atrb; + + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/MycParam.cpp b/src/MycParam.cpp index 8d3119f2..969f4419 100644 --- a/src/MycParam.cpp +++ b/src/MycParam.cpp @@ -1,6 +1,7 @@ #include "MycParam.h" #include +#include #include "WindowsWrapper.h" @@ -435,15 +436,17 @@ BOOL SaveTimeCounter(void) int i; unsigned char p[4]; REC rec; + FILE *fp; + std::string path; // Quit if player doesn't have the Nikumaru Counter if (!(gMC.equip & EQUIP_NIKUMARU_COUNTER)) return TRUE; // Get last time - auto path = gModulePath + "/290.rec"; + path = gModulePath + "/290.rec"; - FILE *fp = fopen(path.c_str(), "rb"); + fp = fopen(path.c_str(), "rb"); if (fp != NULL) { // Read data @@ -505,9 +508,13 @@ int LoadTimeCounter(void) int i; unsigned char p[4]; REC rec; + FILE *fp; + std::string path; // Open file - FILE *fp = fopen((gModulePath + "/290.rec").c_str(), "rb"); + path = gModulePath + "/290.rec"; + + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return 0; diff --git a/src/NpChar.cpp b/src/NpChar.cpp index 55a01d55..98616622 100644 --- a/src/NpChar.cpp +++ b/src/NpChar.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -54,11 +55,14 @@ void InitNpChar(void) BOOL LoadEvent(const char *path_event) { int i, n; + FILE *fp; int count; char code[4]; EVENT eve; - FILE *fp = fopen((gDataPath + '/' + path_event).c_str(), "rb"); + std::string path = gDataPath + '/' + path_event; + + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Profile.cpp b/src/Profile.cpp index 8642df63..dae0d570 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -28,7 +29,9 @@ const char* const gProfileCode = "Do041220"; BOOL IsProfile(void) { - FILE *file = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb"); + std::string path = gModulePath + '/' + gDefaultName; + + FILE *file = fopen(path.c_str(), "rb"); if (file == NULL) return FALSE; @@ -38,15 +41,24 @@ BOOL IsProfile(void) BOOL SaveProfile(const char *name) { + FILE *fp; + PROFILE profile; const char *FLAG = "FLAG"; + std::string path; + + // Get path + if (name != NULL) + path = gModulePath + '/' + name; + else + path = gModulePath + '/' + gDefaultName; + // Open file - FILE *fp = fopen((gModulePath + '/' + ((name != NULL) ? name : gDefaultName)).c_str(), "wb"); + fp = fopen(path.c_str(), "wb"); if (fp == NULL) return FALSE; // Set up profile - PROFILE profile; memset(&profile, 0, sizeof(PROFILE)); memcpy(profile.code, gProfileCode, sizeof(profile.code)); memcpy(profile.FLAG, FLAG, sizeof(profile.FLAG)); @@ -110,15 +122,18 @@ BOOL SaveProfile(const char *name) BOOL LoadProfile(const char *name) { - PROFILE profile; - - // Open file FILE *fp; - if (name != NULL) - fp = fopen(name, "rb"); - else - fp = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb"); + PROFILE profile; + std::string path; + // Get path + if (name != NULL) + path = name; + else + path = gModulePath + '/' + gDefaultName; + + // Open file + fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/SelStage.cpp b/src/SelStage.cpp index 8686fb8e..17f68e1f 100644 --- a/src/SelStage.cpp +++ b/src/SelStage.cpp @@ -1,6 +1,7 @@ #include "SelStage.h" #include +#include #include "WindowsWrapper.h" @@ -156,11 +157,13 @@ void PutStageSelectObject(void) int StageSelectLoop(int *p_event) { + std::string old_script_path; + RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; gSelectedStage = 0; BackupSurface(SURFACE_ID_SCREEN_GRAB, &grcFull); - auto old_script_path = GetTextScriptPath(); + old_script_path = GetTextScriptPath(); LoadTextScript2("StageSelect.tsc"); gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66; StartTextScript(gPermitStage[gSelectedStage].index + 1000); diff --git a/src/Stage.cpp b/src/Stage.cpp index c2caf013..5e9f89ce 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "WindowsWrapper.h" @@ -131,6 +132,8 @@ const STAGE_TABLE gTMT[95] = { BOOL TransferStage(int no, int w, int x, int y) { + std::string path; + std::string path_dir; BOOL bError; // Move character @@ -138,40 +141,47 @@ BOOL TransferStage(int no, int w, int x, int y) bError = FALSE; + // Get path + path_dir = "Stage"; + // Load tileset - auto path = std::string{"Stage/Prt"} + gTMT[no].parts; + path = path_dir + "/Prt" + gTMT[no].parts; if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_TILESET)) bError = TRUE; - path = std::string{"Stage/"} + gTMT[no].parts + ".pxa"; + path = path_dir + "Stage/" + gTMT[no].parts + ".pxa"; if (!LoadAttributeData(path.c_str())) bError = TRUE; // Load tilemap - path = std::string{"Stage/"} + gTMT[no].map + ".pxm"; + path = path_dir + "Stage/" + gTMT[no].map + ".pxm"; if (!LoadMapData2(path.c_str())) bError = TRUE; // Load NPCs - path = std::string{"Stage/"} + gTMT[no].map + ".pxe"; + path = path_dir + "Stage/" + gTMT[no].map + ".pxe"; if (!LoadEvent(path.c_str())) bError = TRUE; // Load script - path = std::string{"Stage/"} + gTMT[no].map + ".tsc"; + path = path_dir + "Stage/" + gTMT[no].map + ".tsc"; if (!LoadTextScript_Stage(path.c_str())) bError = TRUE; // Load background - if (!InitBack(gTMT[no].back, gTMT[no].bkType)) + path = gTMT[no].back; + if (!InitBack(path.c_str(), gTMT[no].bkType)) bError = TRUE; + // Get path + path_dir = "Npc"; + // Load NPC sprite sheets - path = std::string{"Npc/Npc"} + gTMT[no].npc; + path = path_dir + "/Npc" + gTMT[no].npc; if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_1)) bError = TRUE; - path = std::string{"Npc/Npc"} + gTMT[no].boss; + path = path_dir + "/Npc" + gTMT[no].boss; if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_2)) bError = TRUE; diff --git a/src/TextScr.cpp b/src/TextScr.cpp index e0fd8150..e3ba2079 100644 --- a/src/TextScr.cpp +++ b/src/TextScr.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WindowsWrapper.h" @@ -124,9 +125,10 @@ void EncryptionBinaryData2(unsigned char *pData, long size) BOOL LoadTextScript2(const char *name) { FILE *fp; + std::string path; // Get path - auto path = gDataPath + '/' + name; + path = gDataPath + '/' + name; gTS.size = GetFileSizeLong(path.c_str()); if (gTS.size == -1) @@ -155,11 +157,13 @@ BOOL LoadTextScript2(const char *name) BOOL LoadTextScript_Stage(const char *name) { FILE *fp; + std::string path; long head_size; long body_size; // Open Head.tsc - auto path = gDataPath + "/Head.tsc"; + path = gDataPath + "/Head.tsc"; + head_size = GetFileSizeLong(path.c_str()); if (head_size == -1) return FALSE; @@ -176,6 +180,7 @@ BOOL LoadTextScript_Stage(const char *name) // Open stage's .tsc path = gDataPath + '/' + name; + body_size = GetFileSizeLong(path.c_str()); if (body_size == -1) return FALSE; @@ -198,7 +203,7 @@ BOOL LoadTextScript_Stage(const char *name) } // Get current path -std::string GetTextScriptPath() +std::string GetTextScriptPath(void) { return gTS.path; } @@ -1277,13 +1282,11 @@ int TextScriptProc(void) } else { - // The size of str_0 is the size of the format string (includes null terminator so that's the space we'll be using for ours) + the 3 chars from the format string + char str_0[0x40]; #ifdef JAPANESE - char str_0[sizeof("不明のコード:<") + (sizeof(char) * 3)]; sprintf(str_0, "不明のコード:<%c%c%c", gTS.data[gTS.p_read + 1], gTS.data[gTS.p_read + 2], gTS.data[gTS.p_read + 3]); Backend_ShowMessageBox("エラー", str_0); #else - char str_0[sizeof("Unknown code:<%c%c%c") + (sizeof(char) * 3)]; sprintf(str_0, "Unknown code:<%c%c%c", gTS.data[gTS.p_read + 1], gTS.data[gTS.p_read + 2], gTS.data[gTS.p_read + 3]); Backend_ShowMessageBox("Error", str_0); #endif diff --git a/src/TextScr.h b/src/TextScr.h index 0f74c0fa..d73cbdd2 100644 --- a/src/TextScr.h +++ b/src/TextScr.h @@ -1,8 +1,9 @@ #pragma once -#include "WindowsWrapper.h" #include +#include "WindowsWrapper.h" + typedef struct TEXT_SCRIPT { // Path (reload when exit teleporter menu/inventory) @@ -63,7 +64,7 @@ void EndTextScript(void); void EncryptionBinaryData2(unsigned char *pData, long size); BOOL LoadTextScript2(const char *name); BOOL LoadTextScript_Stage(const char *name); -std::string GetTextScriptPath(); +std::string GetTextScriptPath(void); BOOL StartTextScript(int no); void StopTextScript(void); void PutTextScript(void); From ccffba8a80e6b939e81868ad935f7cf770de4b7d Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 13:49:14 +0100 Subject: [PATCH 7/9] Remove user-sabotage They could have their reasons. --- src/WindowsWrapper.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/WindowsWrapper.h b/src/WindowsWrapper.h index 1db7be95..50a61ea8 100644 --- a/src/WindowsWrapper.h +++ b/src/WindowsWrapper.h @@ -4,7 +4,6 @@ #define WIN32_LEAN_AND_MEAN #include #undef FindResource -#undef MAX_PATH // Explicitly undefine MAX_PATH to avoid accidental usage of it #else #define RGB(r,g,b) ((r) | ((g) << 8) | ((b) << 16)) From 56bf0f8c763809bc7521538c2203d8ea3582b3e0 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 13:52:11 +0100 Subject: [PATCH 8/9] Fix some typos --- src/Stage.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Stage.cpp b/src/Stage.cpp index 5e9f89ce..b9370f3b 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -149,22 +149,22 @@ BOOL TransferStage(int no, int w, int x, int y) if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_TILESET)) bError = TRUE; - path = path_dir + "Stage/" + gTMT[no].parts + ".pxa"; + path = path_dir + '/' + gTMT[no].parts + ".pxa"; if (!LoadAttributeData(path.c_str())) bError = TRUE; // Load tilemap - path = path_dir + "Stage/" + gTMT[no].map + ".pxm"; + path = path_dir + '/' + gTMT[no].map + ".pxm"; if (!LoadMapData2(path.c_str())) bError = TRUE; // Load NPCs - path = path_dir + "Stage/" + gTMT[no].map + ".pxe"; + path = path_dir + '/' + gTMT[no].map + ".pxe"; if (!LoadEvent(path.c_str())) bError = TRUE; // Load script - path = path_dir + "Stage/" + gTMT[no].map + ".tsc"; + path = path_dir + '/' + gTMT[no].map + ".tsc"; if (!LoadTextScript_Stage(path.c_str())) bError = TRUE; From 47367614a37f53168eba3bff38b386eb007b4c57 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Tue, 30 Jun 2020 14:04:53 +0100 Subject: [PATCH 9/9] More accuracy improvements That one comment should go in the accurate branch, not here --- src/Backends/Platform/WiiU.cpp | 5 +++-- src/Config.cpp | 3 ++- src/Generic.cpp | 5 +++-- src/Profile.cpp | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Backends/Platform/WiiU.cpp b/src/Backends/Platform/WiiU.cpp index 28dc8d8e..f18be654 100644 --- a/src/Backends/Platform/WiiU.cpp +++ b/src/Backends/Platform/WiiU.cpp @@ -57,10 +57,11 @@ void Backend_PostWindowCreation(void) bool Backend_GetBasePath(std::string *string_buffer) { + *string_buffer = WHBGetSdCardMountPath(); #ifdef JAPANESE - *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE2-portable-jp"; + *string_buffer += "/CSE2-portable-jp"; #else - *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE2-portable-en"; + *string_buffer += "/CSE2-portable-en"; #endif return true; diff --git a/src/Config.cpp b/src/Config.cpp index 52a26456..02bf5da3 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -17,9 +17,10 @@ BOOL LoadConfigData(CONFIG *conf) // Clear old configuration data memset(conf, 0, sizeof(CONFIG)); - // Open file + // Get path std::string path = gModulePath + '/' + gConfigName; + // Open file FILE *fp = fopen(path.c_str(), "rb"); if (fp == NULL) return FALSE; diff --git a/src/Generic.cpp b/src/Generic.cpp index 64dd00d9..6312bfc1 100644 --- a/src/Generic.cpp +++ b/src/Generic.cpp @@ -50,7 +50,9 @@ BOOL GetCompileVersion(int *v1, int *v2, int *v3, int *v4) void DeleteLog(void) { - std::string path = gModulePath + "/debug.txt"; + std::string path; + + path = gModulePath + "/debug.txt"; remove(path.c_str()); } @@ -83,7 +85,6 @@ BOOL IsKeyFile(const char *name) return TRUE; } -// Some code uses this as CheckFileExists, checking if the return value is -1 long GetFileSizeLong(const char *path) { long len; diff --git a/src/Profile.cpp b/src/Profile.cpp index dae0d570..093ca1ec 100644 --- a/src/Profile.cpp +++ b/src/Profile.cpp @@ -131,7 +131,7 @@ BOOL LoadProfile(const char *name) path = name; else path = gModulePath + '/' + gDefaultName; - + // Open file fp = fopen(path.c_str(), "rb"); if (fp == NULL)