#include "StdAfx.h"
#include "Assist.h"

#include <D3D9.h>
#include<direct.h>
//#include <D3dx9tex.h>
//#pragma comment(lib, "D3dx9.lib")

// #pragma comment(lib, "d3d11.lib")
// #pragma comment(lib, "dxgi.lib")



namespace GameAssist
{
	RECT g_rcRoleIdleHP = { 58, 34, 119, 40 };
	RECT g_rcRoleIdleSP = { 58, 46, 119, 52 };
	RECT g_rcSummonerIdleHP = { 168, 34, 202, 40 };
	RECT g_rcSummonerIdleSP = { 168, 46, 202, 52 };
	RECT g_rcRoleFightingHP = { 656, 34, 717, 40 };
	RECT g_rcRoleFightingSP = { 656, 46, 717, 52 };
	RECT g_rcSummonerFightingHP = { 766, 34, 800, 40 };
	RECT g_rcSummonerFightingSP = { 766, 46, 800, 52 };
	std::vector<CtrlInfo> g_vtCtrlInfo;
	std::vector<GameWndInfo> g_vtGameWndInfo;
	void ClearGameInfo(std::vector<GameWndInfo>& vtGWnd)
	{
		for (std::vector<GameWndInfo>::iterator it = vtGWnd.begin(); it != vtGWnd.end(); it++)
		{
			if (it->pAction)
				delete it->pAction;
			it->pAction = NULL;
		}
	}
	
	void SetGameForeground(HWND hWnd)
	{
		if (!IsWindow(hWnd))
			return;

		::ShowWindow(hWnd, SW_SHOWNORMAL);
		::SetForegroundWindow(hWnd); // 窗口前置才能单击成功;
	}

	bool IsGameForeground(HWND hWnd)
	{
		return false;
	}

	BOOL GamePos2Screen(HWND hWnd, POINT& pt, bool bOffset)
	{
		if (!::IsWindow(hWnd)) return FALSE;
		if (bOffset) {
			pt = { pt.x - 2, pt.y - 27 };
		}
		return ClientToScreen(hWnd, &pt);
	}

	BOOL ScreenPos2Game(HWND hWnd, POINT& pt)
	{
		if (!::IsWindow(hWnd)) return FALSE;
		return ScreenToClient(hWnd, &pt);
	}

	void MouseMove(POINT pt, bool bSetCurPos)
	{
		static long fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;
		static long fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;
		double fx = (double)pt.x * (65535.0f / fScreenWidth);
		double fy = (double)pt.y * (65535.0f / fScreenHeight);
#if 1 // mouse_event处理
		// MOUSEEVENTF_ABSOLUTE 使用绝对鼠标位置;
		mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, fx, fy, 0, 0);
#else // setinput处理;
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
		Input.mi.dx = static_cast<LONG>(fx);
		Input.mi.dy = static_cast<LONG>(fy);
		::SendInput(1, &Input, sizeof(INPUT));
#endif
		// Move之后要单击,必须setpos;
		if (bSetCurPos)::SetCursorPos(pt.x, pt.y);
	}

	void MouseClick()
	{
		POINT pt;
		::GetCursorPos(&pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		Sleep(10);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseClick(POINT pt)
	{
		MouseMove(pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		Sleep(10);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseDbClick()
	{
		POINT pt;
		::GetCursorPos(&pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));

		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseDbClick(POINT pt)
	{
		MouseMove(pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));

		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseRClick()
	{
		POINT pt;
		::GetCursorPos(&pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_RIGHTDOWN, pt.x, pt.y, 0, 0);
		Sleep(10);
		mouse_event(MOUSEEVENTF_RIGHTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseRClick(POINT pt)
	{
		MouseMove(pt);
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_RIGHTDOWN, pt.x, pt.y, 0, 0);
		Sleep(10);
		mouse_event(MOUSEEVENTF_RIGHTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void MouseDrag(POINT pt)
	{
#if 0
		// 建议使用这种方式,可以看到鼠标;
		::SetCursorPos(pt.x, pt.y); // 必须将鼠标定位到目标单击点;
		mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
		Sleep(10);
		MouseMove(pt, false);
		Sleep(30);
		mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
#else
		INPUT Input = { 0 };
		Input.type = INPUT_MOUSE;
		Input.mi.dx = pt.x;
		Input.mi.dy = pt.y;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
		::SendInput(1, &Input, sizeof(INPUT));

		Sleep(10);
		MouseMove(pt, false);
		Sleep(30);
		//::ZeroMemory(&Input, sizeof(INPUT));
		Input.type = INPUT_MOUSE;
		Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
		::SendInput(1, &Input, sizeof(INPUT));
#endif
	}

	void SendKey(DWORD key, BOOL bCtrl, BOOL bAtl, BOOL bShift)
	{
		if (bCtrl)
			keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0);
		if (bAtl)
			keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
		if (bShift)
			keybd_event(VK_SHIFT, 0x2A, KEYEVENTF_EXTENDEDKEY | 0, 0);

		Sleep(120);	// 注意：有些电脑能响应Ctrl+V,有些不行，则需要加大CTRL与V之间的时间间隔;
		// key down;
		keybd_event(key, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
		// key up;
		keybd_event(key, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

		Sleep(120);
		if (bCtrl)
			keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		if (bAtl)
			keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
		if (bShift)
			keybd_event(VK_SHIFT, 0x2A, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
	}

	CString GetClipboradString()
	{
		if (OpenClipboard(NULL))
		{
			HANDLE hData = GetClipboardData(CF_TEXT);
			if (hData)
			{
				char* lpBuffer = (char*)GlobalLock(hData);
				//memcpy(pString, lpBuffer, len);
				CString str(lpBuffer);
				GlobalUnlock(hData);
				CloseClipboard();

				return str;
			}
		}

		return CString();
	}

	BOOL SetClipboardString(LPCTSTR lpString)
	{
		if (lpString == NULL || lpString[0] == '\0')
			return FALSE;

		BOOL bRet = FALSE;
		if (!OpenClipboard(NULL))
			return FALSE;

		HGLOBAL hClipString;
		EmptyClipboard();
		hClipString = GlobalAlloc(GMEM_DDESHARE, _tcslen(lpString) + 1);
		if (!hClipString)
			goto end;

		char* lpBuffer = (char*)GlobalLock(hClipString);
		if (!lpBuffer)
			goto end;

		_tcscpy_s(lpBuffer, _tcslen(lpString) + 1, lpString);
		GlobalUnlock(hClipString);
		SetClipboardData(CF_TEXT, hClipString);
		bRet = TRUE;
	end:
		if (hClipString)
			GlobalFree(hClipString);
		
		CloseClipboard();

		return bRet;
	}

	// 返回第一个找到的进程pid
	DWORD GetProcessId(LPCTSTR lpProcessName)
	{
		DWORD dwProcessId = 0;
		PROCESSENTRY32	pe32 = { 0 };

		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (hProcessSnap == NULL) return 0;

		// 遍历进程;
		pe32.dwSize = sizeof(PROCESSENTRY32);
		for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
			if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
				dwProcessId = pe32.th32ProcessID;
				break;
			}
		}

		CloseHandle(hProcessSnap);

		return dwProcessId;
	}

	void GetAllProcessId(std::vector<DWORD>& vtPid, LPCTSTR lpProcessName)
	{
		vtPid.clear();
		PROCESSENTRY32	pe32 = { 0 };

		HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (hProcessSnap == NULL) return;

		// 遍历进程;
		pe32.dwSize = sizeof(PROCESSENTRY32);
		for (BOOL bRet = Process32First(hProcessSnap, &pe32); bRet; bRet = Process32Next(hProcessSnap, &pe32)) {
			if (_tcscmp(lpProcessName, pe32.szExeFile) == 0) {
				vtPid.push_back(pe32.th32ProcessID);
			}
		}

		CloseHandle(hProcessSnap);
	}

	HWND GetProcessMainWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWndName)
	{
		DWORD dwCurPorcessId = 0;
		HWND hTagetProcessWnd = NULL;
		TCHAR szWndName[MAX_PATH] = { 0 };
		TCHAR szClassName[MAX_PATH] = { 0 };

		// 取得第一个窗口句柄;
		for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
			// 重置为0;
			dwCurPorcessId = 0;
			// 通过窗口句柄反查进程pid;
			DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
			if (dwThreadId != 0) {
				// 判断当前进程id是否和目标进程id相同;
				if (dwCurPorcessId == dwTagetProcessId) {
					if (lpTagetWndName == NULL) {
						hTagetProcessWnd = hCurWnd;
						break;
					}
					else {
						// 获取窗口名称;
						::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
						// 获取窗口类名;
						::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
#ifdef _DEBUG
						TCHAR szLogMsg[MAX_PATH] = { 0 };
						_stprintf_s(szLogMsg, _T("类名：%s， 窗口名：%s，窗口地址：%p \n"), szClassName, szWndName, hCurWnd);
						OutputDebugString(szLogMsg);
#endif
						if (_tcsstr(szWndName, lpTagetWndName) != NULL) {
							hTagetProcessWnd = hCurWnd;
							break;
						}
					}
				}
			}
		}

		// 当前窗口有可能不是进程父窗口;
		HWND hParentWnd = hTagetProcessWnd;
		while (hParentWnd) {
			hParentWnd = ::GetParent(hTagetProcessWnd);
			if (hParentWnd == NULL)
				break;
			hTagetProcessWnd = hParentWnd;
		}

		return hTagetProcessWnd;
	}

	HWND GetProcessMainWnd(LPCTSTR lpProcessName, LPCTSTR lpTagetWndName)
	{
		HWND hTagetWnd = NULL;
		DWORD dwProcessId = 0;
		if ((dwProcessId = GetProcessId(lpProcessName)) != 0) {
			hTagetWnd = GetProcessMainWnd(dwProcessId, lpTagetWndName);
		}

		return hTagetWnd;
	}

	void GetAllProcessMainWnd(const std::vector<DWORD>& vtPid, std::vector<GameWndInfo>& vtGWnd, LPCTSTR lpTagetWndName)
	{
		DWORD dwCurPorcessId = 0;
		HWND hTagetProcessWnd = NULL;
		TCHAR szWndName[MAX_PATH] = { 0 };
		TCHAR szClassName[MAX_PATH] = { 0 };
		// 游戏窗口信息;
		TCHAR szGameName[MAX_PATH] = { 0 };
		TCHAR szGameRelease[MAX_PATH] = { 0 };
		TCHAR szGameVersion[MAX_PATH] = { 0 };
		TCHAR szGameZoon[MAX_PATH] = { 0 };
		TCHAR szGameServer[MAX_PATH] = { 0 };
		TCHAR szGameRoleName[MAX_PATH] = { 0 };
		// 清空队列;
		ClearGameInfo(vtGWnd);
		vtGWnd.clear();
		for (std::vector<DWORD>::const_iterator it = vtPid.begin(); it != vtPid.end(); it++)
		{
			// 取得第一个窗口句柄;
			for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
				// 重置为0;
				dwCurPorcessId = 0;
				// 通过窗口句柄反查进程pid;
				DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
				if (dwThreadId != 0) {
					// 判断当前进程id是否和目标进程id相同;
					if (dwCurPorcessId == *it) {
						if (lpTagetWndName == NULL) {
							hTagetProcessWnd = hCurWnd;
							break;
						}
						else {
							// 获取窗口名称;
							::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
							// 获取窗口类名;
							::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
#ifdef _DEBUG
							TCHAR szLogMsg[MAX_PATH] = { 0 };
							_stprintf_s(szLogMsg, _T("类名：%s， 窗口名：%s，窗口地址：%p \n"), szClassName, szWndName, hCurWnd);
							OutputDebugString(szLogMsg);
#endif
							if (_tcsstr(szWndName, lpTagetWndName) != NULL) {
								_stscanf_s(szWndName, _T("%[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %[^ ]"), szGameName, MAX_PATH, szGameRelease, MAX_PATH, szGameVersion, MAX_PATH, szGameZoon, MAX_PATH, szGameServer, MAX_PATH, szGameRoleName, MAX_PATH);
								hTagetProcessWnd = hCurWnd;
								break;
							}
						}
					}
				}
			}

			// 当前窗口有可能不是进程父窗口;
			HWND hParentWnd = hTagetProcessWnd;
			while (hParentWnd) {
				hParentWnd = ::GetParent(hTagetProcessWnd);
				if (hParentWnd == NULL)
					break;
				hTagetProcessWnd = hParentWnd;
			}

			if (hTagetProcessWnd)
			{
				GameWndInfo gameInfo;
				gameInfo.hWnd = hTagetProcessWnd;
				gameInfo.lProcId = *it;
				gameInfo.strGameZoon = szGameZoon;
				gameInfo.strGameServer = szGameServer;
				gameInfo.strGameRoleName = szGameRoleName;
				gameInfo.pAction = NULL;
				vtGWnd.push_back(gameInfo);
				ZeroMemory(szGameZoon, MAX_PATH);
				ZeroMemory(szGameServer, MAX_PATH);
				ZeroMemory(szGameRoleName, MAX_PATH);
			}
		}
	}

	BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)
	{
		DWORD dwPid = 0;
		GetWindowThreadProcessId(hWnd, &dwPid); // 获得找到窗口所属的进程   
		if (dwPid == lParam) // 判断是否是目标进程的窗口   
		{
			TCHAR szName[MAX_PATH] = { 0 };
			TCHAR szClass[MAX_PATH] = { 0 };
			TCHAR szLogMsg[MAX_PATH] = { 0 };
			::GetWindowText(hWnd, szName, sizeof(szName) / sizeof(TCHAR));
			::GetClassName(hWnd, szClass, sizeof(szClass) / sizeof(TCHAR)); // 窗口类
			DWORD dwId = ::GetDlgCtrlID(hWnd);
			//TRACE3(_T("%s %s %s"), szClass, szName, "\n");
			_stprintf_s(szLogMsg, _T("类名：%s， 窗口名：%s，ID：%d，窗口地址：%p \n"), szClass, szName, dwId, hWnd);
			OutputDebugString(szLogMsg);

			CtrlInfo ctrl;
			ctrl.dwId = dwId;
			ctrl.hwnd = hWnd;
			ctrl.strWinText.append(szName, _tcslen(szName));
			ctrl.strClassName = szClass;
			g_vtCtrlInfo.push_back(ctrl);

			_tprintf_s(_T("0x%08X    "), hWnd); // 输出窗口信息   
			TCHAR buf[MAX_PATH];
			SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)buf);
			_tprintf_s(_T("%s/n"), buf);
			EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam);    // 递归查找子窗口   
		}
		return  TRUE;
	}
};

namespace GameGlobal 
{
	int frameWidth = 2;//6
	int titleBarHeight = 27;//29

	TCHAR g_szCurModuleFileName[MAX_PATH];
	TCHAR g_szCurModuleDir[MAX_PATH];
	TCHAR g_szCurModulePath[MAX_PATH];
	TCHAR g_szFna[_MAX_FNAME];
	TCHAR g_szExt[_MAX_EXT];
	TCHAR g_szAssistConfig[MAX_PATH];
	std::string g_strAppdir;
	std::string g_strGameDir;
	TCHAR g_szGameApp[MAX_PATH];
	TCHAR g_szTempDir[MAX_PATH] = {0};
	int nGameWndType;
	int nAttackCount = 0;

	void Init()
	{
		TCHAR szDrive[_MAX_DRIVE] = { 0 };
		TCHAR szDir[_MAX_DIR] = { 0 };
		TCHAR szFna[_MAX_FNAME] = { 0 };
		TCHAR szExt[_MAX_EXT] = { 0 };
		DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));

		_tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
		_tcscat_s(g_szCurModuleDir, MAX_PATH, szDrive);
		_tcscat_s(g_szCurModuleDir, MAX_PATH, szDir);

		_stprintf_s(g_szAssistConfig, _T("%s%s"), g_szCurModuleDir, _T("Assist.ini"));

		TCHAR szValue[MAX_PATH] = { 0 };
		GetPrivateProfileString(_T("Assist"), _T("GamePath"), _T(""), szValue, MAX_PATH, g_szAssistConfig);
		g_strGameDir = szValue;
		GetPrivateProfileString(_T("Assist"), _T("GameApp"), _T(""), g_szGameApp, MAX_PATH, g_szAssistConfig);

		g_strAppdir = g_szCurModuleDir;
		_stprintf_s(g_szTempDir, _T("%s%s\\"), g_szCurModuleDir, _T("temp"));
		if (!PathFileExists(g_szTempDir))
			_mkdir(g_szTempDir);

		// 读取窗口类型;
		_stprintf_s(szValue, _T("%s%s"), g_szGameApp, _T("save\\config.ini"));
		int ScreenWidth = GetPrivateProfileInt(_T("SysCfg"), _T("ScreenWidth"), 640, szValue);
		if (ScreenWidth == 640)
			nGameWndType = GW_SMALL;
		else if (ScreenWidth == 800)
			nGameWndType = GW_LARGE;
		else
			nGameWndType = GW_FULL;

		// Alt+A次数;
		nAttackCount = GetPrivateProfileInt(_T("SysCfg"), _T("AttackCount"), 120, szValue);
	}

	std::string BuildImgPath(HWND hWnd, LPCTSTR lpName)
	{
		TCHAR szName[MAX_PATH] = { 0 };
		if ( lpName == NULL || lpName[0] == '\0')
			_stprintf_s(szName, _T("%s%p.bmp"), GameGlobal::g_szTempDir, hWnd);
		else
		_stprintf_s(szName, _T("%s%p-%s.bmp"), GameGlobal::g_szTempDir, hWnd, lpName);

		return std::string(szName);
	}

	VOID DebugLog(CHAR* pszStr, ...)
	{
		char szData[MAX_PATH] = { 0 };
		_stprintf_s(szData, _T("%s %s "), _T("[GameAssist] "), CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")).GetString());
		int len = strlen(szData);
		va_list args;
		va_start(args, pszStr);
		_vsnprintf_s(szData + len, MAX_PATH - len, MAX_PATH - len, pszStr, args);
		va_end(args);
		strcat_s(szData, "\n");
		OutputDebugStringA(szData);
	}
};
