editor_export.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*************************************************************************/
  2. /* editor_export.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_H
  31. #define EDITOR_EXPORT_H
  32. #include "core/io/dir_access.h"
  33. #include "core/io/resource.h"
  34. #include "scene/main/node.h"
  35. #include "scene/main/timer.h"
  36. #include "scene/resources/texture.h"
  37. class FileAccess;
  38. class EditorExportPlatform;
  39. class EditorFileSystemDirectory;
  40. struct EditorProgress;
  41. class EditorExportPreset : public RefCounted {
  42. GDCLASS(EditorExportPreset, RefCounted);
  43. public:
  44. enum ExportFilter {
  45. EXPORT_ALL_RESOURCES,
  46. EXPORT_SELECTED_SCENES,
  47. EXPORT_SELECTED_RESOURCES,
  48. EXCLUDE_SELECTED_RESOURCES,
  49. };
  50. enum ScriptExportMode {
  51. MODE_SCRIPT_TEXT,
  52. MODE_SCRIPT_COMPILED,
  53. };
  54. private:
  55. Ref<EditorExportPlatform> platform;
  56. ExportFilter export_filter = EXPORT_ALL_RESOURCES;
  57. String include_filter;
  58. String exclude_filter;
  59. String export_path;
  60. String exporter;
  61. Set<String> selected_files;
  62. bool runnable = false;
  63. friend class EditorExport;
  64. friend class EditorExportPlatform;
  65. List<PropertyInfo> properties;
  66. Map<StringName, Variant> values;
  67. String name;
  68. String custom_features;
  69. String enc_in_filters;
  70. String enc_ex_filters;
  71. bool enc_pck = false;
  72. bool enc_directory = false;
  73. int script_mode = MODE_SCRIPT_COMPILED;
  74. String script_key;
  75. protected:
  76. bool _set(const StringName &p_name, const Variant &p_value);
  77. bool _get(const StringName &p_name, Variant &r_ret) const;
  78. void _get_property_list(List<PropertyInfo> *p_list) const;
  79. public:
  80. Ref<EditorExportPlatform> get_platform() const;
  81. bool has(const StringName &p_property) const { return values.has(p_property); }
  82. void update_files_to_export();
  83. Vector<String> get_files_to_export() const;
  84. void add_export_file(const String &p_path);
  85. void remove_export_file(const String &p_path);
  86. bool has_export_file(const String &p_path);
  87. void set_name(const String &p_name);
  88. String get_name() const;
  89. void set_runnable(bool p_enable);
  90. bool is_runnable() const;
  91. void set_export_filter(ExportFilter p_filter);
  92. ExportFilter get_export_filter() const;
  93. void set_include_filter(const String &p_include);
  94. String get_include_filter() const;
  95. void set_exclude_filter(const String &p_exclude);
  96. String get_exclude_filter() const;
  97. void set_custom_features(const String &p_custom_features);
  98. String get_custom_features() const;
  99. void set_export_path(const String &p_path);
  100. String get_export_path() const;
  101. void set_enc_in_filter(const String &p_filter);
  102. String get_enc_in_filter() const;
  103. void set_enc_ex_filter(const String &p_filter);
  104. String get_enc_ex_filter() const;
  105. void set_enc_pck(bool p_enabled);
  106. bool get_enc_pck() const;
  107. void set_enc_directory(bool p_enabled);
  108. bool get_enc_directory() const;
  109. void set_script_export_mode(int p_mode);
  110. int get_script_export_mode() const;
  111. void set_script_encryption_key(const String &p_key);
  112. String get_script_encryption_key() const;
  113. const List<PropertyInfo> &get_properties() const { return properties; }
  114. EditorExportPreset() {}
  115. };
  116. struct SharedObject {
  117. String path;
  118. Vector<String> tags;
  119. String target;
  120. SharedObject(const String &p_path, const Vector<String> &p_tags, const String &p_target) :
  121. path(p_path),
  122. tags(p_tags),
  123. target(p_target) {
  124. }
  125. SharedObject() {}
  126. };
  127. class EditorExportPlatform : public RefCounted {
  128. GDCLASS(EditorExportPlatform, RefCounted);
  129. public:
  130. 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);
  131. typedef Error (*EditorExportSaveSharedObject)(void *p_userdata, const SharedObject &p_so);
  132. private:
  133. struct SavedData {
  134. uint64_t ofs = 0;
  135. uint64_t size = 0;
  136. bool encrypted = false;
  137. Vector<uint8_t> md5;
  138. CharString path_utf8;
  139. bool operator<(const SavedData &p_data) const {
  140. return path_utf8 < p_data.path_utf8;
  141. }
  142. };
  143. struct PackData {
  144. FileAccess *f = nullptr;
  145. Vector<SavedData> file_ofs;
  146. EditorProgress *ep = nullptr;
  147. Vector<SharedObject> *so_files = nullptr;
  148. };
  149. struct ZipData {
  150. void *zip = nullptr;
  151. EditorProgress *ep = nullptr;
  152. };
  153. struct FeatureContainers {
  154. Set<String> features;
  155. Vector<String> features_pv;
  156. };
  157. void _export_find_resources(EditorFileSystemDirectory *p_dir, Set<String> &p_paths);
  158. void _export_find_dependencies(const String &p_path, Set<String> &p_paths);
  159. void gen_debug_flags(Vector<String> &r_flags, int p_flags);
  160. 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);
  161. 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);
  162. void _edit_files_with_filter(DirAccess *da, const Vector<String> &p_filters, Set<String> &r_list, bool exclude);
  163. void _edit_filter_list(Set<String> &r_list, const String &p_filter, bool exclude);
  164. static Error _add_shared_object(void *p_userdata, const SharedObject &p_so);
  165. protected:
  166. struct ExportNotifier {
  167. ExportNotifier(EditorExportPlatform &p_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags);
  168. ~ExportNotifier();
  169. };
  170. FeatureContainers get_feature_containers(const Ref<EditorExportPreset> &p_preset, bool p_debug);
  171. bool exists_export_template(String template_file_name, String *err) const;
  172. String find_export_template(String template_file_name, String *err = nullptr) const;
  173. void gen_export_flags(Vector<String> &r_flags, int p_flags);
  174. public:
  175. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) = 0;
  176. struct ExportOption {
  177. PropertyInfo option;
  178. Variant default_value;
  179. ExportOption(const PropertyInfo &p_info, const Variant &p_default) :
  180. option(p_info),
  181. default_value(p_default) {
  182. }
  183. ExportOption() {}
  184. };
  185. virtual Ref<EditorExportPreset> create_preset();
  186. virtual void get_export_options(List<ExportOption> *r_options) = 0;
  187. virtual bool should_update_export_options() { return false; }
  188. virtual bool get_export_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const { return true; }
  189. virtual String get_os_name() const = 0;
  190. virtual String get_name() const = 0;
  191. virtual Ref<Texture2D> get_logo() const = 0;
  192. Error export_project_files(const Ref<EditorExportPreset> &p_preset, bool p_debug, EditorExportSaveFunction p_func, void *p_udata, EditorExportSaveSharedObject p_so_func = nullptr);
  193. Error save_pack(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);
  194. Error save_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  195. virtual bool poll_export() { return false; }
  196. virtual int get_options_count() const { return 0; }
  197. virtual String get_options_tooltip() const { return ""; }
  198. virtual Ref<ImageTexture> get_option_icon(int p_index) const;
  199. virtual String get_option_label(int p_device) const { return ""; }
  200. virtual String get_option_tooltip(int p_device) const { return ""; }
  201. enum DebugFlags {
  202. DEBUG_FLAG_DUMB_CLIENT = 1,
  203. DEBUG_FLAG_REMOTE_DEBUG = 2,
  204. DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST = 4,
  205. DEBUG_FLAG_VIEW_COLLISONS = 8,
  206. DEBUG_FLAG_VIEW_NAVIGATION = 16,
  207. };
  208. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) { return OK; }
  209. virtual Ref<Texture2D> get_run_icon() const { return get_logo(); }
  210. String test_etc2() const;
  211. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const = 0;
  212. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const = 0;
  213. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) = 0;
  214. virtual Error export_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  215. virtual Error export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  216. virtual void get_platform_features(List<String> *r_features) = 0;
  217. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) = 0;
  218. virtual String get_debug_protocol() const { return "tcp://"; }
  219. EditorExportPlatform();
  220. };
  221. class EditorExportPlugin : public RefCounted {
  222. GDCLASS(EditorExportPlugin, RefCounted);
  223. friend class EditorExportPlatform;
  224. Ref<EditorExportPreset> export_preset;
  225. Vector<SharedObject> shared_objects;
  226. struct ExtraFile {
  227. String path;
  228. Vector<uint8_t> data;
  229. bool remap = false;
  230. };
  231. Vector<ExtraFile> extra_files;
  232. bool skipped = false;
  233. Vector<String> ios_frameworks;
  234. Vector<String> ios_embedded_frameworks;
  235. Vector<String> ios_project_static_libs;
  236. String ios_plist_content;
  237. String ios_linker_flags;
  238. Vector<String> ios_bundle_files;
  239. String ios_cpp_code;
  240. Vector<String> osx_plugin_files;
  241. _FORCE_INLINE_ void _clear() {
  242. shared_objects.clear();
  243. extra_files.clear();
  244. skipped = false;
  245. }
  246. _FORCE_INLINE_ void _export_end() {
  247. ios_frameworks.clear();
  248. ios_embedded_frameworks.clear();
  249. ios_bundle_files.clear();
  250. ios_plist_content = "";
  251. ios_linker_flags = "";
  252. ios_cpp_code = "";
  253. osx_plugin_files.clear();
  254. }
  255. void _export_file_script(const String &p_path, const String &p_type, const Vector<String> &p_features);
  256. void _export_begin_script(const Vector<String> &p_features, bool p_debug, const String &p_path, int p_flags);
  257. void _export_end_script();
  258. protected:
  259. void set_export_preset(const Ref<EditorExportPreset> &p_preset);
  260. Ref<EditorExportPreset> get_export_preset() const;
  261. void add_file(const String &p_path, const Vector<uint8_t> &p_file, bool p_remap);
  262. void add_shared_object(const String &p_path, const Vector<String> &tags, const String &p_target = String());
  263. void add_ios_framework(const String &p_path);
  264. void add_ios_embedded_framework(const String &p_path);
  265. void add_ios_project_static_lib(const String &p_path);
  266. void add_ios_plist_content(const String &p_plist_content);
  267. void add_ios_linker_flags(const String &p_flags);
  268. void add_ios_bundle_file(const String &p_path);
  269. void add_ios_cpp_code(const String &p_code);
  270. void add_osx_plugin_file(const String &p_path);
  271. void skip();
  272. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features);
  273. virtual void _export_begin(const Set<String> &p_features, bool p_debug, const String &p_path, int p_flags);
  274. static void _bind_methods();
  275. GDVIRTUAL3(_export_file, String, String, Vector<String>)
  276. GDVIRTUAL4(_export_begin, Vector<String>, bool, String, uint32_t)
  277. GDVIRTUAL0(_export_end)
  278. public:
  279. Vector<String> get_ios_frameworks() const;
  280. Vector<String> get_ios_embedded_frameworks() const;
  281. Vector<String> get_ios_project_static_libs() const;
  282. String get_ios_plist_content() const;
  283. String get_ios_linker_flags() const;
  284. Vector<String> get_ios_bundle_files() const;
  285. String get_ios_cpp_code() const;
  286. const Vector<String> &get_osx_plugin_files() const;
  287. EditorExportPlugin();
  288. };
  289. class EditorExport : public Node {
  290. GDCLASS(EditorExport, Node);
  291. Vector<Ref<EditorExportPlatform>> export_platforms;
  292. Vector<Ref<EditorExportPreset>> export_presets;
  293. Vector<Ref<EditorExportPlugin>> export_plugins;
  294. StringName _export_presets_updated;
  295. Timer *save_timer = nullptr;
  296. bool block_save = false;
  297. static EditorExport *singleton;
  298. void _save();
  299. protected:
  300. friend class EditorExportPreset;
  301. void save_presets();
  302. void _notification(int p_what);
  303. static void _bind_methods();
  304. public:
  305. static EditorExport *get_singleton() { return singleton; }
  306. void add_export_platform(const Ref<EditorExportPlatform> &p_platform);
  307. int get_export_platform_count();
  308. Ref<EditorExportPlatform> get_export_platform(int p_idx);
  309. void add_export_preset(const Ref<EditorExportPreset> &p_preset, int p_at_pos = -1);
  310. int get_export_preset_count() const;
  311. Ref<EditorExportPreset> get_export_preset(int p_idx);
  312. void remove_export_preset(int p_idx);
  313. void add_export_plugin(const Ref<EditorExportPlugin> &p_plugin);
  314. void remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin);
  315. Vector<Ref<EditorExportPlugin>> get_export_plugins();
  316. void load_config();
  317. void update_export_presets();
  318. bool poll_export_platforms();
  319. EditorExport();
  320. ~EditorExport();
  321. };
  322. class EditorExportPlatformPC : public EditorExportPlatform {
  323. GDCLASS(EditorExportPlatformPC, EditorExportPlatform);
  324. private:
  325. Ref<ImageTexture> logo;
  326. String name;
  327. String os_name;
  328. int chmod_flags = -1;
  329. public:
  330. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) override;
  331. virtual void get_export_options(List<ExportOption> *r_options) override;
  332. virtual String get_name() const override;
  333. virtual String get_os_name() const override;
  334. virtual Ref<Texture2D> get_logo() const override;
  335. virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override;
  336. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override;
  337. virtual Error sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  338. virtual String get_template_file_name(const String &p_target, const String &p_arch) const = 0;
  339. void set_name(const String &p_name);
  340. void set_os_name(const String &p_name);
  341. void set_logo(const Ref<Texture2D> &p_logo);
  342. void add_platform_feature(const String &p_feature);
  343. virtual void get_platform_features(List<String> *r_features) override;
  344. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) override;
  345. int get_chmod_flags() const;
  346. void set_chmod_flags(int p_flags);
  347. virtual Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) const {
  348. return Error::OK;
  349. }
  350. };
  351. class EditorExportTextSceneToBinaryPlugin : public EditorExportPlugin {
  352. GDCLASS(EditorExportTextSceneToBinaryPlugin, EditorExportPlugin);
  353. public:
  354. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features) override;
  355. EditorExportTextSceneToBinaryPlugin();
  356. };
  357. #endif // EDITOR_IMPORT_EXPORT_H