Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
bestkakkoii authored and bestkakkoii committed Feb 13, 2024
1 parent 240e8cd commit a1c7eac
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 59 deletions.
7 changes: 5 additions & 2 deletions SaSH/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ bool __fastcall mem::writeString(HANDLE hProcess, unsigned long long baseAddress
unsigned long long __fastcall mem::virtualAlloc(HANDLE hProcess, unsigned long long size)
{
if (hProcess == nullptr || size == 0)
throw std::invalid_argument("Invalid handle or size");
return 0;

// Ensure size is aligned to page size
size = (size + 4095) & ~4095ULL;
Expand All @@ -186,7 +186,10 @@ unsigned long long __fastcall mem::virtualAlloc(HANDLE hProcess, unsigned long l

NTSTATUS status = MINT::NtAllocateVirtualMemory(hProcess, &ptr, NULL, &sizet, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!NT_SUCCESS(status))
throw std::runtime_error("Memory allocation failed");
return 0;

if (ptr == nullptr)
return 0;

return reinterpret_cast<unsigned long long>(ptr);
}
Expand Down
65 changes: 28 additions & 37 deletions sadll/sadll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")

WNDPROC g_OldWndProc = nullptr;
HWND g_MainHwnd = nullptr;
HMODULE g_hGameModule = nullptr;
HMODULE g_hDllModule = nullptr;
WCHAR g_szGameModulePath[MAX_PATH] = {};
DWORD g_MainThreadId = 0;
HANDLE g_MainThreadHandle = nullptr;
HWND g_ParenthWnd = nullptr;

template<typename T>
inline static T CONVERT_GAMEVAR(ULONG_PTR offset) { return (T)((reinterpret_cast<ULONG_PTR>(g_hGameModule) + offset)); }

#pragma region Debug
#ifdef USE_MINIDUMP
#include <DbgHelp.h>
Expand Down Expand Up @@ -530,14 +518,14 @@ void GameService::New_lssproto_TK_send(int fd, int x, int y, const char* message
//W2移動收包攔截
void GameService::New_lssproto_W2_send(int fd, int x, int y, const char* message) const
{
//PostMessageW(g_ParenthWnd, kSetMove, NULL, MAKELPARAM(x, y));
//postMessage(kSetMove, NULL, MAKELPARAM(x, y));
pLssproto_W2_send(fd, x, y, message);
}

void GameService::New_CreateDialog(int unk, int type, int button, int unitid, int dialogid, const char* data) const
{
pCreateDialog(unk, type, button, unitid, dialogid, data);
*CONVERT_GAMEVAR<int*>(0x4200004ul) = 1;
*(CONVERT_GAMEVAR<int*>(0x4200004ul)) = 1;

}
#pragma endregion
Expand Down Expand Up @@ -1325,7 +1313,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
case WM_MOUSEMOVE:
{
//通知外掛更新當前鼠標坐標顯示
PostMessageW(g_ParenthWnd, message + static_cast<UINT>(WM_USER), wParam, lParam);
g_GameService.postMessage(message + static_cast<UINT>(WM_USER), wParam, lParam);
break;
}
case WM_KEYDOWN:
Expand All @@ -1346,19 +1334,19 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
{
constexpr size_t inputBoxBufSize = 20u;

int index = *CONVERT_GAMEVAR<int*>(0x415EF50ul);
int index = *g_GameService.CONVERT_GAMEVAR<int*>(0x415EF50ul);

if ((index == 0) || (strlen(CONVERT_GAMEVAR<char*>(0x414F278ul)) == 0))
if ((index == 0) || (strlen(g_GameService.CONVERT_GAMEVAR<char*>(0x414F278ul)) == 0))
{
//account
memset(CONVERT_GAMEVAR<char*>(0x414F278ul), 0, inputBoxBufSize);
_snprintf_s(CONVERT_GAMEVAR<char*>(0x414F278ul), inputBoxBufSize, _TRUNCATE, "%s", pszText);
memset(g_GameService.CONVERT_GAMEVAR<char*>(0x414F278ul), 0, inputBoxBufSize);
_snprintf_s(g_GameService.CONVERT_GAMEVAR<char*>(0x414F278ul), inputBoxBufSize, _TRUNCATE, "%s", pszText);
}
else
{
//password
memset(CONVERT_GAMEVAR<char*>(0x415AA58ul), 0, inputBoxBufSize);
_snprintf_s(CONVERT_GAMEVAR<char*>(0x415AA58ul), inputBoxBufSize, _TRUNCATE, "%s", pszText);
memset(g_GameService.CONVERT_GAMEVAR<char*>(0x415AA58ul), 0, inputBoxBufSize);
_snprintf_s(g_GameService.CONVERT_GAMEVAR<char*>(0x415AA58ul), inputBoxBufSize, _TRUNCATE, "%s", pszText);
}
}

Expand All @@ -1369,7 +1357,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
return CloseClipboard();
}

return SendMessageW(hWnd, WM_PASTE, NULL, NULL);
return g_GameService.sendMessage(WM_PASTE, NULL, NULL);
}
break;
}
Expand All @@ -1379,10 +1367,10 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
{
case VK_DELETE: //檢查是否為delete
{
if (nullptr == g_ParenthWnd)
if (nullptr == g_GameService.g_ParenthWnd)
break;

PostMessageW(g_ParenthWnd, message + static_cast<UINT>(WM_USER + VK_DELETE), wParam, lParam);
g_GameService.postMessage(message + static_cast<UINT>(WM_USER + VK_DELETE), wParam, lParam);
break;
}
default:
Expand Down Expand Up @@ -1537,41 +1525,43 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
break;
}

return CallWindowProcW(g_OldWndProc, g_MainHwnd, message, wParam, lParam);
return g_GameService.callWindowProc(message, wParam, lParam);
}
#pragma endregion

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID)
{
if (DLL_PROCESS_ATTACH == ul_reason_for_call)
{
GameService& g_GameService = GameService::getInstance();

DWORD dwThreadId = GetCurrentThreadId();

if ((g_MainThreadId > 0UL) && (g_MainThreadId != dwThreadId))
if ((g_GameService.g_MainThreadId > 0UL) && (g_GameService.g_MainThreadId != dwThreadId))
return TRUE;

HWND hWnd = util::GetCurrentWindowHandle();
if (nullptr == hWnd)
return TRUE;

GetModuleFileNameW(nullptr, g_szGameModulePath, MAX_PATH);
GetModuleFileNameW(nullptr, g_GameService.g_szGameModulePath, MAX_PATH);


GameService& g_GameService = GameService::getInstance();
g_GameService.g_consoleHwnd = util::createConsole();
if (g_GameService.g_consoleHwnd != nullptr)
ShowWindow(g_GameService.g_consoleHwnd, SW_HIDE);

g_hGameModule = GetModuleHandleW(nullptr);
g_GameService.g_hGameModule = GetModuleHandleW(nullptr);

g_MainThreadId = dwThreadId;
g_GameService.g_MainThreadId = dwThreadId;

g_MainThreadHandle = GetCurrentThread();
g_GameService.g_MainThreadHandle = GetCurrentThread();

g_hDllModule = hModule;
g_GameService.g_hDllModule = hModule;

g_MainHwnd = hWnd;
g_GameService.g_MainHwnd = hWnd;

g_OldWndProc = reinterpret_cast<WNDPROC>(GetWindowLongW(hWnd, GWL_WNDPROC));
g_GameService.g_OldWndProc = reinterpret_cast<WNDPROC>(GetWindowLongW(hWnd, GWL_WNDPROC));

SetWindowLongW(hWnd, GWL_WNDPROC, reinterpret_cast<LONG>(WndProc));

Expand All @@ -1585,11 +1575,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID)
{
GameService& g_GameService = GameService::getInstance();
if (g_GameService.uninitialize() == TRUE)
SetWindowLongW(g_MainHwnd, GWL_WNDPROC, reinterpret_cast<LONG>(g_OldWndProc));
SetWindowLongW(g_GameService.g_MainHwnd, GWL_WNDPROC, reinterpret_cast<LONG>(g_GameService.g_OldWndProc));
}

return TRUE;
}
}

BOOL GameService::initialize(long long index, HWND parentHwnd, unsigned short type, unsigned short port)
{
Expand Down Expand Up @@ -1769,6 +1759,7 @@ BOOL GameService::initialize(long long index, HWND parentHwnd, unsigned short ty
if (nullptr == syncClient_)
return FALSE;

syncClient_->setHWnd(g_MainHwnd);
syncClient_->setCloseSocketFunction(pclosesocket);
syncClient_->setRecvFunction(precv);
if (syncClient_->Connect(type, port) == TRUE)
Expand All @@ -1780,7 +1771,7 @@ BOOL GameService::initialize(long long index, HWND parentHwnd, unsigned short ty

return FALSE;
#endif
}
}

/*
This stuff is actually not necessary.
Expand Down
53 changes: 35 additions & 18 deletions sadll/sadll.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <shared_mutex>
#ifndef SADLL_H
#define SADLL_H

#ifdef SADLL_EXPORTS
#define SADLL_API __declspec(dllexport)
#else
#define SADLL_API __declspec(dllimport)
#endif
#include <shared_mutex>

//#ifdef SADLL_EXPORTS
//#define SADLL_API __declspec(dllexport)
//#else
//#define SADLL_API __declspec(dllimport)
//#endif

#ifdef USE_ASYNC_TCP
class AsyncClient;
Expand Down Expand Up @@ -83,16 +84,16 @@ class GameService
DWORD __fastcall New_TimeGetTime() const;
void __fastcall New_Sleep(DWORD dwMilliseconds);

void __cdecl New_PlaySound(int a, int b, int c) const;
void __cdecl New_BattleProc() const;
void __cdecl New_BattleCommandReady();
void __cdecl New_TimeProc(int fd) const;
void __cdecl New_lssproto_EN_recv(int fd, int result, int field) const;
void __cdecl New_lssproto_B_recv(int fd, char* command) const;
void __cdecl New_lssproto_WN_send(int fd, int x, int y, int dialogid, int unitid, int select, const char* data);
void __cdecl New_lssproto_TK_send(int fd, int x, int y, const char* message, int color, int area);
void __cdecl New_lssproto_W2_send(int fd, int x, int y, const char* dir) const;
void __cdecl New_CreateDialog(int unk, int type, int button, int unitid, int dialogid, const char* data) const;
void __fastcall New_PlaySound(int a, int b, int c) const;
void __fastcall New_BattleProc() const;
void __fastcall New_BattleCommandReady();
void __fastcall New_TimeProc(int fd) const;
void __fastcall New_lssproto_EN_recv(int fd, int result, int field) const;
void __fastcall New_lssproto_B_recv(int fd, char* command) const;
void __fastcall New_lssproto_WN_send(int fd, int x, int y, int dialogid, int unitid, int select, const char* data);
void __fastcall New_lssproto_TK_send(int fd, int x, int y, const char* message, int color, int area);
void __fastcall New_lssproto_W2_send(int fd, int x, int y, const char* dir) const;
void __fastcall New_CreateDialog(int unk, int type, int button, int unitid, int dialogid, const char* data) const;

public:
//setwindowtexta
Expand Down Expand Up @@ -169,11 +170,27 @@ class GameService

HWND g_consoleHwnd = nullptr;

WNDPROC g_OldWndProc = nullptr;
HMODULE g_hGameModule = nullptr;
HMODULE g_hDllModule = nullptr;
WCHAR g_szGameModulePath[MAX_PATH] = {};
DWORD g_MainThreadId = 0;
HANDLE g_MainThreadHandle = nullptr;

HWND g_ParenthWnd = nullptr;
HWND g_MainHwnd = nullptr;

mutable std::shared_mutex g_statusLock;

int __fastcall getWorldStatue() const { /*lock read*/ std::shared_lock<std::shared_mutex> lock(g_statusLock); return *g_world_status; }
int __fastcall getGameStatue() const { /*lock read*/ std::shared_lock<std::shared_mutex> lock(g_statusLock); return *g_game_status; }
inline int __fastcall getWorldStatue() const { /*lock read*/ std::shared_lock<std::shared_mutex> lock(g_statusLock); return *g_world_status; }
inline int __fastcall getGameStatue() const { /*lock read*/ std::shared_lock<std::shared_mutex> lock(g_statusLock); return *g_game_status; }

template<typename T>
inline T __fastcall CONVERT_GAMEVAR(ULONG_PTR offset) const { return (T)((reinterpret_cast<ULONG_PTR>(g_hGameModule) + offset)); }

inline LRESULT __fastcall callWindowProc(UINT Msg, WPARAM wParam, LPARAM lParam) const { return CallWindowProc(g_OldWndProc, g_MainHwnd, Msg, wParam, lParam); }
inline BOOL __fastcall postMessage(UINT Msg, WPARAM wParam, LPARAM lParam) const { return PostMessage(g_MainHwnd, Msg, wParam, lParam); }
inline BOOL __fastcall sendMessage(UINT Msg, WPARAM wParam, LPARAM lParam) const { return SendMessage(g_MainHwnd, Msg, wParam, lParam); }

private:
BOOL __fastcall sendToServer(const std::string& text);
Expand Down
9 changes: 7 additions & 2 deletions sadll/syncclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class SyncClient
WSACleanup();
}

void setHWnd(HWND hwnd)
{
mainHwnd_ = hwnd;
}

inline BOOL __fastcall Connect(unsigned short type, unsigned short serverPort)
{
ADDRESS_FAMILY family = AF_UNSPEC;
Expand Down Expand Up @@ -243,8 +248,7 @@ class SyncClient
clientSocket_ = INVALID_SOCKET;
}

extern HWND g_MainHwnd;
PostMessageW(g_MainHwnd, kUninitialize, NULL, NULL);
PostMessageW(mainHwnd_, kUninitialize, NULL, NULL);
}

inline std::wstring __fastcall getLastError()
Expand Down Expand Up @@ -278,6 +282,7 @@ class SyncClient
long long index_;
SOCKET clientSocket_ = INVALID_SOCKET;
HWND parendHwnd_ = nullptr;
HWND mainHwnd_ = nullptr;
int(__stdcall* pclosesocket_)(SOCKET s) = nullptr;
//recv
int(__stdcall* precv_)(SOCKET s, char* buf, int len, int flags) = nullptr;
Expand Down

0 comments on commit a1c7eac

Please sign in to comment.