This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.c
217 lines (165 loc) · 8.37 KB
/
util.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "util.h"
godot_string get_script_path(godot_string *p_class_name, Library *p_lib)
{
// Get the .gdnlib path
godot_char_string gdnlib_path_char_string;
{
godot_method_bind *mb_get_path = p_lib->core_api->godot_method_bind_get_method("Resource", "get_path");
godot_variant_call_error error;
godot_variant ret =
p_lib->core_api->godot_method_bind_call(mb_get_path, (godot_object *)p_lib->gdnlib, NULL, 0, &error);
godot_string gdnlib_path_string = p_lib->core_api->godot_variant_as_string(&ret);
gdnlib_path_char_string = p_lib->core_api->godot_string_utf8(&gdnlib_path_string);
p_lib->core_api->godot_string_destroy(&gdnlib_path_string);
p_lib->core_api->godot_variant_destroy(&ret);
}
const char *gdnlib_path = p_lib->core_api->godot_char_string_get_data(&gdnlib_path_char_string);
int gdnlib_path_size = p_lib->core_api->godot_char_string_length(&gdnlib_path_char_string);
// Get script name
godot_char_string script_name_char_string;
{
godot_string script_name_string =
p_lib->core_api->godot_string_camelcase_to_underscore_lowercased(p_class_name);
script_name_char_string = p_lib->core_api->godot_string_utf8(&script_name_string);
p_lib->core_api->godot_string_destroy(&script_name_string);
}
const char *script_name = p_lib->core_api->godot_char_string_get_data(&script_name_char_string);
int script_name_size = p_lib->core_api->godot_char_string_length(&script_name_char_string);
// Get folder of script
int last_slash_char;
for (int i = 0; i < gdnlib_path_size; i++)
if (gdnlib_path[i] == '/')
last_slash_char = i + 1;
// Join the strings together
char *script_path = p_lib->core_api->godot_alloc(sizeof(char) * (last_slash_char + script_name_size + 6));
memset(script_path, 0, sizeof(char) * (last_slash_char + script_name_size + 6));
memcpy(script_path, gdnlib_path, last_slash_char * sizeof(char));
memcpy(script_path + last_slash_char, script_name, script_name_size * sizeof(char));
memcpy(script_path + last_slash_char + script_name_size, ".gdns", 5 * sizeof(char));
godot_string script_path_string = p_lib->core_api->godot_string_chars_to_utf8(script_path);
p_lib->core_api->godot_free(script_path);
p_lib->core_api->godot_char_string_destroy(&script_name_char_string);
p_lib->core_api->godot_char_string_destroy(&gdnlib_path_char_string);
return script_path_string;
}
godot_object *instantiate_custom_class(const char *p_class_name, const char *p_base, Library *p_lib)
{
// Get the script object
godot_object *script;
{
godot_string class_name = p_lib->core_api->godot_string_chars_to_utf8(p_class_name);
godot_string script_path_string = get_script_path(&class_name, p_lib);
// Load the script
godot_object *resource_loader = p_lib->core_api->godot_global_get_singleton("ResourceLoader");
godot_variant resource_loader_variant;
p_lib->core_api->godot_variant_new_object(&resource_loader_variant, resource_loader);
godot_variant path_variant;
p_lib->core_api->godot_variant_new_string(&path_variant, &script_path_string);
godot_variant script_variant;
{
const godot_variant *args[] = {&path_variant};
godot_string method_name = p_lib->core_api->godot_string_chars_to_utf8("load");
godot_variant_call_error error;
script_variant =
p_lib->core_api->godot_variant_call(&resource_loader_variant, &method_name, args, 1, &error);
p_lib->core_api->godot_string_destroy(&method_name);
}
script = p_lib->core_api->godot_variant_as_object(&script_variant);
p_lib->core_api->godot_variant_destroy(&script_variant);
p_lib->core_api->godot_variant_destroy(&path_variant);
p_lib->core_api->godot_variant_destroy(&resource_loader_variant);
p_lib->core_api->godot_string_destroy(&script_path_string);
p_lib->core_api->godot_string_destroy(&class_name);
}
// Construct the base object
godot_class_constructor base_constructor = p_lib->core_api->godot_get_class_constructor(p_base);
godot_object *object = base_constructor();
// Add the script
{
static godot_method_bind *bind = NULL;
if (!bind)
bind = p_lib->core_api->godot_method_bind_get_method("Object", "set_script");
const void *args[] = {script};
p_lib->core_api->godot_method_bind_ptrcall(bind, object, args, NULL);
}
return object;
}
godot_variant_call_error object_emit_signal(godot_object *p_object, godot_string *p_signal_name, int p_num_args,
const godot_variant **p_args, Library *p_lib)
{
godot_variant variant;
p_lib->core_api->godot_variant_new_object(&variant, p_object);
godot_string method_name = p_lib->core_api->godot_string_chars_to_utf8("emit_signal");
const godot_variant **args = p_lib->core_api->godot_alloc(sizeof(godot_variant *) * (p_num_args + 1));
godot_variant variant_signal_name;
p_lib->core_api->godot_variant_new_string(&variant_signal_name, p_signal_name);
args[0] = &variant_signal_name;
for (int i = 0; i < p_num_args; i++)
{
args[i + 1] = p_args[i];
}
godot_variant_call_error error;
p_lib->core_api->godot_variant_call(&variant, &method_name, args, p_num_args + 1, &error);
p_lib->core_api->godot_variant_destroy(&variant_signal_name);
p_lib->core_api->godot_free(args);
p_lib->core_api->godot_string_destroy(&method_name);
p_lib->core_api->godot_variant_destroy(&variant);
return error;
}
godot_variant_call_error object_emit_signal_deferred(godot_object *p_object, godot_string *p_signal_name,
int p_num_args, const godot_variant **p_args, Library *p_lib)
{
godot_variant variant;
p_lib->core_api->godot_variant_new_object(&variant, p_object);
godot_string method = p_lib->core_api->godot_string_chars_to_utf8("call_deferred");
const godot_variant **args = p_lib->core_api->godot_alloc(sizeof(godot_variant *) * (p_num_args + 2));
godot_variant variant_method_name;
godot_string method_name = p_lib->core_api->godot_string_chars_to_utf8("emit_signal");
p_lib->core_api->godot_variant_new_string(&variant_method_name, &method_name);
p_lib->core_api->godot_string_destroy(&method_name);
godot_variant variant_signal_name;
p_lib->core_api->godot_variant_new_string(&variant_signal_name, p_signal_name);
args[0] = &variant_method_name;
args[1] = &variant_signal_name;
for (int i = 0; i < p_num_args; i++)
{
args[i + 2] = p_args[i];
}
godot_variant_call_error error;
p_lib->core_api->godot_variant_call(&variant, &method, args, p_num_args + 2, &error);
p_lib->core_api->godot_variant_destroy(&variant_signal_name);
p_lib->core_api->godot_string_destroy(&method_name);
p_lib->core_api->godot_variant_destroy(&variant_method_name);
p_lib->core_api->godot_free(args);
p_lib->core_api->godot_string_destroy(&method);
p_lib->core_api->godot_variant_destroy(&variant);
return error;
}
godot_variant_call_error object_call(godot_object *p_object, godot_string *p_method_name, int p_num_args,
const godot_variant **p_args, godot_variant *r_return, Library *p_lib)
{
godot_variant variant;
p_lib->core_api->godot_variant_new_object(&variant, p_object);
godot_variant_call_error error;
p_lib->core_api->godot_variant_call(&variant, p_method_name, p_args, p_num_args, &error);
p_lib->core_api->godot_variant_destroy(&variant);
return error;
}
void godot_reference(godot_object *p_object, Library *p_lib)
{
static godot_method_bind *bind = NULL;
if (!bind)
bind = p_lib->core_api->godot_method_bind_get_method("Reference", "reference");
godot_bool ret;
p_lib->core_api->godot_method_bind_ptrcall(bind, p_object, NULL, &ret);
}
void godot_unreference(godot_object *p_object, Library *p_lib)
{
static godot_method_bind *bind = NULL;
if (!bind)
bind = p_lib->core_api->godot_method_bind_get_method("Reference", "unreference");
godot_bool ret;
p_lib->core_api->godot_method_bind_ptrcall(bind, p_object, NULL, &ret);
if (ret)
p_lib->core_api->godot_object_destroy(p_object);
}