-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathconfig.c
More file actions
188 lines (164 loc) · 6.92 KB
/
config.c
File metadata and controls
188 lines (164 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "../config/config.h"
#include "../crt.h"
#include "../util/logging.h"
#include "../util/util.h"
#define CONFIG_NAME TEXT("doorstop_config.ini")
#define DEFAULT_TARGET_ASSEMBLY TEXT("Doorstop.dll")
#define EXE_EXTENSION_LENGTH 4
#define STR_EQUAL(str1, str2) (lstrcmpi(str1, str2) == 0)
void load_bool_file(const char_t *path, const char_t *section,
const char_t *key, const char_t *def, bool_t *value) {
char_t enabled_string[256] = TEXT("true");
GetPrivateProfileString(section, key, def, enabled_string, 256, path);
LOG("CONFIG: %s.%s = %s", section, key, enabled_string);
if (STR_EQUAL(enabled_string, TEXT("true")))
*value = TRUE;
else if (STR_EQUAL(enabled_string, TEXT("false")))
*value = FALSE;
}
char_t *get_ini_entry(const char_t *config_file, const char_t *section,
const char_t *key, const char_t *default_val) {
DWORD i = 0;
DWORD size, read;
char_t *result = NULL;
do {
if (result != NULL)
free(result);
i++;
size = i * MAX_PATH + 1;
result = malloc(sizeof(char_t) * size);
read = GetPrivateProfileString(section, key, default_val, result, size,
config_file);
} while (read == size - 1);
return result;
}
bool_t load_str_file(const char_t *path, const char_t *section,
const char_t *key, const char_t *def, char_t **value) {
char_t *tmp = get_ini_entry(path, section, key, def);
LOG("CONFIG: %s.%s = %s", section, key, tmp);
if (!tmp || strlen(tmp) == 0)
return FALSE;
*value = tmp;
return TRUE;
}
void load_path_file(const char_t *path, const char_t *section,
const char_t *key, const char_t *def, char_t **value) {
if (!load_str_file(path, section, key, def, value))
return;
char_t *tmp = *value;
*value = get_full_path(tmp);
LOG("(%s.%s) %s => %s", section, key, tmp, *value);
free(tmp);
}
static inline void init_config_file() {
if (!file_exists(CONFIG_NAME))
return;
char_t *config_path = get_full_path(CONFIG_NAME);
load_bool_file(config_path, TEXT("General"), TEXT("enabled"), TEXT("true"),
&config.enabled);
load_bool_file(config_path, TEXT("General"), TEXT("ignore_disable_switch"),
TEXT("false"), &config.ignore_disabled_env);
load_bool_file(config_path, TEXT("General"), TEXT("redirect_output_log"),
TEXT("false"), &config.redirect_output_log);
load_path_file(config_path, TEXT("General"), TEXT("target_native_library"),
NULL, &config.target_native_library);
load_path_file(config_path, TEXT("General"), TEXT("target_assembly"),
DEFAULT_TARGET_ASSEMBLY, &config.target_assembly);
load_path_file(config_path, TEXT("General"), TEXT("boot_config_override"),
NULL, &config.boot_config_override);
load_str_file(config_path, TEXT("UnityMono"),
TEXT("dll_search_path_override"), TEXT(""),
&config.mono_dll_search_path_override);
load_bool_file(config_path, TEXT("UnityMono"), TEXT("debug_enabled"),
TEXT("false"), &config.mono_debug_enabled);
load_bool_file(config_path, TEXT("UnityMono"), TEXT("debug_suspend"),
TEXT("false"), &config.mono_debug_suspend);
load_str_file(config_path, TEXT("UnityMono"), TEXT("debug_address"),
TEXT("127.0.0.1:10000"), &config.mono_debug_address);
load_path_file(config_path, TEXT("Il2Cpp"), TEXT("coreclr_path"), NULL,
&config.clr_runtime_coreclr_path);
load_path_file(config_path, TEXT("Il2Cpp"), TEXT("corlib_dir"), NULL,
&config.clr_corlib_dir);
free(config_path);
}
bool_t load_bool_argv(char_t **argv, int *i, int argc, const char_t *arg_name,
bool_t *value) {
if (STR_EQUAL(argv[*i], arg_name) && *i < argc) {
char_t *par = argv[++*i];
if (STR_EQUAL(par, TEXT("true")))
*value = TRUE;
else if (STR_EQUAL(par, TEXT("false")))
*value = FALSE;
LOG("ARGV: %s = %s", arg_name, par);
return TRUE;
}
return FALSE;
}
bool_t load_str_argv(char_t **argv, int *i, int argc, const char_t *arg_name,
char_t **value) {
if (STR_EQUAL(argv[*i], arg_name) && *i < argc) {
if (*value != NULL)
free(*value);
const size_t len = strlen(argv[*i + 1]) + 1;
*value = malloc(sizeof(char_t) * len);
strncpy(*value, argv[++*i], len);
LOG("ARGV: %s = %s", arg_name, *value);
return TRUE;
}
return FALSE;
}
bool_t load_path_argv(char_t **argv, int *i, int argc, const char_t *arg_name,
char_t **value) {
if (!load_str_argv(argv, i, argc, arg_name, value))
return FALSE;
char_t *tmp = *value;
*value = get_full_path(tmp);
LOG("(%s) %s => %s", arg_name, tmp, *value);
free(tmp);
return TRUE;
}
static inline void init_cmd_args() {
char_t *args = GetCommandLine();
int argc = 0;
char_t **argv = CommandLineToArgv(args, &argc);
#define PARSE_ARG(name, dest, parser) \
if (parser(argv, &i, argc, name, &(dest))) \
continue;
for (int i = 0; i < argc; i++) {
PARSE_ARG(TEXT("--doorstop-enabled"), config.enabled, load_bool_argv);
PARSE_ARG(TEXT("--doorstop-redirect-output-log"),
config.redirect_output_log, load_bool_argv);
PARSE_ARG(TEXT("--doorstop-target-assembly"), config.target_assembly,
load_path_argv);
PARSE_ARG(TEXT("--doorstop-boot-config-override"),
config.boot_config_override, load_path_argv);
PARSE_ARG(TEXT("--doorstop-mono-dll-search-path-override"),
config.mono_dll_search_path_override, load_path_argv);
PARSE_ARG(TEXT("--doorstop-mono-debug-enabled"),
config.mono_debug_enabled, load_bool_argv);
PARSE_ARG(TEXT("--doorstop-mono-debug-suspend"),
config.mono_debug_suspend, load_bool_argv);
PARSE_ARG(TEXT("--doorstop-mono-debug-address"),
config.mono_debug_address, load_str_argv);
PARSE_ARG(TEXT("--doorstop-clr-corlib-dir"), config.clr_corlib_dir,
load_path_argv);
PARSE_ARG(TEXT("--doorstop-clr-runtime-coreclr-path"),
config.clr_runtime_coreclr_path, load_path_argv);
}
LocalFree(argv);
#undef PARSE_ARG
}
static inline void init_env_vars() {
char_t *disable_env = getenv(TEXT("DOORSTOP_DISABLE"));
if (!config.ignore_disabled_env && disable_env != 0) {
LOG("DOORSTOP_DISABLE is set! Disabling Doorstop!");
config.enabled = FALSE;
}
shutenv(disable_env);
}
void load_config() {
init_config_defaults();
init_config_file();
init_cmd_args();
init_env_vars();
}