Merge branch 'cmdline-scenario'

This commit is contained in:
IntelOrca 2014-10-09 01:40:47 +01:00
commit faaba5addb
22 changed files with 945 additions and 135 deletions

323
lib/argparse/argparse.c Normal file
View File

@ -0,0 +1,323 @@
#include "argparse.h"
#define OPT_UNSET 1
static const char *
prefix_skip(const char *str, const char *prefix)
{
size_t len = strlen(prefix);
return strncmp(str, prefix, len) ? NULL : str + len;
}
int
prefix_cmp(const char *str, const char *prefix)
{
for (;; str++, prefix++)
if (!*prefix)
return 0;
else if (*str != *prefix)
return (unsigned char)*prefix - (unsigned char)*str;
}
static void
argparse_error(struct argparse *this, const struct argparse_option *opt,
const char *reason)
{
if (!strncmp(this->argv[0], "--", 2)) {
fprintf(stderr, "error: option `%s` %s\n", opt->long_name, reason);
exit(1);
} else {
fprintf(stderr, "error: option `%c` %s\n", opt->short_name, reason);
exit(1);
}
}
static int
argparse_getvalue(struct argparse *this, const struct argparse_option *opt,
int flags)
{
const char *s = NULL;
if (!opt->value)
goto skipped;
switch (opt->type) {
case ARGPARSE_OPT_BOOLEAN:
if (flags & OPT_UNSET) {
*(int *)opt->value = *(int *)opt->value - 1;
} else {
*(int *)opt->value = *(int *)opt->value + 1;
}
if (*(int *)opt->value < 0) {
*(int *)opt->value = 0;
}
break;
case ARGPARSE_OPT_BIT:
if (flags & OPT_UNSET) {
*(int *)opt->value &= ~opt->data;
} else {
*(int *)opt->value |= opt->data;
}
break;
case ARGPARSE_OPT_STRING:
if (this->optvalue) {
*(const char **)opt->value = this->optvalue;
this->optvalue = NULL;
} else if (this->argc > 1) {
this->argc--;
*(const char **)opt->value = *++this->argv;
} else {
argparse_error(this, opt, "requires a value");
}
break;
case ARGPARSE_OPT_INTEGER:
if (this->optvalue) {
*(int *)opt->value = strtol(this->optvalue, (char **)&s, 0);
this->optvalue = NULL;
} else if (this->argc > 1) {
this->argc--;
*(int *)opt->value = strtol(*++this->argv, (char **)&s, 0);
} else {
argparse_error(this, opt, "requires a value");
}
if (s[0] != '\0')
argparse_error(this, opt, "expects a numerical value");
break;
default:
assert(0);
}
skipped:
if (opt->callback) {
return opt->callback(this, opt);
}
return 0;
}
static void
argparse_options_check(const struct argparse_option *options)
{
for (; options->type != ARGPARSE_OPT_END; options++) {
switch (options->type) {
case ARGPARSE_OPT_END:
case ARGPARSE_OPT_BOOLEAN:
case ARGPARSE_OPT_BIT:
case ARGPARSE_OPT_INTEGER:
case ARGPARSE_OPT_STRING:
case ARGPARSE_OPT_GROUP:
continue;
default:
fprintf(stderr, "wrong option type: %d", options->type);
break;
}
}
}
static int
argparse_short_opt(struct argparse *this, const struct argparse_option *options)
{
for (; options->type != ARGPARSE_OPT_END; options++) {
if (options->short_name == *this->optvalue) {
this->optvalue = this->optvalue[1] ? this->optvalue + 1 : NULL;
return argparse_getvalue(this, options, 0);
}
}
return -2;
}
static int
argparse_long_opt(struct argparse *this, const struct argparse_option *options)
{
for (; options->type != ARGPARSE_OPT_END; options++) {
const char *rest;
int opt_flags = 0;
if (!options->long_name)
continue;
rest = prefix_skip(this->argv[0] + 2, options->long_name);
if (!rest) {
// Negation allowed?
if (options->flags & OPT_NONEG) {
continue;
}
// Only boolean/bit allow negation.
if (options->type != ARGPARSE_OPT_BOOLEAN && options->type != ARGPARSE_OPT_BIT) {
continue;
}
if (!prefix_cmp(this->argv[0] + 2, "no-")) {
rest = prefix_skip(this->argv[0] + 2 + 3, options->long_name);
if (!rest)
continue;
opt_flags |= OPT_UNSET;
} else {
continue;
}
}
if (*rest) {
if (*rest != '=')
continue;
this->optvalue = rest + 1;
}
return argparse_getvalue(this, options, opt_flags);
}
return -2;
}
int
argparse_init(struct argparse *this, struct argparse_option *options,
const char *const *usage, int flags)
{
memset(this, 0, sizeof(*this));
this->options = options;
this->usage = usage;
this->flags = flags;
return 0;
}
int
argparse_parse(struct argparse *this, int argc, const char **argv)
{
this->argc = argc - 1;
this->argv = argv + 1;
this->out = argv;
argparse_options_check(this->options);
for (; this->argc; this->argc--, this->argv++) {
const char *arg = this->argv[0];
if (arg[0] != '-' || !arg[1]) {
if (this->flags & ARGPARSE_STOP_AT_NON_OPTION) {
goto end;
}
// if it's not option or is a single char '-', copy verbatimly
this->out[this->cpidx++] = this->argv[0];
continue;
}
// short option
if (arg[1] != '-') {
this->optvalue = arg + 1;
switch (argparse_short_opt(this, this->options)) {
case -1:
break;
case -2:
goto unknown;
}
while (this->optvalue) {
switch (argparse_short_opt(this, this->options)) {
case -1:
break;
case -2:
goto unknown;
}
}
continue;
}
// if '--' presents
if (!arg[2]) {
this->argc--;
this->argv++;
break;
}
// long option
switch (argparse_long_opt(this, this->options)) {
case -1:
break;
case -2:
goto unknown;
}
continue;
unknown:
fprintf(stderr, "error: unknown option `%s`\n", this->argv[0]);
argparse_usage(this);
exit(1);
}
end:
memmove(this->out + this->cpidx, this->argv,
this->argc * sizeof(*this->out));
this->out[this->cpidx + this->argc] = NULL;
return this->cpidx + this->argc;
}
void
argparse_usage(struct argparse *this)
{
fprintf(stdout, "Usage: %s\n", *this->usage++);
while (*this->usage && **this->usage)
fprintf(stdout, " or: %s\n", *this->usage++);
fputc('\n', stdout);
const struct argparse_option *options;
// figure out best width
size_t usage_opts_width = 0;
size_t len;
options = this->options;
for (; options->type != ARGPARSE_OPT_END; options++) {
len = 0;
if ((options)->short_name) {
len += 2;
}
if ((options)->short_name && (options)->long_name) {
len += 2; // separator ", "
}
if ((options)->long_name) {
len += strlen((options)->long_name) + 2;
}
if (options->type == ARGPARSE_OPT_INTEGER) {
len += strlen("=<int>");
} else if (options->type == ARGPARSE_OPT_STRING) {
len += strlen("=<str>");
}
len = ceil((float)len / 4) * 4;
if (usage_opts_width < len) {
usage_opts_width = len;
}
}
usage_opts_width += 4; // 4 spaces prefix
options = this->options;
for (; options->type != ARGPARSE_OPT_END; options++) {
size_t pos = 0;
int pad = 0;
if (options->type == ARGPARSE_OPT_GROUP) {
fputc('\n', stdout);
pos += fprintf(stdout, "%s", options->help);
fputc('\n', stdout);
continue;
}
pos = fprintf(stdout, " ");
if (options->short_name) {
pos += fprintf(stdout, "-%c", options->short_name);
}
if (options->long_name && options->short_name) {
pos += fprintf(stdout, ", ");
}
if (options->long_name) {
pos += fprintf(stdout, "--%s", options->long_name);
}
if (options->type == ARGPARSE_OPT_INTEGER) {
pos += fprintf(stdout, "=<int>");
} else if (options->type == ARGPARSE_OPT_STRING) {
pos += fprintf(stdout, "=<str>");
}
if (pos <= usage_opts_width) {
pad = usage_opts_width - pos;
} else {
fputc('\n', stdout);
pad = usage_opts_width;
}
fprintf(stdout, "%*s%s\n", pad + 2, "", options->help);
}
}
int
argparse_help_cb(struct argparse *this, const struct argparse_option *option)
{
(void)option;
argparse_usage(this);
exit(0);
return 0;
}

132
lib/argparse/argparse.h Normal file
View File

@ -0,0 +1,132 @@
#ifndef ARGPARSE_H
#define ARGPARSE_H
/**
* Command-line arguments parsing library.
*
* This module is inspired by parse-options.c (git) and python's argparse
* module.
*
* Arguments parsing is common task in cli program, but traditional `getopt`
* libraries are not easy to use. This library provides high-level arguments
* parsing solutions.
*
* The program defines what arguments it requires, and `argparse` will figure
* out how to parse those out of `argc` and `argv`, it also automatically
* generates help and usage messages and issues errors when users give the
* program invalid arguments.
*
* Reserved namespaces:
* argparse
* OPT
* Author: Yecheng Fu <cofyc.jackson@gmail.com>
*/
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct argparse;
struct argparse_option;
typedef int argparse_callback(struct argparse *this,
const struct argparse_option *option);
enum argparse_flag {
ARGPARSE_STOP_AT_NON_OPTION = 1,
};
enum argparse_option_type {
/* special */
ARGPARSE_OPT_END,
ARGPARSE_OPT_GROUP,
/* options with no arguments */
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_BIT,
/* options with arguments (optional or required) */
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_STRING,
};
enum argparse_option_flags {
OPT_NONEG = 1, /* Negation disabled. */
};
/*
* Argparse option struct.
*
* `type`:
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
* array.
*
* `short_name`:
* the character to use as a short option name, '\0' if none.
*
* `long_name`:
* the long option name, without the leading dash, NULL if none.
*
* `value`:
* stores pointer to the value to be filled.
*
* `help`:
* the short help message associated to what the option does.
* Must never be NULL (except for ARGPARSE_OPT_END).
*
* `callback`:
* function is called when corresponding argument is parsed.
*
* `data`:
* associated data. Callbacks can use it like they want.
*
* `flags`:
* option flags.
*
*/
struct argparse_option {
enum argparse_option_type type;
const char short_name;
const char *long_name;
void *value;
const char *help;
argparse_callback *callback;
intptr_t data;
int flags;
};
/*
* argpparse
*/
struct argparse {
// user supplied
const struct argparse_option *options;
const char *const *usage;
int flags;
// internal context
int argc;
const char **argv;
const char **out;
int cpidx;
const char *optvalue; // current option value
};
// builtin callbacks
int argparse_help_cb(struct argparse *this,
const struct argparse_option *option);
// builtin option macros
#define OPT_END() { ARGPARSE_OPT_END }
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL }
#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, "show this help message and exit", argparse_help_cb)
int argparse_init(struct argparse *this, struct argparse_option *options,
const char *const *usage, int flags);
int argparse_parse(struct argparse *this, int argc, const char **argv);
void argparse_usage(struct argparse *this);
#endif

View File

@ -28,6 +28,7 @@
<Text Include="..\data\language\swedish.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\argparse\argparse.h" />
<ClInclude Include="..\lib\libspeex\arch.h" />
<ClInclude Include="..\lib\libspeex\config.h" />
<ClInclude Include="..\lib\libspeex\os_support.h" />
@ -35,6 +36,7 @@
<ClInclude Include="..\lib\libspeex\speex\speex_types.h" />
<ClInclude Include="..\lib\libspeex\stack_alloc.h" />
<ClInclude Include="..\lib\lodepng\lodepng.h" />
<ClCompile Include="..\lib\argparse\argparse.c" />
<ClCompile Include="..\lib\libspeex\resample.c;..\lib\lodepng\lodepng.c">
<WarningLevel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">TurnOffAllWarnings</WarningLevel>
</ClCompile>
@ -42,6 +44,7 @@
<ItemGroup>
<ClCompile Include="..\src\audio\audio.c" />
<ClCompile Include="..\src\audio\mixer.cpp" />
<ClCompile Include="..\src\cmdline.c" />
<ClCompile Include="..\src\config.c" />
<ClCompile Include="..\src\drawing\drawing.c" />
<ClCompile Include="..\src\drawing\line.c" />
@ -71,6 +74,7 @@
<ClCompile Include="..\src\management\research.c" />
<ClCompile Include="..\src\object.c" />
<ClCompile Include="..\src\object_list.c" />
<ClCompile Include="..\src\openrct2.c" />
<ClCompile Include="..\src\peep\peep.c" />
<ClCompile Include="..\src\peep\staff.c" />
<ClCompile Include="..\src\platform\shared.c" />
@ -139,6 +143,7 @@
<ClInclude Include="..\src\addresses.h" />
<ClInclude Include="..\src\audio\audio.h" />
<ClInclude Include="..\src\audio\mixer.h" />
<ClInclude Include="..\src\cmdline.h" />
<ClInclude Include="..\src\common.h" />
<ClInclude Include="..\src\config.h" />
<ClInclude Include="..\src\cursors.h" />
@ -165,6 +170,7 @@
<ClInclude Include="..\src\management\news_item.h" />
<ClInclude Include="..\src\management\research.h" />
<ClInclude Include="..\src\object.h" />
<ClInclude Include="..\src\openrct2.h" />
<ClInclude Include="..\src\peep\peep.h" />
<ClInclude Include="..\src\peep\staff.h" />
<ClInclude Include="..\src\platform\osinterface.h" />

View File

@ -59,6 +59,9 @@
<Filter Include="Source\Peep">
<UniqueIdentifier>{51e38783-5334-464c-8f90-61d725dc8013}</UniqueIdentifier>
</Filter>
<Filter Include="Libraries\argparse">
<UniqueIdentifier>{7e9587b2-333f-42ca-8a56-b77070828b17}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\openrct2.exe">
@ -367,7 +370,6 @@
</ClCompile>
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\src\windows\shortcut_key_change.c">
<Filter>Source\Windows</Filter>
</ClCompile>
@ -377,9 +379,6 @@
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\libspeex\resample.c">
<Filter>Libraries\libspeex</Filter>
</ClCompile>
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\lodepng\lodepng.c" />
@ -415,6 +414,17 @@
<Filter>Libraries\lodepng</Filter>
</ClCompile>
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\libspeex\resample.c;..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\lib\libspeex\resample.c;..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\src\cmdline.c">
<Filter>Source</Filter>
</ClCompile>
<ClCompile Include="..\src\openrct2.c">
<Filter>Source</Filter>
</ClCompile>
<ClCompile Include="..\lib\argparse\argparse.c">
<Filter>Libraries\argparse</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\management\award.h">
@ -600,5 +610,14 @@
<ClInclude Include="..\src\management\research.h">
<Filter>Source\Management</Filter>
</ClInclude>
<ClInclude Include="..\src\cmdline.h">
<Filter>Source</Filter>
</ClInclude>
<ClInclude Include="..\src\openrct2.h">
<Filter>Source</Filter>
</ClInclude>
<ClInclude Include="..\lib\argparse\argparse.h">
<Filter>Libraries\argparse</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -12,5 +12,6 @@
<LocalDebuggerCommand>$(TargetDir)\openrct2.exe</LocalDebuggerCommand>
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>"C:\Users\Ted\Documents\OpenRCT2\scenarios\RCT Forest Frontiers.SC6"</LocalDebuggerCommandArguments>
</PropertyGroup>
</Project>

118
src/cmdline.c Normal file
View File

@ -0,0 +1,118 @@
/*****************************************************************************
* 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 <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <string.h>
#ifdef _MSC_VER
#include <time.h>
#endif
#include <argparse/argparse.h>
#include "addresses.h"
#include "cmdline.h"
#include "openrct2.h"
#include "platform/osinterface.h"
typedef struct tm tm_t;
typedef struct argparse_option argparse_option_t;
typedef struct argparse argparse_t;
int gExitCode = 0;
static void print_launch_information();
static const char *const usage[] = {
"openrct2 <command> [options] [<args>]",
"openrct2 <path> [options]",
"openrct2 intro [options]",
"openrct2 edit [path] [options]",
NULL
};
/**
* A shared entry point to OpenRCT2. The command lines must be parsed before any further action is done. Invalid command lines
* will then terminate before any initialisation has even been done.
* @returns 1 if the game should run, otherwise 0.
*/
int cmdline_run(char *argv[], int argc)
{
// For argparse's sake, add virtual first argument process path
argc++;
argv--;
//
int version = 0, width = 0, height = 0;
argparse_option_t options[] = {
OPT_HELP(),
OPT_BOOLEAN('v', "version", &version, "show version information and exit"),
OPT_END()
};
argparse_t argparse;
argparse_init(&argparse, options, usage, 0);
argc = argparse_parse(&argparse, argc, argv);
if (version) {
printf("%s v%s\n", OPENRCT2_NAME, OPENRCT2_VERSION);
printf("%s (%s)\n", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
printf("%s\n", OPENRCT2_TIMESTAMP);
return 0;
}
if (argc != 0) {
if (_stricmp(argv[0], "intro") == 0) {
gOpenRCT2StartupAction = STARTUP_ACTION_INTRO;
} else if (_stricmp(argv[0], "edit") == 0) {
gOpenRCT2StartupAction = STARTUP_ACTION_EDIT;
if (argc >= 2)
strcpy(gOpenRCT2StartupActionPath, argv[1]);
} else {
if (osinterface_file_exists(argv[0])) {
gOpenRCT2StartupAction = STARTUP_ACTION_OPEN;
strcpy(gOpenRCT2StartupActionPath, argv[0]);
} else {
fprintf(stderr, "error: %s does not exist\n", argv[0]);
return 0;
}
}
}
print_launch_information();
return 1;
}
static void print_launch_information()
{
char buffer[32];
time_t timer;
tm_t* tmInfo;
// Print version information
printf("Starting %s v%s\n", OPENRCT2_NAME, OPENRCT2_VERSION);
printf(" %s (%s)\n", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
printf(" %s\n\n", OPENRCT2_TIMESTAMP);
// Print current time
time(&timer);
tmInfo = localtime(&timer);
strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", tmInfo);
printf("Time: %s\n", buffer);
// TODO Print other potential information (e.g. user, hardware)
}

31
src/cmdline.h Normal file
View File

@ -0,0 +1,31 @@
/*****************************************************************************
* 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 <http://www.gnu.org/licenses/>.
*****************************************************************************/
#ifndef _CMDLINE_H_
#define _CMDLINE_H_
#include "common.h"
/** The exit code for OpenRCT2 when it exits. */
extern int gExitCode;
int cmdline_run(char *argv[], int argc);
#endif

View File

@ -74,21 +74,23 @@ static const uint16 _defaultShortcutKeys[SHORTCUT_COUNT] = {
general_configuration_t gGeneral_config;
general_configuration_t gGeneral_config_default = {
0, // play_intro
1, // confirmation_prompt
SCREENSHOT_FORMAT_PNG, // screenshot_format
"", // game_path
MEASUREMENT_FORMAT_IMPERIAL, // measurement_format
TEMPERATURE_FORMAT_F, // temperature_format
CURRENCY_POUNDS, // currency_format
0, // construction_marker_colour
1, // edge_scrolling
0, // always_show_gridlines
1, // landscape_smoothing
0, // show_height_as_units
1, // save_plugin_data
0, // fullscreen mode (default: windowed)
LANGUAGE_ENGLISH_UK
0, // play_intro
1, // confirmation_prompt
SCREENSHOT_FORMAT_PNG, // screenshot_format
"", // game_path
MEASUREMENT_FORMAT_IMPERIAL, // measurement_format
TEMPERATURE_FORMAT_F, // temperature_format
CURRENCY_POUNDS, // currency_format
0, // construction_marker_colour
1, // edge_scrolling
0, // always_show_gridlines
1, // landscape_smoothing
0, // show_height_as_units
1, // save_plugin_data
0, // fullscreen mode (default: windowed)
-1, // window_width
-1, // window_height
LANGUAGE_ENGLISH_UK // language
};
sound_configuration_t gSound_config;

View File

@ -130,6 +130,8 @@ typedef struct general_configuration {
//new
uint8 fullscreen_mode;
sint16 window_width;
sint16 window_height;
uint16 language;
} general_configuration_t;

View File

@ -78,7 +78,7 @@ void editor_load()
RCT2_CALLPROC_EBPSAFE(0x006837E3);
gfx_invalidate_screen();
RCT2_GLOBAL(0x009DEA66, sint16) = 0;
rct2_endupdate();
// rct2_endupdate();
}
/**
@ -166,6 +166,16 @@ void trackmanager_load()
rct2_endupdate();
}
/**
*
* rct2: 0x006758C0
*/
void editor_load_landscape(const char *path)
{
strcpy((char *)0x0141EF68, path);
RCT2_CALLPROC_EBPSAFE(0x006758C0);
}
/**
*
* rct2: 0x0068ABEC

View File

@ -25,6 +25,7 @@ void editor_load();
void editor_convert_save_to_scenario();
void trackdesigner_load();
void trackmanager_load();
void editor_load_landscape(const char *path);
void sub_6BD3A4();

View File

@ -598,14 +598,13 @@ static void load_landscape()
*
* rct2: 0x00675E1B
*/
int game_load_save()
int game_load_save(const char *path)
{
rct_window *mainWindow;
FILE *file;
char *path;
int i, j;
path = (char*)0x0141EF68;
strcpy((char*)0x0141EF68, path);
file = fopen(path, "rb");
if (file == NULL) {
RCT2_GLOBAL(0x009AC31B, uint8) = 255;
@ -735,7 +734,7 @@ static void load_game()
}
strcpy((char*)RCT2_ADDRESS_SAVED_GAMES_PATH_2, (char*)0x0141EF68);
if (game_load_save()) {
if (game_load_save((char *)0x0141EF68)) {
gfx_invalidate_screen();
rct2_endupdate();
} else {

View File

@ -99,7 +99,7 @@ void game_increase_game_speed();
void game_reduce_game_speed();
void game_load_or_quit_no_save_prompt();
int game_load_save();
int game_load_save(const char *path);
void game_pause_toggle();
char save_game();
void rct2_exit();

76
src/openrct2.c Normal file
View File

@ -0,0 +1,76 @@
/*****************************************************************************
* 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 <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "addresses.h"
#include "audio/audio.h"
#include "audio/mixer.h"
#include "cmdline.h"
#include "config.h"
#include "editor.h"
#include "localisation/localisation.h"
#include "openrct2.h"
#include "platform/osinterface.h"
int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE;
char gOpenRCT2StartupActionPath[512] = { 0 };
/**
* Launches the game, after command line arguments have been parsed and processed.
*/
void openrct2_launch()
{
get_system_info();
audio_init();
audio_get_devices();
get_dsound_devices();
config_init();
language_open(gGeneral_config.language);
rct2_init();
Mixer_Init(NULL);
switch (gOpenRCT2StartupAction) {
case STARTUP_ACTION_INTRO:
RCT2_GLOBAL(RCT2_ADDRESS_RUN_INTRO_TICK_PART, uint8) = 8;
break;
case STARTUP_ACTION_TITLE:
RCT2_GLOBAL(RCT2_ADDRESS_RUN_INTRO_TICK_PART, uint8) = 0;
RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) = SCREEN_FLAGS_TITLE_DEMO;
break;
case STARTUP_ACTION_OPEN:
assert(gOpenRCT2StartupActionPath != NULL);
rct2_open_file(gOpenRCT2StartupActionPath);
RCT2_GLOBAL(RCT2_ADDRESS_RUN_INTRO_TICK_PART, uint8) = 0;
RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) = SCREEN_FLAGS_PLAYING;
break;
case STARTUP_ACTION_EDIT:
if (strlen(gOpenRCT2StartupActionPath) == 0)
editor_load();
else
editor_load_landscape(gOpenRCT2StartupActionPath);
break;
}
rct2_loop();
osinterface_free();
// HACK Some threads are still running which causes the game to not terminate. Investigation required!
exit(gExitCode);
}

38
src/openrct2.h Normal file
View File

@ -0,0 +1,38 @@
/*****************************************************************************
* 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 <http://www.gnu.org/licenses/>.
*****************************************************************************/
#ifndef _OPENRCT2_H_
#define _OPENRCT2_H_
#include "common.h"
enum {
STARTUP_ACTION_INTRO,
STARTUP_ACTION_TITLE,
STARTUP_ACTION_OPEN,
STARTUP_ACTION_EDIT
};
extern int gOpenRCT2StartupAction;
extern char gOpenRCT2StartupActionPath[512];
void openrct2_launch();
#endif

View File

@ -178,11 +178,11 @@ static void osinterface_create_window()
osinterface_load_cursors();
RCT2_CALLPROC_EBPSAFE(0x0068371D);
width = RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_RESOLUTION_WIDTH, sint16);
height = RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_RESOLUTION_HEIGHT, sint16);
width = gGeneral_config.window_width;
height = gGeneral_config.window_height;
width = 640;
height = 480;
if (width == -1) width = 640;
if (height == -1) height = 480;
}
RCT2_GLOBAL(0x009E2D8C, sint32) = 0;

View File

@ -21,12 +21,18 @@
#ifndef _WIN32
#ifndef __APPLE__
#include "../cmdline.h"
#include "../openrct2.h"
/**
* Unix, linux and fallback entry point to OpenRCT2.
*/
// int main(char *argv[], int argc)
// {
// return 0;
// if (cmdline_run(argv, argc))
// openrct2_launch();
//
// return gExitCode;
// }
char platform_get_path_separator()

View File

@ -21,6 +21,11 @@
#ifdef _WIN32
#include <windows.h>
#include "../addresses.h"
#include "../cmdline.h"
#include "../openrct2.h"
LPSTR *CommandLineToArgvA(LPSTR lpCmdLine, int *argc);
/**
* Windows entry point to OpenRCT2 without a console window.
@ -50,9 +55,24 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
* The function that is called directly from the host application (rct2.exe)'s WinMain. This will be removed when OpenRCT2 can
* be built as a stand alone application.
*/
// __declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
// {
// }
__declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int argc, runGame;
char **argv;
RCT2_GLOBAL(RCT2_ADDRESS_HINSTANCE, HINSTANCE) = hInstance;
RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, LPSTR) = lpCmdLine;
// Get command line arguments in standard form
argv = CommandLineToArgvA(lpCmdLine, &argc);
runGame = cmdline_run(argv, argc);
LocalFree(argv);
if (runGame)
openrct2_launch();
return gExitCode;
}
char platform_get_path_separator()
{
@ -73,4 +93,87 @@ int platform_ensure_directory_exists(const char *path)
return CreateDirectory(path, NULL);
}
/**
* http://alter.org.ua/en/docs/win/args/
*/
PCHAR *CommandLineToArgvA(PCHAR CmdLine, int *_argc)
{
PCHAR* argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len + 2) / 2)*sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR*)GlobalAlloc(GMEM_FIXED,
i + (len + 2)*sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv) + i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while (a = CmdLine[i]) {
if (in_QM) {
if (a == '\"') {
in_QM = FALSE;
} else {
_argv[j] = a;
j++;
}
} else {
switch (a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if (in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if (in_SPACE) {
argv[argc] = _argv + j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
#endif

View File

@ -58,58 +58,12 @@ void print_launch_information();
void rct2_init_directories();
void rct2_startup_checks();
static void rct2_init();
static void rct2_loop();
static void rct2_update();
static void rct2_update_2();
static int _finished;
static jmp_buf _end_update_jump;
__declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
print_launch_information();
// Begin RCT2
RCT2_GLOBAL(RCT2_ADDRESS_HINSTANCE, HINSTANCE) = hInstance;
RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, LPSTR) = lpCmdLine;
get_system_info();
audio_init();
audio_get_devices();
get_dsound_devices();
config_init();
language_open(gGeneral_config.language);
rct2_init();
Mixer_Init(NULL);
rct2_loop();
osinterface_free();
exit(0);
return 0;
}
void print_launch_information()
{
char buffer[32];
time_t timer;
tm_t* tmInfo;
// Print version information
printf("Starting %s v%s\n", OPENRCT2_NAME, OPENRCT2_VERSION);
printf(" %s (%s)\n", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
printf(" %s\n\n", OPENRCT2_TIMESTAMP);
// Print current time
time(&timer);
tmInfo = localtime(&timer);
strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", tmInfo);
printf("Time: %s\n", buffer);
// TODO Print other potential information (e.g. user, hardware)
}
void rct2_loop()
{
int last_tick = 0;
@ -265,51 +219,26 @@ void rct2_update()
rct2_update_2();
}
void check_cmdline_arg()
int rct2_open_file(const char *path)
{
if(RCT2_GLOBAL(0x009AC310, uint32) == 0xFFFFFFFF)
return;
char *extension = strrchr(path, '.');
if (extension == NULL)
return 0;
extension++;
char *arg = RCT2_GLOBAL(0x009AC310, char *);
char processed_arg[255];
int len, i, j;
int quote = 0;
int last_period = 0;
if (_stricmp(extension, "sv6") == 0) {
game_load_save(path);
return 1;
} else if (_stricmp(extension, "sc6") == 0) {
// TODO scenario install
rct_scenario_basic scenarioBasic;
strcpy(scenarioBasic.path, path);
scenario_load_and_play_from_path(scenarioBasic.path);
} else if (_stricmp(extension, "td6") == 0 || _stricmp(extension, "td4") == 0) {
// TODO track design install
}
RCT2_GLOBAL(0x009AC310, uint32) = 0xFFFFFFFF;
len = strlen(arg);
for(i = 0, j = 0; i < len; i ++)
{
if(arg[i] == '\"')
{
if(quote)
quote = 0;
else
quote = 1;
continue;
}
if(arg[i] == ' ' && !quote)
break;
if(arg[i] == '.')
last_period = i;
processed_arg[j ++] = arg[i];
}
processed_arg[j ++] = 0;
if (!_stricmp(processed_arg + last_period, "sv6"))
{
strcpy((char*)0x00141EF68, processed_arg);
game_load_save();
}
else if (!_stricmp(processed_arg + last_period, "sc6"))
{
//TODO: scenario install
}
else if (!_stricmp(processed_arg + last_period, "td6") || !_stricmp(processed_arg + last_period, "td4"))
{
//TODO: track design install
}
return 0;
}
// rct2: 0x00407DB0
@ -427,7 +356,7 @@ void rct2_update_2()
// TODO: screenshot countdown process
check_cmdline_arg();
// check_cmdline_arg();
// Screens
if (RCT2_GLOBAL(RCT2_ADDRESS_RUN_INTRO_TICK_PART, uint8) != 0)
intro_update();
@ -600,4 +529,4 @@ void *rct2_realloc(void *block, size_t numBytes)
void rct2_free(void *block)
{
RCT2_CALLPROC_1(0x004068DE, void*, block);
}
}

View File

@ -21,6 +21,7 @@
#ifndef _RCT2_H_
#define _RCT2_H_
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
@ -268,6 +269,8 @@ static const struct file_to_check
{ PATH_ID_END, 0 }
};
void rct2_init();
void rct2_loop();
void rct2_endupdate();
void subsitute_path(char *dest, const char *path, const char *filename);
int check_mutex();
@ -283,4 +286,6 @@ void *rct2_realloc(void *block, size_t numBytes);
void rct2_free(void *block);
void rct2_quit();
int rct2_open_file(const char *path);
#endif

View File

@ -82,7 +82,7 @@ int scenario_load_basic(const char *path)
* rct2: 0x00676053
* scenario (ebx)
*/
void scenario_load(const char *path)
int scenario_load(const char *path)
{
FILE *file;
int i, j;
@ -95,7 +95,7 @@ void scenario_load(const char *path)
fclose(file);
RCT2_GLOBAL(0x009AC31B, uint8) = 255;
RCT2_GLOBAL(0x009AC31C, uint16) = STR_FILE_CONTAINS_INVALID_DATA;
return;
return 0;
}
// Read first chunk
@ -157,7 +157,7 @@ void scenario_load(const char *path)
RCT2_CALLPROC_EBPSAFE(0x006A9FC0);
map_update_tile_pointers();
reset_0x69EBE4();// RCT2_CALLPROC_EBPSAFE(0x0069EBE4);
return;
return 1;
}
fclose(file);
@ -165,6 +165,7 @@ void scenario_load(const char *path)
RCT2_GLOBAL(0x009AC31B, uint8) = 255;
RCT2_GLOBAL(0x009AC31C, uint16) = STR_FILE_CONTAINS_INVALID_DATA;
return 0;
}
/**
@ -172,7 +173,15 @@ void scenario_load(const char *path)
* rct2: 0x00678282
* scenario (ebx)
*/
void scenario_load_and_play(const rct_scenario_basic *scenario)
int scenario_load_and_play(const rct_scenario_basic *scenario)
{
char path[MAX_PATH];
subsitute_path(path, RCT2_ADDRESS(RCT2_ADDRESS_SCENARIOS_PATH, char), scenario->path);
return scenario_load_and_play_from_path(path);
}
int scenario_load_and_play_from_path(const char *path)
{
rct_window *mainWindow;
rct_s6_info *s6Info = (rct_s6_info*)0x0141F570;
@ -184,12 +193,9 @@ void scenario_load_and_play(const rct_scenario_basic *scenario)
window_close_construction_windows();
subsitute_path(
RCT2_ADDRESS(0x0141EF68, char),
RCT2_ADDRESS(RCT2_ADDRESS_SCENARIOS_PATH, char),
scenario->path
);
scenario_load((char*)0x0141EF68);
if (!scenario_load(path))
return 0;
RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) = SCREEN_FLAGS_PLAYING;
viewport_init_all();
game_create_windows();
@ -312,6 +318,8 @@ void scenario_load_and_play(const rct_scenario_basic *scenario)
gfx_invalidate_screen();
RCT2_GLOBAL(0x009DEA66, uint16) = 0;
RCT2_GLOBAL(0x009DEA5C, uint16) = 62000; // (doesn't appear to ever be read)
return 1;
}

View File

@ -403,8 +403,9 @@ extern rct_scenario_basic *gScenarioList;
int scenario_scores_save();
void scenario_load_list();
int scenario_load_basic(const char *path);
void scenario_load(const char *path);
void scenario_load_and_play(const rct_scenario_basic *scenario);
int scenario_load(const char *path);
int scenario_load_and_play(const rct_scenario_basic *scenario);
int scenario_load_and_play_from_path(const char *path);
void scenario_update();
int scenario_rand();