gradle_export_util.cpp 14 KB

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