Path: blob/master/platform/windows/export/template_modifier.cpp
10278 views
/**************************************************************************/1/* template_modifier.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "template_modifier.h"3132#include "core/config/project_settings.h"3334void TemplateModifier::ByteStream::save(uint8_t p_value, Vector<uint8_t> &r_bytes) const {35save(p_value, r_bytes, 1);36}3738void TemplateModifier::ByteStream::save(uint16_t p_value, Vector<uint8_t> &r_bytes) const {39save(p_value, r_bytes, 2);40}4142void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes) const {43save(p_value, r_bytes, 4);44}4546void TemplateModifier::ByteStream::save(const String &p_value, Vector<uint8_t> &r_bytes) const {47r_bytes.append_array(p_value.to_utf16_buffer());48save((uint16_t)0, r_bytes);49}5051void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes, uint32_t p_count) const {52for (uint32_t i = 0; i < p_count; i++) {53r_bytes.append((uint8_t)(p_value & 0xff));54p_value >>= 8;55}56}5758Vector<uint8_t> TemplateModifier::ByteStream::save() const {59return Vector<uint8_t>();60}6162Vector<uint8_t> TemplateModifier::Structure::save() const {63Vector<uint8_t> bytes;64ByteStream::save(length, bytes);65ByteStream::save(value_length, bytes);66ByteStream::save(type, bytes);67ByteStream::save(key, bytes);68while (bytes.size() % 4) {69bytes.append(0);70}71return bytes;72}7374Vector<uint8_t> &TemplateModifier::Structure::add_length(Vector<uint8_t> &r_bytes) const {75r_bytes.write[0] = r_bytes.size() & 0xff;76r_bytes.write[1] = r_bytes.size() >> 8 & 0xff;77return r_bytes;78}7980Vector<uint8_t> TemplateModifier::ResourceDirectoryTable::save() const {81Vector<uint8_t> bytes;82bytes.resize_initialized(12);83ByteStream::save(name_entry_count, bytes);84ByteStream::save(id_entry_count, bytes);85return bytes;86}8788Vector<uint8_t> TemplateModifier::ResourceDirectoryEntry::save() const {89Vector<uint8_t> bytes;90ByteStream::save(id | (name ? HIGH_BIT : 0), bytes);91ByteStream::save(data_offset | (subdirectory ? HIGH_BIT : 0), bytes);92return bytes;93}9495Vector<uint8_t> TemplateModifier::FixedFileInfo::save() const {96Vector<uint8_t> bytes;97ByteStream::save(signature, bytes);98ByteStream::save(struct_version, bytes);99ByteStream::save(file_version_ms, bytes);100ByteStream::save(file_version_ls, bytes);101ByteStream::save(product_version_ms, bytes);102ByteStream::save(product_version_ls, bytes);103ByteStream::save(file_flags_mask, bytes);104ByteStream::save(file_flags, bytes);105ByteStream::save(file_os, bytes);106ByteStream::save(file_type, bytes);107ByteStream::save(file_subtype, bytes);108ByteStream::save(file_date_ms, bytes);109ByteStream::save(file_date_ls, bytes);110return bytes;111}112113void TemplateModifier::FixedFileInfo::set_file_version(const String &p_file_version) {114Vector<String> parts = p_file_version.split(".");115while (parts.size() < 4) {116parts.append("0");117}118file_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);119file_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);120}121122void TemplateModifier::FixedFileInfo::set_product_version(const String &p_product_version) {123Vector<String> parts = p_product_version.split(".");124while (parts.size() < 4) {125parts.append("0");126}127product_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);128product_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);129}130131Vector<uint8_t> TemplateModifier::StringStructure::save() const {132Vector<uint8_t> bytes = Structure::save();133ByteStream::save(value, bytes);134return add_length(bytes);135}136137TemplateModifier::StringStructure::StringStructure() {138type = 1;139}140141TemplateModifier::StringStructure::StringStructure(const String &p_key, const String &p_value) {142type = 1;143value_length = p_value.length() + 1;144key = p_key;145value = p_value;146}147148Vector<uint8_t> TemplateModifier::StringTable::save() const {149Vector<uint8_t> bytes = Structure::save();150for (const StringStructure &string : strings) {151bytes.append_array(string.save());152while (bytes.size() % 4) {153bytes.append(0);154}155}156return add_length(bytes);157}158159void TemplateModifier::StringTable::put(const String &p_key, const String &p_value) {160strings.append(StringStructure(p_key, p_value));161}162163TemplateModifier::StringTable::StringTable() {164key = "040904b0";165type = 1;166}167168TemplateModifier::StringFileInfo::StringFileInfo() {169key = "StringFileInfo";170value_length = 0;171type = 1;172}173174Vector<uint8_t> TemplateModifier::StringFileInfo::save() const {175Vector<uint8_t> bytes = Structure::save();176bytes.append_array(string_table.save());177return add_length(bytes);178}179180Vector<uint8_t> TemplateModifier::Var::save() const {181Vector<uint8_t> bytes = Structure::save();182ByteStream::save(value, bytes);183return add_length(bytes);184}185186TemplateModifier::Var::Var() {187value_length = 4;188key = "Translation";189}190191Vector<uint8_t> TemplateModifier::VarFileInfo::save() const {192Vector<uint8_t> bytes = Structure::save();193bytes.append_array(var.save());194return add_length(bytes);195}196197TemplateModifier::VarFileInfo::VarFileInfo() {198type = 1;199key = "VarFileInfo";200}201202Vector<uint8_t> TemplateModifier::VersionInfo::save() const {203Vector<uint8_t> fixed_file_info = value.save();204Vector<uint8_t> bytes = Structure::save();205bytes.append_array(fixed_file_info);206bytes.append_array(string_file_info.save());207while (bytes.size() % 4) {208bytes.append(0);209}210bytes.append_array(var_file_info.save());211return add_length(bytes);212}213214TemplateModifier::VersionInfo::VersionInfo() {215key = "VS_VERSION_INFO";216value_length = 52;217}218219Vector<uint8_t> TemplateModifier::IconEntry::save() const {220Vector<uint8_t> bytes;221ByteStream::save(width, bytes);222ByteStream::save(height, bytes);223ByteStream::save(colors, bytes);224ByteStream::save((uint8_t)0, bytes);225ByteStream::save(planes, bytes);226ByteStream::save(bits_per_pixel, bytes);227ByteStream::save(image_size, bytes);228ByteStream::save((uint16_t)image_offset, bytes);229return bytes;230}231232void TemplateModifier::IconEntry::load(Ref<FileAccess> p_file) {233width = p_file->get_8(); // Width in pixels.234height = p_file->get_8(); // Height in pixels.235colors = p_file->get_8(); // Number of colors in the palette (0 - no palette).236p_file->get_8(); // Reserved.237planes = p_file->get_16(); // Number of color planes.238bits_per_pixel = p_file->get_16(); // Bits per pixel.239image_size = p_file->get_32(); // Image data size in bytes.240image_offset = p_file->get_32(); // Image data offset.241}242243Vector<uint8_t> TemplateModifier::GroupIcon::save() const {244Vector<uint8_t> bytes;245ByteStream::save(reserved, bytes);246ByteStream::save(type, bytes);247ByteStream::save(image_count, bytes);248for (const IconEntry &icon_entry : icon_entries) {249bytes.append_array(icon_entry.save());250}251return bytes;252}253254void TemplateModifier::GroupIcon::load(Ref<FileAccess> p_icon_file) {255if (p_icon_file->get_32() != 0x10000) { // Wrong reserved bytes256ERR_FAIL_MSG("Wrong icon file type.");257}258259image_count = p_icon_file->get_16();260for (uint16_t i = 0; i < image_count; i++) {261IconEntry icon_entry;262icon_entry.load(p_icon_file);263icon_entries.append(icon_entry);264}265266int id = 1;267for (IconEntry &icon_entry : icon_entries) {268Vector<uint8_t> image;269image.resize(icon_entry.image_size);270p_icon_file->seek(icon_entry.image_offset);271p_icon_file->get_buffer(image.ptrw(), image.size());272icon_entry.image_offset = id++;273images.append(image);274}275}276277void TemplateModifier::GroupIcon::fill_with_godot_blue() {278uint32_t id = 1;279for (uint8_t size : SIZES) {280Ref<Image> image = Image::create_empty(size ? size : 256, size ? size : 256, false, Image::FORMAT_RGB8);281image->fill(Color::hex(0x478cbfff));282Vector<uint8_t> data = image->save_png_to_buffer();283IconEntry icon_entry;284icon_entry.width = size;285icon_entry.height = size;286icon_entry.bits_per_pixel = 24;287icon_entry.image_size = data.size();288icon_entry.image_offset = id++;289icon_entries.append(icon_entry);290images.append(data);291}292}293294Vector<uint8_t> TemplateModifier::SectionEntry::save() const {295Vector<uint8_t> bytes;296bytes.append_array(name.to_utf8_buffer());297while (bytes.size() < 8) {298bytes.append(0);299}300ByteStream::save(virtual_size, bytes);301ByteStream::save(virtual_address, bytes);302ByteStream::save(size_of_raw_data, bytes);303ByteStream::save(pointer_to_raw_data, bytes);304ByteStream::save(pointer_to_relocations, bytes);305ByteStream::save(pointer_to_line_numbers, bytes);306ByteStream::save(number_of_relocations, bytes);307ByteStream::save(number_of_line_numbers, bytes);308ByteStream::save(characteristics, bytes);309return bytes;310}311312void TemplateModifier::SectionEntry::load(Ref<FileAccess> p_file) {313uint8_t section_name[8];314p_file->get_buffer(section_name, 8);315name = String::utf8((char *)section_name, 8);316virtual_size = p_file->get_32();317virtual_address = p_file->get_32();318size_of_raw_data = p_file->get_32();319pointer_to_raw_data = p_file->get_32();320pointer_to_relocations = p_file->get_32();321pointer_to_line_numbers = p_file->get_32();322number_of_relocations = p_file->get_16();323number_of_line_numbers = p_file->get_16();324characteristics = p_file->get_32();325}326327Vector<uint8_t> TemplateModifier::ResourceDataEntry::save() const {328Vector<uint8_t> bytes;329ByteStream::save(rva, bytes);330ByteStream::save(size, bytes);331ByteStream::save(0, bytes, 8);332return bytes;333}334335uint32_t TemplateModifier::_get_pe_header_offset(Ref<FileAccess> p_executable) const {336p_executable->seek(POINTER_TO_PE_HEADER_OFFSET);337uint32_t pe_header_offset = p_executable->get_32();338339p_executable->seek(pe_header_offset);340uint32_t magic = p_executable->get_32();341342return magic == 0x00004550 ? pe_header_offset : 0;343}344345uint32_t TemplateModifier::_snap(uint32_t p_value, uint32_t p_size) const {346return p_value + (p_value % p_size ? p_size - (p_value % p_size) : 0);347}348349Vector<uint8_t> TemplateModifier::_create_resources(uint32_t p_virtual_address, const GroupIcon &p_group_icon, const VersionInfo &p_version_info) const {350// 0x04, 0x00 as string length ICON in UTF16 and padding to 32 bits351const uint8_t ICON_DIRECTORY_STRING[] = { 0x04, 0x00, 0x49, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00, 0x00, 0x00 };352const uint16_t RT_ENTRY_COUNT = 3;353const uint32_t icon_count = p_group_icon.images.size();354355ResourceDirectoryTable root_directory_table;356root_directory_table.id_entry_count = RT_ENTRY_COUNT;357358Vector<uint8_t> resources = root_directory_table.save();359360ResourceDirectoryEntry rt_icon_entry;361rt_icon_entry.id = ResourceDirectoryEntry::ICON;362rt_icon_entry.data_offset = ResourceDirectoryTable::SIZE + RT_ENTRY_COUNT * ResourceDirectoryEntry::SIZE;363rt_icon_entry.subdirectory = true;364resources.append_array(rt_icon_entry.save());365366ResourceDirectoryEntry rt_group_icon_entry;367rt_group_icon_entry.id = ResourceDirectoryEntry::GROUP_ICON;368rt_group_icon_entry.data_offset = (2 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count) * ResourceDirectoryEntry::SIZE;369rt_group_icon_entry.subdirectory = true;370resources.append_array(rt_group_icon_entry.save());371372ResourceDirectoryEntry rt_version_entry;373rt_version_entry.id = ResourceDirectoryEntry::VERSION;374rt_version_entry.data_offset = (4 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 2) * ResourceDirectoryEntry::SIZE;375rt_version_entry.subdirectory = true;376resources.append_array(rt_version_entry.save());377378ResourceDirectoryTable icon_table;379icon_table.id_entry_count = icon_count;380resources.append_array(icon_table.save());381382for (uint32_t i = 0; i < icon_count; i++) {383ResourceDirectoryEntry icon_entry;384icon_entry.id = i + 1;385icon_entry.data_offset = (2 + i) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count + i) * ResourceDirectoryEntry::SIZE;386icon_entry.subdirectory = true;387resources.append_array(icon_entry.save());388}389390for (uint32_t i = 0; i < icon_count; i++) {391ResourceDirectoryTable language_icon_table;392language_icon_table.id_entry_count = 1;393resources.append_array(language_icon_table.save());394395ResourceDirectoryEntry language_icon_entry;396language_icon_entry.id = ResourceDirectoryEntry::ENGLISH;397language_icon_entry.data_offset = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count * 2 + 4) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + i * ResourceDataEntry::SIZE;398resources.append_array(language_icon_entry.save());399}400401ResourceDirectoryTable group_icon_name_table;402group_icon_name_table.name_entry_count = 1;403resources.append_array(group_icon_name_table.save());404405ResourceDirectoryEntry group_icon_name_entry;406group_icon_name_entry.id = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count * 2 + 4) * ResourceDirectoryEntry::SIZE;407group_icon_name_entry.data_offset = (3 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 1) * ResourceDirectoryEntry::SIZE;408group_icon_name_entry.name = true;409group_icon_name_entry.subdirectory = true;410resources.append_array(group_icon_name_entry.save());411412ResourceDirectoryTable group_icon_language_table;413group_icon_language_table.id_entry_count = 1;414resources.append_array(group_icon_language_table.save());415416ResourceDirectoryEntry group_icon_language_entry;417group_icon_language_entry.id = ResourceDirectoryEntry::ENGLISH;418group_icon_language_entry.data_offset = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 4) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + icon_count * ResourceDataEntry::SIZE;419resources.append_array(group_icon_language_entry.save());420421ResourceDirectoryTable version_table;422version_table.id_entry_count = 1;423resources.append_array(version_table.save());424425ResourceDirectoryEntry version_entry;426version_entry.id = 1;427version_entry.data_offset = (5 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 3) * ResourceDirectoryEntry::SIZE;428version_entry.subdirectory = true;429resources.append_array(version_entry.save());430431ResourceDirectoryTable version_language_table;432version_language_table.id_entry_count = 1;433resources.append_array(version_language_table.save());434435ResourceDirectoryEntry version_language_entry;436version_language_entry.id = ResourceDirectoryEntry::ENGLISH;437version_language_entry.data_offset = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 4) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + (icon_count + 1) * ResourceDataEntry::SIZE;438resources.append_array(version_language_entry.save());439440Vector<uint8_t> icon_directory_string;441icon_directory_string.resize(sizeof(ICON_DIRECTORY_STRING));442memcpy(icon_directory_string.ptrw(), ICON_DIRECTORY_STRING, sizeof(ICON_DIRECTORY_STRING));443resources.append_array(icon_directory_string);444445Vector<Vector<uint8_t>> data_entries;446for (const Vector<uint8_t> &image : p_group_icon.images) {447data_entries.append(image);448}449data_entries.append(p_group_icon.save());450data_entries.append(p_version_info.save());451452uint32_t offset = resources.size() + data_entries.size() * ResourceDataEntry::SIZE;453454for (const Vector<uint8_t> &data_entry : data_entries) {455ResourceDataEntry resource_data_entry;456resource_data_entry.rva = p_virtual_address + offset;457resource_data_entry.size = data_entry.size();458resources.append_array(resource_data_entry.save());459offset += resource_data_entry.size;460while (offset % 4) {461offset += 1;462}463}464465for (const Vector<uint8_t> &data_entry : data_entries) {466resources.append_array(data_entry);467while (resources.size() % 4) {468resources.append(0);469}470}471472return resources;473}474475TemplateModifier::VersionInfo TemplateModifier::_create_version_info(const HashMap<String, String> &p_strings) const {476StringTable string_table;477for (const KeyValue<String, String> &E : p_strings) {478string_table.put(E.key, E.value);479}480481StringFileInfo string_file_info;482string_file_info.string_table = string_table;483484FixedFileInfo fixed_file_info;485if (p_strings.has("FileVersion")) {486fixed_file_info.set_file_version(p_strings["FileVersion"]);487}488if (p_strings.has("ProductVersion")) {489fixed_file_info.set_product_version(p_strings["ProductVersion"]);490}491492VersionInfo version_info;493version_info.value = fixed_file_info;494version_info.string_file_info = string_file_info;495496return version_info;497}498499TemplateModifier::GroupIcon TemplateModifier::_create_group_icon(const String &p_icon_path) const {500GroupIcon group_icon;501502Ref<FileAccess> icon_file = FileAccess::open(p_icon_path, FileAccess::READ);503if (icon_file.is_null()) {504group_icon.fill_with_godot_blue();505return group_icon;506}507508group_icon.load(icon_file);509510return group_icon;511}512513Error TemplateModifier::_truncate(const String &p_path, uint32_t p_size) const {514Error error;515516Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ, &error);517ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);518519String truncated_path = p_path + ".truncated";520Ref<FileAccess> truncated = FileAccess::open(truncated_path, FileAccess::WRITE, &error);521ERR_FAIL_COND_V(error != OK, ERR_CANT_CREATE);522523truncated->store_buffer(file->get_buffer(p_size));524525file->close();526truncated->close();527528DirAccess::remove_absolute(p_path);529DirAccess::rename_absolute(truncated_path, p_path);530531return error;532}533534HashMap<String, String> TemplateModifier::_get_strings(const Ref<EditorExportPreset> &p_preset) const {535String file_version = p_preset->get_version("application/file_version", true);536String product_version = p_preset->get_version("application/product_version", true);537String company_name = p_preset->get("application/company_name");538String product_name = p_preset->get("application/product_name");539String file_description = p_preset->get("application/file_description");540String copyright = p_preset->get("application/copyright");541String trademarks = p_preset->get("application/trademarks");542543HashMap<String, String> strings;544if (!file_version.is_empty()) {545strings["FileVersion"] = file_version;546}547if (!product_version.is_empty()) {548strings["ProductVersion"] = product_version;549}550if (!company_name.is_empty()) {551strings["CompanyName"] = company_name;552}553if (!product_name.is_empty()) {554strings["ProductName"] = product_name;555}556if (!file_description.is_empty()) {557strings["FileDescription"] = file_description;558}559if (!copyright.is_empty()) {560strings["LegalCopyright"] = copyright;561}562if (!trademarks.is_empty()) {563strings["LegalTrademarks"] = trademarks;564}565566return strings;567}568569Error TemplateModifier::_modify_template(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) const {570Error error;571Ref<FileAccess> template_file = FileAccess::open(p_template_path, FileAccess::READ_WRITE, &error);572ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);573574Vector<SectionEntry> section_entries = _get_section_entries(template_file);575ERR_FAIL_COND_V(section_entries.size() < 2, ERR_CANT_OPEN);576577// Find resource (".rsrc") and relocation (".reloc") sections, usually last two, but ".debug_*" sections (referenced as "/[n]"), symbol table, and string table can follow.578int resource_index = section_entries.size() - 2;579int relocations_index = section_entries.size() - 1;580for (int i = 0; i < section_entries.size(); i++) {581if (section_entries[i].name == ".rsrc") {582resource_index = i;583} else if (section_entries[i].name == ".reloc") {584relocations_index = i;585}586}587588ERR_FAIL_COND_V(section_entries[resource_index].name != ".rsrc", ERR_CANT_OPEN);589ERR_FAIL_COND_V(section_entries[relocations_index].name != ".reloc", ERR_CANT_OPEN);590591uint64_t original_template_size = template_file->get_length();592593GroupIcon group_icon = _create_group_icon(p_icon_path);594595VersionInfo version_info = _create_version_info(_get_strings(p_preset));596597SectionEntry &resources_section_entry = section_entries.write[resource_index];598uint32_t old_resources_size_of_raw_data = resources_section_entry.size_of_raw_data;599Vector<uint8_t> resources = _create_resources(resources_section_entry.virtual_address, group_icon, version_info);600resources_section_entry.virtual_size = resources.size();601resources.resize_initialized(_snap(resources.size(), BLOCK_SIZE));602resources_section_entry.size_of_raw_data = resources.size();603604int32_t raw_size_delta = resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;605uint32_t old_last_section_virtual_address = section_entries.get(section_entries.size() - 1).virtual_address;606607// Some data (e.g. DWARF debug symbols) can be placed after the last section.608uint32_t old_footer_offset = section_entries.get(section_entries.size() - 1).pointer_to_raw_data + section_entries.get(section_entries.size() - 1).size_of_raw_data;609610// Copy and update sections after ".rsrc".611Vector<Vector<uint8_t>> moved_section_data;612uint32_t prev_virtual_address = resources_section_entry.virtual_address;613uint32_t prev_virtual_size = resources_section_entry.virtual_size;614for (int i = resource_index + 1; i < section_entries.size(); i++) {615SectionEntry §ion_entry = section_entries.write[i];616template_file->seek(section_entry.pointer_to_raw_data);617Vector<uint8_t> data = template_file->get_buffer(section_entry.size_of_raw_data);618moved_section_data.push_back(data);619section_entry.pointer_to_raw_data += raw_size_delta;620section_entry.virtual_address = prev_virtual_address + _snap(prev_virtual_size, PE_PAGE_SIZE);621prev_virtual_address = section_entry.virtual_address;622prev_virtual_size = section_entry.virtual_size;623}624625// Copy COFF symbol table and string table after the last section.626uint32_t footer_size = template_file->get_length() - old_footer_offset;627template_file->seek(old_footer_offset);628Vector<uint8_t> footer;629if (footer_size > 0) {630footer = template_file->get_buffer(footer_size);631}632633uint32_t pe_header_offset = _get_pe_header_offset(template_file);634635// Update symbol table pointer.636template_file->seek(pe_header_offset + 12);637uint32_t symbols_offset = template_file->get_32();638if (symbols_offset > resources_section_entry.pointer_to_raw_data) {639template_file->seek(pe_header_offset + 12);640template_file->store_32(symbols_offset + raw_size_delta);641}642643template_file->seek(pe_header_offset + MAGIC_NUMBER_OFFSET);644uint16_t magic_number = template_file->get_16();645ERR_FAIL_COND_V_MSG(magic_number != 0x10b && magic_number != 0x20b, ERR_CANT_OPEN, vformat("Magic number has wrong value: %04x", magic_number));646bool pe32plus = magic_number == 0x20b;647648// Update image size.649template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);650uint32_t size_of_initialized_data = template_file->get_32();651size_of_initialized_data += resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;652template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);653template_file->store_32(size_of_initialized_data);654655template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);656uint32_t size_of_image = template_file->get_32();657size_of_image += section_entries.get(section_entries.size() - 1).virtual_address - old_last_section_virtual_address;658template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);659template_file->store_32(size_of_image);660661uint32_t optional_header_offset = pe_header_offset + COFF_HEADER_SIZE;662663// Update resource section size.664template_file->seek(optional_header_offset + (pe32plus ? 132 : 116));665template_file->store_32(resources_section_entry.virtual_size);666667// Update relocation section size and pointer.668template_file->seek(optional_header_offset + (pe32plus ? 152 : 136));669template_file->store_32(section_entries[relocations_index].virtual_address);670template_file->store_32(section_entries[relocations_index].virtual_size);671672template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * resource_index);673template_file->store_buffer(resources_section_entry.save());674for (int i = resource_index + 1; i < section_entries.size(); i++) {675template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * i);676template_file->store_buffer(section_entries[i].save());677}678679// Write new resource section.680template_file->seek(resources_section_entry.pointer_to_raw_data);681template_file->store_buffer(resources);682// Write the rest of sections.683for (const Vector<uint8_t> &data : moved_section_data) {684template_file->store_buffer(data);685}686// Write footer data.687if (footer_size > 0) {688template_file->store_buffer(footer);689}690691if (template_file->get_position() < original_template_size) {692template_file->close();693_truncate(p_template_path, section_entries.get(section_entries.size() - 1).pointer_to_raw_data + section_entries.get(section_entries.size() - 1).size_of_raw_data + footer_size);694}695696return OK;697}698699Vector<TemplateModifier::SectionEntry> TemplateModifier::_get_section_entries(Ref<FileAccess> p_executable) const {700Vector<SectionEntry> section_entries;701702uint32_t pe_header_offset = _get_pe_header_offset(p_executable);703if (pe_header_offset == 0) {704return section_entries;705}706707p_executable->seek(pe_header_offset + 6);708int num_sections = p_executable->get_16();709p_executable->seek(pe_header_offset + 20);710uint16_t size_of_optional_header = p_executable->get_16();711p_executable->seek(pe_header_offset + COFF_HEADER_SIZE + size_of_optional_header);712713for (int i = 0; i < num_sections; ++i) {714SectionEntry section_entry;715section_entry.load(p_executable);716section_entries.append(section_entry);717}718719return section_entries;720}721722Error TemplateModifier::modify(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) {723TemplateModifier template_modifier;724return template_modifier._modify_template(p_preset, p_template_path, p_icon_path);725}726727728