Skip to content

Commit

Permalink
Another patches support added: 1.24e, 1.27a, 1.27b, 1.28f.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ev3nt committed Aug 7, 2022
1 parent 4c1d25e commit c76a857
Show file tree
Hide file tree
Showing 28 changed files with 486 additions and 1,358 deletions.
1,059 changes: 0 additions & 1,059 deletions 3rd/Detours/Include/detours.h

This file was deleted.

27 changes: 0 additions & 27 deletions 3rd/Detours/Include/detver.h

This file was deleted.

89 changes: 0 additions & 89 deletions 3rd/Detours/Include/syelog.h

This file was deleted.

Binary file removed 3rd/Detours/Lib/detours.lib
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# War3 Lua - 1.1.9 (<span style="color:orange">Outland</span>)
# War3 Lua - 1.2.9 (<span style="color:orange">Outland</span>)

[![build](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml/badge.svg)](https://github.com/Ev3nt/war3_lua/actions/workflows/build.yml)
![lua](https://img.shields.io/badge/lua-v5.4.4-blue)
Expand Down
42 changes: 42 additions & 0 deletions Src/Detours.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "pch.h"
#include "Detours.h"

namespace Detour {
BOOL Install(UINT_PTR* pointer_ptr, UINT_PTR detour) {
BOOL error = NO_ERROR;

if ((error = DetourTransactionBegin()) != NO_ERROR) { printf("DetourTransactionBegin failed with error: %d.\n", error); }
if (!error && (error = DetourUpdateThread(GetCurrentThread())) != NO_ERROR) { printf("DetourUpdateThread failed with error: %d.\n", error); }
if (!error && (error = DetourAttach((PVOID*)pointer_ptr, (PVOID)detour)) != NO_ERROR) { printf("DetourAttach failed with error: %d.\n", error); }
if (!error && (error = DetourTransactionCommit()) != NO_ERROR) { printf("DetourTransactionCommit failed with error: %d.\n", error); }

if (error != NO_ERROR) { DetourTransactionAbort(); }

return error;
}

BOOL Uninstall(UINT_PTR* pointer_ptr, UINT_PTR detour) {
BOOL error = NO_ERROR;

if ((error = DetourTransactionBegin()) != NO_ERROR) { wprintf_s(L"DetourTransactionBegin failed with error: %d.\n", error); }
if (!error && (error = DetourUpdateThread(::GetCurrentThread())) != NO_ERROR) { wprintf_s(L"DetourUpdateThread failed with error: %d.\n", error); }
if (!error && (error = DetourDetach((PVOID*)pointer_ptr, (PVOID)detour)) != NO_ERROR) { wprintf_s(L"DetourAttach failed with error: %d.\n", error); }
if (!error && (error = DetourTransactionCommit()) != NO_ERROR) { wprintf_s(L"DetourTransactionCommit failed with error: %d.\n", error); }

if (error != NO_ERROR) { DetourTransactionAbort(); }

return error;
}

BOOL InstallEx(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour) {
if (pointer_ptr && *pointer_ptr != NULL) {
return flag ? Install(pointer_ptr, detour) : Uninstall(pointer_ptr, detour);
}

return -1;
}

BOOL SetState(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour) {
return flag ? Install(pointer_ptr, detour) : Uninstall(pointer_ptr, detour);
}
}
8 changes: 8 additions & 0 deletions Src/Detours.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace Detour {
BOOL Install(UINT_PTR* pointer_ptr, UINT_PTR detour);
BOOL Uninstall(UINT_PTR* pointer_ptr, UINT_PTR detour);
BOOL InstallEx(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour);
BOOL SetState(bool flag, UINT_PTR* pointer_ptr, UINT_PTR detour);
}
24 changes: 9 additions & 15 deletions Src/DllMain.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pch.h"
#include "JassMachine.h"
#include "JassNatives.h"
#include "Offsets.h"
#include "Hooks.h"
#include "Logger.h"
#include "EasterEgg.h"
Expand Down Expand Up @@ -30,12 +31,11 @@ BOOL APIENTRY DllMain(HMODULE module, UINT reason, LPVOID reserved) {

JassMachine::JassOpcodeInitialize();

Hooks::AttachHooks();
Hooks::SetHooksState(true);

break;
case DLL_PROCESS_DETACH:

Hooks::DetachHooks();
Hooks::SetHooksState(false);

Logger::CloseConsole();

Expand All @@ -48,23 +48,17 @@ BOOL APIENTRY DllMain(HMODULE module, UINT reason, LPVOID reserved) {
//---------------------------------------------------------------------

bool StartUp() {
DWORD handle;
DWORD size = GetFileVersionInfoSize("game.dll", &handle);

LPSTR buffer = new char[size];
GetFileVersionInfo("game.dll", handle, size, buffer);
if (!InitOffsets()) {
MessageBox(NULL, "Unsupported version of game.dll.\nLua will be unloaded.", "Error", MB_ICONHAND | MB_TOPMOST);

VS_FIXEDFILEINFO* verInfo;
size = sizeof(VS_FIXEDFILEINFO);
VerQueryValue(buffer, "\\", (LPVOID*)&verInfo, (UINT*)&size);
delete[] buffer;
return false;
}

if (((verInfo->dwFileVersionMS >> 16) & 0xffff) != 1 || ((verInfo->dwFileVersionMS >> 0) & 0xffff) != 26 || ((verInfo->dwFileVersionLS >> 16) & 0xffff) != 0 || ((verInfo->dwFileVersionLS >> 0) & 0xffff) != 6401)
{
/*if (Warcraft::GetWarcraftVersion() != Warcraft::VERSION::V126a) {
MessageBox(NULL, "Unsupported version of game.dll.\nLua will be unloaded.", "Error", MB_ICONHAND | MB_TOPMOST);
return false;
}
}*/

PSTR cmdline = GetCommandLine();
size_t i;
Expand Down
74 changes: 50 additions & 24 deletions Src/Hooks.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
#include "pch.h"
#include "Hooks.h"
#include "Offsets.h"
#include "LuaMachine.h"

#define AttachDetour(pointer, detour) (DetourUpdateThread(GetCurrentThread()), DetourAttach(&(PVOID&)pointer, detour))
#define DetachDetour(pointer, detour) (DetourUpdateThread(GetCurrentThread()), DetourDetach(&(PVOID&)pointer, detour))

namespace Hooks {
auto SetJassState = (void(__fastcall*)(BOOL jassState))((std::ptrdiff_t)gameBase + 0x2ab0e0);
auto GetWarcraftID = (DWORD(__stdcall*)())((std::ptrdiff_t)gameBase + 0x537ed0);
enum class EventTypes : UINT {
EVENT_CNET_GAME_START = 0x4009007e,
EVENT_CNET_GAME_LEAVE = 0x40090081,
};

DWORD GetWarcraftIDCustom()
{
return *(DWORD*)GAME_ID;
}
enum class ObserverRegistryTypes : UINT {
GAME_START_LOADING = 0x00402000,
GAME_FINISH_LOADING = 0x00140800
};

void __fastcall SetJassStateCustom(BOOL jassState) {
if (jassState == TRUE) {
LuaMachine::StartLua();
}
typedef struct {
PVOID vtable; // 0x0
size_t refCount; // 0x4
UINT* registry; // 0x8 ObserverRegistry
} CObserver;

typedef struct {
PVOID vtable; // 0x0
UINT unk_04; // 0x4
UINT id; // 0x8
PVOID object; // 0xC
DWORD data; // 0x10 | serves as keyCode for KeyEvent
} CEvent;

return SetJassState(jassState);
BOOL CGameProcessEvent(CObserver* observer, CEvent* cevent) {
return pOffsets[(UINT)Offset::CGameEventHandler] ? this_call<BOOL>(pOffsets[(UINT)Offset::CGameEventHandler], observer, cevent) : NULL;
}

void AttachHooks() {
DetourTransactionBegin();
DWORD GetWarcraftVersionKey() {
return pOffsets[(UINT)Offset::GetWarcraftVersionKey] ? std_call<DWORD>(pOffsets[(UINT)Offset::GetWarcraftVersionKey]) : NULL;
}

AttachDetour(SetJassState, SetJassStateCustom);
AttachDetour(GetWarcraftID, GetWarcraftIDCustom);
//--------------------------------------------

DetourTransactionCommit();
DWORD GetLuaVersionKey() {
return *(DWORD*)GAME_ID;
}

void DetachHooks() {
DetourTransactionBegin();
BOOL __fastcall CGameEventHandler(CObserver* observer, PVOID, CEvent* cevent) {
if (observer && cevent) {
switch (cevent->id) {
case (UINT)EventTypes::EVENT_CNET_GAME_START:
if (*observer->registry == (UINT)ObserverRegistryTypes::GAME_FINISH_LOADING) {
LuaMachine::StartLua();
}

break;
case (UINT)EventTypes::EVENT_CNET_GAME_LEAVE:
LuaMachine::DestroyLua();

DetachDetour(SetJassState, SetJassStateCustom);
DetachDetour(GetWarcraftID, GetWarcraftIDCustom);
break;
}
}

return CGameProcessEvent(observer, cevent);
}

DetourTransactionCommit();
void SetHooksState(bool flag) {
Detour::SetState(flag,&pOffsets[(UINT)Offset::CGameEventHandler], (UINT_PTR)CGameEventHandler);
Detour::SetState(flag,&pOffsets[(UINT)Offset::GetWarcraftVersionKey], (UINT_PTR)GetLuaVersionKey);
}
}
4 changes: 1 addition & 3 deletions Src/Hooks.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

namespace Hooks {
void AttachHooks();

void DetachHooks();
void SetHooksState(bool flag);
}
14 changes: 7 additions & 7 deletions Src/JassMachine.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#include "pch.h"
#include "JassMachine.h"
#include "LuaMachine.h"
#include "Offsets.h"
#include "fp_call.h"

namespace JassMachine {
PVOID** ppOpcodeList = (PVOID**)((std::ptrdiff_t)gameBase + 0x45ea5a);
BYTE* pOpcodeListSize = (BYTE*)((std::ptrdiff_t)gameBase + 0x45ea4d);
PVOID opcodeDefaultOutput = (PVOID)((std::ptrdiff_t)gameBase + 0x45f79a);

PVOID OPCODE_FUNCTIONS[44];

DWORD OpcodeStartLuaThread() {
LuaMachine::StartLuaThread();

return c_call<int>(JassMachine::opcodeDefaultOutput);
return c_call<int>(pOffsets[(UINT)Offset::OpcodeDefaultOutput]); // jump
}

void JassOpcodeInitialize() {
PVOID** ppOpcodeList = (PVOID**)(pOffsets[(UINT)Offset::OpcodeList]);
BYTE* pOpcodeListSize = (BYTE*)(pOffsets[(UINT)Offset::OpcodeSize]);

CopyMemory(OPCODE_FUNCTIONS, *ppOpcodeList, sizeof(OPCODE_FUNCTIONS));

OPCODE_FUNCTIONS[OPTYPE_STARTLUATHREAD - 2] = OpcodeStartLuaThread; // My own opcode function
OPCODE_FUNCTIONS[(UINT)OPCODE_TYPE::STARTLUATHREAD - 2] = OpcodeStartLuaThread; // My own opcode function

DWORD dwOldProtect;
VirtualProtect(pOpcodeListSize, sizeof(BYTE), PAGE_EXECUTE_READWRITE, &dwOldProtect);
Expand All @@ -34,7 +34,7 @@ namespace JassMachine {
//-----------------------------------------------------------

PJASS_THREAD_LOCAL GetJassThreadLocal() {
return (PJASS_THREAD_LOCAL)GetInstance(5);
return (PJASS_THREAD_LOCAL)Warcraft::GetTLSValueByIndex(5);
}

PJASS_INSTANCE GetJassMachine(UINT index) {
Expand Down
Loading

0 comments on commit c76a857

Please sign in to comment.