template_modifier.cpp 30 KB

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