C # core code of game plug-ins

Source: Internet
Author: User
Tags xsl

Recently, a friend introduced me to start playing classified online games.
After upgrading, I suddenly felt that the keyboard was too expensive, so I wrote Program In place of my operations, I want to beat monsters and upgrade myself.
I have used this item for many upgrades. Now I am posting the source code and sharing it with you. Thank you for your criticism and correction.
The program is divided into two parts: a class library and an application
The general idea is to find the main window handle of the game process, and then send the game button message (analog button ).

Xdf. gameplugincommon Class Library Project

// API. CS file, which defines some common API functions and constants

Using system;
Using system. IO;
Using system. Threading;
Using system. diagnostics;
Using system. runtime. interopservices;

Namespace xdf. gameplugincommon
{
/// <Summary>
/// API summary.
/// </Summary>
Public sealed class API
{
Public static int wm_keydown = 0x0100;
Public static int wm_keyup = 0x0101;
Public static int wm_syskeydown = 0x0104;
Public static int wm_syskeyup = 0x0105;

Public static int wm_mousemove = 0x0200;
Public static int wm_lbuttondown = 0x0201;
Public static int wm_lbuttonup = 0x0202;
Public static int wm_lbuttondblclk = 0x0203;
Public static int wm_rbuttondown = 0x0204;
Public static int wm_rbuttonup = 0x0205;
Public static int wm_rbuttondblclk = 0x0206;
Public static int wm_user = 0x0400;

Public static int mk_lbutton = 0x0001;
Public static int mk_rbutton = 0x0002;
Public static int mk_shift = 0x0004;
Public static int mk_control = 0x0008;
Public static int mk_mbutton = 0x0010;

Public static int mk_xbutton1 = 0x0020;
Public static int mk_xbutton2 = 0x0040;

[Dllimport ("user32.dll")]
Public static extern int sendmessage (intptr hwnd, int MSG, int wparam, int lparam );

// This is mainly used to place the window at the beginning (setwindowpos (this. Handle,-0002, 0, 0, 0x4000 | 0x0001 | 0 x );)
[System. runtime. interopservices. dllimport ("user32.dll")]
Public static extern bool setwindowpos (intptr hwnd,
Int hwndinsertafter,
Int X,
Int y,
Int CX,
Int cy,
Int uflags
);

/// <Summary>
/// Set the window to the front
/// </Summary>
/// <Param name = "hwnd"> </param>
Public static void setwindowpos (intptr hwnd)
{
Setwindowpos (hwnd,-0002, 0, 0, 0x4000 | 0x0001 | 0 X );
}

/// <Summary>
///
/// </Summary>
/// <Param name = "processname"> </param>
/// <Returns> </returns>
Public static process getgameprocess (string processname)
{
Process pro = NULL;
Process [] pros = process. getprocessesbyname (processname );
If (pros. length> 0)
{
Pro = pros [0];
}
Return pro;
}
}
}

Project (Application)
Xdf. tantraplugin
// Controlitem. CS
Using system;
Using system. IO;
Using system. xml. serialization;

namespace xdf. tantraplugin
{< br> ///


/// abstract description of controlitem.
//
[serializable]
Public sealed class controlitem
{< br> private string m_name = "";
Public string name
{< br> Get
{< br> return this. m_name;
}< br> set
{< br> This. m_name = value;
}< BR >}< br> private char m_keychar = 'a ';
Public char keychar
{< br> Get
{< br> return this. m_keychar;
}< br> set
{< br> This. m_keychar = value;
}< BR >}< br> private int m_delaytime = 100;
Public int delaytime
{< br> Get
{< br> return this. m_delaytime;
}< br> set
{< br> This. m_delaytime = value;
}< BR >}< br> Public controlitem ()
{

}
}
[Serializable]
Public sealed class controlitemcollection: system. Collections. collectionbase
{
Public controlitem this [int Index]
{
Get
{
Return (controlitem) list [Index];
}
Set
{
List [Index] = value;
}
}
Public controlitemcollection ()
{
}
Public int add (controlitem item)
{
Return list. Add (item );
}
Public void remove (controlitem item)
{
List. Remove (item );
}
}
}

// Tantraconfig. CS
Using system;
Using system. IO;
Using system. xml. serialization;

Namespace xdf. tantraplugin
{
/// <Summary>
/// Summary of tantraconfig.
/// </Summary>
[Serializable]
Public class tantraconfig
{
Private controlitemcollection m_killcontrols = new controlitemcollection ();
Public controlitemcollection killcontrols
{
Get
{
Return this. m_killcontrols;
}
Set
{
This. m_killcontrols = value;
}
}
Private controlitemcollection m_bloodcontrols = new controlitemcollection ();
Public controlitemcollection bloodcontrols
{
Get
{
Return this. m_bloodcontrols;
}
Set
{
This. m_bloodcontrols = value;
}
}

Private int m_bloodrate = 25;

Public int bloodrate
{
Get
{
Return this. m_bloodrate;
}
Set
{
This. m_bloodrate = value;
}
}

Private string m_processname = "htlauncher ";

Public String processname
{
Get
{
Return this. m_processname;
}
Set
{
This. m_processname = value;
}
}

Public tantraconfig ()
{

}

Public bool save (string file)
{
Bool result = false;
Try
{
Filestream FS = new filestream (file, filemode. Create, fileaccess. Write );
Xmlserializer XSL = new xmlserializer (this. GetType ());
XSL. serialize (FS, this );
FS. Close ();
Result = true;
}
Catch
{
Result = false;
}
Return result;
}
Public static tantraconfig loadfromfile (string file)
{
Tantraconfig Config = NULL;
Try
{
Filestream FS = new filestream (file, filemode. Open, fileaccess. Read );
Xmlserializer XSL = new xmlserializer (typeof (tantraconfig ));
Config = (tantraconfig) XSL. deserialize (FS );
FS. Close ();
}
Catch
{

}
Return config;
}
}
}

// Frmmain. CS
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Threading;

Using xdf. gameplugincommon;

Namespace xdf. tantraplugin
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class frmmain: system. Windows. Forms. Form
{
Private system. Windows. Forms. Button btnsetup;
Private system. Windows. Forms. Timer timermain;
Private system. Windows. Forms. Button btnstart;
Private system. componentmodel. icontainer components;

Public frmmain ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

This. Closing + = new canceleventhandler (frmmain_closing );
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region generated by Windows Form Designer Code
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
System. Resources. ResourceManager resources = new system. Resources. ResourceManager (typeof (frmmain ));
This. btnstart = new system. Windows. Forms. Button ();
This. btnsetup = new system. Windows. Forms. Button ();
This. timermain = new system. Windows. Forms. Timer (this. components );
This. suspendlayout ();
//
// Btnstart
//
This. btnstart. Location = new system. Drawing. Point (8, 16 );
This. btnstart. Name = "btnstart ";
This. btnstart. size = new system. Drawing. Size (65, 22 );
This. btnstart. tabindex = 0;
This. btnstart. Text = "Start (& S )";
This. btnstart. Click + = new system. eventhandler (this. btnstart_click );
//
// Btnsetup
//
This. btnsetup. Location = new system. Drawing. Point (152, 16 );
This. btnsetup. Name = "btnsetup ";
This. btnsetup. size = new system. Drawing. Size (65, 22 );
This. btnsetup. tabindex = 1;
This. btnsetup. Text = "set (& C )";
This. btnsetup. Click + = new system. eventhandler (this. btnsetup_click );
//
// Frmmain
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (226, 55 );
This. Controls. Add (this. btnsetup );
This. Controls. Add (this. btnstart );
This. formborderstyle = system. Windows. Forms. formborderstyle. fixeddialog;
This. Icon = (system. Drawing. Icon) (resources. GetObject ("$ this. Icon ")));
This. maximizebox = false;
This. minimizebox = false;
This. Name = "frmmain ";
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "Tantra plugin beta1 ";
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New frmmain ());
}

Private tantraconfig m_tantraconfig = NULL;
Private thread m_thread = NULL;
Private bool m_stop = true;
Private intptr m_gamemain1_whandle = intptr. zero;

Private void btnsetup_click (Object sender, system. eventargs E)
{
Tantraconfig Config = new tantraconfig ();

Controlitemcollection items = config. killcontrols;

Controlitem item_e = new controlitem ();
Item_e.delaytime = 50;
Item_e.keychar = 'E ';
Item_e.name = "select the nearest attack target ";
Items. Add (item_e );

Controlitem item_r = new controlitem ();
Item_r.delaytime = 6000;
Item_r.keychar = 'R ';
Item_r.name = "Attacked Target ";
Items. Add (item_r );

Controlitem item_f = new controlitem ();
Item_f.delaytime = 500;
Item_f.keychar = 'F ';
Item_f.name = "pick up items that have fallen from monsters ";
Items. Add (item_f );

Controlitem item_f2 = new controlitem ();
Item_f2.delaytime = 500;
Item_f2.keychar = 'F ';
Item_f2.name = "pick up the gold coins that have fallen from monsters ";
Items. Add (item_f2 );

Controlitem item_blood = new controlitem ();
Item_blood.delaytime = 1000;
Item_blood.keychar = '1 ';
Item_blood.name = "automatically adding physical skills ";
Config. bloodcontrols. Add (item_blood );

Config. Save ("C: \ tantra. xml ");

}

Private void btnstart_click (Object sender, system. eventargs E)
{
If (this. m_stop)
{
This. startcontrol ();
}
Else
{
This. stopcontrol ();
}
This. btnstart. Text = (this. m_stop )? "Start (& S)": "Stop (& S )";
}

Private void startcontrol ()
{
String file = environment. currentdirectory + "\ tantra. xml ";
This. m_tantraconfig = tantraconfig. loadfromfile (File );
If (this. m_tantraconfig = NULL)
{
MessageBox. Show ("the configuration file is not found and cannot be started! ");
Return;
}

// Htlauncher
// String proname = "tantraplugin ";
System. Diagnostics. Process pro = API. getgameprocess (this. m_tantraconfig.processname );
If (PRO = NULL)
{
MessageBox. Show ("game process" + this. m_tantraconfig.processname + "not found, cannot start! ");
Return;
}
This. m_gamemain?whandle = pro. main=whandle;
This. Text = "game name:" + pro. processname;

This. m_stop = false;
This. m_thread = new thread (
New threadstart (tantracontrol ));

This. m_thread.start ();
}

Private void stopcontrol ()
{
If (this. m_thread! = NULL)
{
This. m_stop = true;
This. m_thread.abort ();
}
}

private void tantracontrol ()
{< br> int COUNT = 0;
while (! This. m_stop)
{< br> for (INT I = 0; I {< br> API. sendmessage (this. m_gamemain1_whandle, API. wm_keydown,
convert. toint32 (this. m_tantraconfig.killcontrols [I]. keychar), 0);
thread. sleep (this. m_tantraconfig.killcontrols [I]. delaytime);
}< br> count ++;
If (count> = This. m_tantraconfig.bloodrate)
{< br> COUNT = 0;
for (INT I = 0; I {< br> API. sendmessage (this. m_gamemain1_whandle, API. wm_keydown,
convert. toint32 (this. m_tantraconfig.bloodcontrols [I]. keychar), 0);
thread. sleep (this. m_tantraconfig.bloodcontrols [I]. delaytime);
}< BR >}

Protected override void wndproc (ref message m)
{
Base. wndproc (ref m );
If (M. MSG = API. wm_keydown)
{
This. Text = M. wparam. toint32 (). tostring ();
If (this. Text = "1 ")
{
MessageBox. Show ("blood ");
}
}
}

Private void frmmain_closing (Object sender, canceleventargs E)
{
Try
{
This. stopcontrol ();
}
Catch
{
}
}

}
}

Append the plug-in configuration file from Level 12

<? XML version = "1.0"?>
<Tantraconfig xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<Killcontrols>
<Controlitem>
<Name> select the nearest Target </Name>
<Keychar> 69 </keychar>
<Delaytime> 50 </delaytime>
</Controlitem>
<Controlitem>
<Name> target of the attack </Name>
<Keychar> 82 </keychar>
<Delaytime> 5000 </delaytime>
</Controlitem>
<Controlitem>
<Name> pick up the item that falls under the monster. </Name>
<Keychar> 70 </keychar>
<Delaytime> 500 </delaytime>
</Controlitem>
<Controlitem>
<Name> pick up the gold coins that have fallen from the monsters. </Name>
<Keychar> 70 </keychar>
<Delaytime> 500 </delaytime>
</Controlitem>
</Killcontrols>
<Bloodcontrols>
<Controlitem>
<Name> automatically add physical skills </Name>
<Keychar> 49 </keychar>
<Delaytime> 1000 </delaytime>
</Controlitem>
</Bloodcontrols>
<Bloodrate> 20 </bloodrate>
<Processname> htlauncher </processname>
</Tantraconfig>

Although I cannot use the simulated keyboard operation I found today, I hope someone will arrive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.