Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/export/template_modifier.h
21204 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 MANIFEST = 0x18;
69
static const uint32_t VERSION = 0x10;
70
static const uint32_t ENGLISH = 0x0409;
71
static const uint32_t HIGH_BIT = 0x80000000;
72
73
uint32_t id = 0;
74
uint32_t data_offset = 0;
75
bool name = false;
76
bool subdirectory = false;
77
78
Vector<uint8_t> save() const;
79
};
80
81
struct FixedFileInfo : ByteStream {
82
uint32_t signature = 0xfeef04bd;
83
uint32_t struct_version = 0x10000;
84
uint32_t file_version_ms = 0;
85
uint32_t file_version_ls = 0;
86
uint32_t product_version_ms = 0;
87
uint32_t product_version_ls = 0;
88
uint32_t file_flags_mask = 0;
89
uint32_t file_flags = 0;
90
uint32_t file_os = 0x00000004;
91
uint32_t file_type = 0x00000001;
92
uint32_t file_subtype = 0;
93
uint32_t file_date_ms = 0;
94
uint32_t file_date_ls = 0;
95
96
Vector<uint8_t> save() const;
97
void set_file_version(const String &p_file_version);
98
void set_product_version(const String &p_product_version);
99
};
100
101
struct Structure : ByteStream {
102
uint16_t length = 0;
103
uint16_t value_length = 0;
104
uint16_t type = 0;
105
String key;
106
107
Vector<uint8_t> save() const;
108
Vector<uint8_t> &add_length(Vector<uint8_t> &r_bytes) const;
109
};
110
111
struct StringStructure : Structure {
112
String value;
113
114
Vector<uint8_t> save() const;
115
StringStructure();
116
StringStructure(const String &p_key, const String &p_value);
117
};
118
119
struct StringTable : Structure {
120
Vector<StringStructure> strings;
121
122
Vector<uint8_t> save() const;
123
void put(const String &p_key, const String &p_value);
124
StringTable();
125
};
126
127
struct StringFileInfo : Structure {
128
StringTable string_table;
129
130
Vector<uint8_t> save() const;
131
StringFileInfo();
132
};
133
134
struct Var : Structure {
135
const uint32_t value = 0x04b00409;
136
137
Vector<uint8_t> save() const;
138
Var();
139
};
140
141
struct VarFileInfo : Structure {
142
Var var;
143
144
Vector<uint8_t> save() const;
145
VarFileInfo();
146
};
147
148
struct VersionInfo : Structure {
149
FixedFileInfo value;
150
StringFileInfo string_file_info;
151
VarFileInfo var_file_info;
152
153
Vector<uint8_t> save() const;
154
VersionInfo();
155
};
156
157
struct ManifestInfo : Structure {
158
String manifest;
159
160
Vector<uint8_t> save() const;
161
ManifestInfo() {}
162
};
163
164
struct IconEntry : ByteStream {
165
static const uint32_t SIZE = 16;
166
167
uint8_t width = 0;
168
uint8_t height = 0;
169
uint8_t colors = 0;
170
uint8_t reserved = 0;
171
uint16_t planes = 0;
172
uint16_t bits_per_pixel = 32;
173
uint32_t image_size = 0;
174
uint32_t image_offset = 0;
175
Vector<uint8_t> data;
176
177
Vector<uint8_t> save() const;
178
void load(Ref<FileAccess> p_file);
179
};
180
181
struct GroupIcon : ByteStream {
182
static constexpr uint8_t SIZES[6] = { 16, 32, 48, 64, 128, 0 };
183
184
uint16_t reserved = 0;
185
uint16_t type = 1;
186
uint16_t image_count = 0;
187
Vector<IconEntry> icon_entries;
188
Vector<Vector<uint8_t>> images;
189
190
Vector<uint8_t> save() const;
191
void load(Ref<FileAccess> p_icon_file);
192
void fill_with_godot_blue();
193
};
194
195
struct SectionEntry : ByteStream {
196
static const uint32_t SIZE = 40;
197
198
String name;
199
uint32_t virtual_size = 0;
200
uint32_t virtual_address = 0;
201
uint32_t size_of_raw_data = 0;
202
uint32_t pointer_to_raw_data = 0;
203
uint32_t pointer_to_relocations = 0;
204
uint32_t pointer_to_line_numbers = 0;
205
uint16_t number_of_relocations = 0;
206
uint16_t number_of_line_numbers = 0;
207
uint32_t characteristics = 0;
208
209
Vector<uint8_t> save() const;
210
void load(Ref<FileAccess> p_file);
211
};
212
213
struct ResourceDataEntry : ByteStream {
214
static const uint16_t SIZE = 16;
215
216
uint32_t rva = 0;
217
uint32_t size = 0;
218
219
Vector<uint8_t> save() const;
220
};
221
222
uint32_t _snap(uint32_t p_value, uint32_t p_size) const;
223
uint32_t _get_pe_header_offset(Ref<FileAccess> p_executable) const;
224
Vector<SectionEntry> _get_section_entries(Ref<FileAccess> p_executable) const;
225
GroupIcon _create_group_icon(const String &p_icon_path) const;
226
ManifestInfo _create_manifest_info() const;
227
VersionInfo _create_version_info(const HashMap<String, String> &p_strings) const;
228
Vector<uint8_t> _create_resources(uint32_t p_virtual_address, const GroupIcon &p_group_icon, const VersionInfo &p_version_info, const ManifestInfo &p_manifest_info) const;
229
Error _truncate(const String &p_executable_path, uint32_t p_size) const;
230
HashMap<String, String> _get_strings(const Ref<EditorExportPreset> &p_preset) const;
231
Error _modify_template(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path) const;
232
233
public:
234
static Error modify(const Ref<EditorExportPreset> &p_preset, const String &p_template_path, const String &p_icon_path);
235
};
236
237