editor_export_platform.h 19 KB

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