| 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 | #include "template_modifier.h" |
| 36 | |
| 37 | #include "core/config/project_settings.h" |
| 38 | #include "core/io/dir_access.h" |
| 39 | #include "core/io/image_loader.h" |
| 40 | #include "editor/editor_node.h" |
| 41 | #include "editor/editor_string_names.h" |
| 42 | #include "editor/export/editor_export.h" |
| 43 | #include "editor/file_system/editor_paths.h" |
| 44 | #include "editor/themes/editor_scale.h" |
| 45 | #include "scene/resources/image_texture.h" |
| 46 | |
| 47 | #include "modules/svg/image_loader_svg.h" |
| 48 | |
| 49 | #ifdef WINDOWS_ENABLED |
| 50 | #include "shlobj.h" |
| 51 | |
| 52 | // Converts long path to Windows UNC format. |
| 53 | static String fix_path(const String &p_path) { |
| 54 | String path = p_path; |
| 55 | if (p_path.is_relative_path()) { |
| 56 | Char16String current_dir_name; |
| 57 | size_t str_len = GetCurrentDirectoryW(0, nullptr); |
| 58 | current_dir_name.resize_uninitialized(str_len + 1); |
| 59 | GetCurrentDirectoryW(current_dir_name.size(), (LPWSTR)current_dir_name.ptrw()); |
| 60 | path = String::utf16((const char16_t *)current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace_char('\\', '/').path_join(path); |
| 61 | } |
| 62 | path = path.simplify_path(); |
| 63 | path = path.replace_char('/', '\\'); |
| 64 | if (path.size() >= MAX_PATH && !path.is_network_share_path() && !path.begins_with(R"(\\?\)")) { |
| 65 | path = R"(\\?\)" + path; |
| 66 | } |
| 67 | return path; |
| 68 | } |
| 69 | |
| 70 | #endif |
| 71 | |
| 72 | Error EditorExportPlatformWindows::_process_icon(const Ref<EditorExportPreset> &p_preset, const String &p_src_path, const String &p_dst_path) { |
| 73 | static const uint8_t icon_size[] = { 16, 32, 48, 64, 128, 0 /*256*/ }; |
| 74 | |
| 75 | struct IconData { |
| 76 | Vector<uint8_t> data; |
| 77 | uint8_t pal_colors = 0; |
| 78 | uint16_t planes = 0; |
| 79 | uint16_t bpp = 32; |
| 80 | }; |
| 81 | |
| 82 | HashMap<uint8_t, IconData> images; |
| 83 | Error err; |
| 84 | |
| 85 | if (p_src_path.get_extension() == "ico") { |
| 86 | Ref<FileAccess> f = FileAccess::open(p_src_path, FileAccess::READ, &err); |
| 87 | if (err != OK) { |
| 88 | return err; |
| 89 | } |
| 90 | |
| 91 | // Read ICONDIR. |
| 92 | f->get_16(); // Reserved. |
| 93 | uint16_t icon_type = f->get_16(); // Image type: 1 - ICO. |
| 94 | uint16_t icon_count = f->get_16(); // Number of images. |
| 95 | ERR_FAIL_COND_V(icon_type != 1, ERR_CANT_OPEN); |
| 96 | |
| 97 | for (uint16_t i = 0; i < icon_count; i++) { |
| 98 | // Read ICONDIRENTRY. |
| 99 | uint16_t w = f->get_8(); // Width in pixels. |
| 100 | uint16_t h = f->get_8(); // Height in pixels. |
| 101 | uint8_t pal_colors = f->get_8(); // Number of colors in the palette (0 - no palette). |
| 102 | f->get_8(); // Reserved. |
| 103 | uint16_t planes = f->get_16(); // Number of color planes. |
| 104 | uint16_t bpp = f->get_16(); // Bits per pixel. |
| 105 | uint32_t img_size = f->get_32(); // Image data size in bytes. |
| 106 | uint32_t img_offset = f->get_32(); // Image data offset. |
| 107 | if (w != h) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // Read image data. |
| 112 | uint64_t prev_offset = f->get_position(); |
| 113 | images[w].pal_colors = pal_colors; |
| 114 | images[w].planes = planes; |
| 115 | images[w].bpp = bpp; |
| 116 | images[w].data.resize(img_size); |
| 117 | f->seek(img_offset); |
| 118 | f->get_buffer(images[w].data.ptrw(), img_size); |
| 119 | f->seek(prev_offset); |
| 120 | } |
| 121 | } else { |
| 122 | Ref<Image> src_image = _load_icon_or_splash_image(p_src_path, &err); |
| 123 | ERR_FAIL_COND_V(err != OK || src_image.is_null() || src_image->is_empty(), ERR_CANT_OPEN); |
| 124 | |
| 125 | for (size_t i = 0; i < std_size(icon_size); ++i) { |
| 126 | int size = (icon_size[i] == 0) ? 256 : icon_size[i]; |
| 127 | |
| 128 | Ref<Image> res_image = src_image->duplicate(); |
| 129 | ERR_FAIL_COND_V(res_image.is_null() || res_image->is_empty(), ERR_CANT_OPEN); |
| 130 | res_image->resize(size, size, (Image::Interpolation)(p_preset->get("application/icon_interpolation").operator int())); |
| 131 | images[icon_size[i]].data = res_image->save_png_to_buffer(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | uint16_t valid_icon_count = 0; |
| 136 | for (size_t i = 0; i < std_size(icon_size); ++i) { |
| 137 | if (images.has(icon_size[i])) { |
| 138 | valid_icon_count++; |
| 139 | } else { |
| 140 | int size = (icon_size[i] == 0) ? 256 : icon_size[i]; |
| 141 | add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Icon size \"%d\" is missing."), size)); |
| 142 | } |
| 143 | } |
| 144 | ERR_FAIL_COND_V(valid_icon_count == 0, ERR_CANT_OPEN); |
| 145 | |
| 146 | Ref<FileAccess> fw = FileAccess::open(p_dst_path, FileAccess::WRITE, &err); |
| 147 | if (err != OK) { |
| 148 | return err; |
| 149 | } |
| 150 | |
| 151 | // Write ICONDIR. |
| 152 | fw->store_16(0); // Reserved. |
| 153 | fw->store_16(1); // Image type: 1 - ICO. |
| 154 | fw->store_16(valid_icon_count); // Number of images. |
| 155 | |
| 156 | // Write ICONDIRENTRY. |
| 157 | uint32_t img_offset = 6 + 16 * valid_icon_count; |
| 158 | for (size_t i = 0; i < std_size(icon_size); ++i) { |
| 159 | if (images.has(icon_size[i])) { |
| 160 | const IconData &di = images[icon_size[i]]; |
| 161 | fw->store_8(icon_size[i]); // Width in pixels. |
| 162 | fw->store_8(icon_size[i]); // Height in pixels. |
| 163 | fw->store_8(di.pal_colors); // Number of colors in the palette (0 - no palette). |
| 164 | fw->store_8(0); // Reserved. |
| 165 | fw->store_16(di.planes); // Number of color planes. |
| 166 | fw->store_16(di.bpp); // Bits per pixel. |
| 167 | fw->store_32(di.data.size()); // Image data size in bytes. |
| 168 | fw->store_32(img_offset); // Image data offset. |
| 169 | |
| 170 | img_offset += di.data.size(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Write image data. |
| 175 | for (size_t i = 0; i < std_size(icon_size); ++i) { |
| 176 | if (images.has(icon_size[i])) { |
| 177 | const IconData &di = images[icon_size[i]]; |
| 178 | fw->store_buffer(di.data.ptr(), di.data.size()); |
| 179 | } |
| 180 | } |
| 181 | return OK; |
| 182 | } |
| 183 | |
| 184 | Error EditorExportPlatformWindows::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) { |
| 185 | if (p_preset->get("codesign/enable")) { |
| 186 | return _code_sign(p_preset, p_path); |
| 187 | } else { |
| 188 | return OK; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | Error EditorExportPlatformWindows::modify_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 193 | if (p_preset->get("application/modify_resources")) { |
| 194 | _add_data(p_preset, p_path, false); |
| 195 | String wrapper_path = p_path.get_basename() + ".console.exe"; |
| 196 | if (FileAccess::exists(wrapper_path)) { |
| 197 | _add_data(p_preset, wrapper_path, true); |
| 198 | } |
| 199 | } |
| 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | Error EditorExportPlatformWindows::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 204 | String custom_debug = p_preset->get("custom_template/debug"); |
| 205 | String custom_release = p_preset->get("custom_template/release"); |
| 206 | String arch = p_preset->get("binary_format/architecture"); |
| 207 | |
| 208 | String template_path = p_debug ? custom_debug : custom_release; |
| 209 | template_path = template_path.strip_edges(); |
| 210 | if (template_path.is_empty()) { |
| 211 | template_path = find_export_template(get_template_file_name(p_debug ? "debug" : "release", arch)); |
| 212 | } else { |
| 213 | String exe_arch = _get_exe_arch(template_path); |
| 214 | if (arch != exe_arch) { |
| 215 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Mismatching custom export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch)); |
| 216 | return ERR_CANT_CREATE; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | bool export_as_zip = p_path.ends_with("zip"); |
| 221 | bool embedded = p_preset->get("binary_format/embed_pck"); |
| 222 | |
| 223 | String pkg_name; |
| 224 | if (String(get_project_setting(p_preset, "application/config/name")) != "") { |
| 225 | pkg_name = String(get_project_setting(p_preset, "application/config/name")); |
| 226 | } else { |
| 227 | pkg_name = "Unnamed"; |
| 228 | } |
| 229 | |
| 230 | pkg_name = OS::get_singleton()->get_safe_dir_name(pkg_name); |
| 231 | |
| 232 | // Setup temp folder. |
| 233 | String path = p_path; |
| 234 | String tmp_dir_path = EditorPaths::get_singleton()->get_temp_dir().path_join(pkg_name); |
| 235 | Ref<DirAccess> tmp_app_dir = DirAccess::create_for_path(tmp_dir_path); |
| 236 | if (export_as_zip) { |
| 237 | if (tmp_app_dir.is_null()) { |
| 238 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not create and open the directory: \"%s\""), tmp_dir_path)); |
| 239 | return ERR_CANT_CREATE; |
| 240 | } |
| 241 | if (DirAccess::exists(tmp_dir_path)) { |
| 242 | if (tmp_app_dir->change_dir(tmp_dir_path) == OK) { |
| 243 | tmp_app_dir->erase_contents_recursive(); |
| 244 | } |
| 245 | } |
| 246 | tmp_app_dir->make_dir_recursive(tmp_dir_path); |
| 247 | path = tmp_dir_path.path_join(p_path.get_file().get_basename() + ".exe"); |
| 248 | } |
| 249 | |
| 250 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 251 | int export_angle = p_preset->get("application/export_angle"); |
| 252 | bool include_angle_libs = false; |
| 253 | if (export_angle == 0) { |
| 254 | include_angle_libs = (String(get_project_setting(p_preset, "rendering/gl_compatibility/driver.windows")) == "opengl3_angle") && (String(get_project_setting(p_preset, "rendering/renderer/rendering_method")) == "gl_compatibility"); |
| 255 | } else if (export_angle == 1) { |
| 256 | include_angle_libs = true; |
| 257 | } |
| 258 | if (include_angle_libs) { |
| 259 | if (da->file_exists(template_path.get_base_dir().path_join("libEGL." + arch + ".dll"))) { |
| 260 | da->copy(template_path.get_base_dir().path_join("libEGL." + arch + ".dll"), path.get_base_dir().path_join("libEGL.dll"), get_chmod_flags()); |
| 261 | } |
| 262 | if (da->file_exists(template_path.get_base_dir().path_join("libGLESv2." + arch + ".dll"))) { |
| 263 | da->copy(template_path.get_base_dir().path_join("libGLESv2." + arch + ".dll"), path.get_base_dir().path_join("libGLESv2.dll"), get_chmod_flags()); |
| 264 | } |
| 265 | } |
| 266 | if (da->file_exists(template_path.get_base_dir().path_join("accesskit." + arch + ".dll"))) { |
| 267 | da->copy(template_path.get_base_dir().path_join("accesskit." + arch + ".dll"), path.get_base_dir().path_join("accesskit." + arch + ".dll"), get_chmod_flags()); |
| 268 | } |
| 269 | |
| 270 | int export_d3d12 = p_preset->get("application/export_d3d12"); |
| 271 | bool agility_sdk_multiarch = p_preset->get("application/d3d12_agility_sdk_multiarch"); |
| 272 | bool include_d3d12_extra_libs = false; |
| 273 | if (export_d3d12 == 0) { |
| 274 | include_d3d12_extra_libs = (String(get_project_setting(p_preset, "rendering/rendering_device/driver.windows")) == "d3d12") && (String(get_project_setting(p_preset, "rendering/renderer/rendering_method")) != "gl_compatibility"); |
| 275 | } else if (export_d3d12 == 1) { |
| 276 | include_d3d12_extra_libs = true; |
| 277 | } |
| 278 | if (include_d3d12_extra_libs) { |
| 279 | if (da->file_exists(template_path.get_base_dir().path_join("D3D12Core." + arch + ".dll"))) { |
| 280 | if (agility_sdk_multiarch) { |
| 281 | da->make_dir_recursive(path.get_base_dir().path_join(arch)); |
| 282 | da->copy(template_path.get_base_dir().path_join("D3D12Core." + arch + ".dll"), path.get_base_dir().path_join(arch).path_join("D3D12Core.dll"), get_chmod_flags()); |
| 283 | } else { |
| 284 | da->copy(template_path.get_base_dir().path_join("D3D12Core." + arch + ".dll"), path.get_base_dir().path_join("D3D12Core.dll"), get_chmod_flags()); |
| 285 | } |
| 286 | } |
| 287 | if (da->file_exists(template_path.get_base_dir().path_join("d3d12SDKLayers." + arch + ".dll"))) { |
| 288 | if (agility_sdk_multiarch) { |
| 289 | da->make_dir_recursive(path.get_base_dir().path_join(arch)); |
| 290 | da->copy(template_path.get_base_dir().path_join("d3d12SDKLayers." + arch + ".dll"), path.get_base_dir().path_join(arch).path_join("d3d12SDKLayers.dll"), get_chmod_flags()); |
| 291 | } else { |
| 292 | da->copy(template_path.get_base_dir().path_join("d3d12SDKLayers." + arch + ".dll"), path.get_base_dir().path_join("d3d12SDKLayers.dll"), get_chmod_flags()); |
| 293 | } |
| 294 | } |
| 295 | if (da->file_exists(template_path.get_base_dir().path_join("WinPixEventRuntime." + arch + ".dll"))) { |
| 296 | da->copy(template_path.get_base_dir().path_join("WinPixEventRuntime." + arch + ".dll"), path.get_base_dir().path_join("WinPixEventRuntime.dll"), get_chmod_flags()); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Export project. |
| 301 | String pck_path = path; |
| 302 | if (embedded) { |
| 303 | pck_path = pck_path.get_basename() + ".tmp"; |
| 304 | } |
| 305 | |
| 306 | Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, pck_path, p_flags); |
| 307 | if (err != OK) { |
| 308 | // Message is supplied by the subroutine method. |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | if (p_preset->get("codesign/enable")) { |
| 313 | _code_sign(p_preset, pck_path); |
| 314 | String wrapper_path = path.get_basename() + ".console.exe"; |
| 315 | if (FileAccess::exists(wrapper_path)) { |
| 316 | _code_sign(p_preset, wrapper_path); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (embedded) { |
| 321 | Ref<DirAccess> tmp_dir = DirAccess::create_for_path(path.get_base_dir()); |
| 322 | err = tmp_dir->rename(pck_path, path); |
| 323 | if (err != OK) { |
| 324 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to rename temporary file \"%s\"."), pck_path)); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // ZIP project. |
| 329 | if (export_as_zip) { |
| 330 | if (FileAccess::exists(p_path)) { |
| 331 | OS::get_singleton()->move_to_trash(p_path); |
| 332 | } |
| 333 | |
| 334 | Ref<FileAccess> io_fa_dst; |
| 335 | zlib_filefunc_def io_dst = zipio_create_io(&io_fa_dst); |
| 336 | zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io_dst); |
| 337 | |
| 338 | zip_folder_recursive(zip, tmp_dir_path, "", pkg_name); |
| 339 | |
| 340 | zipClose(zip, nullptr); |
| 341 | |
| 342 | if (tmp_app_dir->change_dir(tmp_dir_path) == OK) { |
| 343 | tmp_app_dir->erase_contents_recursive(); |
| 344 | tmp_app_dir->change_dir(".."); |
| 345 | tmp_app_dir->remove(pkg_name); |
| 346 | } |
| 347 | #ifdef WINDOWS_ENABLED |
| 348 | } else { |
| 349 | // Update Windows icon cache. |
| 350 | String w_path = fix_path(path); |
| 351 | SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, (LPCWSTR)w_path.utf16().get_data(), nullptr); |
| 352 | |
| 353 | String wrapper_path = path.get_basename() + ".console.exe"; |
| 354 | if (FileAccess::exists(wrapper_path)) { |
| 355 | String w_wrapper_path = fix_path(wrapper_path); |
| 356 | SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, (LPCWSTR)w_wrapper_path.utf16().get_data(), nullptr); |
| 357 | } |
| 358 | |
| 359 | w_path = fix_path(path.get_base_dir()); |
| 360 | SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, (LPCWSTR)w_path.utf16().get_data(), nullptr); |
| 361 | |
| 362 | SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); |
| 363 | #endif |
| 364 | } |
| 365 | |
| 366 | return err; |
| 367 | } |
| 368 | |
| 369 | String EditorExportPlatformWindows::get_template_file_name(const String &p_target, const String &p_arch) const { |
| 370 | return "windows_" + p_target + "_" + p_arch + ".exe"; |
| 371 | } |
| 372 | |
| 373 | List<String> EditorExportPlatformWindows::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const { |
| 374 | List<String> list; |
| 375 | list.push_back("exe"); |
| 376 | list.push_back("zip"); |
| 377 | return list; |
| 378 | } |
| 379 | |
| 380 | String EditorExportPlatformWindows::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const { |
| 381 | if (p_preset) { |
| 382 | if (p_name == "application/icon") { |
| 383 | String icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/icon")); |
| 384 | if (!icon_path.is_empty() && !FileAccess::exists(icon_path)) { |
| 385 | return TTR("Invalid icon path."); |
| 386 | } |
| 387 | } else if (p_name == "application/file_version") { |
| 388 | String file_version = p_preset->get("application/file_version"); |
| 389 | if (!file_version.is_empty()) { |
| 390 | PackedStringArray version_array = file_version.split(".", false); |
| 391 | if (version_array.size() != 4 || !version_array[0].is_valid_int() || |
| 392 | !version_array[1].is_valid_int() || !version_array[2].is_valid_int() || |
| 393 | !version_array[3].is_valid_int() || file_version.contains_char('-')) { |
| 394 | return TTR("Invalid file version."); |
| 395 | } |
| 396 | } |
| 397 | } else if (p_name == "application/product_version") { |
| 398 | String product_version = p_preset->get("application/product_version"); |
| 399 | if (!product_version.is_empty()) { |
| 400 | PackedStringArray version_array = product_version.split(".", false); |
| 401 | if (version_array.size() != 4 || !version_array[0].is_valid_int() || |
| 402 | !version_array[1].is_valid_int() || !version_array[2].is_valid_int() || |
| 403 | !version_array[3].is_valid_int() || product_version.contains_char('-')) { |
| 404 | return TTR("Invalid product version."); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | return EditorExportPlatformPC::get_export_option_warning(p_preset, p_name); |
| 410 | } |
| 411 | |
| 412 | bool EditorExportPlatformWindows::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const { |
| 413 | if (p_preset == nullptr) { |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | // This option is not supported by "osslsigncode", used on non-Windows host. |
| 418 | if (!OS::get_singleton()->has_feature("windows") && p_option == "codesign/identity_type") { |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | bool advanced_options_enabled = p_preset->are_advanced_options_enabled(); |
| 423 | |
| 424 | // Hide codesign. |
| 425 | bool codesign = p_preset->get("codesign/enable"); |
| 426 | if (!codesign && p_option != "codesign/enable" && p_option.begins_with("codesign/")) { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | // Hide resources. |
| 431 | bool mod_res = p_preset->get("application/modify_resources"); |
| 432 | if (!mod_res && p_option != "application/modify_resources" && p_option != "application/export_angle" && p_option != "application/export_d3d12" && p_option != "application/d3d12_agility_sdk_multiarch" && p_option.begins_with("application/")) { |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // Hide SSH options. |
| 437 | bool ssh = p_preset->get("ssh_remote_deploy/enabled"); |
| 438 | if (!ssh && p_option != "ssh_remote_deploy/enabled" && p_option.begins_with("ssh_remote_deploy/")) { |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | if (p_option == "dotnet/embed_build_outputs" || |
| 443 | p_option == "custom_template/debug" || |
| 444 | p_option == "custom_template/release" || |
| 445 | p_option == "application/d3d12_agility_sdk_multiarch" || |
| 446 | p_option == "application/export_angle" || |
| 447 | p_option == "application/export_d3d12" || |
| 448 | p_option == "application/icon_interpolation") { |
| 449 | return advanced_options_enabled; |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | void EditorExportPlatformWindows::get_export_options(List<ExportOption> *r_options) const { |
| 455 | EditorExportPlatformPC::get_export_options(r_options); |
| 456 | |
| 457 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "binary_format/architecture", PROPERTY_HINT_ENUM, "x86_64,x86_32,arm64"), "x86_64")); |
| 458 | |
| 459 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/enable"), false, true)); |
| 460 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/identity_type", PROPERTY_HINT_ENUM, "Select automatically,Use PKCS12 file (specify *.PFX/*.P12 file),Use certificate store (specify SHA-1 hash)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), 0)); |
| 461 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/identity", PROPERTY_HINT_GLOBAL_FILE, "*.pfx,*.p12", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 462 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/password", PROPERTY_HINT_PASSWORD, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 463 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "codesign/timestamp"), true)); |
| 464 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/timestamp_server_url"), "")); |
| 465 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "codesign/digest_algorithm", PROPERTY_HINT_ENUM, "SHA1,SHA256"), 1)); |
| 466 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "codesign/description"), "")); |
| 467 | r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "codesign/custom_options"), PackedStringArray())); |
| 468 | |
| 469 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/modify_resources"), true, true)); |
| 470 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/icon", PROPERTY_HINT_FILE, "*.ico,*.png,*.webp,*.svg"), "", false, true)); |
| 471 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/console_wrapper_icon", PROPERTY_HINT_FILE, "*.ico,*.png,*.webp,*.svg"), "")); |
| 472 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/icon_interpolation", PROPERTY_HINT_ENUM, "Nearest neighbor,Bilinear,Cubic,Trilinear,Lanczos"), 4)); |
| 473 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), "")); |
| 474 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_version", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), "")); |
| 475 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/company_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Company Name"), "")); |
| 476 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/product_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name"), "")); |
| 477 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/file_description"), "")); |
| 478 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), "")); |
| 479 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/trademarks"), "")); |
| 480 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_angle", PROPERTY_HINT_ENUM, "Auto,Yes,No"), 0, true)); |
| 481 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "application/export_d3d12", PROPERTY_HINT_ENUM, "Auto,Yes,No"), 0, true)); |
| 482 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "application/d3d12_agility_sdk_multiarch"), true, true)); |
| 483 | |
| 484 | String run_script = "Expand-Archive -LiteralPath '{temp_dir}\\{archive_name}' -DestinationPath '{temp_dir}'\n" |
| 485 | "$action = New-ScheduledTaskAction -Execute '{temp_dir}\\{exe_name}' -Argument '{cmd_args}'\n" |
| 486 | "$trigger = New-ScheduledTaskTrigger -Once -At 00:00\n" |
| 487 | "$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries\n" |
| 488 | "$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings\n" |
| 489 | "Register-ScheduledTask godot_remote_debug -InputObject $task -Force:$true\n" |
| 490 | "Start-ScheduledTask -TaskName godot_remote_debug\n" |
| 491 | "while (Get-ScheduledTask -TaskName godot_remote_debug | ? State -eq running) { Start-Sleep -Milliseconds 100 }\n" |
| 492 | "Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue"; |
| 493 | |
| 494 | String cleanup_script = "Stop-ScheduledTask -TaskName godot_remote_debug -ErrorAction:SilentlyContinue\n" |
| 495 | "Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue\n" |
| 496 | "Remove-Item -Recurse -Force '{temp_dir}'"; |
| 497 | |
| 498 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "ssh_remote_deploy/enabled"), false, true)); |
| 499 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/host"), "user@host_ip")); |
| 500 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/port"), "22")); |
| 501 | |
| 502 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_ssh", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), "")); |
| 503 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/extra_args_scp", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), "")); |
| 504 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/run_script", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), run_script)); |
| 505 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "ssh_remote_deploy/cleanup_script", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), cleanup_script)); |
| 506 | } |
| 507 | |
| 508 | Error EditorExportPlatformWindows::_add_data(const Ref<EditorExportPreset> &p_preset, const String &p_path, bool p_console_icon) { |
| 509 | String icon_path; |
| 510 | if (p_preset->get("application/icon") != "") { |
| 511 | icon_path = p_preset->get("application/icon"); |
| 512 | } else if (get_project_setting(p_preset, "application/config/windows_native_icon") != "") { |
| 513 | icon_path = get_project_setting(p_preset, "application/config/windows_native_icon"); |
| 514 | } else { |
| 515 | icon_path = get_project_setting(p_preset, "application/config/icon"); |
| 516 | } |
| 517 | icon_path = ProjectSettings::get_singleton()->globalize_path(icon_path); |
| 518 | |
| 519 | if (p_console_icon) { |
| 520 | String console_icon_path = ProjectSettings::get_singleton()->globalize_path(p_preset->get("application/console_wrapper_icon")); |
| 521 | if (!console_icon_path.is_empty() && FileAccess::exists(console_icon_path)) { |
| 522 | icon_path = console_icon_path; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | String tmp_icon_path = EditorPaths::get_singleton()->get_temp_dir().path_join("_tmp.ico"); |
| 527 | if (!icon_path.is_empty()) { |
| 528 | if (_process_icon(p_preset, icon_path, tmp_icon_path) != OK) { |
| 529 | add_message(EXPORT_MESSAGE_WARNING, TTR("Resources Modification"), vformat(TTR("Invalid icon file \"%s\"."), icon_path)); |
| 530 | icon_path = String(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | TemplateModifier::modify(p_preset, p_path, tmp_icon_path); |
| 535 | |
| 536 | if (FileAccess::exists(tmp_icon_path)) { |
| 537 | DirAccess::remove_file_or_error(tmp_icon_path); |
| 538 | } |
| 539 | |
| 540 | return OK; |
| 541 | } |
| 542 | |
| 543 | Error EditorExportPlatformWindows::_code_sign(const Ref<EditorExportPreset> &p_preset, const String &p_path) { |
| 544 | List<String> args; |
| 545 | |
| 546 | #ifdef WINDOWS_ENABLED |
| 547 | String signtool_path = EDITOR_GET("export/windows/signtool"); |
| 548 | if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) { |
| 549 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find signtool executable at \"%s\"."), signtool_path)); |
| 550 | return ERR_FILE_NOT_FOUND; |
| 551 | } |
| 552 | if (signtool_path.is_empty()) { |
| 553 | signtool_path = "signtool"; // try to run signtool from PATH |
| 554 | } |
| 555 | #else |
| 556 | String signtool_path = EDITOR_GET("export/windows/osslsigncode"); |
| 557 | if (!signtool_path.is_empty() && !FileAccess::exists(signtool_path)) { |
| 558 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Could not find osslsigncode executable at \"%s\"."), signtool_path)); |
| 559 | return ERR_FILE_NOT_FOUND; |
| 560 | } |
| 561 | if (signtool_path.is_empty()) { |
| 562 | signtool_path = "osslsigncode"; // try to run signtool from PATH |
| 563 | } |
| 564 | #endif |
| 565 | |
| 566 | args.push_back("sign"); |
| 567 | |
| 568 | //identity |
| 569 | #ifdef WINDOWS_ENABLED |
| 570 | int id_type = p_preset->get_or_env("codesign/identity_type", ENV_WIN_CODESIGN_ID_TYPE); |
| 571 | if (id_type == 0) { //auto select |
| 572 | args.push_back("/a"); |
| 573 | } else if (id_type == 1) { //pkcs12 |
| 574 | if (p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID) != "") { |
| 575 | args.push_back("/f"); |
| 576 | args.push_back(p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID)); |
| 577 | } else { |
| 578 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found.")); |
| 579 | return FAILED; |
| 580 | } |
| 581 | } else if (id_type == 2) { //Windows certificate store |
| 582 | if (p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID) != "") { |
| 583 | args.push_back("/sha1"); |
| 584 | args.push_back(p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID)); |
| 585 | } else { |
| 586 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found.")); |
| 587 | return FAILED; |
| 588 | } |
| 589 | } else { |
| 590 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid identity type.")); |
| 591 | return FAILED; |
| 592 | } |
| 593 | #else |
| 594 | int id_type = 1; |
| 595 | if (p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID) != "") { |
| 596 | args.push_back("-pkcs12"); |
| 597 | args.push_back(p_preset->get_or_env("codesign/identity", ENV_WIN_CODESIGN_ID)); |
| 598 | } else { |
| 599 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("No identity found.")); |
| 600 | return FAILED; |
| 601 | } |
| 602 | #endif |
| 603 | |
| 604 | //password |
| 605 | if ((id_type == 1) && (p_preset->get_or_env("codesign/password", ENV_WIN_CODESIGN_PASS) != "")) { |
| 606 | #ifdef WINDOWS_ENABLED |
| 607 | args.push_back("/p"); |
| 608 | #else |
| 609 | args.push_back("-pass"); |
| 610 | #endif |
| 611 | args.push_back(p_preset->get_or_env("codesign/password", ENV_WIN_CODESIGN_PASS)); |
| 612 | } |
| 613 | |
| 614 | //timestamp |
| 615 | if (p_preset->get("codesign/timestamp")) { |
| 616 | if (p_preset->get("codesign/timestamp_server") != "") { |
| 617 | #ifdef WINDOWS_ENABLED |
| 618 | args.push_back("/tr"); |
| 619 | args.push_back(p_preset->get("codesign/timestamp_server_url")); |
| 620 | args.push_back("/td"); |
| 621 | if ((int)p_preset->get("codesign/digest_algorithm") == 0) { |
| 622 | args.push_back("sha1"); |
| 623 | } else { |
| 624 | args.push_back("sha256"); |
| 625 | } |
| 626 | #else |
| 627 | args.push_back("-ts"); |
| 628 | args.push_back(p_preset->get("codesign/timestamp_server_url")); |
| 629 | #endif |
| 630 | } else { |
| 631 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Invalid timestamp server.")); |
| 632 | return FAILED; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | //digest |
| 637 | #ifdef WINDOWS_ENABLED |
| 638 | args.push_back("/fd"); |
| 639 | #else |
| 640 | args.push_back("-h"); |
| 641 | #endif |
| 642 | if ((int)p_preset->get("codesign/digest_algorithm") == 0) { |
| 643 | args.push_back("sha1"); |
| 644 | } else { |
| 645 | args.push_back("sha256"); |
| 646 | } |
| 647 | |
| 648 | //description |
| 649 | if (p_preset->get("codesign/description") != "") { |
| 650 | #ifdef WINDOWS_ENABLED |
| 651 | args.push_back("/d"); |
| 652 | #else |
| 653 | args.push_back("-n"); |
| 654 | #endif |
| 655 | args.push_back(p_preset->get("codesign/description")); |
| 656 | } |
| 657 | |
| 658 | //user options |
| 659 | PackedStringArray user_args = p_preset->get("codesign/custom_options"); |
| 660 | for (int i = 0; i < user_args.size(); i++) { |
| 661 | String user_arg = user_args[i].strip_edges(); |
| 662 | if (!user_arg.is_empty()) { |
| 663 | args.push_back(user_arg); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | #ifndef WINDOWS_ENABLED |
| 668 | args.push_back("-in"); |
| 669 | #endif |
| 670 | args.push_back(p_path); |
| 671 | #ifndef WINDOWS_ENABLED |
| 672 | args.push_back("-out"); |
| 673 | args.push_back(p_path + "_signed"); |
| 674 | #endif |
| 675 | |
| 676 | String str; |
| 677 | Error err = OS::get_singleton()->execute(signtool_path, args, &str, nullptr, true); |
| 678 | if (err != OK || str.contains("not found") || str.contains("not recognized")) { |
| 679 | #ifdef WINDOWS_ENABLED |
| 680 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start signtool executable. Configure signtool path in the Editor Settings (Export > Windows > signtool), or disable \"Codesign\" in the export preset.")); |
| 681 | #else |
| 682 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start osslsigncode executable. Configure signtool path in the Editor Settings (Export > Windows > osslsigncode), or disable \"Codesign\" in the export preset.")); |
| 683 | #endif |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | print_line("codesign (" + p_path + "): " + str); |
| 688 | #ifndef WINDOWS_ENABLED |
| 689 | if (str.contains("SignTool Error")) { |
| 690 | #else |
| 691 | if (str.contains("Failed")) { |
| 692 | #endif |
| 693 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Signtool failed to sign executable: %s."), str)); |
| 694 | return FAILED; |
| 695 | } |
| 696 | |
| 697 | #ifndef WINDOWS_ENABLED |
| 698 | Ref<DirAccess> tmp_dir = DirAccess::create_for_path(p_path.get_base_dir()); |
| 699 | |
| 700 | err = tmp_dir->remove(p_path); |
| 701 | if (err != OK) { |
| 702 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to remove temporary file \"%s\"."), p_path)); |
| 703 | return err; |
| 704 | } |
| 705 | |
| 706 | err = tmp_dir->rename(p_path + "_signed", p_path); |
| 707 | if (err != OK) { |
| 708 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("Failed to rename temporary file \"%s\"."), p_path + "_signed")); |
| 709 | return err; |
| 710 | } |
| 711 | #endif |
| 712 | |
| 713 | return OK; |
| 714 | } |
| 715 | |
| 716 | bool EditorExportPlatformWindows::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const { |
| 717 | String err; |
| 718 | bool valid = EditorExportPlatformPC::has_valid_export_configuration(p_preset, err, r_missing_templates, p_debug); |
| 719 | |
| 720 | String custom_debug = p_preset->get("custom_template/debug").operator String().strip_edges(); |
| 721 | String custom_release = p_preset->get("custom_template/release").operator String().strip_edges(); |
| 722 | String arch = p_preset->get("binary_format/architecture"); |
| 723 | |
| 724 | if (!custom_debug.is_empty() && FileAccess::exists(custom_debug)) { |
| 725 | String exe_arch = _get_exe_arch(custom_debug); |
| 726 | if (arch != exe_arch) { |
| 727 | err += vformat(TTR("Mismatching custom debug export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch) + "\n"; |
| 728 | } |
| 729 | } |
| 730 | if (!custom_release.is_empty() && FileAccess::exists(custom_release)) { |
| 731 | String exe_arch = _get_exe_arch(custom_release); |
| 732 | if (arch != exe_arch) { |
| 733 | err += vformat(TTR("Mismatching custom release export template executable architecture: found \"%s\", expected \"%s\"."), exe_arch, arch) + "\n"; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (!err.is_empty()) { |
| 738 | r_error = err; |
| 739 | } |
| 740 | |
| 741 | return valid; |
| 742 | } |
| 743 | |
| 744 | bool EditorExportPlatformWindows::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const { |
| 745 | String err; |
| 746 | bool valid = true; |
| 747 | |
| 748 | List<ExportOption> options; |
| 749 | get_export_options(&options); |
| 750 | for (const EditorExportPlatform::ExportOption &E : options) { |
| 751 | if (get_export_option_visibility(p_preset.ptr(), E.option.name)) { |
| 752 | String warn = get_export_option_warning(p_preset.ptr(), E.option.name); |
| 753 | if (!warn.is_empty()) { |
| 754 | err += warn + "\n"; |
| 755 | if (E.required) { |
| 756 | valid = false; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (!err.is_empty()) { |
| 763 | r_error = err; |
| 764 | } |
| 765 | |
| 766 | return valid; |
| 767 | } |
| 768 | |
| 769 | String EditorExportPlatformWindows::_get_exe_arch(const String &p_path) const { |
| 770 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 771 | if (f.is_null()) { |
| 772 | return "invalid"; |
| 773 | } |
| 774 | |
| 775 | // Jump to the PE header and check the magic number. |
| 776 | { |
| 777 | f->seek(0x3c); |
| 778 | uint32_t pe_pos = f->get_32(); |
| 779 | |
| 780 | f->seek(pe_pos); |
| 781 | uint32_t magic = f->get_32(); |
| 782 | if (magic != 0x00004550) { |
| 783 | return "invalid"; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | // Process header. |
| 788 | uint16_t machine = f->get_16(); |
| 789 | f->close(); |
| 790 | |
| 791 | switch (machine) { |
| 792 | case 0x014c: |
| 793 | return "x86_32"; |
| 794 | case 0x8664: |
| 795 | return "x86_64"; |
| 796 | case 0x01c0: |
| 797 | case 0x01c4: |
| 798 | return "arm32"; |
| 799 | case 0xaa64: |
| 800 | return "arm64"; |
| 801 | default: |
| 802 | return "unknown"; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | Error EditorExportPlatformWindows::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) { |
| 807 | // Patch the header of the "pck" section in the PE file so that it corresponds to the embedded data. |
| 808 | |
| 809 | if (p_embedded_size + p_embedded_start >= 0x100000000) { // Check for total executable size. |
| 810 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Windows executables cannot be >= 4 GiB.")); |
| 811 | return ERR_INVALID_DATA; |
| 812 | } |
| 813 | |
| 814 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE); |
| 815 | if (f.is_null()) { |
| 816 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path)); |
| 817 | return ERR_CANT_OPEN; |
| 818 | } |
| 819 | |
| 820 | // Jump to the PE header and check the magic number. |
| 821 | { |
| 822 | f->seek(0x3c); |
| 823 | uint32_t pe_pos = f->get_32(); |
| 824 | |
| 825 | f->seek(pe_pos); |
| 826 | uint32_t magic = f->get_32(); |
| 827 | if (magic != 0x00004550) { |
| 828 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted.")); |
| 829 | return ERR_FILE_CORRUPT; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Process header. |
| 834 | uint32_t sect_alignment = 0x1000; |
| 835 | uint32_t image_size = 0; |
| 836 | int num_sections; |
| 837 | |
| 838 | int64_t header_pos = f->get_position(); |
| 839 | int64_t opt_header_pos = 0; |
| 840 | { |
| 841 | f->seek(header_pos + 2); |
| 842 | num_sections = f->get_16(); |
| 843 | f->seek(header_pos + 16); |
| 844 | uint16_t opt_header_size = f->get_16(); |
| 845 | opt_header_pos = f->get_position() + 2; |
| 846 | |
| 847 | f->seek(opt_header_pos + 32); |
| 848 | sect_alignment = f->get_32(); |
| 849 | |
| 850 | f->seek(opt_header_pos + 56); |
| 851 | image_size = f->get_32(); |
| 852 | |
| 853 | // Skip rest of header + optional header to go to the section headers. |
| 854 | f->seek(opt_header_pos + opt_header_size); |
| 855 | } |
| 856 | |
| 857 | // Search for the "pck" section. |
| 858 | |
| 859 | int64_t section_table_pos = f->get_position(); |
| 860 | |
| 861 | int pck_old_pos = -1; |
| 862 | for (int i = 0; i < num_sections; i++) { |
| 863 | int64_t section_header_pos = section_table_pos + i * 40; |
| 864 | f->seek(section_header_pos); |
| 865 | |
| 866 | uint8_t section_name[9]; |
| 867 | f->get_buffer(section_name, 8); |
| 868 | section_name[8] = '\0'; |
| 869 | |
| 870 | if (strcmp((char *)section_name, "pck") == 0) { |
| 871 | pck_old_pos = i; |
| 872 | |
| 873 | // Update virtual size of previous section to avoid gaps in the virtual addresses. |
| 874 | f->seek(section_table_pos + (i - 1) * 40 + 8); |
| 875 | uint32_t virt_size = f->get_32(); |
| 876 | f->seek(section_table_pos + (i - 1) * 40 + 8); |
| 877 | f->store_32(virt_size + sect_alignment); |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | if (pck_old_pos >= 0) { |
| 882 | // Move section data. |
| 883 | uint8_t section_data[40]; |
| 884 | for (int i = pck_old_pos; i < num_sections - 1; i++) { |
| 885 | f->seek(section_table_pos + (i + 1) * 40); |
| 886 | f->get_buffer(section_data, 40); |
| 887 | f->seek(section_table_pos + i * 40); |
| 888 | f->store_buffer(section_data, 40); |
| 889 | } |
| 890 | |
| 891 | // Add "pck" at the end. |
| 892 | f->seek(section_table_pos + (num_sections - 1) * 40); |
| 893 | uint8_t section_name[8] = { 'p', 'c', 'k', '\0', '\0', '\0', '\0', '\0' }; |
| 894 | f->store_buffer(section_name, 8); // Name. |
| 895 | f->store_32(8); // VirtualSize, set to a little to avoid it taking memory (zero would give issues). |
| 896 | f->store_32(image_size); // VirtualAddress. |
| 897 | f->store_32(p_embedded_size); // SizeOfRawData. |
| 898 | f->store_32(p_embedded_start); // PointerToRawData. |
| 899 | f->store_32(0); // PointerToRelocations, not used. |
| 900 | f->store_32(0); // PointerToLinenumbers, not used. |
| 901 | f->store_16(0); // NumberOfRelocations. |
| 902 | f->store_16(0); // NumberOfLinenumbers. |
| 903 | f->store_32(0x40000000); // Characteristics: Read. |
| 904 | |
| 905 | // Update image virtual size. |
| 906 | f->seek(opt_header_pos + 56); |
| 907 | f->store_32(image_size + sect_alignment); |
| 908 | } |
| 909 | |
| 910 | f->close(); |
| 911 | |
| 912 | if (pck_old_pos == -1) { |
| 913 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found.")); |
| 914 | return ERR_FILE_CORRUPT; |
| 915 | } |
| 916 | return OK; |
| 917 | } |
| 918 | |
| 919 | void EditorExportPlatformWindows::get_platform_features(List<String> *r_features) const { |
| 920 | EditorExportPlatformPC::get_platform_features(r_features); |
| 921 | r_features->push_back("windows"); |
| 922 | } |
| 923 | |
| 924 | Ref<Texture2D> EditorExportPlatformWindows::get_run_icon() const { |
| 925 | return run_icon; |
| 926 | } |
| 927 | |
| 928 | bool EditorExportPlatformWindows::poll_export() { |
| 929 | Ref<EditorExportPreset> preset; |
| 930 | |
| 931 | for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) { |
| 932 | Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i); |
| 933 | if (ep->is_runnable() && ep->get_platform() == this) { |
| 934 | preset = ep; |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | int prev = menu_options; |
| 940 | menu_options = (preset.is_valid() && preset->get("ssh_remote_deploy/enabled").operator bool()); |
| 941 | if (ssh_pid != 0 || !cleanup_commands.is_empty()) { |
| 942 | if (menu_options == 0) { |
| 943 | cleanup(); |
| 944 | } else { |
| 945 | menu_options += 1; |
| 946 | } |
| 947 | } |
| 948 | return menu_options != prev; |
| 949 | } |
| 950 | |
| 951 | Ref<Texture2D> EditorExportPlatformWindows::get_option_icon(int p_index) const { |
| 952 | if (p_index == 1) { |
| 953 | return stop_icon; |
| 954 | } else { |
| 955 | return EditorExportPlatform::get_option_icon(p_index); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | int EditorExportPlatformWindows::get_options_count() const { |
| 960 | return menu_options; |
| 961 | } |
| 962 | |
| 963 | String EditorExportPlatformWindows::get_option_label(int p_index) const { |
| 964 | return (p_index) ? TTR("Stop and uninstall") : TTR("Run on remote Windows system"); |
| 965 | } |
| 966 | |
| 967 | String EditorExportPlatformWindows::get_option_tooltip(int p_index) const { |
| 968 | return (p_index) ? TTR("Stop and uninstall running project from the remote system") : TTR("Run exported project on remote Windows system"); |
| 969 | } |
| 970 | |
| 971 | void EditorExportPlatformWindows::cleanup() { |
| 972 | if (ssh_pid != 0 && OS::get_singleton()->is_process_running(ssh_pid)) { |
| 973 | print_line("Terminating connection..."); |
| 974 | OS::get_singleton()->kill(ssh_pid); |
| 975 | OS::get_singleton()->delay_usec(1000); |
| 976 | } |
| 977 | |
| 978 | if (!cleanup_commands.is_empty()) { |
| 979 | print_line("Stopping and deleting previous version..."); |
| 980 | for (const SSHCleanupCommand &cmd : cleanup_commands) { |
| 981 | if (cmd.wait) { |
| 982 | ssh_run_on_remote(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args); |
| 983 | } else { |
| 984 | ssh_run_on_remote_no_wait(cmd.host, cmd.port, cmd.ssh_args, cmd.cmd_args); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | ssh_pid = 0; |
| 989 | cleanup_commands.clear(); |
| 990 | } |
| 991 | |
| 992 | Error EditorExportPlatformWindows::run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) { |
| 993 | cleanup(); |
| 994 | if (p_device) { // Stop command, cleanup only. |
| 995 | return OK; |
| 996 | } |
| 997 | |
| 998 | EditorProgress ep("run", TTR("Running..."), 5); |
| 999 | |
| 1000 | const String dest = EditorPaths::get_singleton()->get_temp_dir().path_join("windows"); |
| 1001 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 1002 | if (!da->dir_exists(dest)) { |
| 1003 | Error err = da->make_dir_recursive(dest); |
| 1004 | if (err != OK) { |
| 1005 | EditorNode::get_singleton()->show_warning(TTR("Could not create temp directory:") + "\n" + dest); |
| 1006 | return err; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | String host = p_preset->get("ssh_remote_deploy/host").operator String(); |
| 1011 | String port = p_preset->get("ssh_remote_deploy/port").operator String(); |
| 1012 | if (port.is_empty()) { |
| 1013 | port = "22"; |
| 1014 | } |
| 1015 | Vector<String> extra_args_ssh = p_preset->get("ssh_remote_deploy/extra_args_ssh").operator String().split(" ", false); |
| 1016 | Vector<String> extra_args_scp = p_preset->get("ssh_remote_deploy/extra_args_scp").operator String().split(" ", false); |
| 1017 | |
| 1018 | const String basepath = dest.path_join("tmp_windows_export"); |
| 1019 | |
| 1020 | #define CLEANUP_AND_RETURN(m_err) \ |
| 1021 | { \ |
| 1022 | if (da->file_exists(basepath + ".zip")) { \ |
| 1023 | da->remove(basepath + ".zip"); \ |
| 1024 | } \ |
| 1025 | if (da->file_exists(basepath + "_start.ps1")) { \ |
| 1026 | da->remove(basepath + "_start.ps1"); \ |
| 1027 | } \ |
| 1028 | if (da->file_exists(basepath + "_clean.ps1")) { \ |
| 1029 | da->remove(basepath + "_clean.ps1"); \ |
| 1030 | } \ |
| 1031 | return m_err; \ |
| 1032 | } \ |
| 1033 | ((void)0) |
| 1034 | |
| 1035 | if (ep.step(TTR("Exporting project..."), 1)) { |
| 1036 | return ERR_SKIP; |
| 1037 | } |
| 1038 | Error err = export_project(p_preset, true, basepath + ".zip", p_debug_flags); |
| 1039 | if (err != OK) { |
| 1040 | DirAccess::remove_file_or_error(basepath + ".zip"); |
| 1041 | return err; |
| 1042 | } |
| 1043 | |
| 1044 | String cmd_args; |
| 1045 | { |
| 1046 | Vector<String> cmd_args_list = gen_export_flags(p_debug_flags); |
| 1047 | for (int i = 0; i < cmd_args_list.size(); i++) { |
| 1048 | if (i != 0) { |
| 1049 | cmd_args += " "; |
| 1050 | } |
| 1051 | cmd_args += cmd_args_list[i]; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | const bool use_remote = p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG) || p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT); |
| 1056 | int dbg_port = EDITOR_GET("network/debug/remote_port"); |
| 1057 | |
| 1058 | print_line("Creating temporary directory..."); |
| 1059 | ep.step(TTR("Creating temporary directory..."), 2); |
| 1060 | String temp_dir; |
| 1061 | #ifndef WINDOWS_ENABLED |
| 1062 | err = ssh_run_on_remote(host, port, extra_args_ssh, "powershell -command \\\"\\$tmp = Join-Path \\$Env:Temp \\$(New-Guid); New-Item -Type Directory -Path \\$tmp | Out-Null; Write-Output \\$tmp\\\"", &temp_dir); |
| 1063 | #else |
| 1064 | err = ssh_run_on_remote(host, port, extra_args_ssh, "powershell -command \"$tmp = Join-Path $Env:Temp $(New-Guid); New-Item -Type Directory -Path $tmp ^| Out-Null; Write-Output $tmp\"", &temp_dir); |
| 1065 | #endif |
| 1066 | if (err != OK || temp_dir.is_empty()) { |
| 1067 | CLEANUP_AND_RETURN(err); |
| 1068 | } |
| 1069 | |
| 1070 | print_line("Uploading archive..."); |
| 1071 | ep.step(TTR("Uploading archive..."), 3); |
| 1072 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + ".zip", temp_dir); |
| 1073 | if (err != OK) { |
| 1074 | CLEANUP_AND_RETURN(err); |
| 1075 | } |
| 1076 | |
| 1077 | if (cmd_args.is_empty()) { |
| 1078 | cmd_args = " "; |
| 1079 | } |
| 1080 | |
| 1081 | { |
| 1082 | String run_script = p_preset->get("ssh_remote_deploy/run_script"); |
| 1083 | run_script = run_script.replace("{temp_dir}", temp_dir); |
| 1084 | run_script = run_script.replace("{archive_name}", basepath.get_file() + ".zip"); |
| 1085 | run_script = run_script.replace("{exe_name}", basepath.get_file() + ".exe"); |
| 1086 | run_script = run_script.replace("{cmd_args}", cmd_args); |
| 1087 | |
| 1088 | Ref<FileAccess> f = FileAccess::open(basepath + "_start.ps1", FileAccess::WRITE); |
| 1089 | if (f.is_null()) { |
| 1090 | CLEANUP_AND_RETURN(err); |
| 1091 | } |
| 1092 | |
| 1093 | f->store_string(run_script); |
| 1094 | } |
| 1095 | |
| 1096 | { |
| 1097 | String clean_script = p_preset->get("ssh_remote_deploy/cleanup_script"); |
| 1098 | clean_script = clean_script.replace("{temp_dir}", temp_dir); |
| 1099 | clean_script = clean_script.replace("{archive_name}", basepath.get_file() + ".zip"); |
| 1100 | clean_script = clean_script.replace("{exe_name}", basepath.get_file() + ".exe"); |
| 1101 | clean_script = clean_script.replace("{cmd_args}", cmd_args); |
| 1102 | |
| 1103 | Ref<FileAccess> f = FileAccess::open(basepath + "_clean.ps1", FileAccess::WRITE); |
| 1104 | if (f.is_null()) { |
| 1105 | CLEANUP_AND_RETURN(err); |
| 1106 | } |
| 1107 | |
| 1108 | f->store_string(clean_script); |
| 1109 | } |
| 1110 | |
| 1111 | print_line("Uploading scripts..."); |
| 1112 | ep.step(TTR("Uploading scripts..."), 4); |
| 1113 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_start.ps1", temp_dir); |
| 1114 | if (err != OK) { |
| 1115 | CLEANUP_AND_RETURN(err); |
| 1116 | } |
| 1117 | err = ssh_push_to_remote(host, port, extra_args_scp, basepath + "_clean.ps1", temp_dir); |
| 1118 | if (err != OK) { |
| 1119 | CLEANUP_AND_RETURN(err); |
| 1120 | } |
| 1121 | |
| 1122 | print_line("Starting project..."); |
| 1123 | ep.step(TTR("Starting project..."), 5); |
| 1124 | err = ssh_run_on_remote_no_wait(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_start.ps1"), &ssh_pid, (use_remote) ? dbg_port : -1); |
| 1125 | if (err != OK) { |
| 1126 | CLEANUP_AND_RETURN(err); |
| 1127 | } |
| 1128 | |
| 1129 | cleanup_commands.clear(); |
| 1130 | cleanup_commands.push_back(SSHCleanupCommand(host, port, extra_args_ssh, vformat("powershell -file \"%s\\%s\"", temp_dir, basepath.get_file() + "_clean.ps1"))); |
| 1131 | |
| 1132 | print_line("Project started."); |
| 1133 | |
| 1134 | CLEANUP_AND_RETURN(OK); |
| 1135 | #undef CLEANUP_AND_RETURN |
| 1136 | } |
| 1137 | |
| 1138 | void EditorExportPlatformWindows::initialize() { |
| 1139 | if (EditorNode::get_singleton()) { |
| 1140 | Ref<Image> img = memnew(Image); |
| 1141 | const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE); |
| 1142 | |
| 1143 | ImageLoaderSVG::create_image_from_string(img, _windows_logo_svg, EDSCALE, upsample, false); |
| 1144 | set_logo(ImageTexture::create_from_image(img)); |
| 1145 | |
| 1146 | ImageLoaderSVG::create_image_from_string(img, _windows_run_icon_svg, EDSCALE, upsample, false); |
| 1147 | run_icon = ImageTexture::create_from_image(img); |
| 1148 | |
| 1149 | Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme(); |
| 1150 | if (theme.is_valid()) { |
| 1151 | stop_icon = theme->get_icon(SNAME("Stop"), EditorStringName(EditorIcons)); |
| 1152 | } else { |
| 1153 | stop_icon.instantiate(); |
| 1154 | } |
| 1155 | } |
| 1156 | } |