C # Game plug-in to implement the core code _c# tutorial

Source: Internet
Author: User
Tags serialization xsl
Recently started to play the Secret network game by a friend introduction
Upgrade upgrade, suddenly feel too fee keyboard, so I wrote a program with C #, want to replace my operation, I go to play monsters, their own upgrade
With this thing up a lot of levels, now put the source code out, and everyone to share, welcome criticism, thank you.
The program is divided into two parts, one part is the class library, the other is the application
The idea is to find the main window handle of the game process, and then send the game key message (analog button).


Xdf. Gameplugincommon Class Library Project

API.cs files, defining 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>
Summary description of the API.
</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 primarily used to place the window at the Front (SetWindowPos) (this. HANDLE,-1,0,0,0,0,0X4000|0X0001|0X0002);)
[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>
Pre-window Placement
</summary>
<param name= "HWnd" ></param>
public static void SetWindowPos (IntPtr hWnd)
{
SetWindowPos (hwnd,-1,0,0,0,0,0x4000|0x0001|0x0002);
}

<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
{
<summary>
Summary description of the Controlitem.
</summary>
[Serializable]
public sealed class Controlitem
{
private string m_name = "";
public string Name
{
Get
{
return this.m_name;
}
Set
{
This.m_name = value;
}
}
Private char M_keychar = ' a ';
Public Char Keychar
{
Get
{
return This.m_keychar;
}
Set
{
This.m_keychar = value;
}
}
private int m_delaytime = 100;
public int Delaytime
{
Get
{
return this.m_delaytime;
}
Set
{
This.m_delaytime = value;
}
}
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 description of the 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 description of the 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 Forms Designer support
//
InitializeComponent ();


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

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents 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>
The main entry point for 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_gamemainwindowhandle = 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 a recent attack target";
Items. ADD (item_e);

Controlitem Item_r = new Controlitem ();
Item_r.delaytime = 6000;
Item_r.keychar = ' R ';
Item_r.name = "attack designated target";
Items. ADD (Item_r);

Controlitem Item_f = new Controlitem ();
Item_f.delaytime = 500;
Item_f.keychar = ' F ';
Item_f.name = "Pick up playing after the monster fell off the object";
Items. ADD (Item_f);

Controlitem item_f2 = new Controlitem ();
Item_f2. Delaytime = 500;
Item_f2. Keychar = ' F ';
Item_f2. Name = "Pick up playing after the monster fell off the gold";
Items. ADD (ITEM_F2);

Controlitem Item_blood = new Controlitem ();
Item_blood. Delaytime = 1000;
Item_blood. Keychar = ' 1 ';
Item_blood. Name = "Automatic increase of physical ability";
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 ("config file not found, unable to start!");
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, unable to start!");
Return
}
This.m_gamemainwindowhandle = Pro. Mainwindowhandle;
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 ()
{
int count = 0;
while (!this.m_stop)
{
for (int i=0;i<this.m_tantraconfig.killcontrols.count;i++)
{
Api. SendMessage (This.m_gamemainwindowhandle,api. WM_KEYDOWN,
Convert.ToInt32 (This.m_tantraconfig.killcontrols[i]. Keychar), 0);
Thread.Sleep (This.m_tantraconfig.killcontrols[i]. Delaytime);
}
Count + +;
if (Count >= this.m_TantraConfig.BloodRate)
{
Count = 0;
for (int i=0;i<this.m_tantraconfig.bloodcontrols.count;i++)
{
Api. SendMessage (This.m_gamemainwindowhandle,api. WM_KEYDOWN,
Convert.ToInt32 (This.m_tantraconfig.bloodcontrols[i]. Keychar), 0);
Thread.Sleep (This.m_tantraconfig.bloodcontrols[i]. Delaytime);
}
}
}
}

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
{
}
}

}
}


Attach the profile that I plug in 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 most recent attack target </Name>
<KeyChar>69</KeyChar>
<DelayTime>50</DelayTime>
</ControlItem>
<ControlItem>
<Name> Attack selected targets </Name>
<KeyChar>82</KeyChar>
<DelayTime>5000</DelayTime>
</ControlItem>
<ControlItem>
<Name> pick up playing after the monster fell off </Name>
<KeyChar>70</KeyChar>
<DelayTime>500</DelayTime>
</ControlItem>
<ControlItem>
<Name> pick up the playing of the monster's fallen gold </Name>
<KeyChar>70</KeyChar>
<DelayTime>500</DelayTime>
</ControlItem>
</KillControls>
<BloodControls>
<ControlItem>
<Name> automatically increase physical ability </Name>
<KeyChar>49</KeyChar>
<DelayTime>1000</DelayTime>
</ControlItem>
</BloodControls>
<BloodRate>20</BloodRate>
<ProcessName>HTLauncher</ProcessName>
</TantraConfig>

Today found the analog keyboard operation, although I can not use, I hope someone will come.

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.