Merge branch 'jvlomax-master'

This commit is contained in:
IntelOrca 2014-05-08 15:43:41 +01:00
commit c7b7976743
2 changed files with 193 additions and 56 deletions

View File

@ -78,7 +78,7 @@ void config_reset_shortcut_keys()
} }
/** /**
* * Reads the config file data/config.cfg
* rct2: 0x006752D5 * rct2: 0x006752D5
*/ */
void config_load() void config_load()
@ -86,6 +86,7 @@ void config_load()
HANDLE hFile; HANDLE hFile;
DWORD bytesRead; DWORD bytesRead;
char* path = get_file_path(PATH_ID_GAMECFG);
hFile = CreateFile(get_file_path(PATH_ID_GAMECFG), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, hFile = CreateFile(get_file_path(PATH_ID_GAMECFG), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_RANDOM_ACCESS | FILE_ATTRIBUTE_NORMAL, NULL); FILE_FLAG_RANDOM_ACCESS | FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) { if (hFile != INVALID_HANDLE_VALUE) {
@ -106,6 +107,7 @@ void config_load()
RCT2_GLOBAL(0x009AACBD, sint16) = (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_METRIC, sint8) + 1) * 256; RCT2_GLOBAL(0x009AACBD, sint16) = (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_METRIC, sint8) + 1) * 256;
RCT2_GLOBAL(0x009AA00D, sint8) = 1; RCT2_GLOBAL(0x009AA00D, sint8) = 1;
} }
} }
RCT2_GLOBAL(0x009AAC77, sint8) = 0; RCT2_GLOBAL(0x009AAC77, sint8) = 0;
@ -124,7 +126,7 @@ void config_load()
} }
/** /**
* * Save configuration to the data/config.cfg file
* rct2: 0x00675487 * rct2: 0x00675487
*/ */
void config_save() void config_save()
@ -146,7 +148,11 @@ configuration_t gConfig;
static void config_parse_settings(FILE *fp); static void config_parse_settings(FILE *fp);
static int config_get_line(FILE *fp, char *setting, char *value); static int config_get_line(FILE *fp, char *setting, char *value);
static int config_parse_setting(FILE *fp, char *setting);
static int config_parse_value(FILE *fp, char *value);
static int config_parse_section(FILE *fp, char *setting, char *value);
static void config_create_default(char *path); static void config_create_default(char *path);
static void config_error(char *msg);
/** /**
* Initilise the settings. * Initilise the settings.
@ -163,7 +169,7 @@ void config_init()
DWORD dwAttrib = GetFileAttributes(path); DWORD dwAttrib = GetFileAttributes(path);
if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // folder does not exist if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // folder does not exist
if (!CreateDirectory(path, NULL)) { if (!CreateDirectory(path, NULL)) {
return NULL; // error creating path config_error("Could not create config file (do you have write acces to you documents folder?)");
} }
} }
strcat(path, "\\config.ini"); strcat(path, "\\config.ini");
@ -172,10 +178,12 @@ void config_init()
config_create_default(path); config_create_default(path);
fp = fopen(path, "r"); fp = fopen(path, "r");
if (!fp) if (!fp)
return NULL; config_error("Could not create config file");
} }
config_parse_settings(fp); config_parse_settings(fp);
fclose(fp);
} }
} }
@ -241,18 +249,25 @@ static void config_parse_settings(FILE *fp)
int c = NULL, pos = 0; int c = NULL, pos = 0;
char *setting; char *setting;
char *value; char *value;
setting = (char *)malloc(128); char *section;
value = (char *)malloc(128); setting = (char *)malloc(MAX_CONFIG_LENGTH);
value = (char *)malloc(MAX_CONFIG_LENGTH);
int size = 256; section = (char*)malloc(MAX_CONFIG_LENGTH);
while (config_get_line(fp, setting, value) > 0) { while (config_get_line(fp, setting, value) > 0) {
if (strcmp(setting, "section") == 0){
strcpy(section, value);
continue;
}
if (strcmp(setting, "game_path") == 0){ if (strcmp(setting, "game_path") == 0){
strcpy(gConfig.game_path, value); // TODO: change to copy correct amount of bytes strcpy(gConfig.game_path, value);
} else if(strcmp(setting, "screenshot_format") == 0) { } else if(strcmp(setting, "screenshot_format") == 0) {
if (strcmp(value, "png") == 0 || strcmp(value, "PNG") == 0) { if (strcmp(value, "png") == 0 || strcmp(value, "PNG") == 0) {
gConfig.screenshot_format = SCREENSHOT_FORMAT_PNG; gConfig.screenshot_format = SCREENSHOT_FORMAT_PNG;
} else if (strcmp(value, "1") == 0) { // Maybe remove that? WARNING: Breaks existing config files } else if (strcmp(value, "1") == 0) { //TODO: REMOVE LINE AT LATER DATE WHEN EVERYONE HAS NEW CONFIG FORMAT
gConfig.screenshot_format = SCREENSHOT_FORMAT_PNG; gConfig.screenshot_format = SCREENSHOT_FORMAT_PNG;
} else { } else {
gConfig.screenshot_format = SCREENSHOT_FORMAT_BMP; gConfig.screenshot_format = SCREENSHOT_FORMAT_BMP;
@ -261,78 +276,189 @@ static void config_parse_settings(FILE *fp)
gConfig.play_intro = (strcmp(value, "true") == 0); gConfig.play_intro = (strcmp(value, "true") == 0);
} }
} }
free(setting);
free(value);
free(section);
} }
/** /**
* Read one line in the settings file * Read one line in the settings file
* @param fp filepointer to the settings file * @param fp filepointer to the config file
* @param setting pointer where to to store the setting * @param setting pointer where to to store the setting
* @param value pointer to where to store the value * @param value pointer to where to store the value
* @return < 0 on error * @return < 0 if EOF file is reached or other error
*/ */
static int config_get_line(FILE *fp, char *setting, char *value) static int config_get_line(FILE *fp, char *setting, char *value)
{ {
long start = ftell(fp);
long end;
int c; int c;
int pos = 0;
long size;
c = fgetc(fp); c = fgetc(fp);
if (c == EOF) while (isspace(c)){
c = getc(fp);
}
if (c == '['){
return config_parse_section(fp, setting, value);
}
else if(c == '#'){
while (c != '\n'){
c = fgetc(fp);
}
return 1;
}
if (c == EOF){
return -1; return -1;
}
while (!isalpha(c)){
c = fgetc(fp);
}
//if the first char is not the '[' char, it belongs to the setting name. We want to leave that for the next fn
fseek(fp, -1, SEEK_CUR);
config_parse_setting(fp, setting);
c = fgetc(fp);
while (isspace(c)){
c = getc(fp);
}
if (c != '='){
config_error("There is an error in your configuration file");
return -1;
}
config_parse_value(fp, value);
return 1;
}
/**
* Parse the value of a setting
* @param fp a filepointer to the config file
* @param value a pointer to where to store the setting
* @return < 0 if EOF is reached
*/
static int config_parse_setting(FILE *fp, char *setting){
long start, end;
int size, c, pos = 0;
start = ftell(fp);
c = fgetc(fp);
while (isspace(c)){
start = ftell(fp);
c = fgetc(fp);
}
if (c == EOF){
return -1;
}
while (isalpha(c) || c == '_'){ while (isalpha(c) || c == '_'){
c = fgetc(fp); // find size of setting c = fgetc(fp);
if (c == EOF)
return -1;
} }
end = ftell(fp); end = ftell(fp);
size = end - start; size = end - start;
realloc(setting, size);
fseek(fp, start, SEEK_SET); fseek(fp, start, SEEK_SET);
c = fgetc(fp); c = fgetc(fp);
if (c == '[') { while (isalpha(c) || c == '_'){
// TODO support categories setting[pos] = (char)c;
setting[0] = '\0'; c = fgetc(fp);
value[0] = '\0';
while (c != '\n' && c != EOF) {
pos++; pos++;
}
setting[pos] = '\0';
return 1;
}
/**
* Parse the value of a setting
* @param fp a filepointer to the config file
* @param value a pointer to where to store the value
* @return < 0 if EOF is reached
*/
static int config_parse_value(FILE *fp, char *value){
long start, end;
int size, c, pos = 0;
start = ftell(fp);
c = fgetc(fp);
while (isspace(c)){
start = ftell(fp);
c = fgetc(fp);
}
while (c != EOF && c != '\n'){
c = fgetc(fp); c = fgetc(fp);
} }
end = ftell(fp);
size = end - start;
if (size > MAX_CONFIG_LENGTH){
config_error("One of your settings is too long");
}
fseek(fp, start, SEEK_SET);
c = fgetc(fp);
while (c != EOF && c != '\n'){
value[pos] = (char)c;
c = fgetc(fp);
pos++;
}
value[pos] = '\0';
return;
}
/**
* Parse the current section
* @param fp Filepointer to the config file
* @param setting This is set to contain the string "section"
* @param value Pointer to where the section name should be put
* @return < 0 if EOF is reached
*/
static int config_parse_section(FILE *fp, char *setting, char *value){
int size, c, pos = 0;
long start, end;
strcpy(setting, "section\0");
c = fgetc(fp);
start = ftell(fp);
while (c != ']' && c != EOF){
c = fgetc(fp);
}
end = ftell(fp);
size = end - start;
fseek(fp, start - 1, SEEK_SET);
c = fgetc(fp);
while (c != ']' && c != EOF){
value[pos] = (char)c;
c = fgetc(fp);
pos++;
}
value[pos] = '\0';
if (c != ']'){
config_error("There is an error with the section headers");
}
c = fgetc(fp); //devour ']'
return 1; return 1;
} }
while (isalpha(c) || c == '_'){
setting[pos] = (char)c;
pos++; /**
c = fgetc(fp); * Error with config file. Print error message an quit the game
} * @param msg Message to print in message box
setting[pos] = '\0'; */
while (c != '=') { static void config_error(char *msg){
if (c == EOF || c == '\n') { // this is not a valid setting MessageBox(NULL, msg, "OpenRCT2", MB_OK);
return -1; //TODO:SHUT DOWN EVERYTHING!
}
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';
}

View File

@ -71,6 +71,11 @@ enum {
SCREENSHOT_FORMAT_PNG SCREENSHOT_FORMAT_PNG
}; };
enum {
TEMPERATURE_FORMAT_C,
TEMPERATURE_FORMAT_F
};
extern uint16 gShortcutKeys[SHORTCUT_COUNT]; extern uint16 gShortcutKeys[SHORTCUT_COUNT];
void config_reset_shortcut_keys(); void config_reset_shortcut_keys();
@ -79,6 +84,9 @@ void config_save();
// New config format // New config format
#define MAX_CONFIG_LENGTH 256
typedef struct configuration { typedef struct configuration {
uint8 play_intro; uint8 play_intro;
@ -86,6 +94,9 @@ typedef struct configuration {
char game_path[MAX_PATH]; char game_path[MAX_PATH];
} configuration_t; } configuration_t;
//typedef struct hotkey_configuration{
//};
extern configuration_t gConfig; extern configuration_t gConfig;
void config_init(); void config_init();