gradle_export_util.cpp 14 KB

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