2
0

export_plugin.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef ANDROID_EXPORT_PLUGIN_H
  31. #define ANDROID_EXPORT_PLUGIN_H
  32. #include "core/io/image_loader.h"
  33. #include "core/io/json.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/io/zip_io.h"
  36. #include "core/os/dir_access.h"
  37. #include "core/os/file_access.h"
  38. #include "core/os/os.h"
  39. #include "core/project_settings.h"
  40. #include "core/safe_refcount.h"
  41. #include "core/version.h"
  42. #include "drivers/png/png_driver_common.h"
  43. #include "editor/editor_export.h"
  44. #include "editor/editor_log.h"
  45. #include "editor/editor_node.h"
  46. #include "editor/editor_settings.h"
  47. #include "main/splash.gen.h"
  48. #include "platform/android/logo.gen.h"
  49. #include "platform/android/run_icon.gen.h"
  50. #include "godot_plugin_config.h"
  51. #include "gradle_export_util.h"
  52. #include <string.h>
  53. const String SPLASH_CONFIG_XML_CONTENT = R"SPLASH(<?xml version="1.0" encoding="utf-8"?>
  54. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  55. <item android:drawable="@drawable/splash_bg_color" />
  56. <item>
  57. <bitmap
  58. android:gravity="center"
  59. android:filter="%s"
  60. android:src="@drawable/splash" />
  61. </item>
  62. </layer-list>
  63. )SPLASH";
  64. struct LauncherIcon {
  65. const char *export_path;
  66. int dimensions;
  67. };
  68. class EditorExportPlatformAndroid : public EditorExportPlatform {
  69. GDCLASS(EditorExportPlatformAndroid, EditorExportPlatform);
  70. Ref<ImageTexture> logo;
  71. Ref<ImageTexture> run_icon;
  72. struct Device {
  73. String id;
  74. String name;
  75. String description;
  76. int api_level;
  77. };
  78. struct APKExportData {
  79. zipFile apk;
  80. EditorProgress *ep;
  81. };
  82. Vector<PluginConfigAndroid> plugins;
  83. String last_plugin_names;
  84. uint64_t last_custom_build_time = 0;
  85. SafeFlag plugins_changed;
  86. Mutex plugins_lock;
  87. Vector<Device> devices;
  88. SafeFlag devices_changed;
  89. Mutex device_lock;
  90. #ifndef ANDROID_ENABLED
  91. Thread check_for_changes_thread;
  92. SafeFlag quit_request;
  93. static void _check_for_changes_poll_thread(void *ud);
  94. #endif
  95. String get_project_name(const String &p_name) const;
  96. String get_package_name(const String &p_package) const;
  97. String get_assets_directory(const Ref<EditorExportPreset> &p_preset, int p_export_format) const;
  98. bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const;
  99. static bool _should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data);
  100. static zip_fileinfo get_zip_fileinfo();
  101. static Vector<String> get_abis();
  102. /// List the gdap files in the directory specified by the p_path parameter.
  103. static Vector<String> list_gdap_files(const String &p_path);
  104. static Vector<PluginConfigAndroid> get_plugins();
  105. static Vector<PluginConfigAndroid> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets);
  106. static Error store_in_apk(APKExportData *ed, const String &p_path, const Vector<uint8_t> &p_data, int compression_method = Z_DEFLATED);
  107. static Error save_apk_so(void *p_userdata, const SharedObject &p_so);
  108. static Error save_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total);
  109. static Error ignore_apk_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total);
  110. static Error copy_gradle_so(void *p_userdata, const SharedObject &p_so);
  111. bool _has_read_write_storage_permission(const Vector<String> &p_permissions);
  112. bool _has_manage_external_storage_permission(const Vector<String> &p_permissions);
  113. void _get_permissions(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, Vector<String> &r_permissions);
  114. void _write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug);
  115. void _fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet);
  116. static String _parse_string(const uint8_t *p_bytes, bool p_utf8);
  117. void _fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &r_manifest);
  118. void _load_image_data(const Ref<Image> &p_splash_image, Vector<uint8_t> &p_data);
  119. void _process_launcher_icons(const String &p_file_name, const Ref<Image> &p_source_image, int dimension, Vector<uint8_t> &p_data);
  120. String load_splash_refs(Ref<Image> &splash_image, Ref<Image> &splash_bg_color_image);
  121. void load_icon_refs(const Ref<EditorExportPreset> &p_preset, Ref<Image> &icon, Ref<Image> &foreground, Ref<Image> &background);
  122. void store_image(const LauncherIcon launcher_icon, const Vector<uint8_t> &data);
  123. void store_image(const String &export_path, const Vector<uint8_t> &data);
  124. void _copy_icons_to_gradle_project(const Ref<EditorExportPreset> &p_preset,
  125. const String &processed_splash_config_xml,
  126. const Ref<Image> &splash_image,
  127. const Ref<Image> &splash_bg_color_image,
  128. const Ref<Image> &main_image,
  129. const Ref<Image> &foreground,
  130. const Ref<Image> &background);
  131. static Vector<String> get_enabled_abis(const Ref<EditorExportPreset> &p_preset);
  132. public:
  133. typedef Error (*EditorExportSaveFunction)(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total);
  134. public:
  135. virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);
  136. virtual void get_export_options(List<ExportOption> *r_options);
  137. virtual String get_name() const;
  138. virtual String get_os_name() const;
  139. virtual Ref<Texture> get_logo() const;
  140. virtual bool should_update_export_options();
  141. virtual bool poll_export();
  142. virtual int get_options_count() const;
  143. virtual String get_options_tooltip() const;
  144. virtual String get_option_label(int p_index) const;
  145. virtual String get_option_tooltip(int p_index) const;
  146. virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags);
  147. virtual Ref<Texture> get_run_icon() const;
  148. static String get_adb_path();
  149. static String get_apksigner_path();
  150. static String get_java_path();
  151. virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
  152. virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const;
  153. virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const;
  154. void _update_custom_build_project();
  155. inline bool is_clean_build_required(Vector<PluginConfigAndroid> enabled_plugins) {
  156. String plugin_names = PluginConfigAndroid::get_plugins_names(enabled_plugins);
  157. bool first_build = last_custom_build_time == 0;
  158. bool have_plugins_changed = false;
  159. if (!first_build) {
  160. have_plugins_changed = plugin_names != last_plugin_names;
  161. if (!have_plugins_changed) {
  162. for (int i = 0; i < enabled_plugins.size(); i++) {
  163. if (enabled_plugins.get(i).last_updated > last_custom_build_time) {
  164. have_plugins_changed = true;
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. last_custom_build_time = OS::get_singleton()->get_unix_time();
  171. last_plugin_names = plugin_names;
  172. return have_plugins_changed || first_build;
  173. }
  174. String get_apk_expansion_fullpath(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  175. Error save_apk_expansion_file(const Ref<EditorExportPreset> &p_preset, const String &p_path);
  176. void get_command_line_flags(const Ref<EditorExportPreset> &p_preset, const String &p_path, int p_flags, Vector<uint8_t> &r_command_line_flags);
  177. Error sign_apk(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &export_path, EditorProgress &ep);
  178. void _clear_assets_directory();
  179. void _remove_copied_libs();
  180. String join_list(List<String> parts, const String &separator) const;
  181. virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
  182. Error export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, int p_flags);
  183. virtual void get_platform_features(List<String> *r_features);
  184. virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features);
  185. EditorExportPlatformAndroid();
  186. ~EditorExportPlatformAndroid();
  187. };
  188. #endif // ANDROID_EXPORT_PLUGIN_H