2
0

gradle_export_util.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************/
  2. /* gradle_export_util.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef GODOT_GRADLE_EXPORT_UTIL_H
  31. #define GODOT_GRADLE_EXPORT_UTIL_H
  32. #include "core/io/zip_io.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/file_access.h"
  35. #include "core/os/os.h"
  36. #include "editor/editor_export.h"
  37. const String godot_project_name_xml_string = R"(<?xml version="1.0" encoding="utf-8"?>
  38. <!--WARNING: THIS FILE WILL BE OVERWRITTEN AT BUILD TIME-->
  39. <resources>
  40. <string name="godot_project_name_string">%s</string>
  41. </resources>
  42. )";
  43. // Utility method used to create a directory.
  44. Error create_directory(const String &p_dir) {
  45. if (!DirAccess::exists(p_dir)) {
  46. DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  47. ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  48. Error err = filesystem_da->make_dir_recursive(p_dir);
  49. ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Cannot create directory '" + p_dir + "'.");
  50. memdelete(filesystem_da);
  51. }
  52. return OK;
  53. }
  54. // Implementation of EditorExportSaveSharedObject.
  55. // This method will only be called as an input to export_project_files.
  56. // This method lets the .so files for all ABIs to be copied
  57. // into the gradle project from the .AAR file
  58. Error ignore_so_file(void *p_userdata, const SharedObject &p_so) {
  59. return OK;
  60. }
  61. // Writes p_data into a file at p_path, creating directories if necessary.
  62. // Note: this will overwrite the file at p_path if it already exists.
  63. Error store_file_at_path(const String &p_path, const Vector<uint8_t> &p_data) {
  64. String dir = p_path.get_base_dir();
  65. Error err = create_directory(dir);
  66. if (err != OK) {
  67. return err;
  68. }
  69. FileAccess *fa = FileAccess::open(p_path, FileAccess::WRITE);
  70. ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  71. fa->store_buffer(p_data.ptr(), p_data.size());
  72. memdelete(fa);
  73. return OK;
  74. }
  75. // Writes string p_data into a file at p_path, creating directories if necessary.
  76. // Note: this will overwrite the file at p_path if it already exists.
  77. Error store_string_at_path(const String &p_path, const String &p_data) {
  78. String dir = p_path.get_base_dir();
  79. Error err = create_directory(dir);
  80. if (err != OK) {
  81. return err;
  82. }
  83. FileAccess *fa = FileAccess::open(p_path, FileAccess::WRITE);
  84. ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'.");
  85. fa->store_string(p_data);
  86. memdelete(fa);
  87. return OK;
  88. }
  89. // Implementation of EditorExportSaveFunction.
  90. // This method will only be called as an input to export_project_files.
  91. // It is used by the export_project_files method to save all the asset files into the gradle project.
  92. // It's functionality mirrors that of the method save_apk_file.
  93. // This method will be called ONLY when custom build is enabled.
  94. 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) {
  95. String dst_path = p_path.replace_first("res://", "res://android/build/assets/");
  96. Error err = store_file_at_path(dst_path, p_data);
  97. return err;
  98. }
  99. // Creates strings.xml files inside the gradle project for different locales.
  100. Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset, const String &project_name) {
  101. // Stores the string into the default values directory.
  102. String processed_default_xml_string = vformat(godot_project_name_xml_string, project_name.xml_escape(true));
  103. store_string_at_path("res://android/build/res/values/godot_project_name_string.xml", processed_default_xml_string);
  104. // Searches the Gradle project res/ directory to find all supported locales
  105. DirAccessRef da = DirAccess::open("res://android/build/res");
  106. if (!da) {
  107. return ERR_CANT_OPEN;
  108. }
  109. da->list_dir_begin();
  110. while (true) {
  111. String file = da->get_next();
  112. if (file == "") {
  113. break;
  114. }
  115. if (!file.begins_with("values-")) {
  116. // NOTE: This assumes all directories that start with "values-" are for localization.
  117. continue;
  118. }
  119. String locale = file.replace("values-", "").replace("-r", "_");
  120. String property_name = "application/config/name_" + locale;
  121. String locale_directory = "res://android/build/res/" + file + "/godot_project_name_string.xml";
  122. if (ProjectSettings::get_singleton()->has_setting(property_name)) {
  123. String locale_project_name = ProjectSettings::get_singleton()->get(property_name);
  124. String processed_xml_string = vformat(godot_project_name_xml_string, locale_project_name.xml_escape(true));
  125. store_string_at_path(locale_directory, processed_xml_string);
  126. } else {
  127. // TODO: Once the legacy build system is deprecated we don't need to have xml files for this else branch
  128. store_string_at_path(locale_directory, processed_default_xml_string);
  129. }
  130. }
  131. da->list_dir_end();
  132. return OK;
  133. }
  134. String bool_to_string(bool v) {
  135. return v ? "true" : "false";
  136. }
  137. String _get_gles_tag() {
  138. bool min_gles3 = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES3" &&
  139. !ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2");
  140. return min_gles3 ? " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n" : "";
  141. }
  142. String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
  143. String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
  144. String sizes[] = { "small", "normal", "large", "xlarge" };
  145. size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
  146. for (size_t i = 0; i < num_sizes; i++) {
  147. String feature_name = vformat("screen/support_%s", sizes[i]);
  148. String feature_support = bool_to_string(p_preset->get(feature_name));
  149. String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
  150. manifest_screen_sizes += xml_entry;
  151. }
  152. manifest_screen_sizes += " />\n";
  153. return manifest_screen_sizes;
  154. }
  155. String _get_xr_features_tag(const Ref<EditorExportPreset> &p_preset) {
  156. String manifest_xr_features;
  157. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  158. if (uses_xr) {
  159. int dof_index = p_preset->get("xr_features/degrees_of_freedom"); // 0: none, 1: 3dof and 6dof, 2: 6dof
  160. if (dof_index == 1) {
  161. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"false\" android:version=\"1\" />\n";
  162. } else if (dof_index == 2) {
  163. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"true\" android:version=\"1\" />\n";
  164. }
  165. int hand_tracking_index = p_preset->get("xr_features/hand_tracking"); // 0: none, 1: optional, 2: required
  166. if (hand_tracking_index == 1) {
  167. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"false\" />\n";
  168. } else if (hand_tracking_index == 2) {
  169. manifest_xr_features += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"true\" />\n";
  170. }
  171. }
  172. return manifest_xr_features;
  173. }
  174. String _get_instrumentation_tag(const Ref<EditorExportPreset> &p_preset) {
  175. String package_name = p_preset->get("package/unique_name");
  176. String manifest_instrumentation_text = vformat(
  177. " <instrumentation\n"
  178. " tools:node=\"replace\"\n"
  179. " android:name=\".GodotInstrumentation\"\n"
  180. " android:icon=\"@mipmap/icon\"\n"
  181. " android:label=\"@string/godot_project_name_string\"\n"
  182. " android:targetPackage=\"%s\" />\n",
  183. package_name);
  184. return manifest_instrumentation_text;
  185. }
  186. String _get_plugins_tag(const String &plugins_names) {
  187. if (!plugins_names.empty()) {
  188. return vformat(" <meta-data tools:node=\"replace\" android:name=\"plugins\" android:value=\"%s\" />\n", plugins_names);
  189. } else {
  190. return " <meta-data tools:node=\"remove\" android:name=\"plugins\" />\n";
  191. }
  192. }
  193. String _get_activity_tag(const Ref<EditorExportPreset> &p_preset) {
  194. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  195. String orientation = (int)(p_preset->get("screen/orientation")) == 1 ? "portrait" : "landscape";
  196. String manifest_activity_text = vformat(
  197. " <activity android:name=\"com.godot.game.GodotApp\" "
  198. "tools:replace=\"android:screenOrientation\" "
  199. "android:screenOrientation=\"%s\">\n",
  200. orientation);
  201. if (uses_xr) {
  202. String focus_awareness = bool_to_string(p_preset->get("xr_features/focus_awareness"));
  203. manifest_activity_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"com.oculus.vr.focusaware\" android:value=\"%s\" />\n", focus_awareness);
  204. } else {
  205. manifest_activity_text += " <meta-data tools:node=\"remove\" android:name=\"com.oculus.vr.focusaware\" />\n";
  206. }
  207. manifest_activity_text += " </activity>\n";
  208. return manifest_activity_text;
  209. }
  210. String _get_application_tag(const Ref<EditorExportPreset> &p_preset, const String &plugins_names) {
  211. bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
  212. String manifest_application_text =
  213. " <application android:label=\"@string/godot_project_name_string\"\n"
  214. " android:allowBackup=\"false\" tools:ignore=\"GoogleAppIndexingWarning\"\n"
  215. " android:icon=\"@mipmap/icon\">)\n\n"
  216. " <meta-data tools:node=\"remove\" android:name=\"xr_mode_metadata_name\" />\n";
  217. manifest_application_text += _get_plugins_tag(plugins_names);
  218. if (uses_xr) {
  219. manifest_application_text += " <meta-data tools:node=\"replace\" android:name=\"com.samsung.android.vr.application.mode\" android:value=\"vr_only\" />\n";
  220. }
  221. manifest_application_text += _get_activity_tag(p_preset);
  222. manifest_application_text += " </application>\n";
  223. return manifest_application_text;
  224. }
  225. #endif //GODOT_GRADLE_EXPORT_UTIL_H