Skip to content

Commit

Permalink
input, ime (these are not completed)
Browse files Browse the repository at this point in the history
  • Loading branch information
matanki-saito committed May 31, 2024
1 parent 71b1d3c commit 1d9af95
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Plugin64/byte_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ void BytePattern::get_module_ranges(memory_pointer module)
PIMAGE_DOS_HEADER dosHeader = module.pointer<IMAGE_DOS_HEADER>();
PIMAGE_NT_HEADERS ntHeader = module.pointer<IMAGE_NT_HEADERS>(dosHeader->e_lfanew);



for (int i = 0; i < ntHeader->FileHeader.NumberOfSections; i++)
{
auto sec = getSection(ntHeader, i);
Expand Down
4 changes: 2 additions & 2 deletions Plugin64/dll_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ BOOL APIENTRY DllMain(HMODULE hModule,
e |= Date::Init(options);

// IME
//e |= Ime::Init(options);
e |= Ime::Init(options);

// 入力
//e |= Input::Init(options);
e |= Input::Init(options);

// 文字列順序入れ替え
e |= Localization::Init(options);
Expand Down
157 changes: 135 additions & 22 deletions Plugin64/ime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ime {
extern "C" {
void imeProc1();
void imeProc1V137();
void imeProc2();
void imeProc3();
uintptr_t imeProc1ReturnAddress1;
Expand All @@ -13,6 +14,7 @@ namespace Ime {
uintptr_t imeProc2CallAddress;
uintptr_t imeProc2ReturnAddress1;
uintptr_t imeProc2ReturnAddress2;
uintptr_t imeProc2ReturnAddress3;
uintptr_t rectAddress;
uintptr_t imeProc3ReturnAddress;
uintptr_t imeProc3CallAddress1;
Expand All @@ -38,10 +40,25 @@ namespace Ime {
DllError e = {};

switch (options.version) {
case v1_36_0_0:
case v1_35_1_0:
case v1_29_3_0:
case v1_29_4_0:
case v1_30_1_0:
case v1_30_2_0:
case v1_30_3_0:
case v1_30_4_0:
case v1_30_5_0:
case v1_31_1_0:
case v1_31_2_0:
case v1_31_3_0:
case v1_31_4_0:
case v1_31_5_0:
case v1_31_6_0:
case v1_32_0_1:
case v1_33_0_0:
case v1_33_3_0:
case v1_34_2_0:
// mov edx, r13d
BytePattern::temp_instance().find_pattern("41 8B D5 49 8B CC E8 ? ? ? ? 85 C0 0F 85 F1");
BytePattern::temp_instance().find_pattern("41 8B D5 49 8B CC E8 ? ? ? ? 85 C0 0F 85");
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowsevents.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

Expand All @@ -60,25 +77,10 @@ namespace Ime {
e.ime.unmatchdImeProc1Injector = true;
}
break;
case v1_29_3_0:
case v1_29_4_0:
case v1_30_1_0:
case v1_30_2_0:
case v1_30_3_0:
case v1_30_4_0:
case v1_30_5_0:
case v1_31_1_0:
case v1_31_2_0:
case v1_31_3_0:
case v1_31_4_0:
case v1_31_5_0:
case v1_31_6_0:
case v1_32_0_1:
case v1_33_0_0:
case v1_33_3_0:
case v1_34_2_0:
case v1_35_1_0:
case v1_36_0_0:
// mov edx, r13d
BytePattern::temp_instance().find_pattern("41 8B D5 49 8B CC E8 ? ? ? ? 85 C0 0F 85");
BytePattern::temp_instance().find_pattern("41 8B D5 49 8B CC E8 ? ? ? ? 85 C0 0F 85 F1");
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowsevents.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

Expand All @@ -97,6 +99,27 @@ namespace Ime {
e.ime.unmatchdImeProc1Injector = true;
}
break;
case v1_37_0_0:
// mov edx, edi
BytePattern::temp_instance().find_pattern("8B D7 49 8B CC E8 ? ? ? ? 85 C0 0F 85 5B");
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowsevents.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

// call {sub_xxxxx}
imeProc1CallAddress = Injector::GetBranchDestination(address + 0x5).as_int();

// cmp edi, 0FFh
imeProc1ReturnAddress1 = address + 0x12;

// jz {xxxxx}
imeProc1ReturnAddress2 = address - 0x3A;

Injector::MakeJMP(address, imeProc1V137, true);
}
else {
e.ime.unmatchdImeProc1Injector = true;
}
break;
default:
e.ime.versionImeProc1Injector = true;
}
Expand All @@ -112,6 +135,7 @@ namespace Ime {
// https://github.com/matanki-saito/EU4dll/issues/19#issuecomment-423940649
DllError imeProc2Injector(RunOptions options) {
DllError e = {};
HMODULE handle = NULL;

switch (options.version) {
case v1_29_3_0:
Expand Down Expand Up @@ -188,6 +212,63 @@ namespace Ime {
else {
e.ime.unmatchdImeProc2Injector = true;
}
break;
case v1_37_0_0:
rectAddress = (uintptr_t)&rect;

// SDL_SetTextInputRectの関数を見つける
// add rsp, 20h
handle = GetModuleHandle(NULL);
imeProc2CallAddress = (uintptr_t)GetProcAddress(handle, "SDL_SetTextInputRect");

if (imeProc2CallAddress == NULL) {
e.ime.unmatchdImeProc2Injector = true;
}

// WM_IME_STARTCOMPOSITIONでSDL_SetTextInputRectする
BytePattern::temp_instance().find_pattern("81 EA BC 00 00 00 0F 84 C3 02 00 00"); // sub edx, 0BCh
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowskeyboard.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

// jz {loc_xxxxx}
imeProc2ReturnAddress1 = Injector::GetBranchDestination(address + 0x6).as_int();

// jnz loc_xxxxx
imeProc2ReturnAddress2 = address + 0xF;

Injector::MakeJMP(address, imeProc2, true);
}
else {
e.ime.unmatchdImeProc2Injector = true;
}

// WM_IME_SETCONTEXTで*lParam = 0;をコメントアウトする(nopで埋める)
// mov [r9], r13
BytePattern::temp_instance().find_pattern("4D 89 29 48 8B 5C 24 50");
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowskeyboard.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();
Injector::WriteMemory<uint8_t>(address, 0x90, true);
Injector::WriteMemory<uint8_t>(address + 1, 0x90, true);
Injector::WriteMemory<uint8_t>(address + 2, 0x90, true);
}
else {
e.ime.unmatchdImeProc2Injector = true;
}

// WM_IME_COMPOSITIONのif文のIME_GetCompositionStringとIME_SendInputEventをコメントアウト(jmpさせる)
// mov r8d, 800h
// 二つ目のif文もスキップさせる
// https://github.com/matanki-saito/EU4dll/issues/19#issuecomment-423940364
BytePattern::temp_instance().find_pattern("41 B8 00 08 00 00 48 89 7C 24 58");
if (BytePattern::temp_instance().has_size(1, u8"SDL_windowskeyboard.cの修正")) {
// jnb short loc_1417613D8
uintptr_t address = BytePattern::temp_instance().get_first().address(-2);
Injector::MakeJMP(address, address + 0x9D, true);
}
else {
e.ime.unmatchdImeProc2Injector = true;
}

break;
default:
e.ime.unmatchdImeProc2Injector = true;
Expand Down Expand Up @@ -253,6 +334,38 @@ namespace Ime {
e.ime.unmatchdImeProc3Injector = true;
}
break;
case v1_37_0_0:
// 直前の部分でjmpに使う14byteを確保することができなかった。
// そのためWM_KEYDOWNのコードをすべて移植した
// mov rcx, [rbp+0C0h+hRawInput]
BytePattern::temp_instance().find_pattern("48 8B 8D E8 ? ? ? ? 8B D6 E8 ? ? ? ? 33");
if (BytePattern::temp_instance().has_size(2, u8"SDL_windowsevents.cの修正")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

// call {sub_xxxxx} / WindowsScanCodeToSDLScanCode
imeProc3CallAddress1 = Injector::GetBranchDestination(address + 0xA).as_int();

// call {sub_xxxxx} / SDL_GetKeyboardState
imeProc3CallAddress2 = Injector::GetBranchDestination(address + 0x13).as_int();

// call {sub_xxxxx} / ShouldGenerateWindowCloseOnAltF4
imeProc3CallAddress3 = Injector::GetBranchDestination(address + 0x36).as_int();

// call {sub_xxxxx} / SDL_SendWindowEvent
imeProc3CallAddress4 = Injector::GetBranchDestination(address + 0x50).as_int();

// call {sub_xxxxx} / SDL_SendKeyboardKey
imeProc3CallAddress5 = Injector::GetBranchDestination(address + 0x61).as_int();

// xor edi, edi
imeProc3ReturnAddress = address + 0x66;

Injector::MakeJMP(address, imeProc3, true);
}
else {
e.ime.unmatchdImeProc3Injector = true;
}
break;
default:
e.ime.versionImeProc3Injector = true;
}
Expand All @@ -265,7 +378,7 @@ namespace Ime {

result |= imeProc1Injector(options);
result |= imeProc2Injector(options);
result |= imeProc3Injector(options);
//result |= imeProc3Injector(options);

return result;
}
Expand Down
18 changes: 18 additions & 0 deletions Plugin64/ime_asm.asm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ imeProc1 ENDP

;-------------------------------------------;

imeProc1V137 PROC
mov edx, edi;
mov rcx, r12;
call imeProc1CallAddress;
test eax,eax;
jnz JMP_A;

push imeProc1ReturnAddress1;
ret;

JMP_A:
push imeProc1ReturnAddress2;
ret;

imeProc1V137 ENDP

;-------------------------------------------;

imeProc2 PROC
sub edx, 0BCh;
jz JMP_A;
Expand Down
32 changes: 31 additions & 1 deletion Plugin64/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Input {
extern "C" {
void inputProc1();
void inputProc1V130();
void inputProc1V137();
uintptr_t inputProc1ReturnAddress1;
uintptr_t inputProc1ReturnAddress2;
uintptr_t inputProc1CallAddress;
Expand Down Expand Up @@ -93,6 +94,35 @@ namespace Input {
e.input.unmatchdInputProc1Injector = true;
}

break;
case v1_37_0_0:
// mov eax, dword ptr [rbp+120h+var_18C]
BytePattern::temp_instance().find_pattern("8B 45 BC 32 DB 3C 80 73 05 0F B6 D8 EB 12");
if (BytePattern::temp_instance().has_size(1, u8"入力した文字をutf8からエスケープ列へ変換する1")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();

inputProc1CallAddress = (uintptr_t)utf8ToEscapedStr3;

// mov rax, [r15]
inputProc1ReturnAddress1 = address + 0x20;

Injector::MakeJMP(address, inputProc1V137, true);
}
else {
e.input.unmatchdInputProc1Injector = true;
}

// call qword ptr [rax+18h]
BytePattern::temp_instance().find_pattern("FF 50 18 E9 ? ? ? ? 49 8B 07 45 33 C9");
if (BytePattern::temp_instance().has_size(1, u8"入力した文字をutf8からエスケープ列へ変換する2")) {
uintptr_t address = BytePattern::temp_instance().get_first().address();
// jmp loc_{xxxxx}
inputProc1ReturnAddress2 = Injector::GetBranchDestination(address + 0x3).as_int();
}
else {
e.input.unmatchdInputProc1Injector = true;
}

break;
default:
e.input.versionInputProc1Injector = true;
Expand Down Expand Up @@ -149,7 +179,7 @@ namespace Input {
DllError result = {};

result |= inputProc1Injector(options);
result |= inputProc2Injector(options);
//result |= inputProc2Injector(options);

return result;
}
Expand Down
Loading

0 comments on commit 1d9af95

Please sign in to comment.