mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 23:04:36 +01:00
refactor various platform functions and includes
This commit is contained in:
@@ -20,11 +20,11 @@
|
||||
|
||||
#include <math.h>
|
||||
#include <SDL.h>
|
||||
#include <string.h>
|
||||
|
||||
extern "C" {
|
||||
#include "../config.h"
|
||||
#include "audio.h"
|
||||
#include "../config.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "audio.h"
|
||||
}
|
||||
#include "mixer.h"
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#ifdef _MSC_VER
|
||||
#include <time.h>
|
||||
#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 {
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <SDL_keycode.h>
|
||||
#include <ctype.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
|
||||
#include <windows.h> // for MAX_PATH
|
||||
#include "common.h"
|
||||
#include "localisation/currency.h"
|
||||
#include "platform/platform.h"
|
||||
|
||||
enum {
|
||||
CONFIG_FLAG_ALWAYS_SHOW_GRIDLINES = (1 << 0),
|
||||
|
||||
@@ -19,20 +19,17 @@
|
||||
*****************************************************************************/
|
||||
#pragma pack(1)
|
||||
|
||||
#include <windows.h> // For MAX_PATH
|
||||
#include <lodepng/lodepng.h>
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,14 +18,12 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#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");
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
#ifndef _OBJECT_H_
|
||||
#define _OBJECT_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "rct2.h"
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
* Object entry structure.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 '\\';
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
@@ -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/
|
||||
*/
|
||||
|
||||
29
src/rct2.c
29
src/rct2.c
@@ -20,7 +20,6 @@
|
||||
|
||||
#pragma warning(disable : 4996) // GetVersionExA deprecated
|
||||
|
||||
#include <string.h>
|
||||
#include <setjmp.h>
|
||||
#ifdef _MSC_VER
|
||||
#include <time.h>
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef signed char sint8;
|
||||
typedef signed short sint16;
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include <windows.h>
|
||||
#include <string.h>
|
||||
#include "addresses.h"
|
||||
#include "game.h"
|
||||
#include "interface/viewport.h"
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include <windows.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "addresses.h"
|
||||
#include "audio/audio.h"
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include "addresses.h"
|
||||
#include "localisation/localisation.h"
|
||||
#include "tutorial.h"
|
||||
|
||||
Reference in New Issue
Block a user