editor_export_platform.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**************************************************************************/
  2. /* editor_export_platform.h */
  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. #pragma once
  31. class EditorFileSystemDirectory;
  32. struct EditorProgress;
  33. #include "core/config/project_settings.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/zip_io.h"
  36. #include "core/os/shared_object.h"
  37. #include "editor_export_preset.h"
  38. #include "scene/gui/rich_text_label.h"
  39. #include "scene/main/node.h"
  40. #include "scene/resources/image_texture.h"
  41. class EditorExportPlugin;
  42. const String ENV_SCRIPT_ENCRYPTION_KEY = "GODOT_SCRIPT_ENCRYPTION_KEY";
  43. class EditorExportPlatform : public RefCounted {
  44. GDCLASS(EditorExportPlatform, RefCounted);
  45. protected:
  46. static void _bind_methods();
  47. public:
  48. typedef Error (*EditorExportSaveFunction)(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  49. typedef Error (*EditorExportRemoveFunction)(void *p_userdata, const String &p_path);
  50. typedef Error (*EditorExportSaveSharedObject)(void *p_userdata, const SharedObject &p_so);
  51. enum DebugFlags {
  52. DEBUG_FLAG_DUMB_CLIENT = 1,
  53. DEBUG_FLAG_REMOTE_DEBUG = 2,
  54. DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST = 4,
  55. DEBUG_FLAG_VIEW_COLLISIONS = 8,
  56. DEBUG_FLAG_VIEW_NAVIGATION = 16,
  57. };
  58. enum ExportMessageType {
  59. EXPORT_MESSAGE_NONE,
  60. EXPORT_MESSAGE_INFO,
  61. EXPORT_MESSAGE_WARNING,
  62. EXPORT_MESSAGE_ERROR,
  63. };
  64. struct ExportMessage {
  65. ExportMessageType msg_type;
  66. String category;
  67. String text;
  68. };
  69. struct SavedData {
  70. uint64_t ofs = 0;
  71. uint64_t size = 0;
  72. bool encrypted = false;
  73. bool removal = false;
  74. Vector<uint8_t> md5;
  75. CharString path_utf8;
  76. bool operator<(const SavedData &p_data) const {
  77. return path_utf8 < p_data.path_utf8;
  78. }
  79. };
  80. struct PackData {
  81. String path;
  82. Ref<FileAccess> f;
  83. Vector<SavedData> file_ofs;
  84. EditorProgress *ep = nullptr;
  85. Vector<SharedObject> *so_files = nullptr;
  86. bool use_sparse_pck = false;
  87. };
  88. static bool _store_header(Ref<FileAccess> p_fd, bool p_enc, bool p_sparse, uint64_t &r_file_base_ofs, uint64_t &r_dir_base_ofs);
  89. static bool _encrypt_and_store_directory(Ref<FileAccess> p_fd, PackData &p_pack_data, const Vector<uint8_t> &p_key, uint64_t p_seed, uint64_t p_file_base);
  90. static Error _encrypt_and_store_data(Ref<FileAccess> p_fd, const String &p_path, const Vector<uint8_t> &p_data, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool &r_encrypt);
  91. String _get_script_encryption_key(const Ref<EditorExportPreset> &p_preset) const;
  92. private:
  93. struct ZipData {
  94. void *zip = nullptr;
  95. EditorProgress *ep = nullptr;
  96. Vector<SharedObject> *so_files = nullptr;
  97. int file_count = 0;
  98. };
  99. Vector<ExportMessage> messages;
  100. void _export_find_resources(EditorFileSystemDirectory *p_dir, HashSet<String> &p_paths);
  101. void _export_find_customized_resources(const Ref<EditorExportPreset> &p_preset, EditorFileSystemDirectory *p_dir, EditorExportPreset::FileExportMode p_mode, HashSet<String> &p_paths);
  102. void _export_find_dependencies(const String &p_path, HashSet<String> &p_paths);
  103. static bool _check_hash(const uint8_t *p_hash, const Vector<uint8_t> &p_data);
  104. static Error _save_pack_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  105. static Error _save_pack_patch_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  106. static Error _pack_add_shared_object(void *p_userdata, const SharedObject &p_so);
  107. static Error _remove_pack_file(void *p_userdata, const String &p_path);
  108. static Error _save_zip_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  109. static Error _save_zip_patch_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  110. static Error _zip_add_shared_object(void *p_userdata, const SharedObject &p_so);
  111. struct ScriptCallbackData {
  112. Callable file_cb;
  113. Callable so_cb;
  114. };
  115. static Error _script_save_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed);
  116. static Error _script_add_shared_object(void *p_userdata, const SharedObject &p_so);
  117. void _edit_files_with_filter(Ref<DirAccess> &da, const Vector<String> &p_filters, HashSet<String> &r_list, bool exclude);
  118. void _edit_filter_list(HashSet<String> &r_list, const String &p_filter, bool exclude);
  119. static Vector<uint8_t> _filter_extension_list_config_file(const String &p_config_path, const HashSet<String> &p_paths);
  120. struct FileExportCache {
  121. uint64_t source_modified_time = 0;
  122. String source_md5;
  123. String saved_path;
  124. bool used = false;
  125. };
  126. bool _export_customize_dictionary(Dictionary &dict, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins);
  127. bool _export_customize_array(Array &array, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins);
  128. bool _export_customize_object(Object *p_object, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins);
  129. bool _export_customize_scene_resources(Node *p_root, Node *p_node, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins);
  130. bool _is_editable_ancestor(Node *p_root, Node *p_node);
  131. String _export_customize(const String &p_path, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins, LocalVector<Ref<EditorExportPlugin>> &customize_scenes_plugins, HashMap<String, FileExportCache> &export_cache, const String &export_base_path, bool p_force_save);
  132. protected:
  133. struct ExportNotifier {
  134. ExportNotifier(EditorExportPlatform &p_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags);
  135. ~ExportNotifier();
  136. };
  137. HashSet<String> get_features(const Ref<EditorExportPreset> &p_preset, bool p_debug) const;
  138. Dictionary _find_export_template(const String &p_template_file_name) const {
  139. Dictionary ret;
  140. String err;
  141. String path = find_export_template(p_template_file_name, &err);
  142. ret["result"] = (err.is_empty() && !path.is_empty()) ? OK : FAILED;
  143. ret["path"] = path;
  144. ret["error_string"] = err;
  145. return ret;
  146. }
  147. bool exists_export_template(const String &p_template_file_name, String *r_err) const;
  148. String find_export_template(const String &p_template_file_name, String *r_err = nullptr) const;
  149. Vector<String> gen_export_flags(BitField<EditorExportPlatform::DebugFlags> p_flags);
  150. virtual void zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name);
  151. Error _ssh_run_on_remote(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, Array r_output = Array(), int p_port_fwd = -1) const {
  152. String pipe;
  153. Error err = ssh_run_on_remote(p_host, p_port, p_ssh_args, p_cmd_args, &pipe, p_port_fwd);
  154. r_output.push_back(pipe);
  155. return err;
  156. }
  157. OS::ProcessID _ssh_run_on_remote_no_wait(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, int p_port_fwd = -1) const {
  158. OS::ProcessID pid = 0;
  159. Error err = ssh_run_on_remote_no_wait(p_host, p_port, p_ssh_args, p_cmd_args, &pid, p_port_fwd);
  160. if (err != OK) {
  161. return -1;
  162. } else {
  163. return pid;
  164. }
  165. }
  166. Error ssh_run_on_remote(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, String *r_out = nullptr, int p_port_fwd = -1) const;
  167. Error ssh_run_on_remote_no_wait(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, OS::ProcessID *r_pid = nullptr, int p_port_fwd = -1) const;
  168. Error ssh_push_to_remote(const String &p_host, const String &p_port, const Vector<String> &p_scp_args, const String &p_src_file, const String &p_dst_file) const;
  169. Error _load_patches(const Vector<String> &p_patches);
  170. void _unload_patches();
  171. Ref<Image> _load_icon_or_splash_image(const String &p_path, Error *r_error) const;
  172. #ifndef DISABLE_DEPRECATED
  173. static Vector<String> _get_forced_export_files_bind_compat_71542();
  174. static void _bind_compatibility_methods();
  175. #endif
  176. public:
  177. static Variant get_project_setting(const Ref<EditorExportPreset> &p_preset, const StringName &p_name);
  178. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const = 0;
  179. struct ExportOption {
  180. PropertyInfo option;
  181. Variant default_value;
  182. bool update_visibility = false;
  183. bool required = false;
  184. ExportOption(const PropertyInfo &p_info, const Variant &p_default, bool p_update_visibility = false, bool p_required = false) :
  185. option(p_info),
  186. default_value(p_default),
  187. update_visibility(p_update_visibility),
  188. required(p_required) {
  189. }
  190. ExportOption() {}
  191. };
  192. virtual Ref<EditorExportPreset> create_preset();
  193. virtual bool is_executable(const String &p_path) const { return false; }
  194. virtual void clear_messages() { messages.clear(); }
  195. virtual void add_message(ExportMessageType p_type, const String &p_category, const String &p_message) {
  196. ExportMessage msg;
  197. msg.category = p_category;
  198. msg.text = p_message;
  199. msg.msg_type = p_type;
  200. messages.push_back(msg);
  201. switch (p_type) {
  202. case EXPORT_MESSAGE_INFO: {
  203. print_line(vformat("%s: %s", msg.category, msg.text));
  204. } break;
  205. case EXPORT_MESSAGE_WARNING: {
  206. WARN_PRINT(vformat("%s: %s", msg.category, msg.text));
  207. } break;
  208. case EXPORT_MESSAGE_ERROR: {
  209. ERR_PRINT(vformat("%s: %s", msg.category, msg.text));
  210. } break;
  211. default:
  212. break;
  213. }
  214. }
  215. virtual int get_message_count() const {
  216. return messages.size();
  217. }
  218. virtual ExportMessage get_message(int p_index) const {
  219. ERR_FAIL_INDEX_V(p_index, messages.size(), ExportMessage());
  220. return messages[p_index];
  221. }
  222. virtual ExportMessageType _get_message_type(int p_index) const {
  223. ERR_FAIL_INDEX_V(p_index, messages.size(), EXPORT_MESSAGE_NONE);
  224. return messages[p_index].msg_type;
  225. }
  226. virtual String _get_message_category(int p_index) const {
  227. ERR_FAIL_INDEX_V(p_index, messages.size(), String());
  228. return messages[p_index].category;
  229. }
  230. virtual String _get_message_text(int p_index) const {
  231. ERR_FAIL_INDEX_V(p_index, messages.size(), String());
  232. return messages[p_index].text;
  233. }
  234. virtual ExportMessageType get_worst_message_type() const {
  235. ExportMessageType worst_type = EXPORT_MESSAGE_NONE;
  236. for (int i = 0; i < messages.size(); i++) {
  237. worst_type = MAX(worst_type, messages[i].msg_type);
  238. }
  239. return worst_type;
  240. }
  241. Dictionary get_internal_export_files(const Ref<EditorExportPreset> &p_preset, bool p_debug);
  242. static Vector<String> get_forced_export_files(const Ref<EditorExportPreset> &p_preset);
  243. virtual bool fill_log_messages(RichTextLabel *p_log, Error p_err);
  244. virtual void get_export_options(List<ExportOption> *r_options) const = 0;
  245. virtual bool should_update_export_options() { return false; }
  246. virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const { return true; }
  247. virtual String get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const { return String(); }
  248. virtual String get_os_name() const = 0;
  249. virtual String get_name() const = 0;
  250. virtual Ref<Texture2D> get_logo() const = 0;
  251. Array get_current_presets() const;
  252. Error _export_project_files(const Ref<EditorExportPreset> &p_preset, bool p_debug, const Callable &p_save_func, const Callable &p_so_func);
  253. Error export_project_files(const Ref<EditorExportPreset> &p_preset, bool p_debug, EditorExportSaveFunction p_save_func, EditorExportRemoveFunction p_remove_func, void *p_udata, EditorExportSaveSharedObject p_so_func = nullptr);
  254. Dictionary _save_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, bool p_embed = false);
  255. Dictionary _save_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  256. Dictionary _save_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  257. Dictionary _save_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  258. Error save_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files = nullptr, EditorExportSaveFunction p_save_func = nullptr, EditorExportRemoveFunction p_remove_func = nullptr, bool p_embed = false, int64_t *r_embedded_start = nullptr, int64_t *r_embedded_size = nullptr);
  259. Error save_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files = nullptr, EditorExportSaveFunction p_save_func = nullptr);
  260. Error save_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files = nullptr, bool p_embed = false, int64_t *r_embedded_start = nullptr, int64_t *r_embedded_size = nullptr);
  261. Error save_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files = nullptr);
  262. virtual bool poll_export() { return false; }
  263. virtual int get_options_count() const { return 0; }
  264. virtual String get_options_tooltip() const { return ""; }
  265. virtual Ref<Texture2D> get_option_icon(int p_index) const;
  266. virtual String get_option_label(int p_device) const { return ""; }
  267. virtual String get_option_tooltip(int p_device) const { return ""; }
  268. virtual String get_device_architecture(int p_device) const { return ""; }
  269. virtual void cleanup() {}
  270. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) { return OK; }
  271. virtual Ref<Texture2D> get_run_icon() const { return get_logo(); }
  272. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const;
  273. virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const = 0;
  274. virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const = 0;
  275. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const = 0;
  276. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags = 0) = 0;
  277. virtual Error export_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags = 0);
  278. virtual Error export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags = 0);
  279. virtual Error export_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, const Vector<String> &p_patches = Vector<String>(), BitField<EditorExportPlatform::DebugFlags> p_flags = 0);
  280. virtual Error export_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, const Vector<String> &p_patches = Vector<String>(), BitField<EditorExportPlatform::DebugFlags> p_flags = 0);
  281. virtual void get_platform_features(List<String> *r_features) const = 0;
  282. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) {}
  283. virtual String get_debug_protocol() const { return "tcp://"; }
  284. virtual HashMap<String, Variant> get_custom_project_settings(const Ref<EditorExportPreset> &p_preset) const { return HashMap<String, Variant>(); }
  285. };
  286. VARIANT_ENUM_CAST(EditorExportPlatform::ExportMessageType)
  287. VARIANT_BITFIELD_CAST(EditorExportPlatform::DebugFlags);