editor_export_platform.h 17 KB

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