Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/export/template_modifier.cpp
10278 views
1
/**************************************************************************/
2
/* template_modifier.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 "template_modifier.h"
32
33
#include "core/config/project_settings.h"
34
35
void TemplateModifier::ByteStream::save(uint8_t p_value, Vector<uint8_t> &r_bytes) const {
36
save(p_value, r_bytes, 1);
37
}
38
39
void TemplateModifier::ByteStream::save(uint16_t p_value, Vector<uint8_t> &r_bytes) const {
40
save(p_value, r_bytes, 2);
41
}
42
43
void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes) const {
44
save(p_value, r_bytes, 4);
45
}
46
47
void TemplateModifier::ByteStream::save(const String &p_value, Vector<uint8_t> &r_bytes) const {
48
r_bytes.append_array(p_value.to_utf16_buffer());
49
save((uint16_t)0, r_bytes);
50
}
51
52
void TemplateModifier::ByteStream::save(uint32_t p_value, Vector<uint8_t> &r_bytes, uint32_t p_count) const {
53
for (uint32_t i = 0; i < p_count; i++) {
54
r_bytes.append((uint8_t)(p_value & 0xff));
55
p_value >>= 8;
56
}
57
}
58
59
Vector<uint8_t> TemplateModifier::ByteStream::save() const {
60
return Vector<uint8_t>();
61
}
62
63
Vector<uint8_t> TemplateModifier::Structure::save() const {
64
Vector<uint8_t> bytes;
65
ByteStream::save(length, bytes);
66
ByteStream::save(value_length, bytes);
67
ByteStream::save(type, bytes);
68
ByteStream::save(key, bytes);
69
while (bytes.size() % 4) {
70
bytes.append(0);
71
}
72
return bytes;
73
}
74
75
Vector<uint8_t> &TemplateModifier::Structure::add_length(Vector<uint8_t> &r_bytes) const {
76
r_bytes.write[0] = r_bytes.size() & 0xff;
77
r_bytes.write[1] = r_bytes.size() >> 8 & 0xff;
78
return r_bytes;
79
}
80
81
Vector<uint8_t> TemplateModifier::ResourceDirectoryTable::save() const {
82
Vector<uint8_t> bytes;
83
bytes.resize_initialized(12);
84
ByteStream::save(name_entry_count, bytes);
85
ByteStream::save(id_entry_count, bytes);
86
return bytes;
87
}
88
89
Vector<uint8_t> TemplateModifier::ResourceDirectoryEntry::save() const {
90
Vector<uint8_t> bytes;
91
ByteStream::save(id | (name ? HIGH_BIT : 0), bytes);
92
ByteStream::save(data_offset | (subdirectory ? HIGH_BIT : 0), bytes);
93
return bytes;
94
}
95
96
Vector<uint8_t> TemplateModifier::FixedFileInfo::save() const {
97
Vector<uint8_t> bytes;
98
ByteStream::save(signature, bytes);
99
ByteStream::save(struct_version, bytes);
100
ByteStream::save(file_version_ms, bytes);
101
ByteStream::save(file_version_ls, bytes);
102
ByteStream::save(product_version_ms, bytes);
103
ByteStream::save(product_version_ls, bytes);
104
ByteStream::save(file_flags_mask, bytes);
105
ByteStream::save(file_flags, bytes);
106
ByteStream::save(file_os, bytes);
107
ByteStream::save(file_type, bytes);
108
ByteStream::save(file_subtype, bytes);
109
ByteStream::save(file_date_ms, bytes);
110
ByteStream::save(file_date_ls, bytes);
111
return bytes;
112
}
113
114
void TemplateModifier::FixedFileInfo::set_file_version(const String &p_file_version) {
115
Vector<String> parts = p_file_version.split(".");
116
while (parts.size() < 4) {
117
parts.append("0");
118
}
119
file_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);
120
file_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);
121
}
122
123
void TemplateModifier::FixedFileInfo::set_product_version(const String &p_product_version) {
124
Vector<String> parts = p_product_version.split(".");
125
while (parts.size() < 4) {
126
parts.append("0");
127
}
128
product_version_ms = parts[0].to_int() << 16 | (parts[1].to_int() & 0xffff);
129
product_version_ls = parts[2].to_int() << 16 | (parts[3].to_int() & 0xffff);
130
}
131
132
Vector<uint8_t> TemplateModifier::StringStructure::save() const {
133
Vector<uint8_t> bytes = Structure::save();
134
ByteStream::save(value, bytes);
135
return add_length(bytes);
136
}
137
138
TemplateModifier::StringStructure::StringStructure() {
139
type = 1;
140
}
141
142
TemplateModifier::StringStructure::StringStructure(const String &p_key, const String &p_value) {
143
type = 1;
144
value_length = p_value.length() + 1;
145
key = p_key;
146
value = p_value;
147
}
148
149
Vector<uint8_t> TemplateModifier::StringTable::save() const {
150
Vector<uint8_t> bytes = Structure::save();
151
for (const StringStructure &string : strings) {
152
bytes.append_array(string.save());
153
while (bytes.size() % 4) {
154
bytes.append(0);
155
}
156
}
157
return add_length(bytes);
158
}
159
160
void TemplateModifier::StringTable::put(const String &p_key, const String &p_value) {
161
strings.append(StringStructure(p_key, p_value));
162
}
163
164
TemplateModifier::StringTable::StringTable() {
165
key = "040904b0";
166
type = 1;
167
}
168
169
TemplateModifier::StringFileInfo::StringFileInfo() {
170
key = "StringFileInfo";
171
value_length = 0;
172
type = 1;
173
}
174
175
Vector<uint8_t> TemplateModifier::StringFileInfo::save() const {
176
Vector<uint8_t> bytes = Structure::save();
177
bytes.append_array(string_table.save());
178
return add_length(bytes);
179
}
180
181
Vector<uint8_t> TemplateModifier::Var::save() const {
182
Vector<uint8_t> bytes = Structure::save();
183
ByteStream::save(value, bytes);
184
return add_length(bytes);
185
}
186
187
TemplateModifier::Var::Var() {
188
value_length = 4;
189
key = "Translation";
190
}
191
192
Vector<uint8_t> TemplateModifier::VarFileInfo::save() const {
193
Vector<uint8_t> bytes = Structure::save();
194
bytes.append_array(var.save());
195
return add_length(bytes);
196
}
197
198
TemplateModifier::VarFileInfo::VarFileInfo() {
199
type = 1;
200
key = "VarFileInfo";
201
}
202
203
Vector<uint8_t> TemplateModifier::VersionInfo::save() const {
204
Vector<uint8_t> fixed_file_info = value.save();
205
Vector<uint8_t> bytes = Structure::save();
206
bytes.append_array(fixed_file_info);
207
bytes.append_array(string_file_info.save());
208
while (bytes.size() % 4) {
209
bytes.append(0);
210
}
211
bytes.append_array(var_file_info.save());
212
return add_length(bytes);
213
}
214
215
TemplateModifier::VersionInfo::VersionInfo() {
216
key = "VS_VERSION_INFO";
217
value_length = 52;
218
}
219
220
Vector<uint8_t> TemplateModifier::IconEntry::save() const {
221
Vector<uint8_t> bytes;
222
ByteStream::save(width, bytes);
223
ByteStream::save(height, bytes);
224
ByteStream::save(colors, bytes);
225
ByteStream::save((uint8_t)0, bytes);
226
ByteStream::save(planes, bytes);
227
ByteStream::save(bits_per_pixel, bytes);
228
ByteStream::save(image_size, bytes);
229
ByteStream::save((uint16_t)image_offset, bytes);
230
return bytes;
231
}
232
233
void TemplateModifier::IconEntry::load(Ref<FileAccess> p_file) {
234
width = p_file->get_8(); // Width in pixels.
235
height = p_file->get_8(); // Height in pixels.
236
colors = p_file->get_8(); // Number of colors in the palette (0 - no palette).
237
p_file->get_8(); // Reserved.
238
planes = p_file->get_16(); // Number of color planes.
239
bits_per_pixel = p_file->get_16(); // Bits per pixel.
240
image_size = p_file->get_32(); // Image data size in bytes.
241
image_offset = p_file->get_32(); // Image data offset.
242
}
243
244
Vector<uint8_t> TemplateModifier::GroupIcon::save() const {
245
Vector<uint8_t> bytes;
246
ByteStream::save(reserved, bytes);
247
ByteStream::save(type, bytes);
248
ByteStream::save(image_count, bytes);
249
for (const IconEntry &icon_entry : icon_entries) {
250
bytes.append_array(icon_entry.save());
251
}
252
return bytes;
253
}
254
255
void TemplateModifier::GroupIcon::load(Ref<FileAccess> p_icon_file) {
256
if (p_icon_file->get_32() != 0x10000) { // Wrong reserved bytes
257
ERR_FAIL_MSG("Wrong icon file type.");
258
}
259
260
image_count = p_icon_file->get_16();
261
for (uint16_t i = 0; i < image_count; i++) {
262
IconEntry icon_entry;
263
icon_entry.load(p_icon_file);
264
icon_entries.append(icon_entry);
265
}
266
267
int id = 1;
268
for (IconEntry &icon_entry : icon_entries) {
269
Vector<uint8_t> image;
270
image.resize(icon_entry.image_size);
271
p_icon_file->seek(icon_entry.image_offset);
272
p_icon_file->get_buffer(image.ptrw(), image.size());
273
icon_entry.image_offset = id++;
274
images.append(image);
275
}
276
}
277
278
void TemplateModifier::GroupIcon::fill_with_godot_blue() {
279
uint32_t id = 1;
280
for (uint8_t size : SIZES) {
281
Ref<Image> image = Image::create_empty(size ? size : 256, size ? size : 256, false, Image::FORMAT_RGB8);
282
image->fill(Color::hex(0x478cbfff));
283
Vector<uint8_t> data = image->save_png_to_buffer();
284
IconEntry icon_entry;
285
icon_entry.width = size;
286
icon_entry.height = size;
287
icon_entry.bits_per_pixel = 24;
288
icon_entry.image_size = data.size();
289
icon_entry.image_offset = id++;
290
icon_entries.append(icon_entry);
291
images.append(data);
292
}
293
}
294
295
Vector<uint8_t> TemplateModifier::SectionEntry::save() const {
296
Vector<uint8_t> bytes;
297
bytes.append_array(name.to_utf8_buffer());
298
while (bytes.size() < 8) {
299
bytes.append(0);
300
}
301
ByteStream::save(virtual_size, bytes);
302
ByteStream::save(virtual_address, bytes);
303
ByteStream::save(size_of_raw_data, bytes);
304
ByteStream::save(pointer_to_raw_data, bytes);
305
ByteStream::save(pointer_to_relocations, bytes);
306
ByteStream::save(pointer_to_line_numbers, bytes);
307
ByteStream::save(number_of_relocations, bytes);
308
ByteStream::save(number_of_line_numbers, bytes);
309
ByteStream::save(characteristics, bytes);
310
return bytes;
311
}
312
313
void TemplateModifier::SectionEntry::load(Ref<FileAccess> p_file) {
314
uint8_t section_name[8];
315
p_file->get_buffer(section_name, 8);
316
name = String::utf8((char *)section_name, 8);
317
virtual_size = p_file->get_32();
318
virtual_address = p_file->get_32();
319
size_of_raw_data = p_file->get_32();
320
pointer_to_raw_data = p_file->get_32();
321
pointer_to_relocations = p_file->get_32();
322
pointer_to_line_numbers = p_file->get_32();
323
number_of_relocations = p_file->get_16();
324
number_of_line_numbers = p_file->get_16();
325
characteristics = p_file->get_32();
326
}
327
328
Vector<uint8_t> TemplateModifier::ResourceDataEntry::save() const {
329
Vector<uint8_t> bytes;
330
ByteStream::save(rva, bytes);
331
ByteStream::save(size, bytes);
332
ByteStream::save(0, bytes, 8);
333
return bytes;
334
}
335
336
uint32_t TemplateModifier::_get_pe_header_offset(Ref<FileAccess> p_executable) const {
337
p_executable->seek(POINTER_TO_PE_HEADER_OFFSET);
338
uint32_t pe_header_offset = p_executable->get_32();
339
340
p_executable->seek(pe_header_offset);
341
uint32_t magic = p_executable->get_32();
342
343
return magic == 0x00004550 ? pe_header_offset : 0;
344
}
345
346
uint32_t TemplateModifier::_snap(uint32_t p_value, uint32_t p_size) const {
347
return p_value + (p_value % p_size ? p_size - (p_value % p_size) : 0);
348
}
349
350
Vector<uint8_t> TemplateModifier::_create_resources(uint32_t p_virtual_address, const GroupIcon &p_group_icon, const VersionInfo &p_version_info) const {
351
// 0x04, 0x00 as string length ICON in UTF16 and padding to 32 bits
352
const uint8_t ICON_DIRECTORY_STRING[] = { 0x04, 0x00, 0x49, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00, 0x00, 0x00 };
353
const uint16_t RT_ENTRY_COUNT = 3;
354
const uint32_t icon_count = p_group_icon.images.size();
355
356
ResourceDirectoryTable root_directory_table;
357
root_directory_table.id_entry_count = RT_ENTRY_COUNT;
358
359
Vector<uint8_t> resources = root_directory_table.save();
360
361
ResourceDirectoryEntry rt_icon_entry;
362
rt_icon_entry.id = ResourceDirectoryEntry::ICON;
363
rt_icon_entry.data_offset = ResourceDirectoryTable::SIZE + RT_ENTRY_COUNT * ResourceDirectoryEntry::SIZE;
364
rt_icon_entry.subdirectory = true;
365
resources.append_array(rt_icon_entry.save());
366
367
ResourceDirectoryEntry rt_group_icon_entry;
368
rt_group_icon_entry.id = ResourceDirectoryEntry::GROUP_ICON;
369
rt_group_icon_entry.data_offset = (2 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count) * ResourceDirectoryEntry::SIZE;
370
rt_group_icon_entry.subdirectory = true;
371
resources.append_array(rt_group_icon_entry.save());
372
373
ResourceDirectoryEntry rt_version_entry;
374
rt_version_entry.id = ResourceDirectoryEntry::VERSION;
375
rt_version_entry.data_offset = (4 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 2) * ResourceDirectoryEntry::SIZE;
376
rt_version_entry.subdirectory = true;
377
resources.append_array(rt_version_entry.save());
378
379
ResourceDirectoryTable icon_table;
380
icon_table.id_entry_count = icon_count;
381
resources.append_array(icon_table.save());
382
383
for (uint32_t i = 0; i < icon_count; i++) {
384
ResourceDirectoryEntry icon_entry;
385
icon_entry.id = i + 1;
386
icon_entry.data_offset = (2 + i) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count + i) * ResourceDirectoryEntry::SIZE;
387
icon_entry.subdirectory = true;
388
resources.append_array(icon_entry.save());
389
}
390
391
for (uint32_t i = 0; i < icon_count; i++) {
392
ResourceDirectoryTable language_icon_table;
393
language_icon_table.id_entry_count = 1;
394
resources.append_array(language_icon_table.save());
395
396
ResourceDirectoryEntry language_icon_entry;
397
language_icon_entry.id = ResourceDirectoryEntry::ENGLISH;
398
language_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;
399
resources.append_array(language_icon_entry.save());
400
}
401
402
ResourceDirectoryTable group_icon_name_table;
403
group_icon_name_table.name_entry_count = 1;
404
resources.append_array(group_icon_name_table.save());
405
406
ResourceDirectoryEntry group_icon_name_entry;
407
group_icon_name_entry.id = (6 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + icon_count * 2 + 4) * ResourceDirectoryEntry::SIZE;
408
group_icon_name_entry.data_offset = (3 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 1) * ResourceDirectoryEntry::SIZE;
409
group_icon_name_entry.name = true;
410
group_icon_name_entry.subdirectory = true;
411
resources.append_array(group_icon_name_entry.save());
412
413
ResourceDirectoryTable group_icon_language_table;
414
group_icon_language_table.id_entry_count = 1;
415
resources.append_array(group_icon_language_table.save());
416
417
ResourceDirectoryEntry group_icon_language_entry;
418
group_icon_language_entry.id = ResourceDirectoryEntry::ENGLISH;
419
group_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;
420
resources.append_array(group_icon_language_entry.save());
421
422
ResourceDirectoryTable version_table;
423
version_table.id_entry_count = 1;
424
resources.append_array(version_table.save());
425
426
ResourceDirectoryEntry version_entry;
427
version_entry.id = 1;
428
version_entry.data_offset = (5 + icon_count) * ResourceDirectoryTable::SIZE + (RT_ENTRY_COUNT + 2 * icon_count + 3) * ResourceDirectoryEntry::SIZE;
429
version_entry.subdirectory = true;
430
resources.append_array(version_entry.save());
431
432
ResourceDirectoryTable version_language_table;
433
version_language_table.id_entry_count = 1;
434
resources.append_array(version_language_table.save());
435
436
ResourceDirectoryEntry version_language_entry;
437
version_language_entry.id = ResourceDirectoryEntry::ENGLISH;
438
version_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;
439
resources.append_array(version_language_entry.save());
440
441
Vector<uint8_t> icon_directory_string;
442
icon_directory_string.resize(sizeof(ICON_DIRECTORY_STRING));
443
memcpy(icon_directory_string.ptrw(), ICON_DIRECTORY_STRING, sizeof(ICON_DIRECTORY_STRING));
444
resources.append_array(icon_directory_string);
445
446
Vector<Vector<uint8_t>> data_entries;
447
for (const Vector<uint8_t> &image : p_group_icon.images) {
448
data_entries.append(image);
449
}
450
data_entries.append(p_group_icon.save());
451
data_entries.append(p_version_info.save());
452
453
uint32_t offset = resources.size() + data_entries.size() * ResourceDataEntry::SIZE;
454
455
for (const Vector<uint8_t> &data_entry : data_entries) {
456
ResourceDataEntry resource_data_entry;
457
resource_data_entry.rva = p_virtual_address + offset;
458
resource_data_entry.size = data_entry.size();
459
resources.append_array(resource_data_entry.save());
460
offset += resource_data_entry.size;
461
while (offset % 4) {
462
offset += 1;
463
}
464
}
465
466
for (const Vector<uint8_t> &data_entry : data_entries) {
467
resources.append_array(data_entry);
468
while (resources.size() % 4) {
469
resources.append(0);
470
}
471
}
472
473
return resources;
474
}
475
476
TemplateModifier::VersionInfo TemplateModifier::_create_version_info(const HashMap<String, String> &p_strings) const {
477
StringTable string_table;
478
for (const KeyValue<String, String> &E : p_strings) {
479
string_table.put(E.key, E.value);
480
}
481
482
StringFileInfo string_file_info;
483
string_file_info.string_table = string_table;
484
485
FixedFileInfo fixed_file_info;
486
if (p_strings.has("FileVersion")) {
487
fixed_file_info.set_file_version(p_strings["FileVersion"]);
488
}
489
if (p_strings.has("ProductVersion")) {
490
fixed_file_info.set_product_version(p_strings["ProductVersion"]);
491
}
492
493
VersionInfo version_info;
494
version_info.value = fixed_file_info;
495
version_info.string_file_info = string_file_info;
496
497
return version_info;
498
}
499
500
TemplateModifier::GroupIcon TemplateModifier::_create_group_icon(const String &p_icon_path) const {
501
GroupIcon group_icon;
502
503
Ref<FileAccess> icon_file = FileAccess::open(p_icon_path, FileAccess::READ);
504
if (icon_file.is_null()) {
505
group_icon.fill_with_godot_blue();
506
return group_icon;
507
}
508
509
group_icon.load(icon_file);
510
511
return group_icon;
512
}
513
514
Error TemplateModifier::_truncate(const String &p_path, uint32_t p_size) const {
515
Error error;
516
517
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ, &error);
518
ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);
519
520
String truncated_path = p_path + ".truncated";
521
Ref<FileAccess> truncated = FileAccess::open(truncated_path, FileAccess::WRITE, &error);
522
ERR_FAIL_COND_V(error != OK, ERR_CANT_CREATE);
523
524
truncated->store_buffer(file->get_buffer(p_size));
525
526
file->close();
527
truncated->close();
528
529
DirAccess::remove_absolute(p_path);
530
DirAccess::rename_absolute(truncated_path, p_path);
531
532
return error;
533
}
534
535
HashMap<String, String> TemplateModifier::_get_strings(const Ref<EditorExportPreset> &p_preset) const {
536
String file_version = p_preset->get_version("application/file_version", true);
537
String product_version = p_preset->get_version("application/product_version", true);
538
String company_name = p_preset->get("application/company_name");
539
String product_name = p_preset->get("application/product_name");
540
String file_description = p_preset->get("application/file_description");
541
String copyright = p_preset->get("application/copyright");
542
String trademarks = p_preset->get("application/trademarks");
543
544
HashMap<String, String> strings;
545
if (!file_version.is_empty()) {
546
strings["FileVersion"] = file_version;
547
}
548
if (!product_version.is_empty()) {
549
strings["ProductVersion"] = product_version;
550
}
551
if (!company_name.is_empty()) {
552
strings["CompanyName"] = company_name;
553
}
554
if (!product_name.is_empty()) {
555
strings["ProductName"] = product_name;
556
}
557
if (!file_description.is_empty()) {
558
strings["FileDescription"] = file_description;
559
}
560
if (!copyright.is_empty()) {
561
strings["LegalCopyright"] = copyright;
562
}
563
if (!trademarks.is_empty()) {
564
strings["LegalTrademarks"] = trademarks;
565
}
566
567
return strings;
568
}
569
570
Error TemplateModifier::_modify_template(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) const {
571
Error error;
572
Ref<FileAccess> template_file = FileAccess::open(p_template_path, FileAccess::READ_WRITE, &error);
573
ERR_FAIL_COND_V(error != OK, ERR_CANT_OPEN);
574
575
Vector<SectionEntry> section_entries = _get_section_entries(template_file);
576
ERR_FAIL_COND_V(section_entries.size() < 2, ERR_CANT_OPEN);
577
578
// Find resource (".rsrc") and relocation (".reloc") sections, usually last two, but ".debug_*" sections (referenced as "/[n]"), symbol table, and string table can follow.
579
int resource_index = section_entries.size() - 2;
580
int relocations_index = section_entries.size() - 1;
581
for (int i = 0; i < section_entries.size(); i++) {
582
if (section_entries[i].name == ".rsrc") {
583
resource_index = i;
584
} else if (section_entries[i].name == ".reloc") {
585
relocations_index = i;
586
}
587
}
588
589
ERR_FAIL_COND_V(section_entries[resource_index].name != ".rsrc", ERR_CANT_OPEN);
590
ERR_FAIL_COND_V(section_entries[relocations_index].name != ".reloc", ERR_CANT_OPEN);
591
592
uint64_t original_template_size = template_file->get_length();
593
594
GroupIcon group_icon = _create_group_icon(p_icon_path);
595
596
VersionInfo version_info = _create_version_info(_get_strings(p_preset));
597
598
SectionEntry &resources_section_entry = section_entries.write[resource_index];
599
uint32_t old_resources_size_of_raw_data = resources_section_entry.size_of_raw_data;
600
Vector<uint8_t> resources = _create_resources(resources_section_entry.virtual_address, group_icon, version_info);
601
resources_section_entry.virtual_size = resources.size();
602
resources.resize_initialized(_snap(resources.size(), BLOCK_SIZE));
603
resources_section_entry.size_of_raw_data = resources.size();
604
605
int32_t raw_size_delta = resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;
606
uint32_t old_last_section_virtual_address = section_entries.get(section_entries.size() - 1).virtual_address;
607
608
// Some data (e.g. DWARF debug symbols) can be placed after the last section.
609
uint32_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;
610
611
// Copy and update sections after ".rsrc".
612
Vector<Vector<uint8_t>> moved_section_data;
613
uint32_t prev_virtual_address = resources_section_entry.virtual_address;
614
uint32_t prev_virtual_size = resources_section_entry.virtual_size;
615
for (int i = resource_index + 1; i < section_entries.size(); i++) {
616
SectionEntry &section_entry = section_entries.write[i];
617
template_file->seek(section_entry.pointer_to_raw_data);
618
Vector<uint8_t> data = template_file->get_buffer(section_entry.size_of_raw_data);
619
moved_section_data.push_back(data);
620
section_entry.pointer_to_raw_data += raw_size_delta;
621
section_entry.virtual_address = prev_virtual_address + _snap(prev_virtual_size, PE_PAGE_SIZE);
622
prev_virtual_address = section_entry.virtual_address;
623
prev_virtual_size = section_entry.virtual_size;
624
}
625
626
// Copy COFF symbol table and string table after the last section.
627
uint32_t footer_size = template_file->get_length() - old_footer_offset;
628
template_file->seek(old_footer_offset);
629
Vector<uint8_t> footer;
630
if (footer_size > 0) {
631
footer = template_file->get_buffer(footer_size);
632
}
633
634
uint32_t pe_header_offset = _get_pe_header_offset(template_file);
635
636
// Update symbol table pointer.
637
template_file->seek(pe_header_offset + 12);
638
uint32_t symbols_offset = template_file->get_32();
639
if (symbols_offset > resources_section_entry.pointer_to_raw_data) {
640
template_file->seek(pe_header_offset + 12);
641
template_file->store_32(symbols_offset + raw_size_delta);
642
}
643
644
template_file->seek(pe_header_offset + MAGIC_NUMBER_OFFSET);
645
uint16_t magic_number = template_file->get_16();
646
ERR_FAIL_COND_V_MSG(magic_number != 0x10b && magic_number != 0x20b, ERR_CANT_OPEN, vformat("Magic number has wrong value: %04x", magic_number));
647
bool pe32plus = magic_number == 0x20b;
648
649
// Update image size.
650
template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);
651
uint32_t size_of_initialized_data = template_file->get_32();
652
size_of_initialized_data += resources_section_entry.size_of_raw_data - old_resources_size_of_raw_data;
653
template_file->seek(pe_header_offset + SIZE_OF_INITIALIZED_DATA_OFFSET);
654
template_file->store_32(size_of_initialized_data);
655
656
template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);
657
uint32_t size_of_image = template_file->get_32();
658
size_of_image += section_entries.get(section_entries.size() - 1).virtual_address - old_last_section_virtual_address;
659
template_file->seek(pe_header_offset + SIZE_OF_IMAGE_OFFSET);
660
template_file->store_32(size_of_image);
661
662
uint32_t optional_header_offset = pe_header_offset + COFF_HEADER_SIZE;
663
664
// Update resource section size.
665
template_file->seek(optional_header_offset + (pe32plus ? 132 : 116));
666
template_file->store_32(resources_section_entry.virtual_size);
667
668
// Update relocation section size and pointer.
669
template_file->seek(optional_header_offset + (pe32plus ? 152 : 136));
670
template_file->store_32(section_entries[relocations_index].virtual_address);
671
template_file->store_32(section_entries[relocations_index].virtual_size);
672
673
template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * resource_index);
674
template_file->store_buffer(resources_section_entry.save());
675
for (int i = resource_index + 1; i < section_entries.size(); i++) {
676
template_file->seek(optional_header_offset + (pe32plus ? 240 : 224) + SectionEntry::SIZE * i);
677
template_file->store_buffer(section_entries[i].save());
678
}
679
680
// Write new resource section.
681
template_file->seek(resources_section_entry.pointer_to_raw_data);
682
template_file->store_buffer(resources);
683
// Write the rest of sections.
684
for (const Vector<uint8_t> &data : moved_section_data) {
685
template_file->store_buffer(data);
686
}
687
// Write footer data.
688
if (footer_size > 0) {
689
template_file->store_buffer(footer);
690
}
691
692
if (template_file->get_position() < original_template_size) {
693
template_file->close();
694
_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);
695
}
696
697
return OK;
698
}
699
700
Vector<TemplateModifier::SectionEntry> TemplateModifier::_get_section_entries(Ref<FileAccess> p_executable) const {
701
Vector<SectionEntry> section_entries;
702
703
uint32_t pe_header_offset = _get_pe_header_offset(p_executable);
704
if (pe_header_offset == 0) {
705
return section_entries;
706
}
707
708
p_executable->seek(pe_header_offset + 6);
709
int num_sections = p_executable->get_16();
710
p_executable->seek(pe_header_offset + 20);
711
uint16_t size_of_optional_header = p_executable->get_16();
712
p_executable->seek(pe_header_offset + COFF_HEADER_SIZE + size_of_optional_header);
713
714
for (int i = 0; i < num_sections; ++i) {
715
SectionEntry section_entry;
716
section_entry.load(p_executable);
717
section_entries.append(section_entry);
718
}
719
720
return section_entries;
721
}
722
723
Error TemplateModifier::modify(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) {
724
TemplateModifier template_modifier;
725
return template_modifier._modify_template(p_preset, p_template_path, p_icon_path);
726
}
727
728