From bbae0af0ef66ceb3cb316f01fe5b9315daa959e9 Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Sun, 25 May 2014 22:27:28 +0200 Subject: [PATCH 1/5] Save screenshots in home folder, Add functions for path separator and detection of homefolder to osinterface --- src/config.c | 11 +++++++---- src/osinterface.c | 16 ++++++++++++++++ src/osinterface.h | 3 +++ src/screenshot.c | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/config.c b/src/config.c index 4415705b28..1c428787a2 100644 --- a/src/config.c +++ b/src/config.c @@ -216,20 +216,23 @@ void config_save() */ void config_init() { - TCHAR path[MAX_PATH]; + char path[MAX_PATH]; FILE* fp; memcpy(&gGeneral_config, &gGeneral_config_default, sizeof(general_configuration_t)); - if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { // find home folder - strcat(path, "\\OpenRCT2"); + strncpy(path, osinterface_get_orct2_homefolder(), MAX_PATH); + + if (strcmp(path, "") != 0){ DWORD dwAttrib = GetFileAttributes(path); if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // folder does not exist if (!CreateDirectory(path, NULL)) { config_error("Could not create config file (do you have write access to your documents folder?)"); } } - strcat(path, "\\config.ini"); + + sprintf(path, "%s%c%s", path, osinterface_get_path_separator(), "config.ini"); + fp = fopen(path, "r"); if (!fp) { config_create_default(path); diff --git a/src/osinterface.c b/src/osinterface.c index 8117a31198..974c9da05d 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -428,3 +428,19 @@ char* osinterface_open_directory_browser(char *title) { CoUninitialize(); return outPath; } + +char* osinterface_get_orct2_homefolder() +{ + char path[260]=""; + + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { // find home folder + strcat(path, "\\OpenRCT2"); + } + + return path; +} + +char osinterface_get_path_separator() +{ + return '\\'; +} \ No newline at end of file diff --git a/src/osinterface.h b/src/osinterface.h index 8360d3f1c4..50f78904a0 100644 --- a/src/osinterface.h +++ b/src/osinterface.h @@ -50,4 +50,7 @@ int osinterface_open_common_file_dialog(int type, char *title, char *filename, c void osinterface_show_messagebox(char* message); char* osinterface_open_directory_browser(char *title); +char* osinterface_get_orct2_homefolder(); +char osinterface_get_path_separator(); + #endif diff --git a/src/screenshot.c b/src/screenshot.c index d557297a9e..6e040686b6 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -21,6 +21,7 @@ #include #include #include +#include "osinterface.h" #include "addresses.h" #include "config.h" #include "gfx.h" @@ -64,7 +65,7 @@ static int screenshot_get_next_path(char *path, char *extension) RCT2_GLOBAL(0x013CE952, uint16) = i; // Glue together path and filename - sprintf(path, "%sSCR%d%s", RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH_SLASH, char), i, extension); + sprintf(path, "%s%cSCR%d%s", osinterface_get_orct2_homefolder(), osinterface_get_path_separator(), i, extension); if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) return i; From 07128831fbdd659b1644609b51fd972671aa235c Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Sun, 25 May 2014 22:35:54 +0200 Subject: [PATCH 2/5] (test commit for travis please ignore) --- licence.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/licence.txt b/licence.txt index 20d40b6bce..94a9ed024d 100644 --- a/licence.txt +++ b/licence.txt @@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read -. \ No newline at end of file +. From 35ff9a510d41899087a8927b92eaacebcd3ebbdd Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Mon, 26 May 2014 09:27:54 +0200 Subject: [PATCH 3/5] Fixed returning pointer from a local buffer --- src/config.c | 6 +++--- src/osinterface.c | 3 ++- src/screenshot.c | 6 +++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/config.c b/src/config.c index 1c428787a2..d2ec11087f 100644 --- a/src/config.c +++ b/src/config.c @@ -216,13 +216,11 @@ void config_save() */ void config_init() { - char path[MAX_PATH]; + char *path = osinterface_get_orct2_homefolder(); FILE* fp; memcpy(&gGeneral_config, &gGeneral_config_default, sizeof(general_configuration_t)); - strncpy(path, osinterface_get_orct2_homefolder(), MAX_PATH); - if (strcmp(path, "") != 0){ DWORD dwAttrib = GetFileAttributes(path); if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // folder does not exist @@ -245,6 +243,8 @@ void config_init() fclose(fp); } + + free(path); } /** diff --git a/src/osinterface.c b/src/osinterface.c index 974c9da05d..5a17fae968 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -431,7 +431,8 @@ char* osinterface_open_directory_browser(char *title) { char* osinterface_get_orct2_homefolder() { - char path[260]=""; + char *path; + path = malloc(sizeof(char) * MAX_PATH); if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { // find home folder strcat(path, "\\OpenRCT2"); diff --git a/src/screenshot.c b/src/screenshot.c index 6e040686b6..475b09452c 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -60,17 +60,21 @@ void screenshot_check() static int screenshot_get_next_path(char *path, char *extension) { + char *homePath = osinterface_get_orct2_homefolder(); + int i; for (i = 1; i < 1000; i++) { RCT2_GLOBAL(0x013CE952, uint16) = i; // Glue together path and filename - sprintf(path, "%s%cSCR%d%s", osinterface_get_orct2_homefolder(), osinterface_get_path_separator(), i, extension); + sprintf(path, "%s%cSCR%d%s", homePath, osinterface_get_path_separator(), i, extension); if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) return i; } + free(homePath); + return -1; } From 16c0bde2f43811bde4aa33ed392433e66fa48b9c Mon Sep 17 00:00:00 2001 From: atmaxinger Date: Mon, 26 May 2014 09:34:14 +0200 Subject: [PATCH 4/5] Check if path could be allocated, initialize it with an empty string. --- src/osinterface.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/osinterface.c b/src/osinterface.c index 5a17fae968..fdc237d4ad 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -431,8 +431,14 @@ char* osinterface_open_directory_browser(char *title) { char* osinterface_get_orct2_homefolder() { - char *path; + char *path=NULL; path = malloc(sizeof(char) * MAX_PATH); + if (path == NULL){ + osinterface_show_messagebox("Error allocating memory!"); + exit(EXIT_FAILURE); + } + + path[0] = '\0'; if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { // find home folder strcat(path, "\\OpenRCT2"); From 70d3d652c5fa134b962f779edd140a7f92ae7cde Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Mon, 26 May 2014 14:12:43 +0100 Subject: [PATCH 5/5] add saving of screenshots in subfolder --- src/config.c | 8 +++----- src/osinterface.c | 25 +++++++++++++++++++++++-- src/osinterface.h | 4 ++++ src/screenshot.c | 10 +++++++--- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/config.c b/src/config.c index d2ec11087f..11fdf54b97 100644 --- a/src/config.c +++ b/src/config.c @@ -222,11 +222,9 @@ void config_init() memcpy(&gGeneral_config, &gGeneral_config_default, sizeof(general_configuration_t)); if (strcmp(path, "") != 0){ - DWORD dwAttrib = GetFileAttributes(path); - if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // folder does not exist - if (!CreateDirectory(path, NULL)) { - config_error("Could not create config file (do you have write access to your documents folder?)"); - } + if (!osinterface_ensure_directory_exists(path)) { + config_error("Could not create config file (do you have write access to your documents folder?)"); + return; } sprintf(path, "%s%c%s", path, osinterface_get_path_separator(), "config.ini"); diff --git a/src/osinterface.c b/src/osinterface.c index fdc237d4ad..843b37f712 100644 --- a/src/osinterface.c +++ b/src/osinterface.c @@ -440,13 +440,34 @@ char* osinterface_get_orct2_homefolder() path[0] = '\0'; - if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) { // find home folder + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))) strcat(path, "\\OpenRCT2"); - } return path; } +char *osinterface_get_orct2_homesubfolder(const char *subFolder) +{ + char *path = osinterface_get_orct2_homefolder(); + strcat(path, "\\"); + strcat(path, subFolder); + return path; +} + +int osinterface_directory_exists(const char *path) +{ + DWORD dwAttrib = GetFileAttributes(path); + return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); +} + +int osinterface_ensure_directory_exists(const char *path) +{ + if (osinterface_directory_exists(path)) + return 1; + + return CreateDirectory(path, NULL); +} + char osinterface_get_path_separator() { return '\\'; diff --git a/src/osinterface.h b/src/osinterface.h index 50f78904a0..88576a980a 100644 --- a/src/osinterface.h +++ b/src/osinterface.h @@ -51,6 +51,10 @@ void osinterface_show_messagebox(char* message); char* osinterface_open_directory_browser(char *title); char* osinterface_get_orct2_homefolder(); +char *osinterface_get_orct2_homesubfolder(const char *subFolder); +int osinterface_directory_exists(const char *path); +int osinterface_ensure_directory_exists(const char *path); + char osinterface_get_path_separator(); #endif diff --git a/src/screenshot.c b/src/screenshot.c index 475b09452c..42a0cd21c7 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -60,20 +60,24 @@ void screenshot_check() static int screenshot_get_next_path(char *path, char *extension) { - char *homePath = osinterface_get_orct2_homefolder(); + char *screenshotPath = osinterface_get_orct2_homesubfolder("screenshot"); + if (!osinterface_ensure_directory_exists(screenshotPath)) { + fprintf(stderr, "Unable to save screenshots in OpenRCT2 screenshot directory.\n"); + return -1; + } int i; for (i = 1; i < 1000; i++) { RCT2_GLOBAL(0x013CE952, uint16) = i; // Glue together path and filename - sprintf(path, "%s%cSCR%d%s", homePath, osinterface_get_path_separator(), i, extension); + sprintf(path, "%s%cSCR%d%s", screenshotPath, osinterface_get_path_separator(), i, extension); if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) return i; } - free(homePath); + free(screenshotPath); return -1; }