gradle_export_util.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/string/translation_server.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 simplified_path = EditorExportPlatform::simplify_path(p_path);
  165. Vector<uint8_t> enc_data;
  166. EditorExportPlatform::SavedData sd;
  167. Error err = _store_temp_file(simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, enc_data, sd);
  168. if (err != OK) {
  169. return err;
  170. }
  171. const String dst_path = export_data->assets_directory + String("/") + simplified_path.trim_prefix("res://");
  172. print_verbose("Saving project files from " + simplified_path + " into " + dst_path);
  173. err = store_file_at_path(dst_path, enc_data);
  174. export_data->pd.file_ofs.push_back(sd);
  175. return err;
  176. }
  177. String _android_xml_escape(const String &p_string) {
  178. // Android XML requires strings to be both valid XML (`xml_escape()`) but also
  179. // to escape characters which are valid XML but have special meaning in Android XML.
  180. // https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
  181. // Note: Didn't handle U+XXXX unicode chars, could be done if needed.
  182. return p_string
  183. .replace("@", "\\@")
  184. .replace("?", "\\?")
  185. .replace("'", "\\'")
  186. .replace("\"", "\\\"")
  187. .replace("\n", "\\n")
  188. .replace("\t", "\\t")
  189. .xml_escape(false);
  190. }
  191. // Creates strings.xml files inside the gradle project for different locales.
  192. 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) {
  193. print_verbose("Creating strings resources for supported locales for project " + p_project_name);
  194. // Stores the string into the default values directory.
  195. String processed_default_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(p_project_name));
  196. store_string_at_path(p_gradle_build_dir.path_join("res/values/godot_project_name_string.xml"), processed_default_xml_string);
  197. // Searches the Gradle project res/ directory to find all supported locales
  198. Ref<DirAccess> da = DirAccess::open(p_gradle_build_dir.path_join("res"));
  199. if (da.is_null()) {
  200. if (OS::get_singleton()->is_stdout_verbose()) {
  201. print_error("Unable to open Android resources directory.");
  202. }
  203. return ERR_CANT_OPEN;
  204. }
  205. // Setup a temporary translation domain to translate the project name.
  206. const StringName domain_name = "godot.project_name_localization";
  207. Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(domain_name);
  208. TranslationServer::get_singleton()->load_project_translations(domain);
  209. da->list_dir_begin();
  210. while (true) {
  211. String file = da->get_next();
  212. if (file.is_empty()) {
  213. break;
  214. }
  215. if (!file.begins_with("values-")) {
  216. // NOTE: This assumes all directories that start with "values-" are for localization.
  217. continue;
  218. }
  219. String locale = file.replace("values-", "").replace("-r", "_");
  220. String locale_directory = p_gradle_build_dir.path_join("res/" + file + "/godot_project_name_string.xml");
  221. String locale_project_name;
  222. if (p_appnames.is_empty()) {
  223. domain->set_locale_override(locale);
  224. locale_project_name = domain->translate(p_project_name, String());
  225. } else {
  226. locale_project_name = p_appnames.get(locale, p_project_name);
  227. }
  228. if (locale_project_name != p_project_name) {
  229. String processed_xml_string = vformat(GODOT_PROJECT_NAME_XML_STRING, _android_xml_escape(locale_project_name));
  230. print_verbose("Storing project name for locale " + locale + " under " + locale_directory);
  231. store_string_at_path(locale_directory, processed_xml_string);
  232. } else {
  233. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  234. store_string_at_path(locale_directory, processed_default_xml_string);
  235. }
  236. }
  237. da->list_dir_end();
  238. TranslationServer::get_singleton()->remove_domain(domain_name);
  239. return OK;
  240. }
  241. String bool_to_string(bool v) {
  242. return v ? "true" : "false";
  243. }
  244. String _get_gles_tag() {
  245. return " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n";
  246. }
  247. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  248. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  249. String sizes[] = { "small", "normal", "large", "xlarge" };
  250. constexpr size_t num_sizes = std_size(sizes);
  251. for (size_t i = 0; i < num_sizes; i++) {
  252. String feature_name = vformat("screen/support_%s", sizes[i]);
  253. String feature_support = bool_to_string(p_preset->get(feature_name));
  254. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  255. manifest_screen_sizes += xml_entry;
  256. }
  257. manifest_screen_sizes += " />\n";
  258. return manifest_screen_sizes;
  259. }
  260. String _get_activity_tag(const Ref<EditorExportPlatform> &p_export_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug) {
  261. String orientation = _get_android_orientation_label(DisplayServer::ScreenOrientation(int(p_export_platform->get_project_setting(p_preset, "display/window/handheld/orientation"))));
  262. String manifest_activity_text = vformat(
  263. " <activity android:name=\".GodotApp\" "
  264. "tools:replace=\"android:screenOrientation,android:excludeFromRecents,android:resizeableActivity\" "
  265. "tools:node=\"mergeOnlyAttributes\" "
  266. "android:excludeFromRecents=\"%s\" "
  267. "android:screenOrientation=\"%s\" "
  268. "android:resizeableActivity=\"%s\">\n",
  269. bool_to_string(p_preset->get("package/exclude_from_recents")),
  270. orientation,
  271. bool_to_string(bool(p_export_platform->get_project_setting(p_preset, "display/window/size/resizable"))));
  272. manifest_activity_text += " <intent-filter>\n"
  273. " <action android:name=\"android.intent.action.MAIN\" />\n"
  274. " <category android:name=\"android.intent.category.DEFAULT\" />\n";
  275. bool show_in_app_library = p_preset->get("package/show_in_app_library");
  276. if (show_in_app_library) {
  277. manifest_activity_text += " <category android:name=\"android.intent.category.LAUNCHER\" />\n";
  278. }
  279. bool uses_leanback_category = p_preset->get("package/show_in_android_tv");
  280. if (uses_leanback_category) {
  281. manifest_activity_text += " <category android:name=\"android.intent.category.LEANBACK_LAUNCHER\" />\n";
  282. }
  283. bool uses_home_category = p_preset->get("package/show_as_launcher_app");
  284. if (uses_home_category) {
  285. manifest_activity_text += " <category android:name=\"android.intent.category.HOME\" />\n";
  286. }
  287. manifest_activity_text += " </intent-filter>\n";
  288. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  289. for (int i = 0; i < export_plugins.size(); i++) {
  290. if (export_plugins[i]->supports_platform(p_export_platform)) {
  291. const String contents = export_plugins[i]->get_android_manifest_activity_element_contents(p_export_platform, p_debug);
  292. if (!contents.is_empty()) {
  293. manifest_activity_text += contents;
  294. manifest_activity_text += "\n";
  295. }
  296. }
  297. }
  298. manifest_activity_text += " </activity>\n";
  299. return manifest_activity_text;
  300. }
  301. 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) {
  302. int app_category_index = (int)(p_preset->get("package/app_category"));
  303. bool is_game = app_category_index == APP_CATEGORY_GAME;
  304. String manifest_application_text = vformat(
  305. " <application android:label=\"@string/godot_project_name_string\"\n"
  306. " android:allowBackup=\"%s\"\n"
  307. " android:icon=\"@mipmap/icon\"\n"
  308. " android:isGame=\"%s\"\n"
  309. " android:hasFragileUserData=\"%s\"\n"
  310. " android:requestLegacyExternalStorage=\"%s\"\n",
  311. bool_to_string(p_preset->get("user_data_backup/allow")),
  312. bool_to_string(is_game),
  313. bool_to_string(p_preset->get("package/retain_data_on_uninstall")),
  314. bool_to_string(p_has_read_write_storage_permission));
  315. if (app_category_index != APP_CATEGORY_UNDEFINED) {
  316. manifest_application_text += vformat(" android:appCategory=\"%s\"\n", _get_app_category_label(app_category_index));
  317. manifest_application_text += " tools:replace=\"android:allowBackup,android:appCategory,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  318. } else {
  319. manifest_application_text += " tools:remove=\"android:appCategory\"\n";
  320. manifest_application_text += " tools:replace=\"android:allowBackup,android:isGame,android:hasFragileUserData,android:requestLegacyExternalStorage\"\n";
  321. }
  322. manifest_application_text += " tools:ignore=\"GoogleAppIndexingWarning\">\n\n";
  323. for (int i = 0; i < p_metadata.size(); i++) {
  324. manifest_application_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"%s\" android:value=\"%s\" />\n", p_metadata[i].name, p_metadata[i].value);
  325. }
  326. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  327. for (int i = 0; i < export_plugins.size(); i++) {
  328. if (export_plugins[i]->supports_platform(p_export_platform)) {
  329. const String contents = export_plugins[i]->get_android_manifest_application_element_contents(p_export_platform, p_debug);
  330. if (!contents.is_empty()) {
  331. manifest_application_text += contents;
  332. manifest_application_text += "\n";
  333. }
  334. }
  335. }
  336. manifest_application_text += _get_activity_tag(p_export_platform, p_preset, p_debug);
  337. manifest_application_text += " </application>\n";
  338. return manifest_application_text;
  339. }
  340. Error _store_temp_file(const String &p_simplified_path, const Vector<uint8_t> &p_data, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, Vector<uint8_t> &r_enc_data, EditorExportPlatform::SavedData &r_sd) {
  341. Error err = OK;
  342. Ref<FileAccess> ftmp = FileAccess::create_temp(FileAccess::WRITE_READ, "export", "tmp", false, &err);
  343. if (err != OK) {
  344. return err;
  345. }
  346. r_sd.path_utf8 = p_simplified_path.trim_prefix("res://").utf8();
  347. r_sd.ofs = 0;
  348. r_sd.size = p_data.size();
  349. err = EditorExportPlatform::_encrypt_and_store_data(ftmp, p_simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, r_sd.encrypted);
  350. if (err != OK) {
  351. return err;
  352. }
  353. r_enc_data.resize(ftmp->get_length());
  354. ftmp->seek(0);
  355. ftmp->get_buffer(r_enc_data.ptrw(), r_enc_data.size());
  356. ftmp.unref();
  357. // Store MD5 of original file.
  358. {
  359. unsigned char hash[16];
  360. CryptoCore::md5(p_data.ptr(), p_data.size(), hash);
  361. r_sd.md5.resize(16);
  362. for (int i = 0; i < 16; i++) {
  363. r_sd.md5.write[i] = hash[i];
  364. }
  365. }
  366. return OK;
  367. }