Path: blob/master/platform/windows/export/template_modifier.cpp
20871 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"33#include "core/io/dir_access.h"34#include "core/io/image.h"3536void TemplateModifier::ByteStream::save(uint8_t p_value, Vector<uint8_t> &r_bytes) const {37save(p_value, r_bytes, 1);38}3940void TemplateModifier::ByteStream::save(uint16_t p_value, Vector<uint8_t> &r_bytes) const {41save(p_value, r_bytes, 2);42}4344void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes) const {45save(p_value, r_bytes, 4);46}4748void TemplateModifier::ByteStream::save(const String &p_value, Vector<uint8_t> &r_bytes) const {49r_bytes.append_array(p_value.to_utf16_buffer());50save((uint16_t)0, r_bytes);51}5253void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes, uint32_t p_count) const {54for (uint32_t i = 0; i < p_count; i++) {55r_bytes.append((uint8_t)(p_value & 0xff));56p_value >>= 8;57}58}5960Vector<uint8_t> TemplateModifier::ByteStream::save() const {61return Vector<uint8_t>();62}6364Vector<uint8_t> TemplateModifier::Structure::save() const {65Vector<uint8_t> bytes;66ByteStream::save(length, bytes);67ByteStream::save(value_length, bytes);68ByteStream::save(type, bytes);69ByteStream::save(key, bytes);70while (bytes.size() % 4) {71bytes.append(0);72}73return bytes;74}7576Vector<uint8_t> &TemplateModifier::Structure::add_length(Vector<uint8_t> &r_bytes) const {77r_bytes.write[0] = r_bytes.size() & 0xff;78r_bytes.write[1] = r_bytes.size() >> 8 & 0xff;79return r_bytes;80}8182Vector<uint8_t> TemplateModifier::ResourceDirectoryTable::save() const {83Vector<uint8_t> bytes;84bytes.resize_initialized(12);85ByteStream::save(name_entry_count, bytes);86ByteStream::save(id_entry_count, bytes);87return bytes;88}8990Vector<uint8_t> TemplateModifier::ResourceDirectoryEntry::save() const {91Vector<uint8_t> bytes;92ByteStream::save(id | (name ? HIGH_BIT : 0), bytes);93ByteStream::save(data_offset | (subdirectory ? HIGH_BIT : 0), bytes);94return bytes;95}9697Vector<uint8_t> TemplateModifier::FixedFileInfo::save() const {98Vector<uint8_t> bytes;99ByteStream::save(signature, bytes);100ByteStream::save(struct_version, bytes);101ByteStream::save(file_version_ms, bytes);102ByteStream::save(file_version_ls, bytes);103ByteStream::save(product_version_ms, bytes);104ByteStream::save(product_version_ls, bytes);105ByteStream::save(file_flags_mask, bytes);106ByteStream::save(file_flags, bytes);107ByteStream::save(file_os, bytes);108ByteStream::save(file_type, bytes);109ByteStream::save(file_subtype, bytes);110ByteStream::save(file_date_ms, bytes);111ByteStream::save(file_date_ls, bytes);112return bytes;113}114115void TemplateModifier::FixedFileInfo::set_file_version(const String &p_file_version) {116Vector<String> parts = p_file_version.split(".");117while (parts.size() < 4) {118parts.append("0");119}120file_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);121file_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);122}123124void TemplateModifier::FixedFileInfo::set_product_version(const String &p_product_version) {125Vector<String> parts = p_product_version.split(".");126while (parts.size() < 4) {127parts.append("0");128}129product_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);130product_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);131}132133Vector<uint8_t> TemplateModifier::StringStructure::save() const {134Vector<uint8_t> bytes = Structure::save();135ByteStream::save(value, bytes);136return add_length(bytes);137}138139TemplateModifier::StringStructure::StringStructure() {140type = 1;141}142143TemplateModifier::StringStructure::StringStructure(const String &p_key, const String &p_value) {144type = 1;145value_length = p_value.length() + 1;146key = p_key;147value = p_value;148}149150Vector<uint8_t> TemplateModifier::StringTable::save() const {151Vector<uint8_t> bytes = Structure::save();152for (const StringStructure &string : strings) {153bytes.append_array(string.save());154while (bytes.size() % 4) {155bytes.append(0);156}157}158return add_length(bytes);159}160161void TemplateModifier::StringTable::put(const String &p_key, const String &p_value) {162strings.append(StringStructure(p_key, p_value));163}164165TemplateModifier::StringTable::StringTable() {166key = "040904b0";167type = 1;168}169170TemplateModifier::StringFileInfo::StringFileInfo() {171key = "StringFileInfo";172value_length = 0;173type = 1;174}175176Vector<uint8_t> TemplateModifier::StringFileInfo::save() const {177Vector<uint8_t> bytes = Structure::save();178bytes.append_array(string_table.save());179return add_length(bytes);180}181182Vector<uint8_t> TemplateModifier::Var::save() const {183Vector<uint8_t> bytes = Structure::save();184ByteStream::save(value, bytes);185return add_length(bytes);186}187188TemplateModifier::Var::Var() {189value_length = 4;190key = "Translation";191}192193Vector<uint8_t> TemplateModifier::VarFileInfo::save() const {194Vector<uint8_t> bytes = Structure::save();195bytes.append_array(var.save());196return add_length(bytes);197}198199TemplateModifier::VarFileInfo::VarFileInfo() {200type = 1;201key = "VarFileInfo";202}203204Vector<uint8_t> TemplateModifier::VersionInfo::save() const {205Vector<uint8_t> fixed_file_info = value.save();206Vector<uint8_t> bytes = Structure::save();207bytes.append_array(fixed_file_info);208bytes.append_array(string_file_info.save());209while (bytes.size() % 4) {210bytes.append(0);211}212bytes.append_array(var_file_info.save());213return add_length(bytes);214}215216TemplateModifier::VersionInfo::VersionInfo() {217key = "VS_VERSION_INFO";218value_length = 52;219}220221Vector<uint8_t> TemplateModifier::ManifestInfo::save() const {222Vector<uint8_t> bytes = manifest.to_utf8_buffer();223return bytes;224}225226Vector<uint8_t> TemplateModifier::IconEntry::save() const {227Vector<uint8_t> bytes;228ByteStream::save(width, bytes);229ByteStream::save(height, bytes);230ByteStream::save(colors, bytes);231ByteStream::save((uint8_t)0, bytes);232ByteStream::save(planes, bytes);233ByteStream::save(bits_per_pixel, bytes);234ByteStream::save(image_size, bytes);235ByteStream::save((uint16_t)image_offset, bytes);236return bytes;237}238239void TemplateModifier::IconEntry::load(Ref<FileAccess> p_file) {240width = p_file->get_8(); // Width in pixels.241height = p_file->get_8(); // Height in pixels.242colors = p_file->get_8(); // Number of colors in the palette (0 - no palette).243p_file->get_8(); // Reserved.244planes = p_file->get_16(); // Number of color planes.245bits_per_pixel = p_file->get_16(); // Bits per pixel.246image_size = p_file->get_32(); // Image data size in bytes.247image_offset = p_file->get_32(); // Image data offset.248}249250Vector<uint8_t> TemplateModifier::GroupIcon::save() const {251Vector<uint8_t> bytes;252ByteStream::save(reserved, bytes);253ByteStream::save(type, bytes);254ByteStream::save(image_count, bytes);255for (const IconEntry &icon_entry : icon_entries) {256bytes.append_array(icon_entry.save());257}258return bytes;259}260261void TemplateModifier::GroupIcon::load(Ref<FileAccess> p_icon_file) {262if (p_icon_file->get_32() != 0x10000) { // Wrong reserved bytes263ERR_FAIL_MSG("Wrong icon file type.");264}265266image_count = p_icon_file->get_16();267for (uint16_t i = 0; i < image_count; i++) {268IconEntry icon_entry;269icon_entry.load(p_icon_file);270icon_entries.append(icon_entry);271}272273int id = 1;274for (IconEntry &icon_entry : icon_entries) {275Vector<uint8_t> image;276image.resize(icon_entry.image_size);277p_icon_file->seek(icon_entry.image_offset);278p_icon_file->get_buffer(image.ptrw(), image.size());279icon_entry.image_offset = id++;280images.append(image);281}282}283284void TemplateModifier::GroupIcon::fill_with_godot_blue() {285uint32_t id = 1;286for (uint8_t size : SIZES) {287Ref<Image> image = Image::create_empty(size ? size : 256, size ? size : 256, false, Image::FORMAT_RGB8);288image->fill(Color::hex(0x478cbfff));289Vector<uint8_t> data = image->save_png_to_buffer();290IconEntry icon_entry;291icon_entry.width = size;292icon_entry.height = size;293icon_entry.bits_per_pixel = 24;294icon_entry.image_size = data.size();295icon_entry.image_offset = id++;296icon_entries.append(icon_entry);297images.append(data);298}299}300301Vector<uint8_t> TemplateModifier::SectionEntry::save() const {302Vector<uint8_t> bytes;303bytes.append_array(name.to_utf8_buffer());304while (bytes.size() < 8) {305bytes.append(0);306}307ByteStream::save(virtual_size, bytes);308ByteStream::save(virtual_address, bytes);309ByteStream::save(size_of_raw_data, bytes);310ByteStream::save(pointer_to_raw_data, bytes);311ByteStream::save(pointer_to_relocations, bytes);312ByteStream::save(pointer_to_line_numbers, bytes);313ByteStream::save(number_of_relocations, bytes);314ByteStream::save(number_of_line_numbers, bytes);315ByteStream::save(characteristics, bytes);316return bytes;317}318319void TemplateModifier::SectionEntry::load(Ref<FileAccess> p_file) {320uint8_t section_name[8];321p_file->get_buffer(section_name, 8);322name = String::utf8((char *)section_name, 8);323virtual_size = p_file->get_32();324virtual_address = p_file->get_32();325size_of_raw_data = p_file->get_32();326pointer_to_raw_data = p_file->get_32();327pointer_to_relocations = p_file->get_32();328pointer_to_line_numbers = p_file->get_32();329number_of_relocations = p_file->get_16();330number_of_line_numbers = p_file->get_16();331characteristics = p_file->get_32();332}333334Vector<uint8_t> TemplateModifier::ResourceDataEntry::save() const {335Vector<uint8_t> bytes;336ByteStream::save(rva, bytes);337ByteStream::save(size, bytes);338ByteStream::save(0, bytes, 8);339return bytes;340}341342uint32_t TemplateModifier::_get_pe_header_offset(Ref<FileAccess> p_executable) const {343p_executable->seek(POINTER_TO_PE_HEADER_OFFSET);344uint32_t pe_header_offset = p_executable->get_32();345346p_executable->seek(pe_header_offset);347uint32_t magic = p_executable->get_32();348349return magic == 0x00004550 ? pe_header_offset : 0;350}351352uint32_t TemplateModifier::_snap(uint32_t p_value, uint32_t p_size) const {353return p_value + (p_value % p_size ? p_size - (p_value % p_size) : 0);354}355356Vector<uint8_t> TemplateModifier::_create_resources(uint32_t p_virtual_address, const GroupIcon &p_group_icon, const VersionInfo &p_version_info, const ManifestInfo &p_manifest_info) const {357// 0x04, 0x00 as string length ICON in UTF16 and padding to 32 bits358const uint8_t ICON_DIRECTORY_STRING[] = { 0x04, 0x00, 0x49, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00, 0x00, 0x00 };359const uint16_t RT_ENTRY_COUNT = 4;360const uint32_t icon_count = p_group_icon.images.size();361362ResourceDirectoryTable root_directory_table;363root_directory_table.id_entry_count = RT_ENTRY_COUNT;364365Vector<uint8_t> resources = root_directory_table.save();366367ResourceDirectoryEntry rt_icon_entry;368rt_icon_entry.id = ResourceDirectoryEntry::ICON;369rt_icon_entry.data_offset = ResourceDirectoryTable::SIZE + RT_ENTRY_COUNT * ResourceDirectoryEntry::SIZE;370rt_icon_entry.subdirectory = true;371resources.append_array(rt_icon_entry.save());372373ResourceDirectoryEntry rt_group_icon_entry;374rt_group_icon_entry.id = ResourceDirectoryEntry::GROUP_ICON;375rt_group_icon_entry.data_offset = (2 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count) * ResourceDirectoryEntry::SIZE;376rt_group_icon_entry.subdirectory = true;377resources.append_array(rt_group_icon_entry.save());378379ResourceDirectoryEntry rt_version_entry;380rt_version_entry.id = ResourceDirectoryEntry::VERSION;381rt_version_entry.data_offset = (4 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 2) * ResourceDirectoryEntry::SIZE;382rt_version_entry.subdirectory = true;383resources.append_array(rt_version_entry.save());384385ResourceDirectoryEntry rt_manifest_entry;386rt_manifest_entry.id = ResourceDirectoryEntry::MANIFEST;387rt_manifest_entry.data_offset = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 4) * ResourceDirectoryEntry::SIZE;388rt_manifest_entry.subdirectory = true;389resources.append_array(rt_manifest_entry.save());390391ResourceDirectoryTable icon_table;392icon_table.id_entry_count = icon_count;393resources.append_array(icon_table.save());394395for (uint32_t i = 0; i < icon_count; i++) {396ResourceDirectoryEntry icon_entry;397icon_entry.id = i + 1;398icon_entry.data_offset = (2 + i) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count + i) * ResourceDirectoryEntry::SIZE;399icon_entry.subdirectory = true;400resources.append_array(icon_entry.save());401}402403for (uint32_t i = 0; i < icon_count; i++) {404ResourceDirectoryTable language_icon_table;405language_icon_table.id_entry_count = 1;406resources.append_array(language_icon_table.save());407408ResourceDirectoryEntry language_icon_entry;409language_icon_entry.id = ResourceDirectoryEntry::ENGLISH;410language_icon_entry.data_offset = (8 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count * 2 + 6) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + i * ResourceDataEntry::SIZE;411resources.append_array(language_icon_entry.save());412}413414ResourceDirectoryTable group_icon_name_table;415group_icon_name_table.name_entry_count = 1;416resources.append_array(group_icon_name_table.save());417418ResourceDirectoryEntry group_icon_name_entry;419group_icon_name_entry.id = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count * 2 + 4) * ResourceDirectoryEntry::SIZE;420group_icon_name_entry.data_offset = (3 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 1) * ResourceDirectoryEntry::SIZE;421group_icon_name_entry.name = true;422group_icon_name_entry.subdirectory = true;423resources.append_array(group_icon_name_entry.save());424425ResourceDirectoryTable group_icon_language_table;426group_icon_language_table.id_entry_count = 1;427resources.append_array(group_icon_language_table.save());428429ResourceDirectoryEntry group_icon_language_entry;430group_icon_language_entry.id = ResourceDirectoryEntry::ENGLISH;431group_icon_language_entry.data_offset = (8 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 6) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + icon_count * ResourceDataEntry::SIZE;432resources.append_array(group_icon_language_entry.save());433434ResourceDirectoryTable version_table;435version_table.id_entry_count = 1;436resources.append_array(version_table.save());437438ResourceDirectoryEntry version_entry;439version_entry.id = 1;440version_entry.data_offset = (5 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 3) * ResourceDirectoryEntry::SIZE;441version_entry.subdirectory = true;442resources.append_array(version_entry.save());443444ResourceDirectoryTable version_language_table;445version_language_table.id_entry_count = 1;446resources.append_array(version_language_table.save());447448ResourceDirectoryEntry version_language_entry;449version_language_entry.id = ResourceDirectoryEntry::ENGLISH;450version_language_entry.data_offset = (8 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 6) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + (icon_count + 1) * ResourceDataEntry::SIZE;451resources.append_array(version_language_entry.save());452453ResourceDirectoryTable manifest_table;454manifest_table.id_entry_count = 1;455resources.append_array(manifest_table.save());456457ResourceDirectoryEntry manifest_entry;458manifest_entry.id = 1;459manifest_entry.data_offset = (7 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 5) * ResourceDirectoryEntry::SIZE;460manifest_entry.subdirectory = true;461resources.append_array(manifest_entry.save());462463ResourceDirectoryTable manifest_language_table;464manifest_language_table.id_entry_count = 1;465resources.append_array(manifest_language_table.save());466467ResourceDirectoryEntry manifest_language_entry;468manifest_language_entry.id = ResourceDirectoryEntry::ENGLISH;469manifest_language_entry.data_offset = (8 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 6) * ResourceDirectoryEntry::SIZE + sizeof(ICON_DIRECTORY_STRING) + (icon_count + 2) * ResourceDataEntry::SIZE;470resources.append_array(manifest_language_entry.save());471472Vector<uint8_t> icon_directory_string;473icon_directory_string.resize(sizeof(ICON_DIRECTORY_STRING));474memcpy(icon_directory_string.ptrw(), ICON_DIRECTORY_STRING, sizeof(ICON_DIRECTORY_STRING));475resources.append_array(icon_directory_string);476477Vector<Vector<uint8_t>> data_entries;478for (const Vector<uint8_t> &image : p_group_icon.images) {479data_entries.append(image);480}481data_entries.append(p_group_icon.save());482data_entries.append(p_version_info.save());483data_entries.append(p_manifest_info.save());484485uint32_t offset = resources.size() + data_entries.size() * ResourceDataEntry::SIZE;486487for (const Vector<uint8_t> &data_entry : data_entries) {488ResourceDataEntry resource_data_entry;489resource_data_entry.rva = p_virtual_address + offset;490resource_data_entry.size = data_entry.size();491resources.append_array(resource_data_entry.save());492offset += resource_data_entry.size;493while (offset % 4) {494offset += 1;495}496}497498for (const Vector<uint8_t> &data_entry : data_entries) {499resources.append_array(data_entry);500while (resources.size() % 4) {501resources.append(0);502}503}504505return resources;506}507508TemplateModifier::VersionInfo TemplateModifier::_create_version_info(const HashMap<String, String> &p_strings) const {509StringTable string_table;510for (const KeyValue<String, String> &E : p_strings) {511string_table.put(E.key, E.value);512}513514StringFileInfo string_file_info;515string_file_info.string_table = string_table;516517FixedFileInfo fixed_file_info;518if (p_strings.has("FileVersion")) {519fixed_file_info.set_file_version(p_strings["FileVersion"]);520}521if (p_strings.has("ProductVersion")) {522fixed_file_info.set_product_version(p_strings["ProductVersion"]);523}524525VersionInfo version_info;526version_info.value = fixed_file_info;527version_info.string_file_info = string_file_info;528529return version_info;530}531532TemplateModifier::ManifestInfo TemplateModifier::_create_manifest_info() const {533ManifestInfo manifest_info;534manifest_info.manifest = R"MANIFEST(<?xml version="1.0" encoding="utf-8"?>535<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">536<application xmlns="urn:schemas-microsoft-com:asm.v3">537<windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">538<ws2:longPathAware>true</ws2:longPathAware>539</windowsSettings>540</application>541<dependency>542<dependentAssembly>543<assemblyIdentity type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/>544</dependentAssembly>545</dependency>546</assembly>)MANIFEST";547return manifest_info;548}549550TemplateModifier::GroupIcon TemplateModifier::_create_group_icon(const String &p_icon_path) const {551GroupIcon group_icon;552553Ref<FileAccess> icon_file = FileAccess::open(p_icon_path, FileAccess::READ);554if (icon_file.is_null()) {555group_icon.fill_with_godot_blue();556return group_icon;557}558559group_icon.load(icon_file);560561return group_icon;562}563564Error TemplateModifier::_truncate(const String &p_path, uint32_t p_size) const {565Error error;566567Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ, &error);568ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);569570String truncated_path = p_path + ".truncated";571Ref<FileAccess> truncated = FileAccess::open(truncated_path, FileAccess::WRITE, &error);572ERR_FAIL_COND_V(error != OK, ERR_CANT_CREATE);573574truncated->store_buffer(file->get_buffer(p_size));575576file->close();577truncated->close();578579DirAccess::remove_absolute(p_path);580DirAccess::rename_absolute(truncated_path, p_path);581582return error;583}584585HashMap<String, String> TemplateModifier::_get_strings(const Ref<EditorExportPreset> &p_preset) const {586String file_version = p_preset->get_version("application/file_version", true);587String product_version = p_preset->get_version("application/product_version", true);588String company_name = p_preset->get("application/company_name");589String product_name = p_preset->get("application/product_name");590String file_description = p_preset->get("application/file_description");591String copyright = p_preset->get("application/copyright");592String trademarks = p_preset->get("application/trademarks");593594HashMap<String, String> strings;595if (!file_version.is_empty()) {596strings["FileVersion"] = file_version;597}598if (!product_version.is_empty()) {599strings["ProductVersion"] = product_version;600}601if (!company_name.is_empty()) {602strings["CompanyName"] = company_name;603}604if (!product_name.is_empty()) {605strings["ProductName"] = product_name;606}607if (!file_description.is_empty()) {608strings["FileDescription"] = file_description;609}610if (!copyright.is_empty()) {611strings["LegalCopyright"] = copyright;612}613if (!trademarks.is_empty()) {614strings["LegalTrademarks"] = trademarks;615}616617return strings;618}619620Error TemplateModifier::_modify_template(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) const {621Error error;622Ref<FileAccess> template_file = FileAccess::open(p_template_path, FileAccess::READ_WRITE, &error);623ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);624625Vector<SectionEntry> section_entries = _get_section_entries(template_file);626ERR_FAIL_COND_V(section_entries.size() < 2, ERR_CANT_OPEN);627628// Find resource (".rsrc") and relocation (".reloc") sections, usually last two, but ".debug_*" sections (referenced as "/[n]"), symbol table, and string table can follow.629int resource_index = section_entries.size() - 2;630int relocations_index = section_entries.size() - 1;631for (int i = 0; i < section_entries.size(); i++) {632if (section_entries[i].name == ".rsrc") {633resource_index = i;634} else if (section_entries[i].name == ".reloc") {635relocations_index = i;636}637}638639ERR_FAIL_COND_V(section_entries[resource_index].name != ".rsrc", ERR_CANT_OPEN);640ERR_FAIL_COND_V(section_entries[relocations_index].name != ".reloc", ERR_CANT_OPEN);641642uint64_t original_template_size = template_file->get_length();643644GroupIcon group_icon = _create_group_icon(p_icon_path);645646VersionInfo version_info = _create_version_info(_get_strings(p_preset));647ManifestInfo manifest_info = _create_manifest_info();648649SectionEntry &resources_section_entry = section_entries.write[resource_index];650uint32_t old_resources_size_of_raw_data = resources_section_entry.size_of_raw_data;651Vector<uint8_t> resources = _create_resources(resources_section_entry.virtual_address, group_icon, version_info, manifest_info);652resources_section_entry.virtual_size = resources.size();653resources.resize_initialized(_snap(resources.size(), BLOCK_SIZE));654resources_section_entry.size_of_raw_data = resources.size();655656int32_t raw_size_delta = resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;657uint32_t old_last_section_virtual_address = section_entries.get(section_entries.size() - 1).virtual_address;658659// Some data (e.g. DWARF debug symbols) can be placed after the last section.660uint32_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;661662// Copy and update sections after ".rsrc".663Vector<Vector<uint8_t>> moved_section_data;664uint32_t prev_virtual_address = resources_section_entry.virtual_address;665uint32_t prev_virtual_size = resources_section_entry.virtual_size;666for (int i = resource_index + 1; i < section_entries.size(); i++) {667SectionEntry §ion_entry = section_entries.write[i];668template_file->seek(section_entry.pointer_to_raw_data);669Vector<uint8_t> data = template_file->get_buffer(section_entry.size_of_raw_data);670moved_section_data.push_back(data);671section_entry.pointer_to_raw_data += raw_size_delta;672section_entry.virtual_address = prev_virtual_address + _snap(prev_virtual_size, PE_PAGE_SIZE);673prev_virtual_address = section_entry.virtual_address;674prev_virtual_size = section_entry.virtual_size;675}676677// Copy COFF symbol table and string table after the last section.678uint32_t footer_size = template_file->get_length() - old_footer_offset;679template_file->seek(old_footer_offset);680Vector<uint8_t> footer;681if (footer_size > 0) {682footer = template_file->get_buffer(footer_size);683}684685uint32_t pe_header_offset = _get_pe_header_offset(template_file);686687// Update symbol table pointer.688template_file->seek(pe_header_offset + 12);689uint32_t symbols_offset = template_file->get_32();690if (symbols_offset > resources_section_entry.pointer_to_raw_data) {691template_file->seek(pe_header_offset + 12);692template_file->store_32(symbols_offset + raw_size_delta);693}694695template_file->seek(pe_header_offset + MAGIC_NUMBER_OFFSET);696uint16_t magic_number = template_file->get_16();697ERR_FAIL_COND_V_MSG(magic_number != 0x10b && magic_number != 0x20b, ERR_CANT_OPEN, vformat("Magic number has wrong value: %04x", magic_number));698bool pe32plus = magic_number == 0x20b;699700// Update image size.701template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);702uint32_t size_of_initialized_data = template_file->get_32();703size_of_initialized_data += resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;704template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);705template_file->store_32(size_of_initialized_data);706707template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);708uint32_t size_of_image = template_file->get_32();709size_of_image += section_entries.get(section_entries.size() - 1).virtual_address - old_last_section_virtual_address;710template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);711template_file->store_32(size_of_image);712713uint32_t optional_header_offset = pe_header_offset + COFF_HEADER_SIZE;714715// Update resource section size.716template_file->seek(optional_header_offset + (pe32plus ? 132 : 116));717template_file->store_32(resources_section_entry.virtual_size);718719// Update relocation section size and pointer.720template_file->seek(optional_header_offset + (pe32plus ? 152 : 136));721template_file->store_32(section_entries[relocations_index].virtual_address);722template_file->store_32(section_entries[relocations_index].virtual_size);723724template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * resource_index);725template_file->store_buffer(resources_section_entry.save());726for (int i = resource_index + 1; i < section_entries.size(); i++) {727template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * i);728template_file->store_buffer(section_entries[i].save());729}730731// Write new resource section.732template_file->seek(resources_section_entry.pointer_to_raw_data);733template_file->store_buffer(resources);734// Write the rest of sections.735for (const Vector<uint8_t> &data : moved_section_data) {736template_file->store_buffer(data);737}738// Write footer data.739if (footer_size > 0) {740template_file->store_buffer(footer);741}742743if (template_file->get_position() < original_template_size) {744template_file->close();745_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);746}747748return OK;749}750751Vector<TemplateModifier::SectionEntry> TemplateModifier::_get_section_entries(Ref<FileAccess> p_executable) const {752Vector<SectionEntry> section_entries;753754uint32_t pe_header_offset = _get_pe_header_offset(p_executable);755if (pe_header_offset == 0) {756return section_entries;757}758759p_executable->seek(pe_header_offset + 6);760int num_sections = p_executable->get_16();761p_executable->seek(pe_header_offset + 20);762uint16_t size_of_optional_header = p_executable->get_16();763p_executable->seek(pe_header_offset + COFF_HEADER_SIZE + size_of_optional_header);764765for (int i = 0; i < num_sections; ++i) {766SectionEntry section_entry;767section_entry.load(p_executable);768section_entries.append(section_entry);769}770771return section_entries;772}773774Error TemplateModifier::modify(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) {775TemplateModifier template_modifier;776return template_modifier._modify_template(p_preset, p_template_path, p_icon_path);777}778779780