gradle_export_util.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**************************************************************************/
  2. /* gradle_export_util.cpp */
  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. #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. int _get_app_category_value(int category_index) {
  71. switch (category_index) {
  72. case APP_CATEGORY_ACCESSIBILITY:
  73. return 8;
  74. case APP_CATEGORY_AUDIO:
  75. return 1;
  76. case APP_CATEGORY_IMAGE:
  77. return 3;
  78. case APP_CATEGORY_MAPS:
  79. return 6;
  80. case APP_CATEGORY_NEWS:
  81. return 5;
  82. case APP_CATEGORY_PRODUCTIVITY:
  83. return 7;
  84. case APP_CATEGORY_SOCIAL:
  85. return 4;
  86. case APP_CATEGORY_VIDEO:
  87. return 2;
  88. case APP_CATEGORY_GAME:
  89. default:
  90. return 0;
  91. }
  92. }
  93. String _get_app_category_label(int category_index) {
  94. switch (category_index) {
  95. case APP_CATEGORY_ACCESSIBILITY:
  96. return "accessibility";
  97. case APP_CATEGORY_AUDIO:
  98. return "audio";
  99. case APP_CATEGORY_IMAGE:
  100. return "image";
  101. case APP_CATEGORY_MAPS:
  102. return "maps";
  103. case APP_CATEGORY_NEWS:
  104. return "news";
  105. case APP_CATEGORY_PRODUCTIVITY:
  106. return "productivity";
  107. case APP_CATEGORY_SOCIAL:
  108. return "social";
  109. case APP_CATEGORY_VIDEO:
  110. return "video";
  111. case APP_CATEGORY_GAME:
  112. default:
  113. return "game";
  114. }
  115. }
  116. // Utility method used to create a directory.
  117. Error create_directory(const String &p_dir) {
  118. if (!DirAccess::exists(p_dir)) {
  119. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  120. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  121. Error err = filesystem_da->make_dir_recursive(p_dir);
  122. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  123. }
  124. return OK;
  125. }
  126. // Writes p_data into a file at p_path, creating directories if necessary.
  127. // Note: this will overwrite the file at p_path if it already exists.
  128. Error store_file_at_path(const String &p_path, const Vector<uint8_t> &p_data) {
  129. String dir = p_path.get_base_dir();
  130. Error err = create_directory(dir);
  131. if (err != OK) {
  132. return err;
  133. }
  134. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  135. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  136. fa->store_buffer(p_data.ptr(), p_data.size());
  137. return OK;
  138. }
  139. // Writes string p_data into a file at p_path, creating directories if necessary.
  140. // Note: this will overwrite the file at p_path if it already exists.
  141. Error store_string_at_path(const String &p_path, const String &p_data) {
  142. String dir = p_path.get_base_dir();
  143. Error err = create_directory(dir);
  144. if (err != OK) {
  145. if (OS::get_singleton()->is_stdout_verbose()) {
  146. print_error("Unable to write data into " + p_path);
  147. }
  148. return err;
  149. }
  150. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  151. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  152. fa->store_string(p_data);
  153. return OK;
  154. }
  155. // Implementation of EditorExportSaveFunction.
  156. // This method will only be called as an input to export_project_files.
  157. // It is used by the export_project_files method to save all the asset files into the gradle project.
  158. // It's functionality mirrors that of the method save_apk_file.
  159. // This method will be called ONLY when gradle build is enabled.
  160. 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) {
  161. CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata);
  162. String dst_path = p_path.replace_first("res://", export_data->assets_directory + "/");
  163. print_verbose("Saving project files from " + p_path + " into " + dst_path);
  164. Error err = store_file_at_path(dst_path, p_data);
  165. return err;
  166. }
  167. String _android_xml_escape(const String &p_string) {
  168. // Android XML requires strings to be both valid XML (`xml_escape()`) but also
  169. // to escape characters which are valid XML but have special meaning in Android XML.
  170. // https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
  171. // Note: Didn't handle U+XXXX unicode chars, could be done if needed.
  172. return p_string
  173. .replace("@", "\\@")
  174. .replace("?", "\\?")
  175. .replace("'", "\\'")
  176. .replace("\"", "\\\"")
  177. .replace("\n", "\\n")
  178. .replace("\t", "\\t")
  179. .xml_escape(false);
  180. }
  181. // Creates strings.xml files inside the gradle project for different locales.
  182. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &project_name) {
  183. print_verbose("Creating strings resources for supported locales for project " + project_name);
  184. // Stores the string into the default values directory.
  185. String processed_default_xml_string = vformat(godot_project_name_xml_string, _android_xml_escape(project_name));
  186. store_string_at_path("res://android/build/res/values/godot_project_name_string.xml", processed_default_xml_string);
  187. // Searches the Gradle project res/ directory to find all supported locales
  188. Ref<DirAccess> da = DirAccess::open("res://android/build/res");
  189. if (da.is_null()) {
  190. if (OS::get_singleton()->is_stdout_verbose()) {
  191. print_error("Unable to open Android resources directory.");
  192. }
  193. return ERR_CANT_OPEN;
  194. }
  195. da->list_dir_begin();
  196. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  197. while (true) {
  198. String file = da->get_next();
  199. if (file.is_empty()) {
  200. break;
  201. }
  202. if (!file.begins_with("values-")) {
  203. // NOTE: This assumes all directories that start with "values-" are for localization.
  204. continue;
  205. }
  206. String locale = file.replace("values-", "").replace("-r", "_");
  207. String locale_directory = "res://android/build/res/" + file + "/godot_project_name_string.xml";
  208. if (appnames.has(locale)) {
  209. String locale_project_name = appnames[locale];
  210. String processed_xml_string = vformat(godot_project_name_xml_string, _android_xml_escape(locale_project_name));
  211. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  212. store_string_at_path(locale_directory, processed_xml_string);
  213. } else {
  214. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  215. store_string_at_path(locale_directory, processed_default_xml_string);
  216. }
  217. }
  218. da->list_dir_end();
  219. return OK;
  220. }
  221. String bool_to_string(bool v) {
  222. return v ? "true" : "false";
  223. }
  224. String _get_gles_tag() {
  225. return " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n";
  226. }
  227. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  228. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  229. String sizes[] = { "small", "normal", "large", "xlarge" };
  230. size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  231. for (size_t i = 0; i < num_sizes; i++) {
  232. String feature_name = vformat("screen/support_%s", sizes[i]);
  233. String feature_support = bool_to_string(p_preset->get(feature_name));
  234. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  235. manifest_screen_sizes += xml_entry;
  236. }
  237. manifest_screen_sizes += " />\n";
  238. return manifest_screen_sizes;
  239. }
  240. String _get_xr_features_tag(const Ref<EditorExportPreset> &p_preset, bool p_uses_vulkan) {
  241. String manifest_xr_features;
  242. int xr_mode_index = (int)(p_preset->get("xr_features/xr_mode"));
  243. bool uses_xr = xr_mode_index == XR_MODE_OPENXR;
  244. if (uses_xr) {
  245. int hand_tracking_index = p_preset->get("xr_features/hand_tracking"); // 0: none, 1: optional, 2: required
  246. if (hand_tracking_index == XR_HAND_TRACKING_OPTIONAL) {
  247. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"false\" />\n";
  248. } else if (hand_tracking_index == XR_HAND_TRACKING_REQUIRED) {
  249. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"true\" />\n";
  250. }
  251. int passthrough_mode = p_preset->get("xr_features/passthrough");
  252. if (passthrough_mode == XR_PASSTHROUGH_OPTIONAL) {
  253. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"com.oculus.feature.PASSTHROUGH\" android:required=\"false\" />\n";
  254. } else if (passthrough_mode == XR_PASSTHROUGH_REQUIRED) {
  255. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"com.oculus.feature.PASSTHROUGH\" android:required=\"true\" />\n";
  256. }
  257. }
  258. if (p_uses_vulkan) {
  259. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vulkan.level\" android:required=\"false\" android:version=\"1\" />\n";
  260. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vulkan.version\" android:required=\"true\" android:version=\"0x400003\" />\n";
  261. }
  262. return manifest_xr_features;
  263. }
  264. String _get_activity_tag(const Ref<EditorExportPreset> &p_preset, bool p_uses_xr) {
  265. String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation"))));
  266. String manifest_activity_text = vformat(
  267. " <activity android:name=\"com.godot.game.GodotApp\" "
  268. "tools:replace=\"android:screenOrientation,android:excludeFromRecents,android:resizeableActivity\" "
  269. "tools:node=\"mergeOnlyAttributes\" "
  270. "android:excludeFromRecents=\"%s\" "
  271. "android:screenOrientation=\"%s\" "
  272. "android:resizeableActivity=\"%s\">\n",
  273. bool_to_string(p_preset->get("package/exclude_from_recents")),
  274. orientation,
  275. bool_to_string(bool(GLOBAL_GET("display/window/size/resizable"))));
  276. if (p_uses_xr) {
  277. manifest_activity_text += " <intent-filter>\n"
  278. " <action android:name=\"android.intent.action.MAIN\" />\n"
  279. " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
  280. "\n"
  281. " <!-- Enable access to OpenXR on Oculus mobile devices, no-op on other Android\n"
  282. " platforms. -->\n"
  283. " <category android:name=\"com.oculus.intent.category.VR\" />\n"
  284. "\n"
  285. " <!-- OpenXR category tag to indicate the activity starts in an immersive OpenXR mode. \n"
  286. " See https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#android-runtime-category. -->\n"
  287. " <category android:name=\"org.khronos.openxr.intent.category.IMMERSIVE_HMD\" />\n"
  288. "\n"
  289. " <!-- Enable VR access on HTC Vive Focus devices. -->\n"
  290. " <category android:name=\"com.htc.intent.category.VRAPP\" />\n"
  291. " </intent-filter>\n";
  292. } else {
  293. manifest_activity_text += " <intent-filter>\n"
  294. " <action android:name=\"android.intent.action.MAIN\" />\n"
  295. " <category android:name=\"android.intent.category.LAUNCHER\" />\n"
  296. " </intent-filter>\n";
  297. }
  298. manifest_activity_text += " </activity>\n";
  299. return manifest_activity_text;
  300. }
  301. String _get_application_tag(const Ref<EditorExportPreset> &p_preset, bool p_has_read_write_storage_permission) {
  302. int app_category_index = (int)(p_preset->get("package/app_category"));
  303. bool is_game = app_category_index == APP_CATEGORY_GAME;
  304. int xr_mode_index = (int)(p_preset->get("xr_features/xr_mode"));
  305. bool uses_xr = xr_mode_index == XR_MODE_OPENXR;
  306. String manifest_application_text = vformat(
  307. " <application android:label=\"@string/godot_project_name_string\"\n"
  308. " android:allowBackup=\"%s\"\n"
  309. " android:icon=\"@mipmap/icon\"\n"
  310. " android:appCategory=\"%s\"\n"
  311. " android:isGame=\"%s\"\n"
  312. " android:hasFragileUserData=\"%s\"\n"
  313. " android:requestLegacyExternalStorage=\"%s\"\n"
  314. " tools:replace=\"android:allowBackup,android:appCategory,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n"
  315. " tools:ignore=\"GoogleAppIndexingWarning\">\n\n",
  316. bool_to_string(p_preset->get("user_data_backup/allow")),
  317. _get_app_category_label(app_category_index),
  318. bool_to_string(is_game),
  319. bool_to_string(p_preset->get("package/retain_data_on_uninstall")),
  320. bool_to_string(p_has_read_write_storage_permission));
  321. if (uses_xr) {
  322. bool hand_tracking_enabled = (int)(p_preset->get("xr_features/hand_tracking")) > XR_HAND_TRACKING_NONE;
  323. if (hand_tracking_enabled) {
  324. int hand_tracking_frequency_index = p_preset->get("xr_features/hand_tracking_frequency");
  325. String hand_tracking_frequency = hand_tracking_frequency_index == XR_HAND_TRACKING_FREQUENCY_LOW ? "LOW" : "HIGH";
  326. manifest_application_text += vformat(
  327. " <meta-data tools:node=\"replace\" android:name=\"com.oculus.handtracking.frequency\" android:value=\"%s\" />\n",
  328. hand_tracking_frequency);
  329. manifest_application_text += " <meta-data tools:node=\"replace\" android:name=\"com.oculus.handtracking.version\" android:value=\"V2.0\" />\n";
  330. }
  331. }
  332. manifest_application_text += _get_activity_tag(p_preset, uses_xr);
  333. manifest_application_text += " </application>\n";
  334. return manifest_application_text;
  335. }