gradle_export_util.cpp 17 KB

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