Skip to content

Commit

Permalink
update msbuild_win7.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Feb 25, 2024
1 parent 36f1ae3 commit 448df31
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/msbuild_win7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ jobs:
node-version: '14.16'
architecture: 'x86'

- name: Set up Python 2.7
uses: actions/setup-python@v2
with:
python-version: '2.7'

- name: Install node-gyp
working-directory: ${{env.GITHUB_WORKSPACE}}
run: npm install -g node-gyp
Expand Down
60 changes: 36 additions & 24 deletions CGAssistant/psworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ CProcessWorker::CProcessWorker(QObject *parent) : QObject(parent)
qRegisterMetaType<CProcessItemList>("CProcessItemList");

QTimer *timer = new QTimer(this);

connect(timer, SIGNAL(timeout()), this, SLOT(OnQueueQueryProcess()));
timer->start(500);
connect(timer, SIGNAL(timeout()), this, SLOT(OnCheckFreezeProcess()));
timer->start(1000);

QTimer *timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(OnCheckFreezeProcess()));
timer2->start(1000);

m_AttachMutex = NULL;
m_AttachHwnd = 0;
Expand All @@ -42,11 +45,12 @@ quint32 CProcessWorker::GetAttachedHwnd()
return (quint32)m_AttachHwnd;
}

bool CProcessWorker::IsProcessAttached(quint32 ProcessId)
bool CProcessWorker::IsProcessAttached(quint32 ProcessId, quint32 ThreadId)
{
bool bAttached = false;
WCHAR szMutex[32];
wsprintfW(szMutex, L"CGAAttachMutex_%d", ProcessId);

WCHAR szMutex[64];
wsprintfW(szMutex, L"CGAAttachMutex_%d_%d", ProcessId, ThreadId);
HANDLE hAttachMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, szMutex);
if(hAttachMutex)
{
Expand Down Expand Up @@ -97,20 +101,20 @@ bool CProcessWorker::InjectByMsgHook(quint32 ThreadId, quint32 hWnd, QString &dl
return (errorCode == 0) ? true : false;
}

bool CProcessWorker::ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd)
bool CProcessWorker::ReadSharedData(quint32 ProcessId, quint32 ThreadId, int &port, quint32 &hWnd)
{
bool bSuccess = false;

WCHAR szLockName[32];
wsprintfW(szLockName, L"CGASharedDataLock_%d", ProcessId);
wsprintfW(szLockName, L"CGASharedDataLock_%d_%d", ProcessId, ThreadId);

HANDLE hDataLock = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, szLockName);
if(hDataLock)
{
if(WAIT_OBJECT_0 == WaitForSingleObject(hDataLock, 500))
{
WCHAR szMappingName[32];
wsprintfW(szMappingName, L"CGASharedData_%d", ProcessId);
wsprintfW(szMappingName, L"CGASharedData_%d_%d", ProcessId, ThreadId);
HANDLE hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY, 0, sizeof(CGA::CGAShare_t), szMappingName);
if (hFileMapping)
{
Expand All @@ -122,6 +126,7 @@ bool CProcessWorker::ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd)
port = data->Port;
hWnd = (quint32)data->hWnd;
bSuccess = true;

UnmapViewOfFile(pViewOfFile);
}
CloseHandle(hFileMapping);
Expand All @@ -140,7 +145,7 @@ void CProcessWorker::OnRetryAttachProcess()

int port = 0;
quint32 hwnd = 0;
if(!ReadSharedData(timer->m_ProcessId, port, hwnd))
if(!ReadSharedData(timer->m_ProcessId, timer->m_ThreadId, port, hwnd))
{
if(timer->m_retry > 10)
{
Expand Down Expand Up @@ -186,6 +191,7 @@ void CProcessWorker::ConnectToServer(quint32 ProcessId, quint32 ThreadId, int po
void CProcessWorker::Disconnect()
{
g_CGAInterface->Disconnect();

if(m_AttachMutex != NULL)
{
CloseHandle(m_AttachMutex);
Expand All @@ -200,22 +206,22 @@ void CProcessWorker::OnAutoAttachProcess(quint32 ProcessId, quint32 ThreadId)
m_AutoAttachPID = ProcessId;
m_AutoAttachTID = ThreadId;

qDebug("OnAutoAttachProcess %d %d", ProcessId, ThreadId);
// qDebug("OnAutoAttachProcess %d %d", ProcessId, ThreadId);
}

void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, quint32 hWnd, QString dllPath)
{
qDebug("OnQueueAttachProcess %d %d", ProcessId, ThreadId);
//qDebug("OnQueueAttachProcess %d %d", ProcessId, ThreadId);

int port = 0;
quint32 hwnd = 0;
if(!ReadSharedData(ProcessId, port, hwnd))
if(!ReadSharedData(ProcessId, ThreadId, port, hwnd))
{
int errorCode = -1;
QString errorString;
if(!InjectByMsgHook(ThreadId, hWnd, dllPath, errorCode, errorString))
{
qDebug("OnQueueAttachProcess %d %d, failed to InjectByMsgHook", ProcessId, ThreadId);
//qDebug("OnQueueAttachProcess %d %d, failed to InjectByMsgHook", ProcessId, ThreadId);
NotifyAttachProcessFailed(ProcessId, ThreadId, errorCode, errorString);
return;
}
Expand All @@ -225,7 +231,7 @@ void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, q
}
else
{
qDebug("OnQueueAttachProcess %d %d, failed to ReadSharedData", ProcessId, ThreadId);
//qDebug("OnQueueAttachProcess %d %d, failed to ReadSharedData", ProcessId, ThreadId);

//Already attached to game
Disconnect();
Expand All @@ -235,16 +241,18 @@ void CProcessWorker::OnQueueAttachProcess(quint32 ProcessId, quint32 ThreadId, q

bool CProcessWorker::CreateAttachMutex(quint32 ProcessId, quint32 ThreadId)
{
WCHAR szMutex[32];
wsprintfW(szMutex, L"CGAAttachMutex_%d", ProcessId);
auto AttachMutex = CreateMutexW(NULL, TRUE, szMutex);
if(AttachMutex == NULL)
WCHAR szMutex[64];
wsprintfW(szMutex, L"CGAAttachMutex_%d_%d", ProcessId, ThreadId);

auto hAttachMutex = CreateMutexW(NULL, TRUE, szMutex);

if(hAttachMutex == NULL)
{
NotifyAttachProcessFailed(ProcessId, ThreadId, -4, tr("Game already attached by another instance of CGAssistant."));
return false;
}

m_AttachMutex = AttachMutex;
m_AttachMutex = hAttachMutex;
return true;
}

Expand All @@ -255,22 +263,24 @@ void CProcessWorker::OnQueueQueryProcess()
const wchar_t szFindGameClass[] = { 39764, 21147, 23453, 36125, 0 };

HWND hWnd = NULL;
DWORD pid, tid;
DWORD pid = 0, tid = 0;
WCHAR szText[256];
DWORD dwCurrentPID = GetCurrentProcessId();

while ((hWnd = FindWindowExW(NULL, hWnd, szFindGameClass, NULL)) != NULL)
{
if((tid = GetWindowThreadProcessId(hWnd, (LPDWORD)&pid)) != 0 && pid != GetCurrentProcessId())
if((tid = GetWindowThreadProcessId(hWnd, (LPDWORD)&pid)) != 0 && pid != dwCurrentPID)
{
if(GetWindowTextW(hWnd, szText, 256))
{
bool attached = IsProcessAttached(pid);
bool attached = IsProcessAttached(pid, tid);

CProcessItemPtr item(new CProcessItem((quint32)pid, (quint32)tid, (quint32)hWnd, szText, attached));
list.append(item);

if(!attached && m_AutoAttachPID == pid && m_AutoAttachTID == tid){

qDebug("OnQueueAttachProcess %d %d", (quint32)pid, (quint32)tid);
//qDebug("OnQueueAttachProcess %d %d", (quint32)pid, (quint32)tid);

OnQueueAttachProcess( (quint32)pid, (quint32)tid, (quint32)hWnd, QString("cgahook.dll") );

Expand All @@ -286,7 +296,9 @@ void CProcessWorker::OnQueueQueryProcess()
if(ReadSharedData(pid, port, hwnd) && g_CGAInterface->IsConnected() && g_CGAInterface->GetPort() == port)
{
if(hwnd != GetAttachedHwnd()){
qDebug("Should not connect to port %d, disconnect", port);

//qDebug("Should not connect to port %d, disconnect", port);

g_CGAInterface->Disconnect();
}
}
Expand Down
4 changes: 2 additions & 2 deletions CGAssistant/psworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private slots:
void NotifyQueryProcess(CProcessItemList list);
//void NotifyServerShutdown(int port);
private:
bool IsProcessAttached(quint32 ProcessId);
bool IsProcessAttached(quint32 ProcessId, quint32 ThreadId);
bool InjectByMsgHook(quint32 ThreadId, quint32 hWnd, QString &szDllPath, int &errorCode, QString &errorString);
bool ReadSharedData(quint32 ProcessId, int &port, quint32 &hWnd);
bool ReadSharedData(quint32 ProcessId, quint32 ThreadId, int &port, quint32 &hWnd);
void ConnectToServer(quint32 ProcessId, quint32 ThreadId, int port, quint32 hWnd);
bool CreateAttachMutex(quint32 ProcessId, quint32 ThreadId);
void Disconnect();
Expand Down
14 changes: 8 additions & 6 deletions cgahook/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern CGA::CGAService g_CGAService;

int g_MainPort = 0;
HWND g_MainHwnd = NULL;
ULONG g_MainProcessId = 0;
ULONG g_MainThreadId = 0;
WNDPROC g_OldProc = NULL;
HANDLE g_hQuitEvent = NULL;
Expand Down Expand Up @@ -351,11 +352,12 @@ LRESULT CALLBACK NewWndProcPOLCN(HWND hWnd, UINT message, WPARAM wParam, LPARAM
return CallWindowProcA(g_OldProc, hWnd, message, wParam, lParam);
}

void InitializeHooks(int ThreadId, HWND hWnd, CGA::game_type type)
void InitializeHooks(ULONG ProcessId, ULONG ThreadId, HWND hWnd, CGA::game_type type)
{
if (g_MainThreadId && g_MainThreadId != ThreadId)
return;

g_MainProcessId = ProcessId;
g_MainThreadId = ThreadId;
g_MainHwnd = hWnd;

Expand Down Expand Up @@ -387,19 +389,19 @@ extern "C"
LPCWSTR pModuleName = ExtractFileName(szModulePath);
if (!_wcsicmp(pModuleName, L"cg_se_3000.exe") && !strcmp(szClass, "침제괜굔"))
{
//InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_3000);
//InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_3000);
}
else if (!_wcsicmp(pModuleName, L"cg_item_6000.exe") && !strcmp(szClass, "침제괜굔"))
{
InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_item_6000);
InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_item_6000);
}
else if (!_wcsicmp(pModuleName, L"cg_se_6000.exe") && !strcmp(szClass, "침제괜굔"))
{
//InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_6000);
//InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::cg_se_6000);
}
else if (!_wcsicmp(pModuleName, L"POLCN_Launcher.exe"))
{
InitializeHooks(GetCurrentThreadId(), pMsg->hwnd, CGA::polcn);
InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), pMsg->hwnd, CGA::polcn);
}
}
}
Expand Down Expand Up @@ -467,7 +469,7 @@ int WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
return TRUE;
}

InitializeHooks(GetCurrentThreadId(), NULL, CGA::polcn);
InitializeHooks(GetCurrentProcessId(), GetCurrentThreadId(), NULL, CGA::polcn);
}
else if (!_wcsicmp(pModuleName, L"cg_se_6000.exe"))//Patch se_6000 windowed bug
{
Expand Down
9 changes: 4 additions & 5 deletions cgahook/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern CGA::CGAService g_CGAService;

extern int g_MainPort;
extern HWND g_MainHwnd;
extern ULONG g_MainProcessId;
extern ULONG g_MainThreadId;
extern HANDLE g_hQuitEvent;
extern HANDLE g_hPortMutex;
Expand Down Expand Up @@ -125,14 +126,12 @@ BOOL CGA_CreatePortMutex(int port)

void CGA_CreateSharedData(int port)
{
ULONG ProcessId = GetCurrentProcessId();

WCHAR szLockName[64];
wsprintfW(szLockName, L"CGASharedDataLock_%d", ProcessId);
wsprintfW(szLockName, L"CGASharedDataLock_%d_%d", g_MainProcessId, g_MainThreadId);
g_hDataLock = CreateMutexW(NULL, TRUE, szLockName);

WCHAR szMappingName[64];
wsprintfW(szMappingName, L"CGASharedData_%d", ProcessId);
wsprintfW(szMappingName, L"CGASharedData_%d_%d", g_MainProcessId, g_MainThreadId);
g_hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(CGA::CGAShare_t), szMappingName);
if (g_hFileMapping && GetLastError() != ERROR_ALREADY_EXISTS)
{
Expand All @@ -141,7 +140,7 @@ void CGA_CreateSharedData(int port)
{
CGA::CGAShare_t *data = (CGA::CGAShare_t *)pViewOfFile;

data->ProcessId = ProcessId;
data->ProcessId = g_MainProcessId;
data->ThreadId = g_MainThreadId;
data->hWnd = (int)g_MainHwnd;
data->Port = port;
Expand Down

0 comments on commit 448df31

Please sign in to comment.