editor_export_platform.h 16 KB

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