export_plugin.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*************************************************************************/
  2. /* export_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "export_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. Error EditorExportPlatformLinuxBSD::_export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path) {
  34. FileAccessRef f = FileAccess::open(p_path, FileAccess::WRITE);
  35. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE);
  36. f->store_line("#!/bin/sh");
  37. f->store_line("echo -ne '\\033c\\033]0;" + p_app_name + "\\a'");
  38. f->store_line("base_path=\"$(dirname \"$(realpath \"$0\")\")\"");
  39. f->store_line("\"$base_path/" + p_pkg_name + "\" \"$@\"");
  40. return OK;
  41. }
  42. Error EditorExportPlatformLinuxBSD::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  43. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, p_path, p_flags);
  44. if (err != OK) {
  45. return err;
  46. }
  47. String app_name;
  48. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  49. app_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  50. } else {
  51. app_name = "Unnamed";
  52. }
  53. app_name = OS::get_singleton()->get_safe_dir_name(app_name);
  54. // Save console script.
  55. if (err == OK) {
  56. int con_scr = p_preset->get("debug/export_console_script");
  57. if ((con_scr == 1 && p_debug) || (con_scr == 2)) {
  58. String scr_path = p_path.get_basename() + ".sh";
  59. err = _export_debug_script(p_preset, app_name, p_path.get_file(), scr_path);
  60. FileAccess::set_unix_permissions(scr_path, 0755);
  61. }
  62. }
  63. return err;
  64. }
  65. void EditorExportPlatformLinuxBSD::set_extension(const String &p_extension, const String &p_feature_key) {
  66. extensions[p_feature_key] = p_extension;
  67. }
  68. String EditorExportPlatformLinuxBSD::get_template_file_name(const String &p_target, const String &p_arch) const {
  69. return "linux_x11_" + p_arch + "_" + p_target;
  70. }
  71. List<String> EditorExportPlatformLinuxBSD::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  72. List<String> list;
  73. for (const KeyValue<String, String> &E : extensions) {
  74. if (p_preset->get(E.key)) {
  75. list.push_back(extensions[E.key]);
  76. return list;
  77. }
  78. }
  79. if (extensions.has("default")) {
  80. list.push_back(extensions["default"]);
  81. return list;
  82. }
  83. return list;
  84. }
  85. Error EditorExportPlatformLinuxBSD::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) const {
  86. // Patch the header of the "pck" section in the ELF file so that it corresponds to the embedded data
  87. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  88. if (!f) {
  89. return ERR_CANT_OPEN;
  90. }
  91. // Read and check ELF magic number
  92. {
  93. uint32_t magic = f->get_32();
  94. if (magic != 0x464c457f) { // 0x7F + "ELF"
  95. f->close();
  96. return ERR_FILE_CORRUPT;
  97. }
  98. }
  99. // Read program architecture bits from class field
  100. int bits = f->get_8() * 32;
  101. if (bits == 32 && p_embedded_size >= 0x100000000) {
  102. f->close();
  103. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "32-bit executables cannot have embedded data >= 4 GiB.");
  104. }
  105. // Get info about the section header table
  106. int64_t section_table_pos;
  107. int64_t section_header_size;
  108. if (bits == 32) {
  109. section_header_size = 40;
  110. f->seek(0x20);
  111. section_table_pos = f->get_32();
  112. f->seek(0x30);
  113. } else { // 64
  114. section_header_size = 64;
  115. f->seek(0x28);
  116. section_table_pos = f->get_64();
  117. f->seek(0x3c);
  118. }
  119. int num_sections = f->get_16();
  120. int string_section_idx = f->get_16();
  121. // Load the strings table
  122. uint8_t *strings;
  123. {
  124. // Jump to the strings section header
  125. f->seek(section_table_pos + string_section_idx * section_header_size);
  126. // Read strings data size and offset
  127. int64_t string_data_pos;
  128. int64_t string_data_size;
  129. if (bits == 32) {
  130. f->seek(f->get_position() + 0x10);
  131. string_data_pos = f->get_32();
  132. string_data_size = f->get_32();
  133. } else { // 64
  134. f->seek(f->get_position() + 0x18);
  135. string_data_pos = f->get_64();
  136. string_data_size = f->get_64();
  137. }
  138. // Read strings data
  139. f->seek(string_data_pos);
  140. strings = (uint8_t *)memalloc(string_data_size);
  141. if (!strings) {
  142. f->close();
  143. return ERR_OUT_OF_MEMORY;
  144. }
  145. f->get_buffer(strings, string_data_size);
  146. }
  147. // Search for the "pck" section
  148. bool found = false;
  149. for (int i = 0; i < num_sections; ++i) {
  150. int64_t section_header_pos = section_table_pos + i * section_header_size;
  151. f->seek(section_header_pos);
  152. uint32_t name_offset = f->get_32();
  153. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  154. // "pck" section found, let's patch!
  155. if (bits == 32) {
  156. f->seek(section_header_pos + 0x10);
  157. f->store_32(p_embedded_start);
  158. f->store_32(p_embedded_size);
  159. } else { // 64
  160. f->seek(section_header_pos + 0x18);
  161. f->store_64(p_embedded_start);
  162. f->store_64(p_embedded_size);
  163. }
  164. found = true;
  165. break;
  166. }
  167. }
  168. memfree(strings);
  169. f->close();
  170. return found ? OK : ERR_FILE_CORRUPT;
  171. }