Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/plugin_config_apple_embedded.cpp
9902 views
1
/**************************************************************************/
2
/* plugin_config_apple_embedded.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "plugin_config_apple_embedded.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/dir_access.h"
35
#include "core/io/file_access.h"
36
37
String PluginConfigAppleEmbedded::resolve_local_dependency_path(String plugin_config_dir, String dependency_path) {
38
String absolute_path;
39
40
if (dependency_path.is_empty()) {
41
return absolute_path;
42
}
43
44
if (dependency_path.is_absolute_path()) {
45
return dependency_path;
46
}
47
48
String res_path = ProjectSettings::get_singleton()->globalize_path("res://");
49
absolute_path = plugin_config_dir.path_join(dependency_path);
50
51
return absolute_path.replace(res_path, "res://");
52
}
53
54
String PluginConfigAppleEmbedded::resolve_system_dependency_path(String dependency_path) {
55
String absolute_path;
56
57
if (dependency_path.is_empty()) {
58
return absolute_path;
59
}
60
61
if (dependency_path.is_absolute_path()) {
62
return dependency_path;
63
}
64
65
String system_path = "/System/Library/Frameworks";
66
67
return system_path.path_join(dependency_path);
68
}
69
70
Vector<String> PluginConfigAppleEmbedded::resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) {
71
Vector<String> paths;
72
73
for (int i = 0; i < p_paths.size(); i++) {
74
String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]);
75
76
if (path.is_empty()) {
77
continue;
78
}
79
80
paths.push_back(path);
81
}
82
83
return paths;
84
}
85
86
Vector<String> PluginConfigAppleEmbedded::resolve_system_dependencies(Vector<String> p_paths) {
87
Vector<String> paths;
88
89
for (int i = 0; i < p_paths.size(); i++) {
90
String path = resolve_system_dependency_path(p_paths[i]);
91
92
if (path.is_empty()) {
93
continue;
94
}
95
96
paths.push_back(path);
97
}
98
99
return paths;
100
}
101
102
bool PluginConfigAppleEmbedded::validate_plugin(PluginConfigAppleEmbedded &plugin_config) {
103
bool valid_name = !plugin_config.name.is_empty();
104
bool valid_binary_name = !plugin_config.binary.is_empty();
105
bool valid_initialize = !plugin_config.initialization_method.is_empty();
106
bool valid_deinitialize = !plugin_config.deinitialization_method.is_empty();
107
108
bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize;
109
110
if (!fields_value) {
111
return false;
112
}
113
114
String plugin_extension = plugin_config.binary.get_extension().to_lower();
115
116
if ((plugin_extension == "a" && FileAccess::exists(plugin_config.binary)) ||
117
(plugin_extension == "xcframework" && DirAccess::exists(plugin_config.binary))) {
118
plugin_config.valid_config = true;
119
plugin_config.supports_targets = false;
120
} else {
121
String file_path = plugin_config.binary.get_base_dir();
122
String file_name = plugin_config.binary.get_basename().get_file();
123
String file_extension = plugin_config.binary.get_extension();
124
String release_file_name = file_path.path_join(file_name + ".release." + file_extension);
125
String debug_file_name = file_path.path_join(file_name + ".debug." + file_extension);
126
127
if ((plugin_extension == "a" && FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) ||
128
(plugin_extension == "xcframework" && DirAccess::exists(release_file_name) && DirAccess::exists(debug_file_name))) {
129
plugin_config.valid_config = true;
130
plugin_config.supports_targets = true;
131
}
132
}
133
134
return plugin_config.valid_config;
135
}
136
137
String PluginConfigAppleEmbedded::get_plugin_main_binary(PluginConfigAppleEmbedded &plugin_config, bool p_debug) {
138
if (!plugin_config.supports_targets) {
139
return plugin_config.binary;
140
}
141
142
String plugin_binary_dir = plugin_config.binary.get_base_dir();
143
String plugin_name_prefix = plugin_config.binary.get_basename().get_file();
144
String plugin_extension = plugin_config.binary.get_extension();
145
String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + "." + plugin_extension;
146
147
return plugin_binary_dir.path_join(plugin_file);
148
}
149
150
uint64_t PluginConfigAppleEmbedded::get_plugin_modification_time(const PluginConfigAppleEmbedded &plugin_config, const String &config_path) {
151
uint64_t last_updated = FileAccess::get_modified_time(config_path);
152
153
if (!plugin_config.supports_targets) {
154
last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary));
155
} else {
156
String file_path = plugin_config.binary.get_base_dir();
157
String file_name = plugin_config.binary.get_basename().get_file();
158
String plugin_extension = plugin_config.binary.get_extension();
159
String release_file_name = file_path.path_join(file_name + ".release." + plugin_extension);
160
String debug_file_name = file_path.path_join(file_name + ".debug." + plugin_extension);
161
162
last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name));
163
last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name));
164
}
165
166
return last_updated;
167
}
168
169
PluginConfigAppleEmbedded PluginConfigAppleEmbedded::load_plugin_config(Ref<ConfigFile> config_file, const String &path) {
170
PluginConfigAppleEmbedded plugin_config = {};
171
172
if (config_file.is_null()) {
173
return plugin_config;
174
}
175
176
config_file->clear();
177
178
Error err = config_file->load(path);
179
180
if (err != OK) {
181
return plugin_config;
182
}
183
184
String config_base_dir = path.get_base_dir();
185
186
plugin_config.name = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_NAME_KEY, String());
187
plugin_config.use_swift_runtime = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_USE_SWIFT_KEY, false);
188
plugin_config.initialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_INITIALIZE_KEY, String());
189
plugin_config.deinitialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_DEINITIALIZE_KEY, String());
190
191
String binary_path = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_BINARY_KEY, String());
192
plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);
193
194
if (config_file->has_section(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION)) {
195
Vector<String> linked_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKED_KEY, Vector<String>());
196
Vector<String> embedded_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());
197
Vector<String> system_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_SYSTEM_KEY, Vector<String>());
198
Vector<String> files = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_FILES_KEY, Vector<String>());
199
200
plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);
201
plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);
202
plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);
203
204
plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);
205
206
plugin_config.capabilities = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
207
208
plugin_config.linker_flags = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
209
}
210
211
if (config_file->has_section(PluginConfigAppleEmbedded::PLIST_SECTION)) {
212
Vector<String> keys = config_file->get_section_keys(PluginConfigAppleEmbedded::PLIST_SECTION);
213
214
for (const String &key : keys) {
215
Vector<String> key_components = key.split(":");
216
217
String key_value = "";
218
PluginConfigAppleEmbedded::PlistItemType key_type = PluginConfigAppleEmbedded::PlistItemType::UNKNOWN;
219
220
if (key_components.size() == 1) {
221
key_value = key_components[0];
222
key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;
223
} else if (key_components.size() == 2) {
224
key_value = key_components[0];
225
226
if (key_components[1].to_lower() == "string") {
227
key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;
228
} else if (key_components[1].to_lower() == "integer") {
229
key_type = PluginConfigAppleEmbedded::PlistItemType::INTEGER;
230
} else if (key_components[1].to_lower() == "boolean") {
231
key_type = PluginConfigAppleEmbedded::PlistItemType::BOOLEAN;
232
} else if (key_components[1].to_lower() == "raw") {
233
key_type = PluginConfigAppleEmbedded::PlistItemType::RAW;
234
} else if (key_components[1].to_lower() == "string_input") {
235
key_type = PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT;
236
}
237
}
238
239
if (key_value.is_empty() || key_type == PluginConfigAppleEmbedded::PlistItemType::UNKNOWN) {
240
continue;
241
}
242
243
String value;
244
245
switch (key_type) {
246
case PluginConfigAppleEmbedded::PlistItemType::STRING: {
247
String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
248
value = "<string>" + raw_value + "</string>";
249
} break;
250
case PluginConfigAppleEmbedded::PlistItemType::INTEGER: {
251
int raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, 0);
252
Dictionary value_dictionary;
253
String value_format = "<integer>$value</integer>";
254
value_dictionary["value"] = raw_value;
255
value = value_format.format(value_dictionary, "$_");
256
} break;
257
case PluginConfigAppleEmbedded::PlistItemType::BOOLEAN:
258
if (config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, false)) {
259
value = "<true/>";
260
} else {
261
value = "<false/>";
262
}
263
break;
264
case PluginConfigAppleEmbedded::PlistItemType::RAW: {
265
String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
266
value = raw_value;
267
} break;
268
case PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT: {
269
String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
270
value = raw_value;
271
} break;
272
default:
273
continue;
274
}
275
276
plugin_config.plist[key_value] = PluginConfigAppleEmbedded::PlistItem{ key_type, value };
277
}
278
}
279
280
if (validate_plugin(plugin_config)) {
281
plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
282
}
283
284
return plugin_config;
285
}
286
287