gradle_export_util.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_UNDEFINED:
  87. return -1;
  88. case APP_CATEGORY_VIDEO:
  89. return 2;
  90. case APP_CATEGORY_GAME:
  91. default:
  92. return 0;
  93. }
  94. }
  95. String _get_app_category_label(int category_index) {
  96. switch (category_index) {
  97. case APP_CATEGORY_ACCESSIBILITY:
  98. return "accessibility";
  99. case APP_CATEGORY_AUDIO:
  100. return "audio";
  101. case APP_CATEGORY_IMAGE:
  102. return "image";
  103. case APP_CATEGORY_MAPS:
  104. return "maps";
  105. case APP_CATEGORY_NEWS:
  106. return "news";
  107. case APP_CATEGORY_PRODUCTIVITY:
  108. return "productivity";
  109. case APP_CATEGORY_SOCIAL:
  110. return "social";
  111. case APP_CATEGORY_VIDEO:
  112. return "video";
  113. case APP_CATEGORY_GAME:
  114. default:
  115. return "game";
  116. }
  117. }
  118. // Utility method used to create a directory.
  119. Error create_directory(const String &p_dir) {
  120. if (!DirAccess::exists(p_dir)) {
  121. Ref<DirAccess> filesystem_da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  122. ERR_FAIL_COND_V_MSG(filesystem_da.is_null(), ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  123. Error err = filesystem_da->make_dir_recursive(p_dir);
  124. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  125. }
  126. return OK;
  127. }
  128. // Writes p_data into a file at p_path, creating directories if necessary.
  129. // Note: this will overwrite the file at p_path if it already exists.
  130. Error store_file_at_path(const String &p_path, const Vector<uint8_t> &p_data) {
  131. String dir = p_path.get_base_dir();
  132. Error err = create_directory(dir);
  133. if (err != OK) {
  134. return err;
  135. }
  136. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  137. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  138. fa->store_buffer(p_data.ptr(), p_data.size());
  139. return OK;
  140. }
  141. // Writes string p_data into a file at p_path, creating directories if necessary.
  142. // Note: this will overwrite the file at p_path if it already exists.
  143. Error store_string_at_path(const String &p_path, const String &p_data) {
  144. String dir = p_path.get_base_dir();
  145. Error err = create_directory(dir);
  146. if (err != OK) {
  147. if (OS::get_singleton()->is_stdout_verbose()) {
  148. print_error("Unable to write data into " + p_path);
  149. }
  150. return err;
  151. }
  152. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  153. ERR_FAIL_COND_V_MSG(fa.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  154. fa->store_string(p_data);
  155. return OK;
  156. }
  157. // Implementation of EditorExportSaveFunction.
  158. // This method will only be called as an input to export_project_files.
  159. // It is used by the export_project_files method to save all the asset files into the gradle project.
  160. // It's functionality mirrors that of the method save_apk_file.
  161. // This method will be called ONLY when gradle build is enabled.
  162. 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, uint64_t p_seed) {
  163. CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata);
  164. String path = p_path.simplify_path();
  165. if (path.begins_with("uid://")) {
  166. path = ResourceUID::uid_to_path(path).simplify_path();
  167. print_verbose(vformat(R"(UID referenced exported file name "%s" was replaced with "%s".)", p_path, path));
  168. }
  169. const String dst_path = path.replace_first("res://", export_data->assets_directory + "/");
  170. print_verbose("Saving project files from " + path + " into " + dst_path);
  171. Error err = store_file_at_path(dst_path, p_data);
  172. return err;
  173. }
  174. String _android_xml_escape(const String &p_string) {
  175. // Android XML requires strings to be both valid XML (`xml_escape()`) but also
  176. // to escape characters which are valid XML but have special meaning in Android XML.
  177. // https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
  178. // Note: Didn't handle U+XXXX unicode chars, could be done if needed.
  179. return p_string
  180. .replace("@", "\\@")
  181. .replace("?", "\\?")
  182. .replace("'", "\\'")
  183. .replace("\"", "\\\"")
  184. .replace("\n", "\\n")
  185. .replace("\t", "\\t")
  186. .xml_escape(false);
  187. }
  188. // Creates strings.xml files inside the gradle project for different locales.
  189. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &p_project_name, const String &p_gradle_build_dir, const Dictionary &p_appnames) {
  190. print_verbose("Creating strings resources for supported locales for project " + p_project_name);
  191. // Stores the string into the default values directory.
  192. String processed_default_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(p_project_name));
  193. store_string_at_path(p_gradle_build_dir.path_join("res/values/godot_project_name_string.xml"), processed_default_xml_string);
  194. // Searches the Gradle project res/ directory to find all supported locales
  195. Ref<DirAccess> da = DirAccess::open(p_gradle_build_dir.path_join("res"));
  196. if (da.is_null()) {
  197. if (OS::get_singleton()->is_stdout_verbose()) {
  198. print_error("Unable to open Android resources directory.");
  199. }
  200. return ERR_CANT_OPEN;
  201. }
  202. da->list_dir_begin();
  203. while (true) {
  204. String file = da->get_next();
  205. if (file.is_empty()) {
  206. break;
  207. }
  208. if (!file.begins_with("values-")) {
  209. // NOTE: This assumes all directories that start with "values-" are for localization.
  210. continue;
  211. }
  212. String locale = file.replace("values-", "").replace("-r", "_");
  213. String locale_directory = p_gradle_build_dir.path_join("res/" + file + "/godot_project_name_string.xml");
  214. if (p_appnames.has(locale)) {
  215. String locale_project_name = p_appnames[locale];
  216. String processed_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(locale_project_name));
  217. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  218. store_string_at_path(locale_directory, processed_xml_string);
  219. } else {
  220. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  221. store_string_at_path(locale_directory, processed_default_xml_string);
  222. }
  223. }
  224. da->list_dir_end();
  225. return OK;
  226. }
  227. String bool_to_string(bool v) {
  228. return v ? "true" : "false";
  229. }
  230. String _get_gles_tag() {
  231. return " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n";
  232. }
  233. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  234. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  235. String sizes[] = { "small", "normal", "large", "xlarge" };
  236. constexpr size_t num_sizes = std::size(sizes);
  237. for (size_t i = 0; i < num_sizes; i++) {
  238. String feature_name = vformat("screen/support_%s", sizes[i]);
  239. String feature_support = bool_to_string(p_preset->get(feature_name));
  240. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  241. manifest_screen_sizes += xml_entry;
  242. }
  243. manifest_screen_sizes += " />\n";
  244. return manifest_screen_sizes;
  245. }
  246. String _get_activity_tag(const Ref<EditorExportPlatform> &p_export_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug) {
  247. String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(p_export_platform->get_project_setting(p_preset, "display/window/handheld/orientation"))));
  248. String manifest_activity_text = vformat(
  249. " <activity android:name=\".GodotApp\" "
  250. "tools:replace=\"android:screenOrientation,android:excludeFromRecents,android:resizeableActivity\" "
  251. "tools:node=\"mergeOnlyAttributes\" "
  252. "android:excludeFromRecents=\"%s\" "
  253. "android:screenOrientation=\"%s\" "
  254. "android:resizeableActivity=\"%s\">\n",
  255. bool_to_string(p_preset->get("package/exclude_from_recents")),
  256. orientation,
  257. bool_to_string(bool(p_export_platform->get_project_setting(p_preset, "display/window/size/resizable"))));
  258. manifest_activity_text += " <intent-filter>\n"
  259. " <action android:name=\"android.intent.action.MAIN\" />\n"
  260. " <category android:name=\"android.intent.category.DEFAULT\" />\n";
  261. bool show_in_app_library = p_preset->get("package/show_in_app_library");
  262. if (show_in_app_library) {
  263. manifest_activity_text += " <category android:name=\"android.intent.category.LAUNCHER\" />\n";
  264. }
  265. bool uses_leanback_category = p_preset->get("package/show_in_android_tv");
  266. if (uses_leanback_category) {
  267. manifest_activity_text += " <category android:name=\"android.intent.category.LEANBACK_LAUNCHER\" />\n";
  268. }
  269. bool uses_home_category = p_preset->get("package/show_as_launcher_app");
  270. if (uses_home_category) {
  271. manifest_activity_text += " <category android:name=\"android.intent.category.HOME\" />\n";
  272. }
  273. manifest_activity_text += " </intent-filter>\n";
  274. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  275. for (int i = 0; i < export_plugins.size(); i++) {
  276. if (export_plugins[i]->supports_platform(p_export_platform)) {
  277. const String contents = export_plugins[i]->get_android_manifest_activity_element_contents(p_export_platform, p_debug);
  278. if (!contents.is_empty()) {
  279. manifest_activity_text += contents;
  280. manifest_activity_text += "\n";
  281. }
  282. }
  283. }
  284. manifest_activity_text += " </activity>\n";
  285. return manifest_activity_text;
  286. }
  287. String _get_application_tag(const Ref<EditorExportPlatform> &p_export_platform, const Ref<EditorExportPreset> &p_preset, bool p_has_read_write_storage_permission, bool p_debug, const Vector<MetadataInfo> &p_metadata) {
  288. int app_category_index = (int)(p_preset->get("package/app_category"));
  289. bool is_game = app_category_index == APP_CATEGORY_GAME;
  290. String manifest_application_text = vformat(
  291. " <application android:label=\"@string/godot_project_name_string\"\n"
  292. " android:allowBackup=\"%s\"\n"
  293. " android:icon=\"@mipmap/icon\"\n"
  294. " android:isGame=\"%s\"\n"
  295. " android:hasFragileUserData=\"%s\"\n"
  296. " android:requestLegacyExternalStorage=\"%s\"\n",
  297. bool_to_string(p_preset->get("user_data_backup/allow")),
  298. bool_to_string(is_game),
  299. bool_to_string(p_preset->get("package/retain_data_on_uninstall")),
  300. bool_to_string(p_has_read_write_storage_permission));
  301. if (app_category_index != APP_CATEGORY_UNDEFINED) {
  302. manifest_application_text += vformat(" android:appCategory=\"%s\"\n", _get_app_category_label(app_category_index));
  303. manifest_application_text += " tools:replace=\"android:allowBackup,android:appCategory,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  304. } else {
  305. manifest_application_text += " tools:remove=\"android:appCategory\"\n";
  306. manifest_application_text += " tools:replace=\"android:allowBackup,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  307. }
  308. manifest_application_text += " tools:ignore=\"GoogleAppIndexingWarning\">\n\n";
  309. for (int i = 0; i < p_metadata.size(); i++) {
  310. manifest_application_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"%s\" android:value=\"%s\" />\n", p_metadata[i].name, p_metadata[i].value);
  311. }
  312. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  313. for (int i = 0; i < export_plugins.size(); i++) {
  314. if (export_plugins[i]->supports_platform(p_export_platform)) {
  315. const String contents = export_plugins[i]->get_android_manifest_application_element_contents(p_export_platform, p_debug);
  316. if (!contents.is_empty()) {
  317. manifest_application_text += contents;
  318. manifest_application_text += "\n";
  319. }
  320. }
  321. }
  322. manifest_application_text += _get_activity_tag(p_export_platform, p_preset, p_debug);
  323. manifest_application_text += " </application>\n";
  324. return manifest_application_text;
  325. }