gradle_export_util.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* gradle_export_util.cpp */
  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. #include "gradle_export_util.h"
  31. #include "core/config/project_settings.h"
  32. int _get_android_orientation_value(DisplayServer::ScreenOrientation screen_orientation) {
  33. switch (screen_orientation) {
  34. case DisplayServer::SCREEN_PORTRAIT:
  35. return 1;
  36. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  37. return 8;
  38. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  39. return 9;
  40. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  41. return 11;
  42. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  43. return 12;
  44. case DisplayServer::SCREEN_SENSOR:
  45. return 13;
  46. case DisplayServer::SCREEN_LANDSCAPE:
  47. default:
  48. return 0;
  49. }
  50. }
  51. String _get_android_orientation_label(DisplayServer::ScreenOrientation screen_orientation) {
  52. switch (screen_orientation) {
  53. case DisplayServer::SCREEN_PORTRAIT:
  54. return "portrait";
  55. case DisplayServer::SCREEN_REVERSE_LANDSCAPE:
  56. return "reverseLandscape";
  57. case DisplayServer::SCREEN_REVERSE_PORTRAIT:
  58. return "reversePortrait";
  59. case DisplayServer::SCREEN_SENSOR_LANDSCAPE:
  60. return "userLandscape";
  61. case DisplayServer::SCREEN_SENSOR_PORTRAIT:
  62. return "userPortrait";
  63. case DisplayServer::SCREEN_SENSOR:
  64. return "fullUser";
  65. case DisplayServer::SCREEN_LANDSCAPE:
  66. default:
  67. return "landscape";
  68. }
  69. }
  70. // Utility method used to create a directory.
  71. Error create_directory(const String &p_dir) {
  72. if (!DirAccess::exists(p_dir)) {
  73. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  74. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  75. Error err = filesystem_da->make_dir_recursive(p_dir);
  76. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  77. }
  78. return OK;
  79. }
  80. // Writes p_data into a file at p_path, creating directories if necessary.
  81. // Note: this will overwrite the file at p_path if it already exists.
  82. Error store_file_at_path(const String &p_path, const Vector<uint8_t> &p_data) {
  83. String dir = p_path.get_base_dir();
  84. Error err = create_directory(dir);
  85. if (err != OK) {
  86. return err;
  87. }
  88. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  89. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  90. fa->store_buffer(p_data.ptr(), p_data.size());
  91. return OK;
  92. }
  93. // Writes string p_data into a file at p_path, creating directories if necessary.
  94. // Note: this will overwrite the file at p_path if it already exists.
  95. Error store_string_at_path(const String &p_path, const String &p_data) {
  96. String dir = p_path.get_base_dir();
  97. Error err = create_directory(dir);
  98. if (err != OK) {
  99. if (OS::get_singleton()->is_stdout_verbose()) {
  100. print_error("Unable to write data into " + p_path);
  101. }
  102. return err;
  103. }
  104. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  105. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  106. fa->store_string(p_data);
  107. return OK;
  108. }
  109. // Implementation of EditorExportSaveFunction.
  110. // This method will only be called as an input to export_project_files.
  111. // It is used by the export_project_files method to save all the asset files into the gradle project.
  112. // It's functionality mirrors that of the method save_apk_file.
  113. // This method will be called ONLY when custom build is enabled.
  114. 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) {
  115. CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata);
  116. String dst_path = p_path.replace_first("res://", export_data->assets_directory + "/");
  117. print_verbose("Saving project files from " + p_path + " into " + dst_path);
  118. Error err = store_file_at_path(dst_path, p_data);
  119. return err;
  120. }
  121. String _android_xml_escape(const String &p_string) {
  122. // Android XML requires strings to be both valid XML (`xml_escape()`) but also
  123. // to escape characters which are valid XML but have special meaning in Android XML.
  124. // https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
  125. // Note: Didn't handle U+XXXX unicode chars, could be done if needed.
  126. return p_string
  127. .replace("@", "\\@")
  128. .replace("?", "\\?")
  129. .replace("'", "\\'")
  130. .replace("\"", "\\\"")
  131. .replace("\n", "\\n")
  132. .replace("\t", "\\t")
  133. .xml_escape(false);
  134. }
  135. // Creates strings.xml files inside the gradle project for different locales.
  136. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &project_name) {
  137. print_verbose("Creating strings resources for supported locales for project " + project_name);
  138. // Stores the string into the default values directory.
  139. String processed_default_xml_string = vformat(godot_project_name_xml_string, _android_xml_escape(project_name));
  140. store_string_at_path("res://android/build/res/values/godot_project_name_string.xml", processed_default_xml_string);
  141. // Searches the Gradle project res/ directory to find all supported locales
  142. Ref<DirAccess> da = DirAccess::open("res://android/build/res");
  143. if (da.is_null()) {
  144. if (OS::get_singleton()->is_stdout_verbose()) {
  145. print_error("Unable to open Android resources directory.");
  146. }
  147. return ERR_CANT_OPEN;
  148. }
  149. da->list_dir_begin();
  150. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  151. while (true) {
  152. String file = da->get_next();
  153. if (file.is_empty()) {
  154. break;
  155. }
  156. if (!file.begins_with("values-")) {
  157. // NOTE: This assumes all directories that start with "values-" are for localization.
  158. continue;
  159. }
  160. String locale = file.replace("values-", "").replace("-r", "_");
  161. String locale_directory = "res://android/build/res/" + file + "/godot_project_name_string.xml";
  162. if (appnames.has(locale)) {
  163. String locale_project_name = appnames[locale];
  164. String processed_xml_string = vformat(godot_project_name_xml_string, _android_xml_escape(locale_project_name));
  165. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  166. store_string_at_path(locale_directory, processed_xml_string);
  167. } else {
  168. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  169. store_string_at_path(locale_directory, processed_default_xml_string);
  170. }
  171. }
  172. da->list_dir_end();
  173. return OK;
  174. }
  175. String bool_to_string(bool v) {
  176. return v ? "true" : "false";
  177. }
  178. String _get_gles_tag() {
  179. return " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n";
  180. }
  181. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  182. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  183. String sizes[] = { "small", "normal", "large", "xlarge" };
  184. size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  185. for (size_t i = 0; i < num_sizes; i++) {
  186. String feature_name = vformat("screen/support_%s", sizes[i]);
  187. String feature_support = bool_to_string(p_preset->get(feature_name));
  188. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  189. manifest_screen_sizes += xml_entry;
  190. }
  191. manifest_screen_sizes += " />\n";
  192. return manifest_screen_sizes;
  193. }
  194. String _get_xr_features_tag(const Ref<EditorExportPreset> &p_preset) {
  195. String manifest_xr_features;
  196. int xr_mode_index = (int)(p_preset->get("xr_features/xr_mode"));
  197. bool uses_xr = xr_mode_index == XR_MODE_OPENXR;
  198. if (uses_xr) {
  199. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"true\" android:version=\"1\" />\n";
  200. int hand_tracking_index = p_preset->get("xr_features/hand_tracking"); // 0: none, 1: optional, 2: required
  201. if (hand_tracking_index == XR_HAND_TRACKING_OPTIONAL) {
  202. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"false\" />\n";
  203. } else if (hand_tracking_index == XR_HAND_TRACKING_REQUIRED) {
  204. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"true\" />\n";
  205. }
  206. int passthrough_mode = p_preset->get("xr_features/passthrough");
  207. if (passthrough_mode == XR_PASSTHROUGH_OPTIONAL) {
  208. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"com.oculus.feature.PASSTHROUGH\" android:required=\"false\" />\n";
  209. } else if (passthrough_mode == XR_PASSTHROUGH_REQUIRED) {
  210. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"com.oculus.feature.PASSTHROUGH\" android:required=\"true\" />\n";
  211. }
  212. }
  213. return manifest_xr_features;
  214. }
  215. String _get_activity_tag(const Ref<EditorExportPreset> &p_preset) {
  216. int xr_mode_index = (int)(p_preset->get("xr_features/xr_mode"));
  217. bool uses_xr = xr_mode_index == XR_MODE_OPENXR;
  218. String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation"))));
  219. String manifest_activity_text = vformat(
  220. " <activity android:name=\"com.godot.game.GodotApp\" "
  221. "tools:replace=\"android:screenOrientation,android:excludeFromRecents,android:resizeableActivity\" "
  222. "android:excludeFromRecents=\"%s\" "
  223. "android:screenOrientation=\"%s\" "
  224. "android:resizeableActivity=\"%s\">\n",
  225. bool_to_string(p_preset->get("package/exclude_from_recents")),
  226. orientation,
  227. bool_to_string(bool(GLOBAL_GET("display/window/size/resizable"))));
  228. if (uses_xr) {
  229. manifest_activity_text += " <meta-data tools:node=\"replace\" android:name=\"com.oculus.vr.focusaware\" android:value=\"true\" />\n";
  230. } else {
  231. manifest_activity_text += " <meta-data tools:node=\"remove\" android:name=\"com.oculus.vr.focusaware\" />\n";
  232. }
  233. manifest_activity_text += " </activity>\n";
  234. return manifest_activity_text;
  235. }
  236. String _get_application_tag(const Ref<EditorExportPreset> &p_preset, bool p_has_read_write_storage_permission) {
  237. int xr_mode_index = (int)(p_preset->get("xr_features/xr_mode"));
  238. bool uses_xr = xr_mode_index == XR_MODE_OPENXR;
  239. String manifest_application_text = vformat(
  240. " <application android:label=\"@string/godot_project_name_string\"\n"
  241. " android:allowBackup=\"%s\"\n"
  242. " android:icon=\"@mipmap/icon\"\n"
  243. " android:isGame=\"%s\"\n"
  244. " android:hasFragileUserData=\"%s\"\n"
  245. " android:requestLegacyExternalStorage=\"%s\"\n"
  246. " tools:replace=\"android:allowBackup,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n"
  247. " tools:ignore=\"GoogleAppIndexingWarning\">\n\n"
  248. " <meta-data tools:node=\"remove\" android:name=\"xr_hand_tracking_version_name\" />\n"
  249. " <meta-data tools:node=\"remove\" android:name=\"xr_hand_tracking_metadata_name\" />\n",
  250. bool_to_string(p_preset->get("user_data_backup/allow")),
  251. bool_to_string(p_preset->get("package/classify_as_game")),
  252. bool_to_string(p_preset->get("package/retain_data_on_uninstall")),
  253. bool_to_string(p_has_read_write_storage_permission));
  254. if (uses_xr) {
  255. bool hand_tracking_enabled = (int)(p_preset->get("xr_features/hand_tracking")) > XR_HAND_TRACKING_NONE;
  256. if (hand_tracking_enabled) {
  257. int hand_tracking_frequency_index = p_preset->get("xr_features/hand_tracking_frequency");
  258. String hand_tracking_frequency = hand_tracking_frequency_index == XR_HAND_TRACKING_FREQUENCY_LOW ? "LOW" : "HIGH";
  259. manifest_application_text += vformat(
  260. " <meta-data tools:node=\"replace\" android:name=\"com.oculus.handtracking.frequency\" android:value=\"%s\" />\n",
  261. hand_tracking_frequency);
  262. manifest_application_text += " <meta-data tools:node=\"replace\" android:name=\"com.oculus.handtracking.version\" android:value=\"V2.0\" />\n";
  263. }
  264. } else {
  265. manifest_application_text += " <meta-data tools:node=\"remove\" android:name=\"com.oculus.supportedDevices\" />\n";
  266. }
  267. manifest_application_text += _get_activity_tag(p_preset);
  268. manifest_application_text += " </application>\n";
  269. return manifest_application_text;
  270. }