export_plugin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**************************************************************************/
  2. /* export_plugin.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. #ifndef DISABLE_DEPRECATED
  32. #include "godot_plugin_config.h"
  33. #endif // DISABLE_DEPRECATED
  34. #include "gradle_export_util.h"
  35. #include "core/io/image.h"
  36. #include "core/io/zip_io.h"
  37. #include "core/os/os.h"
  38. #include "editor/export/editor_export_platform.h"
  39. class ImageTexture;
  40. // Optional environment variables for defining confidential information. If any
  41. // of these is set, they will override the values set in the credentials file.
  42. const String ENV_ANDROID_KEYSTORE_DEBUG_PATH = "GODOT_ANDROID_KEYSTORE_DEBUG_PATH";
  43. const String ENV_ANDROID_KEYSTORE_DEBUG_USER = "GODOT_ANDROID_KEYSTORE_DEBUG_USER";
  44. const String ENV_ANDROID_KEYSTORE_DEBUG_PASS = "GODOT_ANDROID_KEYSTORE_DEBUG_PASSWORD";
  45. const String ENV_ANDROID_KEYSTORE_RELEASE_PATH = "GODOT_ANDROID_KEYSTORE_RELEASE_PATH";
  46. const String ENV_ANDROID_KEYSTORE_RELEASE_USER = "GODOT_ANDROID_KEYSTORE_RELEASE_USER";
  47. const String ENV_ANDROID_KEYSTORE_RELEASE_PASS = "GODOT_ANDROID_KEYSTORE_RELEASE_PASSWORD";
  48. const String DEFAULT_ANDROID_KEYSTORE_DEBUG_USER = "androiddebugkey";
  49. const String DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD = "android";
  50. struct LauncherIcon {
  51. const char *export_path;
  52. int dimensions = 0;
  53. };
  54. class AndroidEditorGradleRunner;
  55. class EditorExportPlatformAndroid : public EditorExportPlatform {
  56. GDCLASS(EditorExportPlatformAndroid, EditorExportPlatform);
  57. Ref<ImageTexture> logo;
  58. Ref<ImageTexture> run_icon;
  59. struct Device {
  60. String id;
  61. String name;
  62. String description;
  63. int api_level = 0;
  64. String architecture;
  65. };
  66. struct APKExportData {
  67. EditorExportPlatform::PackData pd;
  68. zipFile apk;
  69. EditorProgress *ep = nullptr;
  70. };
  71. struct FeatureInfo {
  72. String name;
  73. bool required;
  74. String version;
  75. };
  76. #ifndef DISABLE_DEPRECATED
  77. mutable Vector<PluginConfigAndroid> android_plugins;
  78. mutable SafeFlag android_plugins_changed;
  79. Mutex android_plugins_lock;
  80. #endif // DISABLE_DEPRECATED
  81. String last_plugin_names;
  82. uint64_t last_gradle_build_time = 0;
  83. String last_gradle_build_dir;
  84. #ifndef ANDROID_ENABLED
  85. bool use_scrcpy = false;
  86. Vector<Device> devices;
  87. SafeFlag devices_changed;
  88. Mutex device_lock;
  89. Thread check_for_changes_thread;
  90. SafeFlag quit_request;
  91. SafeFlag has_runnable_preset;
  92. static void _check_for_changes_poll_thread(void *ud);
  93. void _update_preset_status();
  94. #else // ANDROID_ENABLED
  95. AndroidEditorGradleRunner *android_editor_gradle_runner = nullptr;
  96. #endif // ANDROID_ENABLED
  97. String get_project_name(const Ref<EditorExportPreset> &p_preset, const String &p_name) const;
  98. String get_package_name(const Ref<EditorExportPreset> &p_preset, const String &p_package) const;
  99. String get_valid_basename(const Ref<EditorExportPreset> &p_preset) const;
  100. String get_assets_directory(const Ref<EditorExportPreset> &p_preset, int p_export_format) const;
  101. bool is_package_name_valid(const Ref<EditorExportPreset> &p_preset, const String &p_package, String *r_error = nullptr) const;
  102. bool is_project_name_valid(const Ref<EditorExportPreset> &p_preset) const;
  103. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data);
  104. static zip_fileinfo get_zip_fileinfo();
  105. struct ABI {
  106. String abi;
  107. String arch;
  108. bool operator==(const ABI &p_a) const {
  109. return p_a.abi == abi;
  110. }
  111. ABI(const String &p_abi, const String &p_arch) {
  112. abi = p_abi;
  113. arch = p_arch;
  114. }
  115. ABI() {}
  116. };
  117. static Vector<ABI> get_abis();
  118. #ifndef DISABLE_DEPRECATED
  119. /// List the gdap files in the directory specified by the p_path parameter.
  120. static Vector<String> list_gdap_files(const String &p_path);
  121. static Vector<PluginConfigAndroid> get_plugins();
  122. static Vector<PluginConfigAndroid> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets);
  123. #endif // DISABLE_DEPRECATED
  124. static Error store_in_apk(APKExportData *ed, const String &p_path, const Vector<uint8_t> &p_data, int compression_method = Z_DEFLATED);
  125. static Error save_apk_so(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so);
  126. static Error save_apk_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);
  127. static Error ignore_apk_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);
  128. static Error copy_gradle_so(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so);
  129. bool _has_read_write_storage_permission(const Vector<String> &p_permissions);
  130. bool _has_manage_external_storage_permission(const Vector<String> &p_permissions);
  131. void _get_manifest_info(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, Vector<String> &r_permissions, Vector<FeatureInfo> &r_features, Vector<MetadataInfo> &r_metadata);
  132. void _write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug);
  133. bool _is_transparency_allowed(const Ref<EditorExportPreset> &p_preset) const;
  134. void _fix_themes_xml(const Ref<EditorExportPreset> &p_preset);
  135. void _fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet);
  136. static String _get_keystore_path(const Ref<EditorExportPreset> &p_preset, bool p_debug);
  137. static String _parse_string(const uint8_t *p_bytes, bool p_utf8);
  138. void _fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &r_manifest);
  139. void _process_launcher_icons(const String &p_file_name, const Ref<Image> &p_source_image, int dimension, Vector<uint8_t> &p_data);
  140. void load_icon_refs(const Ref<EditorExportPreset> &p_preset, Ref<Image> &icon, Ref<Image> &foreground, Ref<Image> &background, Ref<Image> &monochrome);
  141. void _copy_icons_to_gradle_project(const Ref<EditorExportPreset> &p_preset,
  142. const Ref<Image> &p_main_image,
  143. const Ref<Image> &p_foreground,
  144. const Ref<Image> &p_background,
  145. const Ref<Image> &p_monochrome);
  146. static void _create_editor_debug_keystore_if_needed();
  147. static Vector<ABI> get_enabled_abis(const Ref<EditorExportPreset> &p_preset);
  148. bool _uses_vulkan(const Ref<EditorExportPreset> &p_preset) const;
  149. Error _generate_sparse_pck_metadata(const Ref<EditorExportPreset> &p_preset, PackData &p_pack_data, Vector<uint8_t> &r_data);
  150. protected:
  151. void _notification(int p_what);
  152. public:
  153. 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);
  154. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
  155. virtual void get_export_options(List<ExportOption> *r_options) const override;
  156. virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const override;
  157. virtual String get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const override;
  158. virtual String get_name() const override;
  159. virtual String get_os_name() const override;
  160. virtual Ref<Texture2D> get_logo() const override;
  161. virtual bool should_update_export_options() override;
  162. #ifndef ANDROID_ENABLED
  163. virtual bool poll_export() override;
  164. virtual int get_options_count() const override;
  165. virtual Ref<Texture2D> get_option_icon(int p_index) const override;
  166. virtual bool is_option_runnable(int p_index) const override { return p_index != 0; }
  167. virtual String get_options_tooltip() const override;
  168. virtual String get_option_label(int p_index) const override;
  169. virtual String get_option_tooltip(int p_index) const override;
  170. virtual String get_device_architecture(int p_index) const override;
  171. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) override;
  172. #endif // ANDROID_ENABLED
  173. virtual Ref<Texture2D> get_run_icon() const override;
  174. static String get_adb_path();
  175. static String get_apksigner_path(int p_target_sdk = -1, bool p_check_executes = false);
  176. static String get_java_path();
  177. static String get_keytool_path();
  178. virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const override;
  179. virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
  180. static bool has_valid_username_and_password(const Ref<EditorExportPreset> &p_preset, String &r_error);
  181. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override;
  182. String _get_deprecated_plugins_names(const Ref<EditorExportPreset> &p_preset) const;
  183. String _get_plugins_names(const Ref<EditorExportPreset> &p_preset) const;
  184. String _resolve_export_plugin_android_library_path(const String &p_android_library_path) const;
  185. bool _is_clean_build_required(const Ref<EditorExportPreset> &p_preset);
  186. String get_apk_expansion_fullpath(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  187. Error save_apk_expansion_file(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path);
  188. void get_command_line_flags(const Ref<EditorExportPreset> &p_preset, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags, Vector<uint8_t> &r_command_line_flags);
  189. Error sign_apk(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &export_path, EditorProgress &ep);
  190. void _clear_assets_directory(const Ref<EditorExportPreset> &p_preset);
  191. void _remove_copied_libs(String p_gdextension_libs_path);
  192. static String join_list(const List<String> &p_parts, const String &p_separator);
  193. static String join_abis(const Vector<ABI> &p_parts, const String &p_separator, bool p_use_arch);
  194. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags = 0) override;
  195. Error export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, BitField<EditorExportPlatform::DebugFlags> p_flags);
  196. virtual void get_platform_features(List<String> *r_features) const override;
  197. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override;
  198. virtual void initialize() override;
  199. ~EditorExportPlatformAndroid();
  200. };