The principle of the preparation of the game plug-in (2)

xiaoxiao2021-03-06  15

Fourth, action simulation technology

We introduced the previous introduction, almost all games have a large number of cumbersome attacks to increase players, and those who are incomplete labyrinth.

These seem to have become synonymous with role games. Now, plug-in can help players from these cumbersome and boring work, focusing on tour

The progress of the play plot. The plug-in program is required to implement functional functions such as automatic role position and automatic attacks, need to use keyboard simulation technology and mouse simulation technology.

Below we will focus on these techniques and prepare a simple instance to help readers understand the implementation of action simulation technology.

1. Mouse simulation technology uses a mouse in almost all games to change the position and direction of the role. Players can use only a small mouse to enable the role to swim.

So, how do we implement the role without players, can also walk automatically. In fact, this is not difficult, just a few Windows API functions

You can get it, let us first understand these API functions.

(1) Simulate the mouse action API function mouse_event, which can implement an analog mouse to press and release the action.

Void mouse_event (DWORD DWFLAGS, / / ​​mouse action ID. DWORD DX, / / ​​mouse horizontal direction. DWORD DY, / / ​​Mouse vertical position. DWORD DWDATA, // Mouse wheel rotation Number. DWORD DWEXTRAINFO // An associated mouse Auxiliary information.);

Among them, DWFlags expressed a variety of mouse action and click activities, and its common value is as follows:

MouseEventf_move represents analog mouse movement event.

MouseEventf_leftdown indicates that the analog presses the left mouse button.

MouseEventf_leftUp indicates that the simulation releases the left mouse button.

MouseEventf_rightdown indicates that the analog is right-click.

MouseEventf_rightUp indicates that the analog releases the right mouse button.

MouseEventf_middledown indicates that the analog Press the mouse button.

MouseEventf_middleUp indicates that the analog release mouse button.

(2) Set and get the API function of the current mouse position. Get the current mouse location using the getCursorpos () function, set the current mouse location

SetCursorpos () function.

Bool getCursorpos (LPPOINT LPPOINT // Returns the current location of the mouse.); Bool SetCursorpos (horizontal direction of the INT X, / / ​​mouse. The vertical position of the int y // mouse.);

Usually the walking role is moving through the mouse to the destination, then press the button to press the button. Below we use the API described above

The function is to simulate the role walking process.

Cpoint OldPoint, NewPoint; getCursorpos (& OLDPOINT); // Save the current mouse position. NewPoint.x = OldPoint.x 40; newpoint.y = OldPoint.y 10; setCursorpos (NewPoint.x, NewPoint.y); // Set the destination location. Mouse_Event (MouseEventf_RightDown, 0, 0, 0); // The analog is right-click on the mouse button. Mouse_event (MouseEventf_Rightup, 0, 0, 0); // Simulate the right mouse button.

2. Keyboard simulation technology

In many games, not only the mouse is provided, but also provides a keyboard operation, and shortcuts can be used when attacking the attack object.

. In order to make these attacks automatically, the plug-in program needs to use keyboard simulation technology. Like mouse simulation technology, Windows API also provides

A series of API functions to complete analog to keyboard actions. Analog Keyboard Action API Function Keydb_Event, which simulates an action on a keyboard on a keyboard to perform or release actions.

Void keybd_event (Byte BVK, // Virtual key value. Byte BSCAN, / / ​​hardware scanning code. DWORD DWFLAGS, // Auxiliary information associated with keyboard action.);

Where BVK represents the virtual key value, it is a macro of a BYTE type value, which ranges from 1-254. For the virtual key value table, please use it on MSDN

Keyword "Virtual-Key Codes" Find relevant information. BSCAN means that when a key is pressed and released on the keyboard, the scan code generated by the keyboard system hardware

We can convert between the MapVirtualKey () function between the virtual key value and the scan code. DWFlags represents a variety of keyboard actions, it has two kinds

Value: KeyEventf_extendedKey and Keyeventf_Keyup.

Below we use a code to implement the Shift R shortcut to attack the attack object in the game.

KeyBD_Event (VK_Control, MapVirtualKey (vk_control, 0), 0, 0); // Press the CTRL button. KeyBD_Event (0x52, MapVirtualKey (0x52, 0), 0, 0); // Under the R key. KeyBD_EVENT (0x52, MapVirtualKey (0x52, 0), KeyEventf_keyup, 0); // release the R key. KeyBD_EVENT (VK_Control, MapVirtualKey (vk_control, 0), keyeventf_keyup, 0); // release the CTRL button.

3. Activate plug-in

The mouse and keyboard simulation technology described above realize the simulation of the action part of the game role, but we need to hang up to work on the game, but also needs

It is linked to the scene window of the game or uses a activation key, just like the activation key of the button. We can use the getWindow function to enumerate

Lift the window, you can also use the FindWindow function to find a specific window. There is also a FindWindowEx function to find the child window of the window, when

When you switch the scene, we can use FindWindowEx to determine some of the characteristics of some current windows, and judge whether it is still in this scenario.

For example, you can get some things, such as when you find a button, you have explained the way the game scene has been switched. Make

Relation with the activation key, you need to use the Hook technology to develop a full keyboard hook, which does not specifically introduce the development process of the global hook, in the back

In the case, we will use the global hook, and will learn about the global hook.

4. Instance implementation

Through the above learning, we have basically have the ability to write actions plug-in. Below we will create a brush program plug-in, it

Now automatically move the position of the screen and write the "R" word in the red "R". Based on this example, join the corresponding game action rules, you can implement

A complete game plug-in. Here, the author does not want to use a game as an example to develop an external hanging (because there is no mandate of the game business!), Such as readers

If you are fun, you can try a game, it is best to do test technology.

First, we need to write a global hook, use it to activate the plug-in, the activation button is F10. Creating a global hook steps are as follows:

(1). Select MFC AppWizard (DLL) to create project ActiveKey and select the MFC Extension DLL type.

(2). Insert a new file ActiveKey.h, enter the following code: #ifndef _keydll_h #define _keydll_h

Class AFX_EXT_CLASS CKEYHOOK: PUBLIC COBJECT {public: ckeyhook (); ~ ckeyhook (); hHOOK START (); // Install hook BOOL stop (); // Uninstall hook}; #ENDIF

(3). Add the statement "#include activityKey.h" in the ActiveKey.cpp file.

(4) Add to the shared data segment in the ActiveKey.cpp file, the code is as follows:

// Shared Data Section #pragma Data_Seg ("Sharedata") hHOOK GLHHOK = NULL; // Hook handle. Hinstance glhinstance = null; // DLL instance handle. #Pragma data_seg ()

(5). Set the shared data segment attribute in the ActiveKey.DEF file, the code is as follows:

SetCTION SharedData Read Write Shared

(6). Join the CKEYHOOK class in the ActiveKey.cpp file and hook function code:

// Keyboard hook processing function. Extern "C" LResult WinAPI KeyboardProc (int Ncode, WPARAM WPARAM, LPARAMLPARAM) {if (Ncode> = 0) {if (wparam == 0x79) // When the F10 button is pressed, the plug-in is activated. {// plug-in implementation code. CPoint newPoint, oldPoint; GetCursorPos (& oldPoint); newPoint.x = oldPoint.x 40; newPoint.y = oldPoint.y 10; SetCursorPos (newPoint.x, newPoint.y); mouse_event (MOUSEEVENTF_LEFTDOWN, 0,0,0 , 0); // simulate the left mouse button. Mouse_event (MouseEventf_leftup, 0,0,0,0); // Simulate the left mouse button. KeyBD_Event (vk_shift, mappvirtualkey (vk_shift, 0), 0, 0); // Press the Shift key. KeyBD_EVENT (0x52, MapVirtualKey (0x52, 0), 0, 0); // Press the R key. KeyBD_EVENT (0x52, MapVirtualKey (0x52, 0), KeyEventf_keyup, 0); // release the R key. KeyBD_EVENT (VK_SHIFT, MAPVIRTUALKEY (VK_SHIFT, 0), KEYEVENTF_KEYUP, 0); // Release the Shift key. SetCursorpos (Oldpoint.x, OldPoint.y);}} Return CallNexthookex (Glhhook, Ncode, WPARAM, LPARAM);

CKEYHOOK :: CKEYHOK () {} ckeyhook :: ~ ckeyhook () {if (glhhook) stop ();} // Install the global hook. HHOOK CKEYHOOK :: Start () {glhhook = setwindowshookex (wh_keyboard, keyboardproc, glhinstance, 0); // Set keyboard hook. Return GLHHOK;} // Uninstall global hook. Bool ckeyhook :: stop; {bool bresult = true; if (glhhook) BRESULT = UNHOOKWINDOWSHOKEX (GLHHOOK); // Uninstall the keyboard hook. Return BRESULT;} (7). Modify the DLLMAIN function, the code is as follows:

Extern "C" int apientry dllmain (Hinstance Hinstance, DWORD DWREASON, LPVOID LPRESERVED) {// If you use the lpreserved parameter, remove the unreferenced_parameter (LPRESERVED);

if (dwReason == DLL_PROCESS_ATTACH) {TRACE0 ( "NOtePadHOOK.DLL Initializing / n!"); // extension DLL initialization only once if (AfxInitExtensionModule (ActiveKeyDLL, hInstance)!) return 0; new CDynLinkLibrary (ActiveKeyDLL); // put DLL joins the dynamic MFC class library GLHINSTANCE = Hinstance; // Insert Save DLL Edessure Handle} Else if (DWREASON == DLL_PROCESS_DETACH) {trace0 ("NOTEPADHOK.DLL TERMINATING! / N"); // Terminate this Link Library before calling it AfxterMextensionModule;} return 1;}

(8). Compile the project ActiveKey to generate ActiveKey.dll and ActiveKey.lib.

Next, we also need to create a housing to install the global hook in a Windows system, this shell programming step is as follows:

(1). Create a dialog mode application, the project is called Simulate.

(2). Add a button in the main dialog and create a CLICK event using ClassWizard.

(3). Copy the ActiveKey.dll and ActiveKey.lib and ActiveKey.lib in the ActiveKey project debug directory to the SIMULATE project directory.

(4). From the "Project" menu, select "Set", pop up the Project Setting dialog, select the Link tab, enter in Object / Library Module

ActiveKey.LIB.

转载请注明原文地址:https://www.9cbs.com/read-45869.html

New Post(0)