From a70021d53ac6f9ae7ce040bb0950fb27b81fc18f Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 16:48:15 +0200 Subject: [PATCH 1/5] Add function for checking wether file exists to osinterface, BMP screenshots can now be saved without WIN32 specific methods. --- src/config.c | 5 ++--- src/osinterface.c | 5 +++++ src/osinterface.h | 1 + src/rct2.c | 3 +-- src/screenshot.c | 44 +++++++++++++++++++++++--------------------- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/config.c b/src/config.c index 11fdf54b97..8d99760217 100644 --- a/src/config.c +++ b/src/config.c @@ -254,7 +254,7 @@ void config_init() static int config_find_rct2_path(char *resultPath) { int i; - DWORD dwAttrib; + const char *searchLocations[] = { "C:\\Program Files\\Infogrames\\RollerCoaster Tycoon 2", "C:\\Program Files (x86)\\Infogrames\\RollerCoaster Tycoon 2", @@ -266,8 +266,7 @@ static int config_find_rct2_path(char *resultPath) }; for (i = 0; i < countof(searchLocations); i++) { - dwAttrib = GetFileAttributes(searchLocations[i]); - if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + if ( osinterface_directory_exists(searchLocations[i]) ) { strcpy(resultPath, searchLocations[i]); return 1; } diff --git a/src/osinterface.c b/src/osinterface.c index 843b37f712..274c2a5f3a 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -454,6 +454,11 @@ char *osinterface_get_orct2_homesubfolder(const char *subFolder) return path; } +int osinterface_file_exists(const char *path) +{ + return !(GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND); +} + int osinterface_directory_exists(const char *path) { DWORD dwAttrib = GetFileAttributes(path); diff --git a/src/osinterface.h b/src/osinterface.h index 88576a980a..59691835c6 100644 --- a/src/osinterface.h +++ b/src/osinterface.h @@ -52,6 +52,7 @@ char* osinterface_open_directory_browser(char *title); char* osinterface_get_orct2_homefolder(); char *osinterface_get_orct2_homesubfolder(const char *subFolder); +int osinterface_file_exists(const char *path); int osinterface_directory_exists(const char *path); int osinterface_ensure_directory_exists(const char *path); diff --git a/src/rct2.c b/src/rct2.c index 76b81e5e6e..7ab6254c34 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -189,8 +189,7 @@ void rct2_init() void rct2_init_directories() { // check install directory - DWORD dwAttrib = GetFileAttributes(gGeneral_config.game_path); - if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { + if ( !osinterface_directory_exists(gGeneral_config.game_path) ) { osinterface_show_messagebox("Invalid RCT2 installation path. Please correct in config.ini."); exit(-1); } diff --git a/src/screenshot.c b/src/screenshot.c index 7e74ed32f1..10350f39da 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -17,10 +17,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . *****************************************************************************/ +#pragma pack(1) #include #include -#include #include "osinterface.h" #include "addresses.h" #include "config.h" @@ -75,8 +75,9 @@ static int screenshot_get_next_path(char *path, char *extension) // Glue together path and filename sprintf(path, "%s%cSCR%d%s", screenshotPath, osinterface_get_path_separator(), i, extension); - if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) + if (!osinterface_file_exists(path)) { return i; + } } free(screenshotPath); @@ -129,22 +130,23 @@ int screenshot_dump_bmp() int i, y, index, width, height, stride; char *buffer, path[MAX_PATH], *row; - HANDLE hFile; - DWORD bytesWritten; + FILE *fp; + unsigned int bytesWritten; // Get a free screenshot path if ((index = screenshot_get_next_path(path, ".bmp")) == -1) return -1; - // Open file for writing - hFile = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile == INVALID_HANDLE_VALUE) + // Open binary file for writing + if ((fp = fopen(path, "wb")) == NULL){ return -1; + } // Allocate buffer buffer = malloc(0xFFFF); if (buffer == NULL) { - CloseHandle(hFile); + //CloseHandle(hFile); + fclose(fp); return -1; } @@ -159,9 +161,9 @@ int screenshot_dump_bmp() header.bfSize = height * stride + 1038; header.bfOffBits = 1038; - WriteFile(hFile, &header, sizeof(header), &bytesWritten, NULL); - if (bytesWritten != sizeof(header)) { - CloseHandle(hFile); + bytesWritten = fwrite(&header, sizeof(BitmapFileHeader), 1, fp); + if (bytesWritten != 1) { + fclose(fp); free(buffer); } @@ -176,9 +178,9 @@ int screenshot_dump_bmp() info.biYPelsPerMeter = 2520; info.biClrUsed = 246; - WriteFile(hFile, &info, sizeof(info), &bytesWritten, NULL); - if (bytesWritten != sizeof(info)) { - CloseHandle(hFile); + bytesWritten=fwrite(&info, sizeof(BitmapInfoHeader), 1, fp); + if (bytesWritten != 1) { + fclose(fp); free(buffer); } @@ -190,9 +192,9 @@ int screenshot_dump_bmp() buffer[i * 4 + 2] = RCT2_ADDRESS(0x01424680, uint8)[i * 4 + 2]; } - WriteFile(hFile, buffer, 246 * 4, &bytesWritten, NULL); - if (bytesWritten != 246 * 4) { - CloseHandle(hFile); + bytesWritten = fwrite(buffer, sizeof(char), 246*4, fp); + if (bytesWritten != 246*4){ + fclose(fp); free(buffer); } @@ -204,14 +206,14 @@ int screenshot_dump_bmp() memset(buffer, 0, stride); memcpy(buffer, row, dpi->width); - WriteFile(hFile, buffer, stride, &bytesWritten, NULL); - if (bytesWritten != stride) { - CloseHandle(hFile); + bytesWritten=fwrite(buffer, sizeof(char), stride, fp); + if (bytesWritten != stride){ + fclose(fp); free(buffer); } } - CloseHandle(hFile); + fclose(fp); free(buffer); return index; From 76a7cc98b8754f90c9181c7b24f48651247cc8b8 Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 17:14:27 +0200 Subject: [PATCH 2/5] replaced WIN32 file i/o in config_load() --- src/config.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/config.c b/src/config.c index 8d99760217..0883849bd2 100644 --- a/src/config.c +++ b/src/config.c @@ -117,20 +117,21 @@ void config_reset_shortcut_keys() */ void config_load() { - HANDLE hFile; - DWORD bytesRead; + unsigned int bytesRead; + FILE *fp=NULL; char* path = get_file_path(PATH_ID_GAMECFG); - hFile = CreateFile(get_file_path(PATH_ID_GAMECFG), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, - FILE_FLAG_RANDOM_ACCESS | FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile != INVALID_HANDLE_VALUE) { + + fp = fopen(path, "rb"); + + if (fp != NULL) { // Read and check magic number - ReadFile(hFile, RCT2_ADDRESS(0x013CE928, void), 4, &bytesRead, NULL); + fread(RCT2_ADDRESS(0x013CE928, void), 1, 4, fp); + if (RCT2_GLOBAL(0x013CE928, int) == MagicNumber) { // Read options - ReadFile(hFile, (void*)0x009AAC5C, 2155, &bytesRead, NULL); - CloseHandle(hFile); - + fread((void*)0x009AAC5C, 1, 2155, fp); + fclose(fp); //general configuration RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_EDGE_SCROLLING, sint8) = gGeneral_config.edge_scrolling; From cdee1dddeac9fce2c6a271ec65a5c90f7c692b6e Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 19:00:21 +0200 Subject: [PATCH 3/5] replaced WIN32 file i/o in config_save() --- src/config.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/config.c b/src/config.c index 0883849bd2..c17625f328 100644 --- a/src/config.c +++ b/src/config.c @@ -117,7 +117,6 @@ void config_reset_shortcut_keys() */ void config_load() { - unsigned int bytesRead; FILE *fp=NULL; char* path = get_file_path(PATH_ID_GAMECFG); @@ -198,14 +197,12 @@ void config_load() */ void config_save() { - HANDLE hFile; - DWORD bytesWritten; - - hFile = CreateFile(get_file_path(PATH_ID_GAMECFG), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile != INVALID_HANDLE_VALUE) { - WriteFile(hFile, &MagicNumber, 4, &bytesWritten, NULL); - WriteFile(hFile, (LPCVOID)0x009AAC5C, 2155, &bytesWritten, NULL); - CloseHandle(hFile); + FILE *fp=NULL; + fp = fopen(get_file_path(PATH_ID_GAMECFG), "wb"); + if (fp != NULL){ + fwrite(&MagicNumber, 4, 1, fp); + fwrite((LPCVOID)0x009AAC5C, 2155, 1, fp); + fclose(fp); } } From 66e6dfc3f9699cc0c6ef8d68b1df4605e0de619a Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 19:22:06 +0200 Subject: [PATCH 4/5] clean up header files, keep windows.h for MAX_PATH --- src/config.c | 8 +++----- src/config.h | 2 +- src/screenshot.c | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/config.c b/src/config.c index c17625f328..ad58991810 100644 --- a/src/config.c +++ b/src/config.c @@ -19,14 +19,12 @@ *****************************************************************************/ #include -#include -#include -#include #include +#include #include "addresses.h" #include "config.h" #include "rct2.h" -#include + #include "osinterface.h" @@ -201,7 +199,7 @@ void config_save() fp = fopen(get_file_path(PATH_ID_GAMECFG), "wb"); if (fp != NULL){ fwrite(&MagicNumber, 4, 1, fp); - fwrite((LPCVOID)0x009AAC5C, 2155, 1, fp); + fwrite((void*)0x009AAC5C, 2155, 1, fp); fclose(fp); } } diff --git a/src/config.h b/src/config.h index ef04a12386..c1139c0a96 100644 --- a/src/config.h +++ b/src/config.h @@ -21,8 +21,8 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -#include #include "rct2.h" +#include // for MAX_PATH enum { CONFIG_FLAG_ALWAYS_SHOW_GRIDLINES = (1 << 0), diff --git a/src/screenshot.c b/src/screenshot.c index 10350f39da..cc8d3d84bc 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -30,6 +30,8 @@ #include "string_ids.h" #include "window_error.h" +#include // For MAX_PATH + static int screenshot_dump_bmp(); static int screenshot_dump_png(); From a0380fc1f83baef96d02cf185ad75401c1d5c3a0 Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Tue, 27 May 2014 19:28:22 +0200 Subject: [PATCH 5/5] travis doesn't like capitalized ... --- src/config.h | 2 +- src/screenshot.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.h b/src/config.h index c1139c0a96..eb86ab5da2 100644 --- a/src/config.h +++ b/src/config.h @@ -22,7 +22,7 @@ #define _CONFIG_H_ #include "rct2.h" -#include // for MAX_PATH +#include // for MAX_PATH enum { CONFIG_FLAG_ALWAYS_SHOW_GRIDLINES = (1 << 0), diff --git a/src/screenshot.c b/src/screenshot.c index cc8d3d84bc..6db7d4f2bb 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -30,7 +30,7 @@ #include "string_ids.h" #include "window_error.h" -#include // For MAX_PATH +#include // For MAX_PATH static int screenshot_dump_bmp();