Skip to content

Commit 2b37810

Browse files
authored
Add the ability to override the Unity's boot.config file path (#59)
1 parent b9f9d8f commit 2b37810

File tree

11 files changed

+170
-6
lines changed

11 files changed

+170
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ All Doorstop arguments start with `--doorstop-` and always contain an argument.
133133
* `string` = any sequence of characters and numbers. Wrap into `"`s if the string contains spaces
134134

135135
| Argument | Description |
136-
| ------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
136+
| ------------------------------------------------- |------------------------------------------------------------------------------------------------------|
137137
| `--doorstop-enabled bool` | Enable or disable Doorstop. |
138138
| `--doorstop-redirect-output-log bool` | *Only on Windows*: If `true` Unity's output log is redirected to `<current folder>\output_log.txt` |
139139
| `--doorstop-target-assembly string` | Path to the assembly to load and execute. |
140+
| `--doorstop-boot-config-override string` | Overrides the boot.config file path. |
140141
| `--doorstop-mono-dll-search-path-override string` | Overrides default Mono DLL search path |
141142
| `--doorstop-mono-debug-enabled bool` | If true, Mono debugger server will be enabled |
142143
| `--doorstop-mono-debug-suspend bool` | Whether to suspend the game execution until the debugger is attached. |

assets/nix/run.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ enabled="1"
2525
# NOTE: The entrypoint must be of format `static void Doorstop.Entrypoint.Start()`
2626
target_assembly="Doorstop.dll"
2727

28+
# Overrides the default boot.config file path
29+
boot_config_override=
30+
2831
# If enabled, DOORSTOP_DISABLE env var value is ignored
2932
# USE THIS ONLY WHEN ASKED TO OR YOU KNOW WHAT THIS MEANS
3033
ignore_disable_switch="0"
@@ -197,6 +200,10 @@ while :; do
197200
target_assembly="$2"
198201
shift
199202
;;
203+
--doorstop-boot-config-override)
204+
boot_config_override="$2"
205+
shift
206+
;;
200207
--doorstop-mono-dll-search-path-override)
201208
dll_search_path_override="$2"
202209
shift
@@ -234,6 +241,7 @@ done
234241
# Move variables to environment
235242
export DOORSTOP_ENABLED="$enabled"
236243
export DOORSTOP_TARGET_ASSEMBLY="$target_assembly"
244+
export DOORSTOP_BOOT_CONFIG_OVERRIDE="$boot_config_override"
237245
export DOORSTOP_IGNORE_DISABLED_ENV="$ignore_disable_switch"
238246
export DOORSTOP_MONO_DLL_SEARCH_PATH_OVERRIDE="$dll_search_path_override"
239247
export DOORSTOP_MONO_DEBUG_ENABLED="$debug_enable"

assets/windows/doorstop_config.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ target_assembly=Doorstop.dll
1111
# If true, Unity's output log is redirected to <current folder>\output_log.txt
1212
redirect_output_log=false
1313

14+
# Overrides the default boot.config file path
15+
boot_config_override=
16+
1417
# If enabled, DOORSTOP_DISABLE env var value is ignored
1518
# USE THIS ONLY WHEN ASKED TO OR YOU KNOW WHAT THIS MEANS
1619
ignore_disable_switch=false

src/bootstrap.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ void il2cpp_doorstop_bootstrap() {
267267
strcat(app_paths_env, config.clr_corlib_dir);
268268
strcat(app_paths_env, PATH_SEP);
269269
strcat(app_paths_env, target_dir);
270-
char *app_paths_env_n = narrow(app_paths_env);
270+
const char *app_paths_env_n = narrow(app_paths_env);
271271

272272
LOG("App path: %s", app_path);
273273
LOG("Target dir: %s", target_dir);
274274
LOG("Target name: %s", target_name);
275275
LOG("APP_PATHS: %s", app_paths_env);
276276

277-
char *props = "APP_PATHS";
277+
const char *props = "APP_PATHS";
278278

279279
setenv(TEXT("DOORSTOP_INITIALIZED"), TEXT("TRUE"), TRUE);
280280
setenv(TEXT("DOORSTOP_INVOKE_DLL_PATH"), config.target_assembly, TRUE);
@@ -293,7 +293,8 @@ void il2cpp_doorstop_bootstrap() {
293293

294294
void (*startup)() = NULL;
295295
result = coreclr.create_delegate(host, domain_id, target_name_n,
296-
"Doorstop.Entrypoint", "Start", &startup);
296+
"Doorstop.Entrypoint", "Start",
297+
(void **)&startup);
297298
if (result != 0) {
298299
LOG("Failed to get entrypoint delegate: 0x%08x", result);
299300
return;

src/config/common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void cleanup_config() {
1111
}
1212

1313
FREE_NON_NULL(config.target_assembly);
14+
FREE_NON_NULL(config.boot_config_override);
1415
FREE_NON_NULL(config.mono_dll_search_path_override);
1516
FREE_NON_NULL(config.clr_corlib_dir);
1617
FREE_NON_NULL(config.clr_runtime_coreclr_path);
@@ -27,6 +28,7 @@ void init_config_defaults() {
2728
config.mono_debug_suspend = FALSE;
2829
config.mono_debug_address = NULL;
2930
config.target_assembly = NULL;
31+
config.boot_config_override = NULL;
3032
config.mono_dll_search_path_override = NULL;
3133
config.clr_corlib_dir = NULL;
3234
config.clr_runtime_coreclr_path = NULL;

src/config/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ typedef struct {
3535
*/
3636
char_t *target_assembly;
3737

38+
/**
39+
* @brief Path to a custom boot.config file to use. If enabled, this file
40+
* takes precedence over the default one in the Data folder.
41+
*/
42+
char_t *boot_config_override;
43+
3844
/**
3945
* @brief Path to use as the main DLL search path. If enabled, this folder
4046
* takes precedence over the default Managed folder.

src/nix/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void get_env_bool(const char_t *name, bool_t *target) {
1010
}
1111
}
1212

13-
void try_get_env(const char_t *name, const char_t *def, char_t **target) {
13+
void try_get_env(const char_t *name, char_t *def, char_t **target) {
1414
char_t *value = getenv(name);
1515
if (value != NULL && strlen(value) > 0) {
1616
*target = strdup(value);
@@ -37,6 +37,7 @@ void load_config() {
3737
try_get_env("DOORSTOP_MONO_DEBUG_ADDRESS", TEXT("127.0.0.1:10000"),
3838
&config.mono_debug_address);
3939
get_env_path("DOORSTOP_TARGET_ASSEMBLY", &config.target_assembly);
40+
get_env_path("DOORSTOP_BOOT_CONFIG_OVERRIDE", &config.boot_config_override);
4041
try_get_env("DOORSTOP_MONO_DLL_SEARCH_PATH_OVERRIDE", TEXT(""),
4142
&config.mono_dll_search_path_override);
4243
get_env_path("DOORSTOP_CLR_RUNTIME_CORECLR_PATH",

src/nix/entrypoint.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ int fclose_hook(FILE *stream) {
5959
return fclose(stream);
6060
}
6161

62+
char_t *default_boot_config_path = NULL;
63+
#if !defined(__APPLE__)
64+
FILE *fopen64_hook(char *filename, char *mode) {
65+
char *actual_file_name = filename;
66+
67+
if (strcmp(filename, default_boot_config_path) == 0) {
68+
actual_file_name = config.boot_config_override;
69+
LOG("Overriding boot.config to %s", actual_file_name);
70+
}
71+
72+
return fopen64(actual_file_name, mode);
73+
}
74+
#endif
75+
76+
FILE *fopen_hook(char *filename, char *mode) {
77+
char *actual_file_name = filename;
78+
79+
if (strcmp(filename, default_boot_config_path) == 0) {
80+
actual_file_name = config.boot_config_override;
81+
LOG("Overriding boot.config to %s", actual_file_name);
82+
}
83+
84+
return fopen(actual_file_name, mode);
85+
}
86+
6287
int dup2_hook(int od, int nd) {
6388
// Newer versions of Unity redirect stdout to player.log, we don't want
6489
// that
@@ -80,7 +105,8 @@ __attribute__((constructor)) void doorstop_ctor() {
80105

81106
void *unity_player = plthook_handle_by_name("UnityPlayer");
82107

83-
if (unity_player && PLTHOOK_OPEN_BY_HANDLE_OR_ADDRESS(&hook, unity_player) == 0) {
108+
if (unity_player &&
109+
PLTHOOK_OPEN_BY_HANDLE_OR_ADDRESS(&hook, unity_player) == 0) {
84110
LOG("Found UnityPlayer, hooking into it instead");
85111
} else if (plthook_open(&hook, NULL) != 0) {
86112
LOG("Failed to open current process PLT! Cannot run Doorstop! "
@@ -94,6 +120,31 @@ __attribute__((constructor)) void doorstop_ctor() {
94120
printf("Failed to hook dlsym, ignoring it. Error: %s\n",
95121
plthook_error());
96122

123+
if (config.boot_config_override) {
124+
if (file_exists(config.boot_config_override)) {
125+
default_boot_config_path = calloc(MAX_PATH, sizeof(char_t));
126+
memset(default_boot_config_path, 0, MAX_PATH * sizeof(char_t));
127+
strcat(default_boot_config_path, get_working_dir());
128+
strcat(default_boot_config_path, TEXT("/"));
129+
strcat(default_boot_config_path,
130+
get_file_name(program_path(), FALSE));
131+
strcat(default_boot_config_path, TEXT("_Data/boot.config"));
132+
133+
#if !defined(__APPLE__)
134+
if (plthook_replace(hook, "fopen64", &fopen64_hook, NULL) != 0)
135+
printf("Failed to hook fopen64, ignoring it. Error: %s\n",
136+
plthook_error());
137+
#endif
138+
if (plthook_replace(hook, "fopen", &fopen_hook, NULL) != 0)
139+
printf("Failed to hook fopen, ignoring it. Error: %s\n",
140+
plthook_error());
141+
} else {
142+
LOG("The boot.config file won't be overriden because the provided "
143+
"one does not exist: %s",
144+
config.boot_config_override);
145+
}
146+
}
147+
97148
if (plthook_replace(hook, "fclose", &fclose_hook, NULL) != 0)
98149
printf("Failed to hook fclose, ignoring it. Error: %s\n",
99150
plthook_error());

src/util/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ typedef int bool_t;
2424

2525
#define TRUE 1
2626
#define FALSE 0
27+
#ifndef NULL
2728
#define NULL 0
29+
#endif
2830

2931
#define TEXT(text) text
3032

src/windows/config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ static inline void init_config_file() {
7171
TEXT("false"), &config.redirect_output_log);
7272
load_path_file(config_path, TEXT("General"), TEXT("target_assembly"),
7373
DEFAULT_TARGET_ASSEMBLY, &config.target_assembly);
74+
load_path_file(config_path, TEXT("General"), TEXT("boot_config_override"),
75+
NULL, &config.boot_config_override);
7476

7577
load_str_file(config_path, TEXT("UnityMono"),
7678
TEXT("dll_search_path_override"), TEXT(""),
@@ -144,6 +146,8 @@ static inline void init_cmd_args() {
144146
config.redirect_output_log, load_bool_argv);
145147
PARSE_ARG(TEXT("--doorstop-target-assembly"), config.target_assembly,
146148
load_path_argv);
149+
PARSE_ARG(TEXT("--doorstop-boot-config-override"),
150+
config.boot_config_override, load_path_argv);
147151

148152
PARSE_ARG(TEXT("--doorstop-mono-dll-search-path-override"),
149153
config.mono_dll_search_path_override, load_path_argv);

0 commit comments

Comments
 (0)