// sends Ctrl-X to the TargetWnd void CCP_MainApp::SendCut() { char ch; //Make sure all the keys are up for(ch = '0'; ch <= '9'; ch++) { keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } for(ch = 'A'; ch <= 'Z'; ch++) { keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); Sleep(50); PumpMessageEx(); CString csToApp = GetProcessName(m_hTargetWnd); CString csString = g_Opt.GetCutString(csToApp); CString csMessage; csMessage = "Sending cut to app " + csToApp + " key stroke " + csString; Log(csMessage); CSendKeys send; //CString cs("^x"); send.SendKeys(csString); }
STDMETHODIMP CShellExt::SendToWindow(HANDLE hProcess, LPCSTR pszText) { //Wait for the process to be ready to accept input WaitForInputIdle(hProcess, INFINITE); CSendKeys sk; sk.SendKeys(pszText); return NOERROR; }
// sends Ctrl-V to the TargetWnd void CCP_MainApp::SendPaste(bool bActivateTarget) { char ch; //Make sure all the keys are up for(ch = '0'; ch <= '9'; ch++) { keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } for(ch = 'A'; ch <= 'Z'; ch++) { keybd_event(ch, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } keybd_event(VK_SHIFT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); Sleep(50); if(bActivateTarget && !ActivateTarget()) { SetStatus(_T("SendPaste FAILED!"), TRUE); return; } PumpMessageEx(); CString csPasteToApp = GetProcessName(m_hTargetWnd); CString csPasteString = g_Opt.GetPasteString(csPasteToApp); CString csMessage; csMessage = "Sending paste to app " + csPasteToApp + " key stroke " + csPasteString; Log(csMessage); CSendKeys send; //CString cs("^v"); //CString cs("%e{DELAY=50}p"); send.SendKeys(csPasteString); }
int waitForWindow(char *name, int secs=10, int doEsc=0) { CMemReaderProxy reader; CSendKeys sk; int i; char buf[128]; sprintf(buf,"Waiting for window '%s' up to %d seconds",name,secs); registerDebug(buf); for (i=0;i<secs*10;i++) { if (strcmp(reader.getOpenWindowName(),name)) { if (doEsc) sk.SendKeys("{ESC}",true); Sleep(100); } else { return 0; } if (toolThreadShouldStop) return 1; } registerDebug("WARN: Waiting for window failed"); return 1; }
DWORD WINAPI toolThreadProc( LPVOID lpParam ) { int i; CMemReaderProxy reader; CPackSenderProxy sender; CTibiaItemProxy itemProxy; CMemConstData memConstData = reader.getMemConstData(); CConfigData *config = (CConfigData *)lpParam; int origExp=getSelfExp(); long prevCount; int shouldBeOpenedContainers=0; if (config->openMain) shouldBeOpenedContainers++; if (config->openCont1) shouldBeOpenedContainers++; if (config->openCont2) shouldBeOpenedContainers++; if (config->openCont3) shouldBeOpenedContainers++; if (config->openCont4) shouldBeOpenedContainers++; if (config->openCont5) shouldBeOpenedContainers++; if (config->openCont6) shouldBeOpenedContainers++; if (config->openCont7) shouldBeOpenedContainers++; if (config->openCont8) shouldBeOpenedContainers++; HANDLE hSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS,true,"TibiaAuto_modLogin_semaphore"); if (hSemaphore==NULL) { hSemaphore = CreateSemaphore(NULL,1,1,"TibiaAuto_modLogin_semaphore"); } while (!toolThreadShouldStop) { Sleep(100); int connectionState = reader.getConnectionState(); if (connectionState!=8) { char buf[128]; sprintf(buf,"Connection Status Changed to:%d",connectionState); registerDebug(buf); registerDebug("No connection: entering wait area"); if (!loginTime) loginTime = time(NULL)+config->loginDelay; while (loginTime>time(NULL) && reader.getConnectionState()!=8){ if (toolThreadShouldStop){ registerDebug("Forced Exit. Module stopped."); goto exitFunction; } Sleep(500); } } connectionState = reader.getConnectionState(); if (connectionState!=8) { registerDebug("No connection: entering relogin mode"); if (origExp>getSelfExp()) { registerDebug("ERROR: exp dropped? was I killed?"); Sleep(1000); while (reader.getConnectionState()!=8){ if (toolThreadShouldStop){ goto exitFunction; } Sleep(1000); } continue; } if (hSemaphore) { registerDebug("Waiting on global login semaphore"); WaitForSingleObject(hSemaphore,INFINITE); registerDebug("Got on semaphore"); } CRect wndRect; CSendKeys sk; HWND hwnd=getTibiaWindow(reader.getProcessId()); reader.setGlobalVariable("walking_control","login"); reader.setGlobalVariable("walking_priority","10"); int wndIconic=::IsIconic(hwnd); int wndMaximized=::IsZoomed(hwnd); int wndTrayed=!::IsWindowVisible(hwnd); if (wndTrayed){ ::ShowWindow(hwnd,SW_SHOW); // wait 5s for the window to show for (i=0;i<50;i++) { if (::IsWindowVisible(hwnd)) break; if (toolThreadShouldStop) break; Sleep(100); } } if (wndIconic || !wndMaximized) { ::ShowWindow(hwnd,SW_SHOWMAXIMIZED); // wait 5s for the window to restore for (i=0;i<50;i++) { if (!::IsIconic(hwnd) && wndIconic || ::IsZoomed(hwnd) && wndMaximized) break; if (toolThreadShouldStop) break; Sleep(100); } if (::IsIconic(hwnd) && wndIconic || !::IsZoomed(hwnd) && wndMaximized) { registerDebug("ERROR: tibia window is still not maximized but it should be"); ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } } if (toolThreadShouldStop) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } POINT prevCursorPos; GetCursorPos(&prevCursorPos); if (ensureForeground(hwnd)) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } if (toolThreadShouldStop) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } ::GetWindowRect(hwnd,&wndRect); // STEP0: make sure no window is open waitForWindow("",2,1); // STEP1: show login window if (ensureForeground(hwnd)) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } SetCursorPos(wndRect.left+125,wndRect.bottom-223); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); if (waitOnConnecting() || waitForWindow("Enter Game",5) || ensureForeground(hwnd)) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } // STEP2: enter user and pass char accNum[33]; char pass[33]; accNum[32]=0; pass[32]=0; if (config->autopass){ CTibiaItemProxy itemProxy; int addr=itemProxy.getValueForConst("addrConnectionState"); reader.getMemRange(addr-0x24,addr-0x24+32,accNum); reader.getMemRange(addr-0x44,addr-0x44+32,pass); } else { strncpy(accNum,config->accountNumber,32); strncpy(pass,config->password,32); } sk.SendKeys(accNum,true); SetCursorPos(wndRect.left+(wndRect.right-wndRect.left)/2+50,wndRect.top+(wndRect.bottom-wndRect.top)/2-15); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); sk.SendKeys(pass,true); SetCursorPos(wndRect.left+(wndRect.right-wndRect.left)/2+50-20,wndRect.top+(wndRect.bottom-wndRect.top)/2-15+90); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); waitOnConnecting(); if (waitForWindow("Select Character",2) && !waitForWindow("Message of the Day",5)) sk.SendKeys("~",true); // STEP3: select char if (waitForWindow("Select Character",5) || ensureForeground(hwnd)) { ReleaseSemaphore(hSemaphore,1,&prevCount); continue; } if (!config->autopass){ sk.SendKeys("a");// goes to beginning of list for (int i=1;i<config->charPos;i++){ sk.SendKeys("{DOWN}"); } } SetCursorPos(wndRect.left+(wndRect.right-wndRect.left)/2+50-20,wndRect.top+(wndRect.bottom-wndRect.top)/2-15-70+217); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); registerDebug("Waiting fo establishing connection up to 15s"); for (i=0;i<150;i++) { if (reader.getConnectionState()==8) break; if (toolThreadShouldStop) break; Sleep(100); } //SetCursorPos(prevCursorPos.x,prevCursorPos.y); //Click on the X of Possibly open skills/battle windows for (i=0;i<4;i++){ SetCursorPos(wndRect.right-15,wndRect.top+387);//inventory maximized mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); SetCursorPos(wndRect.right-15,wndRect.top+382);//inventory minimized mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); Sleep(100); } // finalize if (reader.getConnectionState()==8&&config->openMain) { // open the main backpack flow //Comemnted out checking number of backpacks open. Assume infalliability. //This portion is not needed and has caused problems regarding closing already opened backpacks and never reopening them /* int openContainersCount=0; for (i=0;i<7;i++) { CTibiaContainer *cont = reader.readContainer(i); if (cont->flagOnOff) openContainersCount++; delete cont; } if (openContainersCount!=shouldBeOpenedContainers) { int pos; // no open containers found; proceed. // first: close existing containers for (pos=0;pos<memConstData.m_memMaxContainers;pos++) { CTibiaContainer *cont = reader.readContainer(pos); if (cont->flagOnOff) { sender.closeContainer(pos); CModuleUtil::waitForOpenContainer(pos,0); } delete cont; } */ // now open containers CTibiaItem *item = reader.readItem(itemProxy.getValueForConst("addrBackpack")); sender.openContainerFromContainer(item->objectId,0x03,0,0); delete item; CModuleUtil::waitForOpenContainer(0,1); //Double click on the main bar of the container SetCursorPos(wndRect.right-100,wndRect.top+387); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); SetCursorPos(wndRect.right-100,wndRect.top+282); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); CTibiaContainer *cont = reader.readContainer(0); if (cont->flagOnOff) { int foundContNr=1; int foundContOpenNr=1; for (int pos=0;pos<cont->itemsInside;pos++) { CTibiaItem *itemInside = (CTibiaItem *)cont->items.GetAt(pos); CTibiaTile *itemTile = reader.getTibiaTile(itemInside->objectId); if (itemTile->isContainer) { int doOpen=0; if (foundContNr==1&&config->openCont1) doOpen=1; if (foundContNr==2&&config->openCont2) doOpen=1; if (foundContNr==3&&config->openCont3) doOpen=1; if (foundContNr==4&&config->openCont4) doOpen=1; if (foundContNr==5&&config->openCont5) doOpen=1; if (foundContNr==6&&config->openCont6) doOpen=1; if (foundContNr==7&&config->openCont7) doOpen=1; if (foundContNr==8&&config->openCont8) doOpen=1; if (doOpen) { sender.openContainerFromContainer(itemInside->objectId,0x40,pos,foundContOpenNr); CModuleUtil::waitForOpenContainer(foundContOpenNr,1); //Double click on main bar of open container(each container heign =19 SetCursorPos(wndRect.right-100,wndRect.top+387+19*foundContOpenNr); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); SetCursorPos(wndRect.right-100,wndRect.top+282+19*foundContOpenNr); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); foundContOpenNr++; } foundContNr++; } } } else { // something went wrong sender.closeContainer(0); } delete cont; //} Comemnted out checking number of backpacks open. } Sleep(100);//Wait for clicks to finish //reset window back to original state if (wndIconic)//prioritize wndIconic over !wndMaxmized { ::ShowWindow(hwnd,SW_MINIMIZE); // wait 5s for the window to minimize for (i=0;i<50;i++) { if (::IsIconic(hwnd)) break; if (toolThreadShouldStop) break; Sleep(100); } } else if (!wndMaximized){ ::ShowWindow(hwnd,SW_RESTORE); // wait 5s for the window to minimize for (i=0;i<50;i++) { if (!::IsZoomed(hwnd)) break; if (toolThreadShouldStop) break; Sleep(100); } } if (wndTrayed) { ::ShowWindow(hwnd,SW_HIDE); // wait 5s for the window to minimize for (i=0;i<50;i++) { if (!::IsWindowVisible(hwnd)) break; if (toolThreadShouldStop) break; Sleep(100); } } SetCursorPos(prevCursorPos.x,prevCursorPos.y); ReleaseSemaphore(hSemaphore,1,&prevCount); registerDebug("Relogin procedure completed."); loginTime=0; reader.setGlobalVariable("walking_control",""); reader.setGlobalVariable("walking_priority","0"); } // if (connectionState!=8) else loginTime=0; } exitFunction: if (hSemaphore) { CloseHandle(hSemaphore); } toolThreadShouldStop=0; loginTime=0; return 0; }