Client/GamerServices/Systems/Forms/FormGameDev.cs

267 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace GamerServices {
public partial class FormGameDev : Form {
private int TotalIn = 0;
private int TotalOut = 0;
private double[] AvIn = new double[30];
private double[] AvOut = new double[30];
private int curin = 0;
private int curout = 0;
private Game G;
private PerformanceCounter prefcounter;
private PerformanceCounter memcounter;
public FormGameDev(Game g) {
G = g;
CheckForIllegalCrossThreadCalls = false;
this.Icon = G.Main.Icon;
InitializeComponent();
}
public void AddIn(int val) {
TotalIn += val;
curin += val;
this.label_traf_totin.Text = TotalIn.ToString();
}
public void AddOut(int val) {
TotalOut += val;
curout += val;
this.label_traf_toto.Text = TotalOut.ToString();
}
public void UpdateAv() {
if (G.ForceStop){
this.Close();
return;
}
double curavin = 0;
double curavout = 0;
for (int i = 1; i < AvIn.Length; i++) {
curavin += AvIn[i];
curavout += AvOut[i];
AvIn[i - 1] = AvIn[i];
AvOut[i - 1] = AvOut[i];
}
AvIn[AvIn.Length - 1] = curin;
AvOut[AvOut.Length - 1] = curout;
curavin += curin;
curavout += curout;
curin = 0;
curout = 0;
bool foundproc = false;
Process[] procs = Process.GetProcessesByName(G.MainExe.Substring(0, G.MainExe.Length - 4));
foreach (Process proc in procs) {
foundproc = true;
if (prefcounter == null){
prefcounter = new PerformanceCounter("Process", "% Processor Time", proc.ProcessName);
memcounter = new PerformanceCounter("Process", "Working Set", proc.ProcessName);
AddMessage("Starting process monitor");
}else{
this.label_proc_cpu.Text = (prefcounter.NextValue() / 100).ToString() + "%";
this.label_proc_RAM.Text = ((memcounter.NextValue() / 1024) / 1024).ToString() + " MB";
}
if (this.label_proc_name.Text != proc.ProcessName) this.label_proc_name.Text = proc.ProcessName;
if (this.label_proc_threads.Text != proc.Threads.Count.ToString()) this.label_proc_threads.Text = proc.Threads.Count.ToString();
break;
}
if (!foundproc) {
if (prefcounter != null)
AddMessage("Lost game proccess.");
prefcounter = null;
memcounter = null;
this.label_proc_name.Text = "";
this.label_proc_cpu.Text = "";
this.label_proc_RAM.Text = "";
this.label_proc_threads.Text = "";
}
curavin /= AvIn.Length;
curavout /= AvOut.Length;
curavin = curavin - curavin % 0.1;
curavout = curavout - curavout % 0.1;
this.label_traf_avi.Text = curavin.ToString();
this.label_traf_avo.Text = curavout.ToString();
}
public void AddMessage(string msg) {
textlog.Text += msg + "\r\n";
textlog.SelectionStart = textlog.Text.Length;
textlog.ScrollToCaret();
textlog.Refresh();
}
private void button2_Click(object sender, EventArgs e) {
TotalIn = 0;
TotalOut = 0;
this.label_traf_totin.Text = "0";
this.label_traf_toto.Text = "0";
this.label_traf_avi.Text = "0";
this.label_traf_avo.Text = "0";
AvIn = new double[30];
AvOut = new double[30];
curin = 0;
curout = 0;
textlog.Text = "";
}
private void button1_Click(object sender, EventArgs e) {
G.DevStop();
}
private void FormGameDev_Load(object sender, EventArgs e) {
button2_Click(null, null);
checkBox1.Checked = false;
button4.Enabled = checkBox1.Checked;
textBox1.Enabled = checkBox1.Checked;
comboBox1.Enabled = checkBox1.Checked;
comboBox1.Items.Add("Notice");
comboBox1.Items.Add("General");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
switch(comboBox1.Text){
case "Notice":
textBox1.Text = "# Example of a notice popup\r\n\r\n# The byte 5 is for the inital packet type, leave it alone\r\n(byte)5\r\n(string)Title\r\n(string)Message\r\n\r\n# This is the icon type wich should be displayed whit the message!\r\n(byte)0";
break;
case "General":
textBox1.Text = "# Example of a packet\r\n# This is extremely useful for debugging purposes\r\n# If your game hooks into MrAG.Gamerservices.IncomingTCPPacket,\r\n# you can use this to manually send packets to trigger events for debugging results\r\n# Available packets are 6-254, below 5 are reservated for the tunneling service\r\n(byte)6\r\n";
break;
}
comboBox1.Text = "Examples";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
button4.Enabled = checkBox1.Checked;
textBox1.Enabled = checkBox1.Checked;
comboBox1.Enabled = checkBox1.Checked;
}
private void button4_Click(object sender, EventArgs e) {
string[] tosend = textBox1.Text.Replace("\r", "").Split('\n');
MrAG.Networking.TCPClient c = G.GetAPIConnection();
if (c == null) {
MessageBox.Show("No connection yet!", "Error");
return;
}
bool packetIDSend = false;
c.Connection.Start_Send();
foreach(string line in tosend){
if (line.StartsWith("#")) {
} else if (line.Length > 0) {
string type = line.Substring(1, line.IndexOf(")") - 1);
string data = line.Substring(type.Length + 2);
switch (type) {
case "string":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddString(data);
break;
case "byte":
byte byettosend = byte.Parse(data);
if (!packetIDSend && (byettosend < 5 || byettosend == 6)) {
MessageBox.Show("Error, Packet 0-6 is reserved for internal use!", "Error");
c.Connection.Finish_Send();
return;
}
packetIDSend = true;
c.Connection.AddByte(byettosend);
break;
case "short":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddByte(short.Parse(data));
break;
case "int":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddInt(int.Parse(data));
break;
case "float":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddFloat(float.Parse(data));
break;
case "long":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddLong(long.Parse(data));
break;
case "double":
if (!packetIDSend) {
MessageBox.Show("Error, must send packetID (a byte) first!", "Error");
c.Connection.Finish_Send();
return;
}
c.Connection.AddDouble(double.Parse(data));
break;
}
}
}
c.Connection.Finish_Send();
MessageBox.Show("Sent!", "Packet has been sent.");
}
}
}