From 730fe6d46b921e9f49dafa42cd4cc3e60e7c058e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rn=20Lomax?= Date: Sat, 3 May 2014 13:51:45 +0200 Subject: [PATCH] Added setting file. Settings are placed in \OpenRCT2\config.ini. --- src/rct2.c | 11 ++-- src/settings.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 47 ++++++++++++++ 3 files changed, 221 insertions(+), 4 deletions(-) create mode 100644 src/settings.c create mode 100644 src/settings.h diff --git a/src/rct2.c b/src/rct2.c index df687dca0c..a118b12d5f 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "addresses.h" #include "climate.h" @@ -42,13 +43,13 @@ #include "title.h" #include "track.h" #include "viewport.h" +#include "settings.h" -//Path to load data files from. Temporary solution. -#define GAME_PATH "C:\\Program Files (x86)\\Infogrames\\RollerCoaster Tycoon 2" void rct2_init_directories(); void rct2_startup_checks(); + static void rct2_init(); static void rct2_loop(); static void rct2_update(); @@ -68,7 +69,8 @@ __declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInsta RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, LPSTR) = lpCmdLine; get_system_info(); RCT2_CALLPROC(0x0040502E); // get_dsound_devices() - + + settings_init(); rct2_init(); rct2_loop(); osinterface_free(); @@ -147,7 +149,8 @@ void rct2_init() // rct2: 0x00683499 void rct2_init_directories() { - strcpy(RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH, char), GAME_PATH); + + strcpy(RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH, char), settings.GAME_PATH); strcpy(RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH_SLASH, char), RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH, char)); strcat(RCT2_ADDRESS(RCT2_ADDRESS_APP_PATH_SLASH, char), "\\"); diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000000..e18e9bd437 --- /dev/null +++ b/src/settings.c @@ -0,0 +1,167 @@ +/***************************************************************************** +* Copyright (c) 2014 Ted John +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* This file is part of OpenRCT2. +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. + +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*****************************************************************************/ + +#include +#include +#include +#include +#include +#include "settings.h" +#include "screenshot.h" + +settings_t settings; + + +/** + * Initilize the settings. + * It checks if the openRCT2 folder exists and creates it if it does not + * parsing of the config file is done in settings_parse_settings + */ +void settings_init(){ + + TCHAR path[MAX_PATH]; + FILE* fp; + + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, path))){ //find home folder + strcat(path, "\\OpenRCT2"); + DWORD dwAttrib = GetFileAttributes(path); + if (!(dwAttrib != INVALID_FILE_ATTRIBUTES && dwAttrib & FILE_ATTRIBUTE_DIRECTORY)){ //folder does not exist + if (!CreateDirectory(path, NULL)){ + return NULL; //error createing path + } + } + strcat(path, "\\config.ini"); + fp = fopen(path, "r"); + if (!fp){ + settings_create_default(path); + fp = fopen(path, "r"); + if (!fp){ + return NULL; + } + } + + settings_parse_settings(fp); + + } +} + +/** + * Create a new default settings file. + * This should be created from some other resource when openRCT2 grows + * @param path The aboslute path of the config file + */ +void settings_create_default(char *path){ + FILE* fp = fopen(path, "w"); + fprintf(fp, "GAME_PATH = C:\\GOG Games\\RollerCoaster Tycoon 2 Triple Thrill Pack\n"); + fprintf(fp, "SCREENSHOT_AS_PNG = TRUE\n"); + fclose(fp); +} + +/** + * Parse settings and set the game veriables + * @param fp file pointer to the settings file + */ +void settings_parse_settings(FILE *fp){ + int c = NULL, pos = 0; + char *setting; + char *value; + setting = (char *)malloc(128); + value = (char *)malloc(128); + + int size = 256; + + while (settings_get_line(fp, setting, value) > 0){ + + if (strcmp(setting, "GAME_PATH") == 0){ + strcpy(settings.GAME_PATH, value); //TODO: change to copy correct amount of bytes + } + else if(strcmp(setting, "SCREENSHOT_AS_PNG") == 0){ + + if (value == '1'){ + settings.SCREENSHOT_AS_PNG = 1; + } + else{ + settings.SCREENSHOT_AS_PNG = 0; + } + } + } + +} + +/** + * Read one line in the settings file + * @param fp filepointer to the settings file + * @param setting pointer where to to store the setting + * @param value pointer to where to store the value + * @return < 0 on error + */ +int settings_get_line(FILE *fp, char *setting, char *value){ + long start = ftell(fp); + long end; + int c; + int pos = 0; + long size; + c = fgetc(fp); + if(c == EOF) return -1; + while (isalpha(c) || c == '_'){ + c = fgetc(fp); //find size of setting + if (c == EOF) return -1; + } + + end = ftell(fp); + size = end - start; + realloc(setting, size); + fseek(fp, start, SEEK_SET); + c = fgetc(fp); + while (isalpha(c) || c == '_'){ + setting[pos] = (char)c; + pos++; + c = fgetc(fp); + } + setting[pos] = '\0'; + while (c != '='){ + if (c == EOF || c == '\n'){ // this is not a valid setting + return -1; + } + c = fgetc(fp); + } + c = fgetc(fp); + while (isspace(c)){ + c = fgetc(fp); + } + + start = ftell(fp); + while (c != '\n' && c!= EOF){ + c = fgetc(fp); + } + end = ftell(fp); + size = end - start; + realloc(value, size); + fseek(fp, start - 1, SEEK_SET); + pos = 0; + c = fgetc(fp); + while (c != '\n' && c != EOF){ + value[pos] = (char)c; + pos++; + c = fgetc(fp); + } + value[pos] = '\0'; + +} \ No newline at end of file diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000000..a47c943343 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,47 @@ +/***************************************************************************** +* Copyright (c) 2014 Ted John, Peter Hill +* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. +* +* This file is part of OpenRCT2. +* +* OpenRCT2 is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. + +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. + +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*****************************************************************************/ + +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include +#include +#include "rct2.h" + + +void settings_parse_settings(FILE *fp); +int settings_get_line(FILE *fp, char *setting, char *value); + + +void settings_init(); +void settings_create_default(char *path); + + +typedef struct settings{ + uint8 SCREENSHOT_AS_PNG; + char GAME_PATH[MAX_PATH]; + +} settings_t; + +extern settings_t settings; + + + +#endif \ No newline at end of file