Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/export/template_modifier.h
10278 views
1
/**************************************************************************/
2
/* template_modifier.h */
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
#pragma once
32
33
#include "core/io/file_access.h"
34
#include "editor/export/editor_export_platform_pc.h"
35
36
class TemplateModifier {
37
const uint32_t PE_PAGE_SIZE = 4096;
38
const uint32_t BLOCK_SIZE = 512;
39
const uint32_t COFF_HEADER_SIZE = 24;
40
const uint32_t POINTER_TO_PE_HEADER_OFFSET = 0x3c;
41
// all offsets below are calculated from POINTER_TO_PE_HEADER_OFFSET value, at 0 is magic string PE (0x50450000)
42
const uint32_t MAGIC_NUMBER_OFFSET = 24;
43
const uint32_t SIZE_OF_INITIALIZED_DATA_OFFSET = 32;
44
const uint32_t SIZE_OF_IMAGE_OFFSET = 80;
45
46
struct ByteStream {
47
void save(uint8_t p_value, Vector<uint8_t> &r_bytes) const;
48
void save(uint16_t p_value, Vector<uint8_t> &r_bytes) const;
49
void save(uint32_t p_value, Vector<uint8_t> &r_bytes) const;
50
void save(const String &p_value, Vector<uint8_t> &r_bytes) const;
51
void save(uint32_t p_value, Vector<uint8_t> &r_bytes, uint32_t p_count) const;
52
Vector<uint8_t> save() const;
53
};
54
55
struct ResourceDirectoryTable : ByteStream {
56
static const uint16_t SIZE = 16;
57
58
uint16_t name_entry_count = 0;
59
uint16_t id_entry_count = 0;
60
61
Vector<uint8_t> save() const;
62
};
63
64
struct ResourceDirectoryEntry : ByteStream {
65
static const uint16_t SIZE = 8;
66
static const uint32_t ICON = 0x03;
67
static const uint32_t GROUP_ICON = 0x0e;
68
static const uint32_t VERSION = 0x10;
69
static const uint32_t ENGLISH = 0x0409;
70
static const uint32_t HIGH_BIT = 0x80000000;
71
72
uint32_t id = 0;
73
uint32_t data_offset = 0;
74
bool name = false;
75
bool subdirectory = false;
76
77
Vector<uint8_t> save() const;
78
};
79
80
struct FixedFileInfo : ByteStream {
81
uint32_t signature = 0xfeef04bd;
82
uint32_t struct_version = 0x10000;
83
uint32_t file_version_ms = 0;
84
uint32_t file_version_ls = 0;
85
uint32_t product_version_ms = 0;
86
uint32_t product_version_ls = 0;
87
uint32_t file_flags_mask = 0;
88
uint32_t file_flags = 0;
89
uint32_t file_os = 0x00000004;
90
uint32_t file_type = 0x00000001;
91
uint32_t file_subtype = 0;
92
uint32_t file_date_ms = 0;
93
uint32_t file_date_ls = 0;
94
95
Vector<uint8_t> save() const;
96
void set_file_version(const String &p_file_version);
97
void set_product_version(const String &p_product_version);
98
};
99
100
struct Structure : ByteStream {
101
uint16_t length = 0;
102
uint16_t value_length = 0;
103
uint16_t type = 0;
104
String key;
105
106
Vector<uint8_t> save() const;
107
Vector<uint8_t> &add_length(Vector<uint8_t> &r_bytes) const;
108
};
109
110
struct StringStructure : Structure {
111
String value;
112
113
Vector<uint8_t> save() const;
114
StringStructure();
115
StringStructure(const String &p_key, const String &p_value);
116
};
117
118
struct StringTable : Structure {
119
Vector<StringStructure> strings;
120
121
Vector<uint8_t> save() const;
122
void put(const String &p_key, const String &p_value);
123
StringTable();
124
};
125
126
struct StringFileInfo : Structure {
127
StringTable string_table;
128
129
Vector<uint8_t> save() const;
130
StringFileInfo();
131
};
132
133
struct Var : Structure {
134
const uint32_t value = 0x04b00409;
135
136
Vector<uint8_t> save() const;
137
Var();
138
};
139
140
struct VarFileInfo : Structure {
141
Var var;
142
143
Vector<uint8_t> save() const;
144
VarFileInfo();
145
};
146
147
struct VersionInfo : Structure {
148
FixedFileInfo value;
149
StringFileInfo string_file_info;
150
VarFileInfo var_file_info;
151
152
Vector<uint8_t> save() const;
153
VersionInfo();
154
};
155
156
struct IconEntry : ByteStream {
157
static const uint32_t SIZE = 16;
158
159
uint8_t width = 0;
160
uint8_t height = 0;
161
uint8_t colors = 0;
162
uint8_t reserved = 0;
163
uint16_t planes = 0;
164
uint16_t bits_per_pixel = 32;
165
uint32_t image_size = 0;
166
uint32_t image_offset = 0;
167
Vector<uint8_t> data;
168
169
Vector<uint8_t> save() const;
170
void load(Ref<FileAccess> p_file);
171
};
172
173
struct GroupIcon : ByteStream {
174
static constexpr uint8_t SIZES[6] = { 16, 32, 48, 64, 128, 0 };
175
176
uint16_t reserved = 0;
177
uint16_t type = 1;
178
uint16_t image_count = 0;
179
Vector<IconEntry> icon_entries;
180
Vector<Vector<uint8_t>> images;
181
182
Vector<uint8_t> save() const;
183
void load(Ref<FileAccess> p_icon_file);
184
void fill_with_godot_blue();
185
};
186
187
struct SectionEntry : ByteStream {
188
static const uint32_t SIZE = 40;
189
190
String name;
191
uint32_t virtual_size = 0;
192
uint32_t virtual_address = 0;
193
uint32_t size_of_raw_data = 0;
194
uint32_t pointer_to_raw_data = 0;
195
uint32_t pointer_to_relocations = 0;
196
uint32_t pointer_to_line_numbers = 0;
197
uint16_t number_of_relocations = 0;
198
uint16_t number_of_line_numbers = 0;
199
uint32_t characteristics = 0;
200
201
Vector<uint8_t> save() const;
202
void load(Ref<FileAccess> p_file);
203
};
204
205
struct ResourceDataEntry : ByteStream {
206
static const uint16_t SIZE = 16;
207
208
uint32_t rva = 0;
209
uint32_t size = 0;
210
211
Vector<uint8_t> save() const;
212
};
213
214
uint32_t _snap(uint32_t p_value, uint32_t p_size) const;
215
uint32_t _get_pe_header_offset(Ref<FileAccess> p_executable) const;
216
Vector<SectionEntry> _get_section_entries(Ref<FileAccess> p_executable) const;
217
GroupIcon _create_group_icon(const String &p_icon_path) const;
218
VersionInfo _create_version_info(const HashMap<String, String> &p_strings) const;
219
Vector<uint8_t> _create_resources(uint32_t p_virtual_address, const GroupIcon &p_group_icon, const VersionInfo &p_version_info) const;
220
Error _truncate(const String &p_executable_path, uint32_t p_size) const;
221
HashMap<String, String> _get_strings(const Ref<EditorExportPreset> &p_preset) const;
222
Error _modify_template(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) const;
223
224
public:
225
static Error modify(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path);
226
};
227
228