diff --git a/projects/openrct2.vcxproj b/projects/openrct2.vcxproj index a9edfa7b23..c53fa7bbb8 100644 --- a/projects/openrct2.vcxproj +++ b/projects/openrct2.vcxproj @@ -36,7 +36,9 @@ - + + TurnOffAllWarnings + TurnOffAllWarnings diff --git a/projects/openrct2.vcxproj.filters b/projects/openrct2.vcxproj.filters index f8cb1164cc..c18e8cbaab 100644 --- a/projects/openrct2.vcxproj.filters +++ b/projects/openrct2.vcxproj.filters @@ -369,7 +369,6 @@ Source\Windows - Source\Windows @@ -382,9 +381,6 @@ - - Libraries\lodepng - @@ -425,6 +421,7 @@ Libraries\argparse + diff --git a/projects/openrct2.vcxproj.user b/projects/openrct2.vcxproj.user index 3695926811..d08f3ad23c 100644 --- a/projects/openrct2.vcxproj.user +++ b/projects/openrct2.vcxproj.user @@ -12,6 +12,7 @@ $(TargetDir)\openrct2.exe $(TargetDir) WindowsLocalDebugger - "C:\Users\Ted\Documents\OpenRCT2\scenarios\RCT Forest Frontiers.SC6" + + \ No newline at end of file diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index f71c7bda8e..8b46d05086 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -20,11 +20,11 @@ #include #include -#include extern "C" { -#include "../config.h" -#include "audio.h" + #include "../config.h" + #include "../platform/platform.h" + #include "audio.h" } #include "mixer.h" diff --git a/src/cmdline.c b/src/cmdline.c index 6c24be9d61..10c08c9eef 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -18,7 +18,6 @@ * along with this program. If not, see . *****************************************************************************/ -#include #ifdef _MSC_VER #include #endif @@ -26,7 +25,7 @@ #include "addresses.h" #include "cmdline.h" #include "openrct2.h" -#include "platform/osinterface.h" +#include "platform/platform.h" typedef struct tm tm_t; typedef struct argparse_option argparse_option_t; @@ -83,7 +82,7 @@ int cmdline_run(char *argv[], int argc) if (argc >= 2) strcpy(gOpenRCT2StartupActionPath, argv[1]); } else { - if (osinterface_file_exists(argv[0])) { + if (platform_file_exists(argv[0])) { gOpenRCT2StartupAction = STARTUP_ACTION_OPEN; strcpy(gOpenRCT2StartupActionPath, argv[0]); } else { diff --git a/src/config.c b/src/config.c index 4200d613c4..e3c9852034 100644 --- a/src/config.c +++ b/src/config.c @@ -18,13 +18,13 @@ * along with this program. If not, see . *****************************************************************************/ -#include #include #include #include "addresses.h" #include "config.h" #include "localisation/localisation.h" #include "platform/osinterface.h" +#include "platform/platform.h" // Current keyboard shortcuts uint16 gShortcutKeys[SHORTCUT_COUNT]; @@ -399,7 +399,7 @@ void config_init() memcpy(&gGeneral_config, &gGeneral_config_default, sizeof(general_configuration_t)); if (strcmp(path, "") != 0){ - if (!osinterface_ensure_directory_exists(path)) { + if (!platform_ensure_directory_exists(path)) { config_error("Could not create config file (do you have write access to your documents folder?)"); return; } @@ -443,7 +443,7 @@ static int config_find_rct2_path(char *resultPath) }; for (i = 0; i < countof(searchLocations); i++) { - if ( osinterface_directory_exists(searchLocations[i]) ) { + if (platform_directory_exists(searchLocations[i]) ) { strcpy(resultPath, searchLocations[i]); return 1; } diff --git a/src/config.h b/src/config.h index 6092481635..cb7c47648d 100644 --- a/src/config.h +++ b/src/config.h @@ -21,9 +21,9 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -#include // for MAX_PATH #include "common.h" #include "localisation/currency.h" +#include "platform/platform.h" enum { CONFIG_FLAG_ALWAYS_SHOW_GRIDLINES = (1 << 0), diff --git a/src/interface/screenshot.c b/src/interface/screenshot.c index 5f871f609b..1a8bb459d8 100644 --- a/src/interface/screenshot.c +++ b/src/interface/screenshot.c @@ -19,20 +19,17 @@ *****************************************************************************/ #pragma pack(1) -#include // For MAX_PATH #include -#include -#include "../platform/osinterface.h" #include "../addresses.h" #include "../config.h" #include "../drawing/drawing.h" #include "../game.h" #include "../localisation/localisation.h" +#include "../platform/osinterface.h" +#include "../platform/platform.h" #include "../windows/error.h" #include "screenshot.h" - - static int screenshot_dump_bmp(); static int screenshot_dump_png(); @@ -63,7 +60,7 @@ void screenshot_check() static int screenshot_get_next_path(char *path, char *extension) { char *screenshotPath = osinterface_get_orct2_homesubfolder("screenshot"); - if (!osinterface_ensure_directory_exists(screenshotPath)) { + if (!platform_ensure_directory_exists(screenshotPath)) { free(screenshotPath); fprintf(stderr, "Unable to save screenshots in OpenRCT2 screenshot directory.\n"); @@ -77,7 +74,7 @@ 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 (!osinterface_file_exists(path)) { + if (!platform_file_exists(path)) { return i; } } diff --git a/src/object.c b/src/object.c index a4644602f7..0882307d35 100644 --- a/src/object.c +++ b/src/object.c @@ -18,14 +18,12 @@ * along with this program. If not, see . *****************************************************************************/ -#include -#include -#include #include #include "addresses.h" #include "localisation/localisation.h" #include "object.h" #include "platform/osinterface.h" +#include "platform/platform.h" #include "util/sawyercoding.h" int object_entry_compare(rct_object_entry *a, rct_object_entry *b); @@ -251,7 +249,7 @@ int object_load_packed(FILE *file) strcat(path, ".DAT"); // - for (; osinterface_file_exists(path);){ + for (; platform_file_exists(path);){ for (char* curr_char = last_char - 1;; --curr_char){ if (*curr_char == '\\'){ subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_OBJECT_DATA_PATH, char), "00000000.DAT"); diff --git a/src/object.h b/src/object.h index b0e125ae8d..a28f47a48f 100644 --- a/src/object.h +++ b/src/object.h @@ -21,8 +21,7 @@ #ifndef _OBJECT_H_ #define _OBJECT_H_ -#include -#include "rct2.h" +#include "common.h" /** * Object entry structure. diff --git a/src/openrct2.c b/src/openrct2.c index 8e19f18543..d895bd826d 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -26,6 +26,7 @@ #include "editor.h" #include "localisation/localisation.h" #include "openrct2.h" +#include "platform/platform.h" #include "platform/osinterface.h" int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE; @@ -36,11 +37,18 @@ char gOpenRCT2StartupActionPath[512] = { 0 }; */ void openrct2_launch() { + config_init(); + + // TODO add configuration option to allow multiple instances + if (!platform_lock_single_instance()) { + fprintf(stderr, "OpenRCT2 is already running.\n"); + return; + } + get_system_info(); audio_init(); audio_get_devices(); get_dsound_devices(); - config_init(); language_open(gGeneral_config.language); rct2_init(); Mixer_Init(NULL); diff --git a/src/platform/osinterface.c b/src/platform/osinterface.c index de2a92a862..4cb7af32aa 100644 --- a/src/platform/osinterface.c +++ b/src/platform/osinterface.c @@ -851,25 +851,6 @@ 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); - 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/platform/osinterface.h b/src/platform/osinterface.h index d88eead8bc..fa02afb3dd 100644 --- a/src/platform/osinterface.h +++ b/src/platform/osinterface.h @@ -107,9 +107,6 @@ 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); char osinterface_get_path_separator(); int osinterface_scancode_to_rct_keycode(int sdl_key); diff --git a/src/platform/platform.h b/src/platform/platform.h index 88678c3ec8..63d1806aa2 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -21,10 +21,15 @@ #ifndef _PLATFORM_H_ #define _PLATFORM_H_ +#ifndef MAX_PATH +#define MAX_PATH 260 +#endif // Platform specific definitions char platform_get_path_separator(); +int platform_file_exists(const char *path); int platform_directory_exists(const char *path); int platform_ensure_directory_exists(const char *path); +int platform_lock_single_instance(); #endif \ No newline at end of file diff --git a/src/platform/windows.c b/src/platform/windows.c index 1de14e0272..30b2b3f5b8 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -25,6 +25,9 @@ #include "../cmdline.h" #include "../openrct2.h" +// The name of the mutex used to prevent multiple instances of the game from running +#define SINGLE_INSTANCE_MUTEX_NAME "RollerCoaster Tycoon 2_GSKMUTEX" + LPSTR *CommandLineToArgvA(LPSTR lpCmdLine, int *argc); /** @@ -79,6 +82,11 @@ char platform_get_path_separator() return '\\'; } +int platform_file_exists(const char *path) +{ + return !(GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND); +} + int platform_directory_exists(const char *path) { DWORD dwAttrib = GetFileAttributes(path); @@ -93,6 +101,26 @@ int platform_ensure_directory_exists(const char *path) return CreateDirectory(path, NULL); } +int platform_lock_single_instance() +{ + HANDLE mutex, status; + + // Check if operating system mutex exists + mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, SINGLE_INSTANCE_MUTEX_NAME); + if (mutex == NULL) { + // Create new mutex + status = CreateMutex(NULL, FALSE, SINGLE_INSTANCE_MUTEX_NAME); + if (status == NULL) + fprintf(stderr, "unable to create mutex\n"); + + return 1; + } else { + // Already running + CloseHandle(mutex); + return 0; + } +} + /** * http://alter.org.ua/en/docs/win/args/ */ diff --git a/src/rct2.c b/src/rct2.c index d6c13feee2..9e2c009178 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -20,7 +20,6 @@ #pragma warning(disable : 4996) // GetVersionExA deprecated -#include #include #ifdef _MSC_VER #include @@ -42,6 +41,7 @@ #include "management/news_item.h" #include "object.h" #include "platform/osinterface.h" +#include "platform/platform.h" #include "ride/ride.h" #include "ride/track.h" #include "scenario.h" @@ -147,7 +147,7 @@ void rct2_init() void rct2_init_directories() { // check install directory - if ( !osinterface_directory_exists(gGeneral_config.game_path) ) { + if (!platform_directory_exists(gGeneral_config.game_path) ) { osinterface_show_messagebox("Invalid RCT2 installation path. Please correct in config.ini."); exit(-1); } @@ -186,13 +186,6 @@ void subsitute_path(char *dest, const char *path, const char *filename) // rct2: 0x00674B42 void rct2_startup_checks() { - // Check if game is already running - if (check_mutex()) - { - RCT2_ERROR("Game is already running"); - RCT2_CALLPROC_X(0x006E3838, 0x343, 0xB2B, 0, 0, 0, 0, 0); // exit_with_error - } - // Check data files check_file_paths(); check_files_integrity(); @@ -241,24 +234,6 @@ int rct2_open_file(const char *path) return 0; } -// rct2: 0x00407DB0 -int check_mutex() -{ - const char * const mutex_name = "RollerCoaster Tycoon 2_GSKMUTEX"; // rct2 @ 0x009AAC3D + 0x009A8B50 - - HANDLE mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutex_name); - - if (mutex != NULL) - { - // Already running - CloseHandle(mutex); - return 1; - } - - HANDLE status = CreateMutex(NULL, FALSE, mutex_name); - return 0; -} - // rct2: 0x00674C95 void check_file_paths() { diff --git a/src/rct2.h b/src/rct2.h index 85d13baf0e..b8e6d10a2e 100644 --- a/src/rct2.h +++ b/src/rct2.h @@ -25,6 +25,7 @@ #include #include #include +#include typedef signed char sint8; typedef signed short sint16; diff --git a/src/scenario.c b/src/scenario.c index f1659be109..04494a2cc4 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -19,7 +19,6 @@ *****************************************************************************/ #include -#include #include "addresses.h" #include "game.h" #include "interface/viewport.h" diff --git a/src/title.c b/src/title.c index de0ccf685d..07ab525ef0 100644 --- a/src/title.c +++ b/src/title.c @@ -19,7 +19,6 @@ *****************************************************************************/ #include -#include #include #include "addresses.h" #include "audio/audio.h" diff --git a/src/tutorial.c b/src/tutorial.c index fe42977286..5fc7972287 100644 --- a/src/tutorial.c +++ b/src/tutorial.c @@ -18,7 +18,6 @@ * along with this program. If not, see . *****************************************************************************/ -#include #include "addresses.h" #include "localisation/localisation.h" #include "tutorial.h"