gradle_export_util.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. const String path = ResourceUID::ensure_path(p_path);
  165. const String dst_path = path.replace_first("res://", export_data->assets_directory + "/");
  166. print_verbose("Saving project files from " + path + " into " + dst_path);
  167. Error err = store_file_at_path(dst_path, p_data);
  168. return err;
  169. }
  170. String _android_xml_escape(const String &p_string) {
  171. // Android XML requires strings to be both valid XML (`xml_escape()`) but also
  172. // to escape characters which are valid XML but have special meaning in Android XML.
  173. // https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
  174. // Note: Didn't handle U+XXXX unicode chars, could be done if needed.
  175. return p_string
  176. .replace("@", "\\@")
  177. .replace("?", "\\?")
  178. .replace("'", "\\'")
  179. .replace("\"", "\\\"")
  180. .replace("\n", "\\n")
  181. .replace("\t", "\\t")
  182. .xml_escape(false);
  183. }
  184. // Creates strings.xml files inside the gradle project for different locales.
  185. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &project_name, const String &p_gradle_build_dir) {
  186. print_verbose("Creating strings resources for supported locales for project " + project_name);
  187. // Stores the string into the default values directory.
  188. String processed_default_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(project_name));
  189. store_string_at_path(p_gradle_build_dir.path_join("res/values/godot_project_name_string.xml"), processed_default_xml_string);
  190. // Searches the Gradle project res/ directory to find all supported locales
  191. Ref<DirAccess> da = DirAccess::open(p_gradle_build_dir.path_join("res"));
  192. if (da.is_null()) {
  193. if (OS::get_singleton()->is_stdout_verbose()) {
  194. print_error("Unable to open Android resources directory.");
  195. }
  196. return ERR_CANT_OPEN;
  197. }
  198. da->list_dir_begin();
  199. Dictionary appnames = GLOBAL_GET("application/config/name_localized");
  200. while (true) {
  201. String file = da->get_next();
  202. if (file.is_empty()) {
  203. break;
  204. }
  205. if (!file.begins_with("values-")) {
  206. // NOTE: This assumes all directories that start with "values-" are for localization.
  207. continue;
  208. }
  209. String locale = file.replace("values-", "").replace("-r", "_");
  210. String locale_directory = p_gradle_build_dir.path_join("res/" + file + "/godot_project_name_string.xml");
  211. if (appnames.has(locale)) {
  212. String locale_project_name = appnames[locale];
  213. String processed_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(locale_project_name));
  214. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  215. store_string_at_path(locale_directory, processed_xml_string);
  216. } else {
  217. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  218. store_string_at_path(locale_directory, processed_default_xml_string);
  219. }
  220. }
  221. da->list_dir_end();
  222. return OK;
  223. }
  224. String bool_to_string(bool v) {
  225. return v ? "true" : "false";
  226. }
  227. String _get_gles_tag() {
  228. return " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n";
  229. }
  230. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  231. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  232. String sizes[] = { "small", "normal", "large", "xlarge" };
  233. size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  234. for (size_t i = 0; i < num_sizes; i++) {
  235. String feature_name = vformat("screen/support_%s", sizes[i]);
  236. String feature_support = bool_to_string(p_preset->get(feature_name));
  237. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  238. manifest_screen_sizes += xml_entry;
  239. }
  240. manifest_screen_sizes += " />\n";
  241. return manifest_screen_sizes;
  242. }
  243. String _get_activity_tag(const Ref<EditorExportPlatform> &p_export_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug) {
  244. String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(GLOBAL_GET("display/window/handheld/orientation"))));
  245. String manifest_activity_text = vformat(
  246. " <activity android:name=\".GodotApp\" "
  247. "tools:replace=\"android:screenOrientation,android:excludeFromRecents,android:resizeableActivity\" "
  248. "tools:node=\"mergeOnlyAttributes\" "
  249. "android:excludeFromRecents=\"%s\" "
  250. "android:screenOrientation=\"%s\" "
  251. "android:resizeableActivity=\"%s\">\n",
  252. bool_to_string(p_preset->get("package/exclude_from_recents")),
  253. orientation,
  254. bool_to_string(bool(GLOBAL_GET("display/window/size/resizable"))));
  255. manifest_activity_text += " <intent-filter>\n"
  256. " <action android:name=\"android.intent.action.MAIN\" />\n"
  257. " <category android:name=\"android.intent.category.DEFAULT\" />\n";
  258. bool show_in_app_library = p_preset->get("package/show_in_app_library");
  259. if (show_in_app_library) {
  260. manifest_activity_text += " <category android:name=\"android.intent.category.LAUNCHER\" />\n";
  261. }
  262. bool uses_leanback_category = p_preset->get("package/show_in_android_tv");
  263. if (uses_leanback_category) {
  264. manifest_activity_text += " <category android:name=\"android.intent.category.LEANBACK_LAUNCHER\" />\n";
  265. }
  266. bool uses_home_category = p_preset->get("package/show_as_launcher_app");
  267. if (uses_home_category) {
  268. manifest_activity_text += " <category android:name=\"android.intent.category.HOME\" />\n";
  269. }
  270. manifest_activity_text += " </intent-filter>\n";
  271. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  272. for (int i = 0; i < export_plugins.size(); i++) {
  273. if (export_plugins[i]->supports_platform(p_export_platform)) {
  274. const String contents = export_plugins[i]->get_android_manifest_activity_element_contents(p_export_platform, p_debug);
  275. if (!contents.is_empty()) {
  276. manifest_activity_text += contents;
  277. manifest_activity_text += "\n";
  278. }
  279. }
  280. }
  281. manifest_activity_text += " </activity>\n";
  282. return manifest_activity_text;
  283. }
  284. 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) {
  285. int app_category_index = (int)(p_preset->get("package/app_category"));
  286. bool is_game = app_category_index == APP_CATEGORY_GAME;
  287. String manifest_application_text = vformat(
  288. " <application android:label=\"@string/godot_project_name_string\"\n"
  289. " android:allowBackup=\"%s\"\n"
  290. " android:icon=\"@mipmap/icon\"\n"
  291. " android:isGame=\"%s\"\n"
  292. " android:hasFragileUserData=\"%s\"\n"
  293. " android:requestLegacyExternalStorage=\"%s\"\n",
  294. bool_to_string(p_preset->get("user_data_backup/allow")),
  295. bool_to_string(is_game),
  296. bool_to_string(p_preset->get("package/retain_data_on_uninstall")),
  297. bool_to_string(p_has_read_write_storage_permission));
  298. if (app_category_index != APP_CATEGORY_UNDEFINED) {
  299. manifest_application_text += vformat(" android:appCategory=\"%s\"\n", _get_app_category_label(app_category_index));
  300. manifest_application_text += " tools:replace=\"android:allowBackup,android:appCategory,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  301. } else {
  302. manifest_application_text += " tools:remove=\"android:appCategory\"\n";
  303. manifest_application_text += " tools:replace=\"android:allowBackup,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  304. }
  305. manifest_application_text += " tools:ignore=\"GoogleAppIndexingWarning\">\n\n";
  306. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  307. for (int i = 0; i < export_plugins.size(); i++) {
  308. if (export_plugins[i]->supports_platform(p_export_platform)) {
  309. const String contents = export_plugins[i]->get_android_manifest_application_element_contents(p_export_platform, p_debug);
  310. if (!contents.is_empty()) {
  311. manifest_application_text += contents;
  312. manifest_application_text += "\n";
  313. }
  314. }
  315. }
  316. manifest_application_text += _get_activity_tag(p_export_platform, p_preset, p_debug);
  317. manifest_application_text += " </application>\n";
  318. return manifest_application_text;
  319. }