| 1 | /**************************************************************************/ |
| 2 | /* editor_export_platform_pc.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 "editor_export_platform_pc.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/dir_access.h" |
| 35 | #include "core/os/shared_object.h" |
| 36 | #include "scene/resources/image_texture.h" |
| 37 | |
| 38 | void EditorExportPlatformPC::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const { |
| 39 | if (p_preset->get("texture_format/s3tc_bptc")) { |
| 40 | r_features->push_back("s3tc"); |
| 41 | r_features->push_back("bptc"); |
| 42 | } |
| 43 | if (p_preset->get("texture_format/etc2_astc")) { |
| 44 | r_features->push_back("etc2"); |
| 45 | r_features->push_back("astc"); |
| 46 | } |
| 47 | if (!p_preset->is_dedicated_server() && p_preset->get("shader_baker/enabled")) { |
| 48 | // Don't use the shader baker if exporting as a dedicated server, as no rendering is performed. |
| 49 | r_features->push_back("shader_baker"); |
| 50 | } |
| 51 | // PC platforms only have one architecture per export, since |
| 52 | // we export a single executable instead of a bundle. |
| 53 | r_features->push_back(p_preset->get("binary_format/architecture")); |
| 54 | } |
| 55 | |
| 56 | void EditorExportPlatformPC::get_export_options(List<ExportOption> *r_options) const { |
| 57 | String ext_filter = (get_os_name() == "Windows") ? "*.exe" : ""; |
| 58 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, ext_filter), "")); |
| 59 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, ext_filter), "")); |
| 60 | |
| 61 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "debug/export_console_wrapper", PROPERTY_HINT_ENUM, "No,Debug Only,Debug and Release"), 1)); |
| 62 | |
| 63 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "binary_format/embed_pck"), false)); |
| 64 | |
| 65 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc_bptc"), true)); |
| 66 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2_astc"), false)); |
| 67 | |
| 68 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "shader_baker/enabled"), false)); |
| 69 | } |
| 70 | |
| 71 | String EditorExportPlatformPC::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const { |
| 72 | if (p_name == "shader_baker/enabled" && bool(p_preset->get("shader_baker/enabled"))) { |
| 73 | String export_renderer = GLOBAL_GET("rendering/renderer/rendering_method"); |
| 74 | if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") { |
| 75 | return TTR("\"Shader Baker\" is not supported when using the Compatibility renderer."); |
| 76 | } else if (OS::get_singleton()->get_current_rendering_method() != export_renderer) { |
| 77 | return vformat(TTR("The editor is currently using a different renderer than what the target platform will use. \"Shader Baker\" won't be able to include core shaders. Switch to the \"%s\" renderer temporarily to fix this."), export_renderer); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return String(); |
| 82 | } |
| 83 | |
| 84 | String EditorExportPlatformPC::get_name() const { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | String EditorExportPlatformPC::get_os_name() const { |
| 89 | return os_name; |
| 90 | } |
| 91 | |
| 92 | Ref<Texture2D> EditorExportPlatformPC::get_logo() const { |
| 93 | return logo; |
| 94 | } |
| 95 | |
| 96 | bool EditorExportPlatformPC::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const { |
| 97 | String err; |
| 98 | bool valid = false; |
| 99 | |
| 100 | // Look for export templates (first official, and if defined custom templates). |
| 101 | String arch = p_preset->get("binary_format/architecture"); |
| 102 | bool dvalid = exists_export_template(get_template_file_name("debug", arch), &err); |
| 103 | bool rvalid = exists_export_template(get_template_file_name("release", arch), &err); |
| 104 | |
| 105 | if (p_preset->get("custom_template/debug") != "") { |
| 106 | dvalid = FileAccess::exists(p_preset->get("custom_template/debug")); |
| 107 | if (!dvalid) { |
| 108 | err += TTR("Custom debug template not found.") + "\n"; |
| 109 | } |
| 110 | } |
| 111 | if (p_preset->get("custom_template/release") != "") { |
| 112 | rvalid = FileAccess::exists(p_preset->get("custom_template/release")); |
| 113 | if (!rvalid) { |
| 114 | err += TTR("Custom release template not found.") + "\n"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | valid = dvalid || rvalid; |
| 119 | r_missing_templates = !valid; |
| 120 | |
| 121 | bool uses_s3tc_bptc = p_preset->get("texture_format/s3tc_bptc"); |
| 122 | bool uses_etc2_astc = p_preset->get("texture_format/etc2_astc"); |
| 123 | |
| 124 | if (!uses_s3tc_bptc && !uses_etc2_astc) { |
| 125 | valid = false; |
| 126 | err += TTR("A texture format must be selected to export the project. Please select at least one texture format."); |
| 127 | } |
| 128 | |
| 129 | if (!err.is_empty()) { |
| 130 | r_error = err; |
| 131 | } |
| 132 | return valid; |
| 133 | } |
| 134 | |
| 135 | bool EditorExportPlatformPC::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const { |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | Error EditorExportPlatformPC::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 140 | ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags); |
| 141 | |
| 142 | Error err = prepare_template(p_preset, p_debug, p_path, p_flags); |
| 143 | if (err == OK) { |
| 144 | err = modify_template(p_preset, p_debug, p_path, p_flags); |
| 145 | } |
| 146 | if (err == OK) { |
| 147 | err = export_project_data(p_preset, p_debug, p_path, p_flags); |
| 148 | } |
| 149 | |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | Error EditorExportPlatformPC::prepare_template(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 154 | if (!DirAccess::exists(p_path.get_base_dir())) { |
| 155 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Template"), TTR("The given export path doesn't exist.")); |
| 156 | return ERR_FILE_BAD_PATH; |
| 157 | } |
| 158 | |
| 159 | String custom_debug = p_preset->get("custom_template/debug"); |
| 160 | String custom_release = p_preset->get("custom_template/release"); |
| 161 | |
| 162 | String template_path = p_debug ? custom_debug : custom_release; |
| 163 | |
| 164 | template_path = template_path.strip_edges(); |
| 165 | |
| 166 | if (template_path.is_empty()) { |
| 167 | template_path = find_export_template(get_template_file_name(p_debug ? "debug" : "release", p_preset->get("binary_format/architecture"))); |
| 168 | } |
| 169 | |
| 170 | if (!template_path.is_empty() && !FileAccess::exists(template_path)) { |
| 171 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Template"), vformat(TTR("Template file not found: \"%s\"."), template_path)); |
| 172 | return ERR_FILE_NOT_FOUND; |
| 173 | } |
| 174 | |
| 175 | // Matching the extensions in platform/windows/console_wrapper_windows.cpp |
| 176 | static const char *const wrapper_extensions[] = { |
| 177 | ".console.exe", |
| 178 | "_console.exe", |
| 179 | " console.exe", |
| 180 | "console.exe", |
| 181 | nullptr, |
| 182 | }; |
| 183 | int con_wrapper_mode = p_preset->get("debug/export_console_wrapper"); |
| 184 | bool copy_wrapper = (con_wrapper_mode == 1 && p_debug) || (con_wrapper_mode == 2); |
| 185 | |
| 186 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 187 | da->make_dir_recursive(p_path.get_base_dir()); |
| 188 | Error err = da->copy(template_path, p_path, get_chmod_flags()); |
| 189 | if (err == OK && copy_wrapper) { |
| 190 | for (int i = 0; wrapper_extensions[i]; ++i) { |
| 191 | const String wrapper_path = template_path.get_basename() + wrapper_extensions[i]; |
| 192 | if (FileAccess::exists(wrapper_path)) { |
| 193 | err = da->copy(wrapper_path, p_path.get_basename() + ".console.exe", get_chmod_flags()); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | if (err != OK) { |
| 199 | add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Template"), TTR("Failed to copy export template.")); |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | Error EditorExportPlatformPC::export_project_data(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 207 | String pck_path; |
| 208 | if (p_preset->get("binary_format/embed_pck")) { |
| 209 | pck_path = p_path; |
| 210 | } else { |
| 211 | pck_path = p_path.get_basename() + ".pck"; |
| 212 | } |
| 213 | |
| 214 | Vector<SharedObject> so_files; |
| 215 | |
| 216 | int64_t embedded_pos; |
| 217 | int64_t embedded_size; |
| 218 | Error err = save_pack(p_preset, p_debug, pck_path, &so_files, nullptr, nullptr, p_preset->get("binary_format/embed_pck"), &embedded_pos, &embedded_size); |
| 219 | if (err == OK && p_preset->get("binary_format/embed_pck")) { |
| 220 | if (embedded_size >= 0x100000000 && String(p_preset->get("binary_format/architecture")).contains("32")) { |
| 221 | add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("On 32-bit exports the embedded PCK cannot be bigger than 4 GiB.")); |
| 222 | return ERR_INVALID_PARAMETER; |
| 223 | } |
| 224 | |
| 225 | err = fixup_embedded_pck(p_path, embedded_pos, embedded_size); |
| 226 | } |
| 227 | |
| 228 | if (err == OK && !so_files.is_empty()) { |
| 229 | // If shared object files, copy them. |
| 230 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 231 | for (int i = 0; i < so_files.size() && err == OK; i++) { |
| 232 | String src_path = ProjectSettings::get_singleton()->globalize_path(so_files[i].path); |
| 233 | String target_path; |
| 234 | if (so_files[i].target.is_empty()) { |
| 235 | target_path = p_path.get_base_dir(); |
| 236 | } else { |
| 237 | target_path = p_path.get_base_dir().path_join(so_files[i].target); |
| 238 | da->make_dir_recursive(target_path); |
| 239 | } |
| 240 | target_path = target_path.path_join(src_path.get_file()); |
| 241 | |
| 242 | if (da->dir_exists(src_path)) { |
| 243 | err = da->make_dir_recursive(target_path); |
| 244 | if (err == OK) { |
| 245 | err = da->copy_dir(src_path, target_path, -1, true); |
| 246 | if (err != OK) { |
| 247 | add_message(EXPORT_MESSAGE_ERROR, TTR("GDExtension"), vformat(TTR("Failed to copy shared object \"%s\"."), src_path)); |
| 248 | } |
| 249 | } |
| 250 | } else { |
| 251 | err = da->copy(src_path, target_path); |
| 252 | if (err != OK) { |
| 253 | add_message(EXPORT_MESSAGE_ERROR, TTR("GDExtension"), vformat(TTR("Failed to copy shared object \"%s\"."), src_path)); |
| 254 | } |
| 255 | if (err == OK) { |
| 256 | err = sign_shared_object(p_preset, p_debug, target_path); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | Error EditorExportPlatformPC::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) { |
| 266 | return OK; |
| 267 | } |
| 268 | |
| 269 | void EditorExportPlatformPC::set_name(const String &p_name) { |
| 270 | name = p_name; |
| 271 | } |
| 272 | |
| 273 | void EditorExportPlatformPC::set_os_name(const String &p_name) { |
| 274 | os_name = p_name; |
| 275 | } |
| 276 | |
| 277 | void EditorExportPlatformPC::set_logo(const Ref<Texture2D> &p_logo) { |
| 278 | logo = p_logo; |
| 279 | } |
| 280 | |
| 281 | void EditorExportPlatformPC::get_platform_features(List<String> *r_features) const { |
| 282 | r_features->push_back("pc"); // Identify PC platforms as such. |
| 283 | } |
| 284 | |
| 285 | void EditorExportPlatformPC::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) { |
| 286 | } |
| 287 | |
| 288 | int EditorExportPlatformPC::get_chmod_flags() const { |
| 289 | return chmod_flags; |
| 290 | } |
| 291 | |
| 292 | void EditorExportPlatformPC::set_chmod_flags(int p_flags) { |
| 293 | chmod_flags = p_flags; |
| 294 | } |