| 1 | /**************************************************************************/ |
| 2 | /* export_plugin.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 | |
| 31 | #include "export_plugin.h" |
| 32 | |
| 33 | #include "logo_svg.gen.h" |
| 34 | #include "run_icon_svg.gen.h" |
| 35 | |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "core/io/dir_access.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/editor_string_names.h" |
| 40 | #include "editor/export/editor_export.h" |
| 41 | #include "editor/file_system/editor_paths.h" |
| 42 | #include "editor/themes/editor_scale.h" |
| 43 | |
| 44 | #include "modules/svg/image_loader_svg.h" |
| 45 | |
| 46 | Error EditorExportPlatformLinuxBSD::_export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path) { |
| 47 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE); |
| 48 | if (f.is_null()) { |
| 49 | add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), vformat(TTR("Could not open file \"%s\"."), p_path)); |
| 50 | return ERR_CANT_CREATE; |
| 51 | } |
| 52 | |
| 53 | f->store_line("#!/bin/sh"); |
| 54 | f->store_line("printf '\\033c\\033]0;%s\\a' " + p_app_name); |
| 55 | f->store_line("base_path=\"$(dirname \"$(realpath \"$0\")\")\""); |
| 56 | f->store_line("\"$base_path/" + p_pkg_name + "\" \"$@\""); |
| 57 | |
| 58 | return OK; |
| 59 | } |
| 60 | |
| 61 | Error EditorExportPlatformLinuxBSD::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 62 | String custom_debug = p_preset->get("custom_template/debug"); |
| 63 | String custom_release = p_preset->get("custom_template/release"); |
| 64 | String arch = p_preset->get("binary_format/architecture"); |
| 65 | |
| 66 | String template_path = p_debug ? custom_debug : custom_release; |
| 67 | template_path = template_path.strip_edges(); |
| 68 | if (!template_path.is_empty()) { |
| 69 | String exe_arch = _get_exe_arch(template_path); |
| 70 | if (arch != exe_arch) { |
| 71 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Mismatching custom export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch)); |
| 72 | return ERR_CANT_CREATE; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 77 | if (da->file_exists(template_path.get_base_dir().path_join("libaccesskit." + arch + ".so"))) { |
| 78 | da->copy(template_path.get_base_dir().path_join("libaccesskit." + arch + ".so"), p_path.get_base_dir().path_join("libaccesskit." + arch + ".so"), get_chmod_flags()); |
| 79 | } |
| 80 | |
| 81 | bool export_as_zip = p_path.ends_with("zip"); |
| 82 | |
| 83 | String pkg_name; |
| 84 | if (String(get_project_setting(p_preset, "application/config/name")) != "") { |
| 85 | pkg_name = String(get_project_setting(p_preset, "application/config/name")); |
| 86 | } else { |
| 87 | pkg_name = "Unnamed"; |
| 88 | } |
| 89 | |
| 90 | pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name); |
| 91 | |
| 92 | // Setup temp folder. |
| 93 | String path = p_path; |
| 94 | String tmp_dir_path = EditorPaths::get_singleton()->get_temp_dir().path_join(pkg_name); |
| 95 | |
| 96 | Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_dir_path); |
| 97 | if (export_as_zip) { |
| 98 | if (tmp_app_dir.is_null()) { |
| 99 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), tmp_dir_path)); |
| 100 | return ERR_CANT_CREATE; |
| 101 | } |
| 102 | if (DirAccess::exists(tmp_dir_path)) { |
| 103 | if (tmp_app_dir->change_dir(tmp_dir_path) == OK) { |
| 104 | tmp_app_dir->erase_contents_recursive(); |
| 105 | } |
| 106 | } |
| 107 | tmp_app_dir->make_dir_recursive(tmp_dir_path); |
| 108 | path = tmp_dir_path.path_join(p_path.get_file().get_basename()); |
| 109 | } |
| 110 | |
| 111 | // Export project. |
| 112 | Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, path, p_flags); |
| 113 | if (err != OK) { |
| 114 | // Message is supplied by the subroutine method. |
| 115 | return err; |
| 116 | } |
| 117 | |
| 118 | // Save console wrapper. |
| 119 | int con_scr = p_preset->get("debug/export_console_wrapper"); |
| 120 | if ((con_scr == 1 && p_debug) || (con_scr == 2)) { |
| 121 | String scr_path = path.get_basename() + ".sh"; |
| 122 | err = _export_debug_script(p_preset, pkg_name, path.get_file(), scr_path); |
| 123 | FileAccess::set_unix_permissions(scr_path, 0755); |
| 124 | if (err != OK) { |
| 125 | add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Console Export"), TTR("Could not create console wrapper.")); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // ZIP project. |
| 130 | if (export_as_zip) { |
| 131 | if (FileAccess::exists(p_path)) { |
| 132 | OS::get_singleton()->move_to_trash(p_path); |
| 133 | } |
| 134 | |
| 135 | Ref<FileAccess> io_fa_dst; |
| 136 | zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst); |
| 137 | zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst); |
| 138 | |
| 139 | zip_folder_recursive(zip, tmp_dir_path, "", pkg_name); |
| 140 | |
| 141 | zipClose(zip, nullptr); |
| 142 | |
| 143 | if (tmp_app_dir->change_dir(tmp_dir_path) == OK) { |
| 144 | tmp_app_dir->erase_contents_recursive(); |
| 145 | tmp_app_dir->change_dir(".."); |
| 146 | tmp_app_dir->remove(pkg_name); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | String EditorExportPlatformLinuxBSD::get_template_file_name(const String &p_target, const String &p_arch) const { |
| 154 | return "linux_" + p_target + "." + p_arch; |
| 155 | } |
| 156 | |
| 157 | List<String> EditorExportPlatformLinuxBSD::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const { |
| 158 | List<String> list; |
| 159 | list.push_back(p_preset->get("binary_format/architecture")); |
| 160 | list.push_back("zip"); |
| 161 | |
| 162 | return list; |
| 163 | } |
| 164 | |
| 165 | bool EditorExportPlatformLinuxBSD::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const { |
| 166 | if (p_preset == nullptr) { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool advanced_options_enabled = p_preset->are_advanced_options_enabled(); |
| 171 | |
| 172 | // Hide SSH options. |
| 173 | bool ssh = p_preset->get("ssh_remote_deploy/enabled"); |
| 174 | if (!ssh && p_option != "ssh_remote_deploy/enabled" && p_option.begins_with("ssh_remote_deploy/")) { |
| 175 | return false; |
| 176 | } |
| 177 | if (p_option == "dotnet/embed_build_outputs" || |
| 178 | p_option == "custom_template/debug" || |
| 179 | p_option == "custom_template/release") { |
| 180 | return advanced_options_enabled; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | void EditorExportPlatformLinuxBSD::get_export_options(List<ExportOption> *r_options) const { |
| 186 | EditorExportPlatformPC::get_export_options(r_options); |
| 187 | |
| 188 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64,arm32,rv64,ppc64,loongarch64"), "x86_64")); |
| 189 | |
| 190 | String run_script = "#!/usr/bin/env bash\n" |
| 191 | "export DISPLAY=:0\n" |
| 192 | "unzip -o -q \"{temp_dir}/{archive_name}\" -d \"{temp_dir}\"\n" |
| 193 | "\"{temp_dir}/{exe_name}\" {cmd_args}"; |
| 194 | |
| 195 | String cleanup_script = "#!/usr/bin/env bash\n" |
| 196 | "pkill -x -f \"{temp_dir}/{exe_name} {cmd_args}\"\n" |
| 197 | "rm -rf \"{temp_dir}\""; |
| 198 | |
| 199 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "ssh_remote_deploy/enabled"), false, true)); |
| 200 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/host"), "user@host_ip")); |
| 201 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/port"), "22")); |
| 202 | |
| 203 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_ssh", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), "")); |
| 204 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_scp", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), "")); |
| 205 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/run_script", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), run_script)); |
| 206 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/cleanup_script", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), cleanup_script)); |
| 207 | } |
| 208 | |
| 209 | bool EditorExportPlatformLinuxBSD::is_elf(const String &p_path) const { |
| 210 | Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ); |
| 211 | ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("Can't open file: \"%s\".", p_path)); |
| 212 | uint32_t magic = fb->get_32(); |
| 213 | return (magic == 0x464c457f); |
| 214 | } |
| 215 | |
| 216 | bool EditorExportPlatformLinuxBSD::is_shebang(const String &p_path) const { |
| 217 | Ref<FileAccess> fb = FileAccess::open(p_path, FileAccess::READ); |
| 218 | ERR_FAIL_COND_V_MSG(fb.is_null(), false, vformat("Can't open file: \"%s\".", p_path)); |
| 219 | uint16_t magic = fb->get_16(); |
| 220 | return (magic == 0x2123); |
| 221 | } |
| 222 | |
| 223 | bool EditorExportPlatformLinuxBSD::is_executable(const String &p_path) const { |
| 224 | return is_elf(p_path) || is_shebang(p_path); |
| 225 | } |
| 226 | |
| 227 | void EditorExportPlatformLinuxBSD::get_platform_features(List<String> *r_features) const { |
| 228 | EditorExportPlatformPC::get_platform_features(r_features); |
| 229 | r_features->push_back("linux"); |
| 230 | r_features->push_back("linuxbsd"); |
| 231 | } |
| 232 | |
| 233 | bool EditorExportPlatformLinuxBSD::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const { |
| 234 | String err; |
| 235 | bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates, p_debug); |
| 236 | |
| 237 | String custom_debug = p_preset->get("custom_template/debug").operator String().strip_edges(); |
| 238 | String custom_release = p_preset->get("custom_template/release").operator String().strip_edges(); |
| 239 | String arch = p_preset->get("binary_format/architecture"); |
| 240 | |
| 241 | if (!custom_debug.is_empty() && FileAccess::exists(custom_debug)) { |
| 242 | String exe_arch = _get_exe_arch(custom_debug); |
| 243 | if (arch != exe_arch) { |
| 244 | err += vformat(TTR("Mismatching custom debug export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch) + "\n"; |
| 245 | } |
| 246 | } |
| 247 | if (!custom_release.is_empty() && FileAccess::exists(custom_release)) { |
| 248 | String exe_arch = _get_exe_arch(custom_release); |
| 249 | if (arch != exe_arch) { |
| 250 | err += vformat(TTR("Mismatching custom release export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch) + "\n"; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (!err.is_empty()) { |
| 255 | r_error = err; |
| 256 | } |
| 257 | |
| 258 | return valid; |
| 259 | } |
| 260 | |
| 261 | String EditorExportPlatformLinuxBSD::_get_exe_arch(const String &p_path) const { |
| 262 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 263 | if (f.is_null()) { |
| 264 | return "invalid"; |
| 265 | } |
| 266 | |
| 267 | // Read and check ELF magic number. |
| 268 | { |
| 269 | uint32_t magic = f->get_32(); |
| 270 | if (magic != 0x464c457f) { // 0x7F + "ELF" |
| 271 | return "invalid"; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // Process header. |
| 276 | int64_t header_pos = f->get_position(); |
| 277 | f->seek(header_pos + 14); |
| 278 | uint16_t machine = f->get_16(); |
| 279 | f->close(); |
| 280 | |
| 281 | switch (machine) { |
| 282 | case 0x0003: |
| 283 | return "x86_32"; |
| 284 | case 0x003e: |
| 285 | return "x86_64"; |
| 286 | case 0x0015: |
| 287 | return "ppc64"; |
| 288 | case 0x0028: |
| 289 | return "arm32"; |
| 290 | case 0x00b7: |
| 291 | return "arm64"; |
| 292 | case 0x00f3: |
| 293 | return "rv64"; |
| 294 | case 0x0102: |
| 295 | return "loongarch64"; |
| 296 | default: |
| 297 | return "unknown"; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | Error EditorExportPlatformLinuxBSD::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) { |
| 302 | // Patch the header of the "pck" section in the ELF file so that it corresponds to the embedded data. |
| 303 | |
| 304 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE); |
| 305 | if (f.is_null()) { |
| 306 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path)); |
| 307 | return ERR_CANT_OPEN; |
| 308 | } |
| 309 | |
| 310 | // Read and check ELF magic number. |
| 311 | { |
| 312 | uint32_t magic = f->get_32(); |
| 313 | if (magic != 0x464c457f) { // 0x7F + "ELF" |
| 314 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted.")); |
| 315 | return ERR_FILE_CORRUPT; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Read program architecture bits from class field. |
| 320 | |
| 321 | int bits = f->get_8() * 32; |
| 322 | |
| 323 | if (bits == 32 && p_embedded_size >= 0x100000000) { |
| 324 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("32-bit executables cannot have embedded data >= 4 GiB.")); |
| 325 | } |
| 326 | |
| 327 | // Get info about the section header table. |
| 328 | |
| 329 | int64_t section_table_pos; |
| 330 | int64_t section_header_size; |
| 331 | if (bits == 32) { |
| 332 | section_header_size = 40; |
| 333 | f->seek(0x20); |
| 334 | section_table_pos = f->get_32(); |
| 335 | f->seek(0x30); |
| 336 | } else { // 64 |
| 337 | section_header_size = 64; |
| 338 | f->seek(0x28); |
| 339 | section_table_pos = f->get_64(); |
| 340 | f->seek(0x3c); |
| 341 | } |
| 342 | int num_sections = f->get_16(); |
| 343 | int string_section_idx = f->get_16(); |
| 344 | |
| 345 | // Load the strings table. |
| 346 | uint8_t *strings; |
| 347 | { |
| 348 | // Jump to the strings section header. |
| 349 | f->seek(section_table_pos + string_section_idx * section_header_size); |
| 350 | |
| 351 | // Read strings data size and offset. |
| 352 | int64_t string_data_pos; |
| 353 | int64_t string_data_size; |
| 354 | if (bits == 32) { |
| 355 | f->seek(f->get_position() + 0x10); |
| 356 | string_data_pos = f->get_32(); |
| 357 | string_data_size = f->get_32(); |
| 358 | } else { // 64 |
| 359 | f->seek(f->get_position() + 0x18); |
| 360 | string_data_pos = f->get_64(); |
| 361 | string_data_size = f->get_64(); |
| 362 | } |
| 363 | |
| 364 | // Read strings data. |
| 365 | f->seek(string_data_pos); |
| 366 | strings = (uint8_t *)memalloc(string_data_size); |
| 367 | if (!strings) { |
| 368 | return ERR_OUT_OF_MEMORY; |
| 369 | } |
| 370 | f->get_buffer(strings, string_data_size); |
| 371 | } |
| 372 | |
| 373 | // Search for the "pck" section. |
| 374 | |
| 375 | bool found = false; |
| 376 | for (int i = 0; i < num_sections; ++i) { |
| 377 | int64_t section_header_pos = section_table_pos + i * section_header_size; |
| 378 | f->seek(section_header_pos); |
| 379 | |
| 380 | uint32_t name_offset = f->get_32(); |
| 381 | if (strcmp((char *)strings + name_offset, "pck") == 0) { |
| 382 | // "pck" section found, let's patch! |
| 383 | |
| 384 | if (bits == 32) { |
| 385 | f->seek(section_header_pos + 0x10); |
| 386 | f->store_32(p_embedded_start); |
| 387 | f->store_32(p_embedded_size); |
| 388 | } else { // 64 |
| 389 | f->seek(section_header_pos + 0x18); |
| 390 | f->store_64(p_embedded_start); |
| 391 | f->store_64(p_embedded_size); |
| 392 | } |
| 393 | |
| 394 | found = true; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | memfree(strings); |
| 400 | |
| 401 | if (!found) { |
| 402 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found.")); |
| 403 | return ERR_FILE_CORRUPT; |
| 404 | } |
| 405 | return OK; |
| 406 | } |
| 407 | |
| 408 | Ref<Texture2D> EditorExportPlatformLinuxBSD::get_run_icon() const { |
| 409 | return run_icon; |
| 410 | } |
| 411 | |
| 412 | bool EditorExportPlatformLinuxBSD::poll_export() { |
| 413 | Ref<EditorExportPreset> preset; |
| 414 | |
| 415 | for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) { |
| 416 | Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i); |
| 417 | if (ep->is_runnable() && ep->get_platform() == this) { |
| 418 | preset = ep; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | int prev = menu_options; |
| 424 | menu_options = (preset.is_valid() && preset->get("ssh_remote_deploy/enabled").operator bool()); |
| 425 | if (ssh_pid != 0 || !cleanup_commands.is_empty()) { |
| 426 | if (menu_options == 0) { |
| 427 | cleanup(); |
| 428 | } else { |
| 429 | menu_options += 1; |
| 430 | } |
| 431 | } |
| 432 | return menu_options != prev; |
| 433 | } |
| 434 | |
| 435 | Ref<Texture2D> EditorExportPlatformLinuxBSD::get_option_icon(int p_index) const { |
| 436 | if (p_index == 1) { |
| 437 | return stop_icon; |
| 438 | } else { |
| 439 | return EditorExportPlatform::get_option_icon(p_index); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | int EditorExportPlatformLinuxBSD::get_options_count() const { |
| 444 | return menu_options; |
| 445 | } |
| 446 | |
| 447 | String EditorExportPlatformLinuxBSD::get_option_label(int p_index) const { |
| 448 | return (p_index) ? TTR("Stop and uninstall") : TTR("Run on remote Linux/BSD system"); |
| 449 | } |
| 450 | |
| 451 | String EditorExportPlatformLinuxBSD::get_option_tooltip(int p_index) const { |
| 452 | return (p_index) ? TTR("Stop and uninstall running project from the remote system") : TTR("Run exported project on remote Linux/BSD system"); |
| 453 | } |
| 454 | |
| 455 | void EditorExportPlatformLinuxBSD::cleanup() { |
| 456 | if (ssh_pid != 0 && OS::get_singleton()->is_process_running(ssh_pid)) { |
| 457 | print_line("Terminating connection..."); |
| 458 | OS::get_singleton()->kill(ssh_pid); |
| 459 | OS::get_singleton()->delay_usec(1000); |
| 460 | } |
| 461 | |
| 462 | if (!cleanup_commands.is_empty()) { |
| 463 | print_line("Stopping and deleting previous version..."); |
| 464 | for (const SSHCleanupCommand &cmd : cleanup_commands) { |
| 465 | if (cmd.wait) { |
| 466 | ssh_run_on_remote(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args); |
| 467 | } else { |
| 468 | ssh_run_on_remote_no_wait(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | ssh_pid = 0; |
| 473 | cleanup_commands.clear(); |
| 474 | } |
| 475 | |
| 476 | Error EditorExportPlatformLinuxBSD::run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) { |
| 477 | cleanup(); |
| 478 | if (p_device) { // Stop command, cleanup only. |
| 479 | return OK; |
| 480 | } |
| 481 | |
| 482 | EditorProgress ep("run", TTR("Running..."), 5); |
| 483 | |
| 484 | const String dest = EditorPaths::get_singleton()->get_temp_dir().path_join("linuxbsd"); |
| 485 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 486 | if (!da->dir_exists(dest)) { |
| 487 | Error err = da->make_dir_recursive(dest); |
| 488 | if (err != OK) { |
| 489 | EditorNode::get_singleton()->show_warning(TTR("Could not create temp directory:") + "\n" + dest); |
| 490 | return err; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | String host = p_preset->get("ssh_remote_deploy/host").operator String(); |
| 495 | String port = p_preset->get("ssh_remote_deploy/port").operator String(); |
| 496 | if (port.is_empty()) { |
| 497 | port = "22"; |
| 498 | } |
| 499 | Vector<String> extra_args_ssh = p_preset->get("ssh_remote_deploy/extra_args_ssh").operator String().split(" ", false); |
| 500 | Vector<String> extra_args_scp = p_preset->get("ssh_remote_deploy/extra_args_scp").operator String().split(" ", false); |
| 501 | |
| 502 | const String basepath = dest.path_join("tmp_linuxbsd_export"); |
| 503 | |
| 504 | #define CLEANUP_AND_RETURN(m_err) \ |
| 505 | { \ |
| 506 | if (da->file_exists(basepath + ".zip")) { \ |
| 507 | da->remove(basepath + ".zip"); \ |
| 508 | } \ |
| 509 | if (da->file_exists(basepath + "_start.sh")) { \ |
| 510 | da->remove(basepath + "_start.sh"); \ |
| 511 | } \ |
| 512 | if (da->file_exists(basepath + "_clean.sh")) { \ |
| 513 | da->remove(basepath + "_clean.sh"); \ |
| 514 | } \ |
| 515 | return m_err; \ |
| 516 | } \ |
| 517 | ((void)0) |
| 518 | |
| 519 | if (ep.step(TTR("Exporting project..."), 1)) { |
| 520 | return ERR_SKIP; |
| 521 | } |
| 522 | Error err = export_project(p_preset, true, basepath + ".zip", p_debug_flags); |
| 523 | if (err != OK) { |
| 524 | DirAccess::remove_file_or_error(basepath + ".zip"); |
| 525 | return err; |
| 526 | } |
| 527 | |
| 528 | String cmd_args; |
| 529 | { |
| 530 | Vector<String> cmd_args_list = gen_export_flags(p_debug_flags); |
| 531 | for (int i = 0; i < cmd_args_list.size(); i++) { |
| 532 | if (i != 0) { |
| 533 | cmd_args += " "; |
| 534 | } |
| 535 | cmd_args += cmd_args_list[i]; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | const bool use_remote = p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG) || p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT); |
| 540 | int dbg_port = EDITOR_GET("network/debug/remote_port"); |
| 541 | |
| 542 | print_line("Creating temporary directory..."); |
| 543 | ep.step(TTR("Creating temporary directory..."), 2); |
| 544 | String temp_dir; |
| 545 | err = ssh_run_on_remote(host, port, extra_args_ssh, "mktemp -d", &temp_dir); |
| 546 | if (err != OK || temp_dir.is_empty()) { |
| 547 | CLEANUP_AND_RETURN(err); |
| 548 | } |
| 549 | |
| 550 | print_line("Uploading archive..."); |
| 551 | ep.step(TTR("Uploading archive..."), 3); |
| 552 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + ".zip", temp_dir); |
| 553 | if (err != OK) { |
| 554 | CLEANUP_AND_RETURN(err); |
| 555 | } |
| 556 | |
| 557 | { |
| 558 | String run_script = p_preset->get("ssh_remote_deploy/run_script"); |
| 559 | run_script = run_script.replace("{temp_dir}", temp_dir); |
| 560 | run_script = run_script.replace("{archive_name}", basepath.get_file() + ".zip"); |
| 561 | run_script = run_script.replace("{exe_name}", basepath.get_file()); |
| 562 | run_script = run_script.replace("{cmd_args}", cmd_args); |
| 563 | |
| 564 | Ref<FileAccess> f = FileAccess::open(basepath + "_start.sh", FileAccess::WRITE); |
| 565 | if (f.is_null()) { |
| 566 | CLEANUP_AND_RETURN(err); |
| 567 | } |
| 568 | |
| 569 | f->store_string(run_script); |
| 570 | } |
| 571 | |
| 572 | { |
| 573 | String clean_script = p_preset->get("ssh_remote_deploy/cleanup_script"); |
| 574 | clean_script = clean_script.replace("{temp_dir}", temp_dir); |
| 575 | clean_script = clean_script.replace("{archive_name}", basepath.get_file() + ".zip"); |
| 576 | clean_script = clean_script.replace("{exe_name}", basepath.get_file()); |
| 577 | clean_script = clean_script.replace("{cmd_args}", cmd_args); |
| 578 | |
| 579 | Ref<FileAccess> f = FileAccess::open(basepath + "_clean.sh", FileAccess::WRITE); |
| 580 | if (f.is_null()) { |
| 581 | CLEANUP_AND_RETURN(err); |
| 582 | } |
| 583 | |
| 584 | f->store_string(clean_script); |
| 585 | } |
| 586 | |
| 587 | print_line("Uploading scripts..."); |
| 588 | ep.step(TTR("Uploading scripts..."), 4); |
| 589 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_start.sh", temp_dir); |
| 590 | if (err != OK) { |
| 591 | CLEANUP_AND_RETURN(err); |
| 592 | } |
| 593 | err = ssh_run_on_remote(host, port, extra_args_ssh, vformat("chmod +x \"%s/%s\"", temp_dir, basepath.get_file() + "_start.sh")); |
| 594 | if (err != OK || temp_dir.is_empty()) { |
| 595 | CLEANUP_AND_RETURN(err); |
| 596 | } |
| 597 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_clean.sh", temp_dir); |
| 598 | if (err != OK) { |
| 599 | CLEANUP_AND_RETURN(err); |
| 600 | } |
| 601 | err = ssh_run_on_remote(host, port, extra_args_ssh, vformat("chmod +x \"%s/%s\"", temp_dir, basepath.get_file() + "_clean.sh")); |
| 602 | if (err != OK || temp_dir.is_empty()) { |
| 603 | CLEANUP_AND_RETURN(err); |
| 604 | } |
| 605 | |
| 606 | print_line("Starting project..."); |
| 607 | ep.step(TTR("Starting project..."), 5); |
| 608 | err = ssh_run_on_remote_no_wait(host, port, extra_args_ssh, vformat("\"%s/%s\"", temp_dir, basepath.get_file() + "_start.sh"), &ssh_pid, (use_remote) ? dbg_port : -1); |
| 609 | if (err != OK) { |
| 610 | CLEANUP_AND_RETURN(err); |
| 611 | } |
| 612 | |
| 613 | cleanup_commands.clear(); |
| 614 | cleanup_commands.push_back(SSHCleanupCommand(host, port, extra_args_ssh, vformat("\"%s/%s\"", temp_dir, basepath.get_file() + "_clean.sh"))); |
| 615 | |
| 616 | print_line("Project started."); |
| 617 | |
| 618 | CLEANUP_AND_RETURN(OK); |
| 619 | #undef CLEANUP_AND_RETURN |
| 620 | } |
| 621 | |
| 622 | void EditorExportPlatformLinuxBSD::initialize() { |
| 623 | if (EditorNode::get_singleton()) { |
| 624 | Ref<Image> img = memnew(Image); |
| 625 | const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE); |
| 626 | |
| 627 | ImageLoaderSVG::create_image_from_string(img, _linuxbsd_logo_svg, EDSCALE, upsample, false); |
| 628 | set_logo(ImageTexture::create_from_image(img)); |
| 629 | |
| 630 | ImageLoaderSVG::create_image_from_string(img, _linuxbsd_run_icon_svg, EDSCALE, upsample, false); |
| 631 | run_icon = ImageTexture::create_from_image(img); |
| 632 | |
| 633 | Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme(); |
| 634 | if (theme.is_valid()) { |
| 635 | stop_icon = theme->get_icon(SNAME("Stop"), EditorStringName(EditorIcons)); |
| 636 | } else { |
| 637 | stop_icon.instantiate(); |
| 638 | } |
| 639 | } |
| 640 | } |