gradle_export_util.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*************************************************************************/
  2. /* gradle_export_util.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 GODOT_GRADLE_EXPORT_UTIL_H
  31. #define GODOT_GRADLE_EXPORT_UTIL_H
  32. #include "core/io/zip_io.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/file_access.h"
  35. #include "core/os/os.h"
  36. #include "editor/editor_export.h"
  37. const String godot_project_name_xml_string = R"(<?xml version="1.0" encoding="utf-8"?>
  38. <!--WARNING: THIS FILE WILL BE OVERWRITTEN AT BUILD TIME-->
  39. <resources>
  40. <string name="godot_project_name_string">%s</string>
  41. </resources>
  42. )";
  43. DisplayServer::ScreenOrientation _get_screen_orientation() {
  44. String orientation_settings = ProjectSettings::get_singleton()->get("display/window/handheld/orientation");
  45. DisplayServer::ScreenOrientation screen_orientation;
  46. if (orientation_settings == "portrait")
  47. screen_orientation = DisplayServer::SCREEN_PORTRAIT;
  48. else if (orientation_settings == "reverse_landscape")
  49. screen_orientation = DisplayServer::SCREEN_REVERSE_LANDSCAPE;
  50. else if (orientation_settings == "reverse_portrait")
  51. screen_orientation = DisplayServer::SCREEN_REVERSE_PORTRAIT;
  52. else if (orientation_settings == "sensor_landscape")
  53. screen_orientation = DisplayServer::SCREEN_SENSOR_LANDSCAPE;
  54. else if (orientation_settings == "sensor_portrait")
  55. screen_orientation = DisplayServer::SCREEN_SENSOR_PORTRAIT;
  56. else if (orientation_settings == "sensor")
  57. screen_orientation = DisplayServer::SCREEN_SENSOR;
  58. else
  59. screen_orientation = DisplayServer::SCREEN_LANDSCAPE;
  60. return screen_orientation;
  61. }
  62. int _get_android_orientation_value(DisplayServer::ScreenOrientation screen_orientation) {
  63. switch (screen_orientation) {
  64. case DisplayServer::SCREEN_PORTRAIT:
  65. return 1;
  66. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  67. return 8;
  68. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  69. return 9;
  70. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  71. return 11;
  72. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  73. return 12;
  74. case DisplayServer::SCREEN_SENSOR:
  75. return 13;
  76. case DisplayServer::SCREEN_LANDSCAPE:
  77. default:
  78. return 0;
  79. }
  80. }
  81. String _get_android_orientation_label(DisplayServer::ScreenOrientation screen_orientation) {
  82. switch (screen_orientation) {
  83. case DisplayServer::SCREEN_PORTRAIT:
  84. return "portrait";
  85. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  86. return "reverseLandscape";
  87. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  88. return "reversePortrait";
  89. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  90. return "userLandscape";
  91. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  92. return "userPortrait";
  93. case DisplayServer::SCREEN_SENSOR:
  94. return "fullUser";
  95. case DisplayServer::SCREEN_LANDSCAPE:
  96. default:
  97. return "landscape";
  98. }
  99. }
  100. // Utility method used to create a directory.
  101. Error create_directory(const String &p_dir) {
  102. if (!DirAccess::exists(p_dir)) {
  103. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  104. ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  105. Error err = filesystem_da->make_dir_recursive(p_dir);
  106. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  107. memdelete(filesystem_da);
  108. }
  109. return OK;
  110. }
  111. // Implementation of EditorExportSaveSharedObject.
  112. // This method will only be called as an input to export_project_files.
  113. // This method lets the .so files for all ABIs to be copied
  114. // into the gradle project from the .AAR file
  115. Error ignore_so_file(void *p_userdata, const SharedObject &p_so) {
  116. return OK;
  117. }
  118. // Writes p_data into a file at p_path, creating directories if necessary.
  119. // Note: this will overwrite the file at p_path if it already exists.
  120. Error store_file_at_path(const String &p_path, const Vector<uint8_t> &p_data) {
  121. String dir = p_path.get_base_dir();
  122. Error err = create_directory(dir);
  123. if (err != OK) {
  124. return err;
  125. }
  126. FileAccess *fa = FileAccess::open(p_path, FileAccess::WRITE);
  127. ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  128. fa->store_buffer(p_data.ptr(), p_data.size());
  129. memdelete(fa);
  130. return OK;
  131. }
  132. // Writes string p_data into a file at p_path, creating directories if necessary.
  133. // Note: this will overwrite the file at p_path if it already exists.
  134. Error store_string_at_path(const String &p_path, const String &p_data) {
  135. String dir = p_path.get_base_dir();
  136. Error err = create_directory(dir);
  137. if (err != OK) {
  138. if (OS::get_singleton()->is_stdout_verbose()) {
  139. print_error("Unable to write data into " + p_path);
  140. }
  141. return err;
  142. }
  143. FileAccess *fa = FileAccess::open(p_path, FileAccess::WRITE);
  144. ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  145. fa->store_string(p_data);
  146. memdelete(fa);
  147. return OK;
  148. }
  149. // Implementation of EditorExportSaveFunction.
  150. // This method will only be called as an input to export_project_files.
  151. // It is used by the export_project_files method to save all the asset files into the gradle project.
  152. // It's functionality mirrors that of the method save_apk_file.
  153. // This method will be called ONLY when custom build is enabled.
  154. Error rename_and_store_file_in_gradle_project(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) {
  155. String dst_path = p_path.replace_first("res://", "res://android/build/assets/");
  156. print_verbose("Saving project files from " + p_path + " into " + dst_path);
  157. Error err = store_file_at_path(dst_path, p_data);
  158. return err;
  159. }
  160. // Creates strings.xml files inside the gradle project for different locales.
  161. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &project_name) {
  162. print_verbose("Creating strings resources for supported locales for project " + project_name);
  163. // Stores the string into the default values directory.
  164. String processed_default_xml_string = vformat(godot_project_name_xml_string, project_name.xml_escape(true));
  165. store_string_at_path("res://android/build/res/values/godot_project_name_string.xml", processed_default_xml_string);
  166. // Searches the Gradle project res/ directory to find all supported locales
  167. DirAccessRef da = DirAccess::open("res://android/build/res");
  168. if (!da) {
  169. if (OS::get_singleton()->is_stdout_verbose()) {
  170. print_error("Unable to open Android resources directory.");
  171. }
  172. return ERR_CANT_OPEN;
  173. }
  174. da->list_dir_begin();
  175. while (true) {
  176. String file = da->get_next();
  177. if (file == "") {
  178. break;
  179. }
  180. if (!file.begins_with("values-")) {
  181. // NOTE: This assumes all directories that start with "values-" are for localization.
  182. continue;
  183. }
  184. String locale = file.replace("values-", "").replace("-r", "_");
  185. String property_name = "application/config/name_" + locale;
  186. String locale_directory = "res://android/build/res/" + file + "/godot_project_name_string.xml";
  187. if (ProjectSettings::get_singleton()->has_setting(property_name)) {
  188. String locale_project_name = ProjectSettings::get_singleton()->get(property_name);
  189. String processed_xml_string = vformat(godot_project_name_xml_string, locale_project_name.xml_escape(true));
  190. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  191. store_string_at_path(locale_directory, processed_xml_string);
  192. } else {
  193. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  194. store_string_at_path(locale_directory, processed_default_xml_string);
  195. }
  196. }
  197. da->list_dir_end();
  198. return OK;
  199. }
  200. String bool_to_string(bool v) {
  201. return v ? "true" : "false";
  202. }
  203. String _get_gles_tag() {
  204. bool min_gles3 = ProjectSettings::get_singleton()->get("rendering/driver/driver_name") == "GLES3" &&
  205. !ProjectSettings::get_singleton()->get("rendering/driver/fallback_to_gles2");
  206. return min_gles3 ? " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n" : "";
  207. }
  208. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  209. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  210. String sizes[] = { "small", "normal", "large", "xlarge" };
  211. size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  212. for (size_t i = 0; i < num_sizes; i++) {
  213. String feature_name = vformat("screen/support_%s", sizes[i]);
  214. String feature_support = bool_to_string(p_preset->get(feature_name));
  215. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  216. manifest_screen_sizes += xml_entry;
  217. }
  218. manifest_screen_sizes += " />\n";
  219. return manifest_screen_sizes;
  220. }
  221. String _get_xr_features_tag(const Ref<EditorExportPreset> &p_preset) {
  222. String manifest_xr_features;
  223. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  224. if (uses_xr) {
  225. int dof_index = p_preset->get("xr_features/degrees_of_freedom"); // 0: none, 1: 3dof and 6dof, 2: 6dof
  226. if (dof_index == 1) {
  227. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"false\" android:version=\"1\" />\n";
  228. } else if (dof_index == 2) {
  229. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"true\" android:version=\"1\" />\n";
  230. }
  231. int hand_tracking_index = p_preset->get("xr_features/hand_tracking"); // 0: none, 1: optional, 2: required
  232. if (hand_tracking_index == 1) {
  233. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"false\" />\n";
  234. } else if (hand_tracking_index == 2) {
  235. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"true\" />\n";
  236. }
  237. }
  238. return manifest_xr_features;
  239. }
  240. String _get_instrumentation_tag(const Ref<EditorExportPreset> &p_preset) {
  241. String package_name = p_preset->get("package/unique_name");
  242. String manifest_instrumentation_text = vformat(
  243. " <instrumentation\n"
  244. " tools:node=\"replace\"\n"
  245. " android:name=\".GodotInstrumentation\"\n"
  246. " android:icon=\"@mipmap/icon\"\n"
  247. " android:label=\"@string/godot_project_name_string\"\n"
  248. " android:targetPackage=\"%s\" />\n",
  249. package_name);
  250. return manifest_instrumentation_text;
  251. }
  252. String _get_plugins_tag(const String &plugins_names) {
  253. if (!plugins_names.is_empty()) {
  254. return vformat(" <meta-data tools:node=\"replace\" android:name=\"plugins\" android:value=\"%s\" />\n", plugins_names);
  255. } else {
  256. return " <meta-data tools:node=\"remove\" android:name=\"plugins\" />\n";
  257. }
  258. }
  259. String _get_activity_tag(const Ref<EditorExportPreset> &p_preset) {
  260. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  261. String orientation = _get_android_orientation_label(_get_screen_orientation());
  262. String manifest_activity_text = vformat(
  263. " <activity android:name=\"com.godot.game.GodotApp\" "
  264. "tools:replace=\"android:screenOrientation\" "
  265. "android:screenOrientation=\"%s\">\n",
  266. orientation);
  267. if (uses_xr) {
  268. String focus_awareness = bool_to_string(p_preset->get("xr_features/focus_awareness"));
  269. manifest_activity_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"com.oculus.vr.focusaware\" android:value=\"%s\" />\n", focus_awareness);
  270. } else {
  271. manifest_activity_text += " <meta-data tools:node=\"remove\" android:name=\"com.oculus.vr.focusaware\" />\n";
  272. }
  273. manifest_activity_text += " </activity>\n";
  274. return manifest_activity_text;
  275. }
  276. String _get_application_tag(const Ref<EditorExportPreset> &p_preset, const String &plugins_names) {
  277. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  278. String manifest_application_text =
  279. " <application android:label=\"@string/godot_project_name_string\"\n"
  280. " android:allowBackup=\"false\" tools:ignore=\"GoogleAppIndexingWarning\"\n"
  281. " android:icon=\"@mipmap/icon\">\n\n"
  282. " <meta-data tools:node=\"remove\" android:name=\"xr_mode_metadata_name\" />\n";
  283. manifest_application_text += _get_plugins_tag(plugins_names);
  284. if (uses_xr) {
  285. manifest_application_text += " <meta-data tools:node=\"replace\" android:name=\"com.samsung.android.vr.application.mode\" android:value=\"vr_only\" />\n";
  286. }
  287. manifest_application_text += _get_activity_tag(p_preset);
  288. manifest_application_text += " </application>\n";
  289. return manifest_application_text;
  290. }
  291. #endif //GODOT_GRADLE_EXPORT_UTIL_H