|
1 | | -from PEData import PEData, PESect |
2 | | -from COFFData import COFFData, COFFSect |
| 1 | +from .PEData import PEData, PESect |
| 2 | +from .COFFData import COFFData, COFFSect |
3 | 3 | import os |
4 | 4 | from pathlib import Path |
5 | 5 | import re |
6 | 6 | from typing import Optional |
7 | 7 | import struct |
8 | 8 | import itertools |
| 9 | +from patcher import Hook |
9 | 10 |
|
10 | | - |
11 | | -CLANG_FLAGS = " ".join(["-pipe -m32 -Os -nostdlib -nostartfiles -w -masm=intel -std=c++20 -march=core2 -c", |
| 11 | +CLANG_FLAGS = " ".join(["-pipe -m32 -Os -nostdlib -Werror -masm=intel -std=c++20 -march=core2 -c", |
12 | 12 | ]) |
13 | 13 |
|
14 | | -GCC_FLAGS = " ".join(["-pipe -m32 -Os -fno-exceptions -nostdlib -nostartfiles -w -fpermissive -masm=intel -std=c++20 -march=core2 -mfpmath=both", |
| 14 | +GCC_FLAGS = " ".join(["-pipe -m32 -Os -fno-exceptions -nostdlib -nostartfiles -fpermissive -masm=intel -std=c++20 -march=core2 -mfpmath=both", |
15 | 15 | ]) |
| 16 | + |
| 17 | +GCC_FLAGS_ASM = " ".join(["-pipe -m32 -Os -fno-exceptions -nostdlib -nostartfiles -w -fpermissive -masm=intel -std=c++20 -march=core2 -mfpmath=both", |
| 18 | + ]) |
16 | 19 | SECT_SIZE = 0x80000 |
17 | 20 |
|
18 | 21 | ASM_RE = re.compile(r"(asm\(\"(0[xX][0-9a-fA-F]{1,8})\"\);)", re.IGNORECASE) |
@@ -380,9 +383,19 @@ def create_defines_file(path: Path, addresses: dict[str, str]): |
380 | 383 | f.write(f"#define {name} {address}\n") |
381 | 384 | create_defines_file(target_path / "define.h", addresses) |
382 | 385 |
|
| 386 | + def generate_hook_files(folder_path: Path): |
| 387 | + for file_path in list_files_at(folder_path, "**/*.hook"): |
| 388 | + hook = Hook. load_hook(folder_path/file_path) |
| 389 | + hook_path = file_path.replace(os.sep, "_") + ".cpp" |
| 390 | + print(f"Generating {hook_path}") |
| 391 | + with open(folder_path/hook_path, "w") as f: |
| 392 | + f.write(hook.to_cpp()) |
| 393 | + |
| 394 | + generate_hook_files(target_path/"hooks") |
| 395 | + |
383 | 396 | if run_system( |
384 | 397 | f"""cd {build_folder_path} & |
385 | | - {gcc_compiler_path} -c {GCC_FLAGS} ../hooks/*.cpp"""): |
| 398 | + {gcc_compiler_path} -c {GCC_FLAGS_ASM} ../hooks/*.cpp"""): |
386 | 399 | raise Exception("Errors occurred during building of hooks files") |
387 | 400 |
|
388 | 401 | hooks: list[COFFData] = [] |
@@ -417,7 +430,7 @@ def create_defines_file(path: Path, addresses: dict[str, str]): |
417 | 430 | for hook in hooks: |
418 | 431 | for sect in hook.sects: |
419 | 432 | pld.writelines([ |
420 | | - f" .h{hi} 0x{sect.offset:x} : SUBALIGN(1) {{\n", |
| 433 | + f" .h{hi:X} 0x{sect.offset:x} : SUBALIGN(1) {{\n", |
421 | 434 | f" {hook.name}({sect.name})\n", |
422 | 435 | " }\n", |
423 | 436 | ]) |
@@ -457,7 +470,7 @@ def replace_data(new_data, offset): |
457 | 470 | print(f"No hooks in {hook.name}") |
458 | 471 | continue |
459 | 472 | for sect in hook.sects: |
460 | | - psect = patch_pe.find_sect(f".h{hi}") |
| 473 | + psect = patch_pe.find_sect(f".h{hi:X}") |
461 | 474 | size = sect.size |
462 | 475 | replace_data( |
463 | 476 | patch_pe.data[psect.f_offset:psect.f_offset + size], psect.v_offset) |
@@ -507,3 +520,4 @@ def save_new_base_data(data: bytearray): |
507 | 520 | save_new_base_data(base_file_data) |
508 | 521 |
|
509 | 522 | remove_files_at(build_folder_path, "**/*.o") |
| 523 | + remove_files_at(target_path/"hooks", "*.hook.cpp") |
0 commit comments