Debian Codegraph @

editor/export/plugin_config_apple_embedded.cpp

raw symbols SCIP
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.initialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_INITIALIZE_KEY, String());
188 plugin_config.deinitialization_method = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_DEINITIALIZE_KEY, String());
189
190 String binary_path = config_file->get_value(PluginConfigAppleEmbedded::CONFIG_SECTION, PluginConfigAppleEmbedded::CONFIG_BINARY_KEY, String());
191 plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path);
192
193 if (config_file->has_section(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION)) {
194 Vector<String> linked_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKED_KEY, Vector<String>());
195 Vector<String> embedded_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_EMBEDDED_KEY, Vector<String>());
196 Vector<String> system_dependencies = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_SYSTEM_KEY, Vector<String>());
197 Vector<String> files = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_FILES_KEY, Vector<String>());
198
199 plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies);
200 plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies);
201 plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies);
202
203 plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files);
204
205 plugin_config.capabilities = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_CAPABILITIES_KEY, Vector<String>());
206
207 plugin_config.linker_flags = config_file->get_value(PluginConfigAppleEmbedded::DEPENDENCIES_SECTION, PluginConfigAppleEmbedded::DEPENDENCIES_LINKER_FLAGS, Vector<String>());
208 }
209
210 if (config_file->has_section(PluginConfigAppleEmbedded::PLIST_SECTION)) {
211 Vector<String> keys = config_file->get_section_keys(PluginConfigAppleEmbedded::PLIST_SECTION);
212
213 for (const String &key : keys) {
214 Vector<String> key_components = key.split(":");
215
216 String key_value = "";
217 PluginConfigAppleEmbedded::PlistItemType key_type = PluginConfigAppleEmbedded::PlistItemType::UNKNOWN;
218
219 if (key_components.size() == 1) {
220 key_value = key_components[0];
221 key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;
222 } else if (key_components.size() == 2) {
223 key_value = key_components[0];
224
225 if (key_components[1].to_lower() == "string") {
226 key_type = PluginConfigAppleEmbedded::PlistItemType::STRING;
227 } else if (key_components[1].to_lower() == "integer") {
228 key_type = PluginConfigAppleEmbedded::PlistItemType::INTEGER;
229 } else if (key_components[1].to_lower() == "boolean") {
230 key_type = PluginConfigAppleEmbedded::PlistItemType::BOOLEAN;
231 } else if (key_components[1].to_lower() == "raw") {
232 key_type = PluginConfigAppleEmbedded::PlistItemType::RAW;
233 } else if (key_components[1].to_lower() == "string_input") {
234 key_type = PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT;
235 }
236 }
237
238 if (key_value.is_empty() || key_type == PluginConfigAppleEmbedded::PlistItemType::UNKNOWN) {
239 continue;
240 }
241
242 String value;
243
244 switch (key_type) {
245 case PluginConfigAppleEmbedded::PlistItemType::STRING: {
246 String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
247 value = "<string>" + raw_value + "</string>";
248 } break;
249 case PluginConfigAppleEmbedded::PlistItemType::INTEGER: {
250 int raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, 0);
251 Dictionary value_dictionary;
252 String value_format = "<integer>$value</integer>";
253 value_dictionary["value"] = raw_value;
254 value = value_format.format(value_dictionary, "$_");
255 } break;
256 case PluginConfigAppleEmbedded::PlistItemType::BOOLEAN:
257 if (config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, false)) {
258 value = "<true/>";
259 } else {
260 value = "<false/>";
261 }
262 break;
263 case PluginConfigAppleEmbedded::PlistItemType::RAW: {
264 String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
265 value = raw_value;
266 } break;
267 case PluginConfigAppleEmbedded::PlistItemType::STRING_INPUT: {
268 String raw_value = config_file->get_value(PluginConfigAppleEmbedded::PLIST_SECTION, key, String());
269 value = raw_value;
270 } break;
271 default:
272 continue;
273 }
274
275 plugin_config.plist[key_value] = PluginConfigAppleEmbedded::PlistItem{ key_type, value };
276 }
277 }
278
279 if (validate_plugin(plugin_config)) {
280 plugin_config.last_updated = get_plugin_modification_time(plugin_config, path);
281 }
282
283 return plugin_config;
284 }