Client/GamerServices/Systems/Networker.cs

498 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GamerServices
{
public class Networker
{
public MrAG.Networking.TCPClient Connection;
public FormMain Main;
public Networker(FormMain mainform){
Main = mainform;
}
public bool Initialize() {
if (!System.IO.Directory.Exists("Games"))
System.IO.Directory.CreateDirectory("Games");
try
{
Connection = MrAG.Serverlist.Connect("mrag.nl", 2222, false);
Connection.DebugMode = false;
Main.Connected = true;
#region packets
// 0-2: User related packets
Connection.Packets[0] = new Action<MrAG.Networking.Packet>(Packet_00); // Login
Connection.Packets[1] = new Action<MrAG.Networking.Packet>(Packet_01); // Logout
Connection.Packets[2] = new Action<MrAG.Networking.Packet>(Packet_02); // Ping/pong
// 3 - 9 Friends & chatting
Connection.Packets[3] = new Action<MrAG.Networking.Packet>(Packet_03); // List Friends
Connection.Packets[4] = new Action<MrAG.Networking.Packet>(Packet_04); // Friend online change
Connection.Packets[5] = new Action<MrAG.Networking.Packet>(Packet_05); // Friend Chatmessage
Connection.Packets[6] = new Action<MrAG.Networking.Packet>(Packet_06); // Friend request (server > client = username, client > server = username + bool)
Connection.Packets[7] = new Action<MrAG.Networking.Packet>(Packet_07); // Friend Add friend
Connection.Packets[8] = new Action<MrAG.Networking.Packet>(Packet_08); // Friend Start/exit game
// 10-29: Game related packets
Connection.Packets[10] = new Action<MrAG.Networking.Packet>(Packet_10); // List Games
Connection.Packets[11] = new Action<MrAG.Networking.Packet>(Packet_11); // Game Update (client > server = GameID)
Connection.Packets[12] = new Action<MrAG.Networking.Packet>(Packet_12); // Game Achievements (client > server = GameID)
Connection.Packets[13] = new Action<MrAG.Networking.Packet>(Packet_13); // Game Leaderboard (client > server = GameID)
Connection.Packets[14] = new Action<MrAG.Networking.Packet>(Packet_14); // Game Login (client > server = GameID, SessID, APIKey, Identifier)
Connection.Packets[15] = new Action<MrAG.Networking.Packet>(Packet_15); // Game ServerList
// 30 - 39: General
Connection.Packets[30] = new Action<MrAG.Networking.Packet>(Packet_30); // Message
Connection.Packets[31] = new Action<MrAG.Networking.Packet>(Packet_31); // Avatar request
// 40 - 9001: Returns
Connection.Packets[42] = new Action<MrAG.Networking.Packet>(Packet_42); // Game Leaderboard submit return
#endregion
}
catch(Exception e)
{
string text = "";
if (System.IO.File.Exists("error.txt")) {
text = System.IO.File.ReadAllText("error.txt");
}
text += "[" + e.Source + "] " + e.Message + "\r\n\tStacktrace: " + e.StackTrace + "\r\n\tHelpLink: " + e.HelpLink + "\r\n\tTargetsite: " + e.TargetSite + "\r\n\tType: " + e.GetType().FullName + "\r\n\r\n";
System.IO.File.WriteAllText("error.txt", text);
return false;
}
return true;
}
public void Update() {
try{
this.Connection.Update();
}catch(Exception e){
try {
Connection.Send(1, 0);
Connection.Connection.Close();
} catch {
}
Connection = null;
Main.Connected = false;
Main.Username = "";
Main.Sessionkey = "";
lock (Main.Friends) {
foreach (Friend f in Main.Friends) {
if (f.ChatWindow != null) f.ChatWindow.Close();
}
}
lock (Main.Games) {
foreach (Game g in Main.Games) {
g.ForceStop = true;
}
}
Main.Hide();
Main.FormLogin.Enabled = true;
Main.FormLogin.textPassword.Text = "";
Main.FormLogin.connected = false;
Main.FormLogin.Show();
Main.Error("Lost connection to GamerServices.\nPlease try reconnecting.");
}
}
public void Login(string user, string pass) {
Connection.Send(0, 0, user, pass);
}
public void Logout()
{
if (this.Main.Connected)
this.Connection.Send(1, 0);
}
public string MD5(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
System.Text.StringBuilder s = new System.Text.StringBuilder();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
foreach (byte b in bs)
{
s.Append(b.ToString("x2"));
}
return s.ToString().ToLower();
}
#region User
public void Packet_00(MrAG.Networking.Packet p)
{
bool Success = p.ReadBool();
Main.FormLogin.Enabled = true;
if (Success) {
Main.Sessionkey = p.ReadString();
Main.Username = Main.FormLogin.textUsername.Text;
Main.Settings_Username = Main.Username;
if (Main.FormLogin.checkRememberMe.Checked) Main.Settings_Password = Main.FormLogin.textPassword.Text;
else Main.Settings_Password = "";
Main.FormLogin.Hide();
Main.Show();
Main.Focus();
Main.trayIcon.Visible = true;
this.Connection.Send(10, 0);
this.Connection.Send(3, 0);
} else {
Main.FormLogin.textPassword.Text = "";
}
this.Connection.Send(15, 0, "FOREVER ABLOM");
}
public void Packet_01(MrAG.Networking.Packet p)
{
}
public void Packet_02(MrAG.Networking.Packet p)
{
Connection.Send(2, 1);
}
#endregion
#region Friends
public void Packet_03(MrAG.Networking.Packet p)
{
short loops = p.ReadShort();
for (short i = 0; i < loops; i++) {
string name = p.ReadString();
int userID = p.ReadInt();
string avatar = p.ReadString();
bool online = p.ReadBool();
short gameid = online ? p.ReadShort() : (short)-1;
this.Main.AddFriend(name, userID, online, avatar, gameid);
if (online && gameid != 0) {
Game g = Main.GetGame(gameid);
if (g != null)
Main.Popup(name, "Is now playing " + g.Name, 0, 1);
}
}
}
public void Packet_04(MrAG.Networking.Packet p)
{
int userid = p.ReadInt();
bool on = p.ReadBool();
Friend f = Main.GetFriend(userid);
if (f != null)
f.SetOnline(on);
}
public void Packet_05(MrAG.Networking.Packet p)
{
int user = p.ReadInt();
string msg = p.ReadString();
Friend f = Main.GetFriend(user);
if (f == null)
return;
f.ChatMessage(msg);
f.ChatWindow.Flash();
if (!f.ChatWindow.ContainsFocus)
Main.Popup(f.Username, msg, 1, 1);
}
public void Packet_06(MrAG.Networking.Packet p)
{
string user = p.ReadString();
Main.AddFriendRequest(user);
}
public void Packet_07(MrAG.Networking.Packet p)
{
string name = p.ReadString();
int userID = p.ReadInt();
string avatar = p.ReadString();
bool online = p.ReadBool();
this.Main.AddFriend(name, userID, online, avatar, -1);
}
public void Packet_08(MrAG.Networking.Packet p)
{
int user = p.ReadInt();
bool ingame = p.ReadBool();
short gameid = ingame ? p.ReadShort() : (short)-1;
Friend f = Main.GetFriend(user);
if (f == null)
return;
if (ingame){
Game g = Main.GetGame(gameid);
f.GameID = g.GameID;
if (g != null)
Main.Popup(f.Username, "Is now playing " + g.Name, 0, 1);
}else{
Game g = Main.GetGame(f.GameID);
if (g != null)
Main.Popup(f.Username, "Stopped playing " + g.Name, 0, 1);
}
}
#endregion
#region Game
public void Packet_10(MrAG.Networking.Packet p)
{
int loops = p.ReadInt();
Main.Games = new Game[loops];
for (int i = 0; i < loops; i++) {
Game newGame = new Game(Main);
newGame.Identifier = p.ReadString();
newGame.Name = p.ReadString();
newGame.GameID = p.ReadShort();
newGame.Description = p.ReadString();
newGame.MainExe = p.ReadString();
newGame.IsDevUser = p.ReadBool();
short tmpport = newGame.GameID;
while (tmpport > 10)
tmpport -= 10;
newGame.GamePort = (byte)tmpport;
newGame.Status = System.IO.Directory.Exists("Games/" + newGame.GameID) ? Game.GameStatus.INSTALLED : Game.GameStatus.UNINSTALLED;
if (newGame.Status == Game.GameStatus.INSTALLED) {
if (System.IO.File.Exists("Games/" + newGame.GameID + "/version.txt"))
newGame.Version = System.IO.File.ReadAllText("Games/" + newGame.GameID + "/version.txt");
else
newGame.Version = "0.0.0";
Connection.Send(11, 3, newGame.GameID);
Connection.Send(12, 3, newGame.GameID);
Connection.Send(13, 3, newGame.GameID);
}
Main.Games[i] = newGame;
}
Main.SortGames();
}
public void Packet_11(MrAG.Networking.Packet p)
{
string serverver = p.ReadString();
short gameID = p.ReadShort();
string ownver = System.IO.File.ReadAllText("Games/" + gameID + "/version.txt");
//if (serverver != ownver)
if (!Main.Downloader.UpdateParser(gameID, ownver))
Main.Error("Could not update game " + Main.GetGame(gameID).Name + ", no version file found.");
}
public void Packet_12(MrAG.Networking.Packet p)
{
short GameID = p.ReadShort();
int loops = p.ReadInt();
Game g = Main.GetGame(GameID);
if (g == null) { // Lets still read out all the crap its sending >.>
for (int i = 0; i < loops; i++) {
p.ReadInt();
p.ReadString();
p.ReadString();
p.ReadShort();
p.ReadInt();
p.ReadInt();
}
} else {
g.Achievements.Clear();
for (int i = 0; i < loops; i++) {
Game.Achievement a = new Game.Achievement();
a.ID = p.ReadInt();
a.Name = p.ReadString();
a.Desc = p.ReadString();
a.Points = p.ReadShort();
a.CurProg = p.ReadInt();
a.TargetProg = p.ReadInt();
a.Hash = MD5(a.Name + a.Desc).Substring(0, 10);
g.Achievements.Add(a);
}
}
}
public void Packet_13(MrAG.Networking.Packet p)
{
short GameID = p.ReadShort();
int looperdieloops = p.ReadInt();
Game g = Main.GetGame(GameID);
if (g == null) {
for (int i = 0; i < looperdieloops; i++) {
p.ReadInt();
p.ReadString();
p.ReadString();
p.ReadString();
}
} else {
Main.GetGame(GameID).LeaderBoards = new Game.Leaderboard[looperdieloops];
for (int i = 0; i < looperdieloops; i++) {
g.LeaderBoards[i] = new Game.Leaderboard();
g.LeaderBoards[i].ID = p.ReadInt();
g.LeaderBoards[i].Name = p.ReadString();
g.LeaderBoards[i].Desc = p.ReadString();
g.LeaderBoards[i].Identifier = p.ReadString();
}
}
}
public void Packet_14(MrAG.Networking.Packet p)
{
short GameID = p.ReadShort();
Game g = Main.GetGame(GameID);
if (g == null) {
p.ReadBool();
return;
}
bool access = p.ReadBool();
if (access) {
g.APIStatus = Game.GameAPIReturn.YES;
} else {
g.APIStatus = Game.GameAPIReturn.NO;
}
}
public void Packet_15(MrAG.Networking.Packet p)
{
int count = p.ReadInt();
if (this.Main.CurrentPlayingGame != null && this.Main.CurrentPlayingGame.GetAPIConnection() != null && this.Main.CurrentPlayingGame.GetAPIConnection().Connected) {
MrAG.Networking.Packet gp = this.Main.CurrentPlayingGame.GetAPIConnection().Connection;
gp.Start_Send();
gp.AddByte(7);
gp.AddInt(count);
for (int i = 0; i < count; i++) {
gp.AddString(p.ReadString());
gp.AddInt(p.ReadInt());
gp.AddString(p.ReadString());
gp.AddString(p.ReadString());
gp.AddInt(p.ReadInt());
gp.AddInt(p.ReadInt());
}
gp.Finish_Send();
} else {
for (int i = 0; i < count; i++) {
p.ReadString();
p.ReadInt();
p.ReadString();
p.ReadString();
p.ReadInt();
p.ReadInt();
}
}
}
#endregion
#region General
public void Packet_30(MrAG.Networking.Packet p)
{
string title = p.ReadString();
string msg = p.ReadString();
byte type = p.ReadByte();
System.Windows.Forms.MessageBoxIcon icon = System.Windows.Forms.MessageBoxIcon.None;
switch (type) {
case 0:
icon = System.Windows.Forms.MessageBoxIcon.Asterisk;
break;
case 1:
icon = System.Windows.Forms.MessageBoxIcon.Error;
break;
case 2:
icon = System.Windows.Forms.MessageBoxIcon.Exclamation;
break;
case 3:
icon = System.Windows.Forms.MessageBoxIcon.Hand;
break;
case 4:
icon = System.Windows.Forms.MessageBoxIcon.Information;
break;
case 5:
icon = System.Windows.Forms.MessageBoxIcon.None;
break;
case 6:
icon = System.Windows.Forms.MessageBoxIcon.Question;
break;
case 7:
icon = System.Windows.Forms.MessageBoxIcon.Stop;
break;
case 8:
icon = System.Windows.Forms.MessageBoxIcon.Warning;
break;
}
Main.Error(title, msg, icon);
}
public void Packet_31(MrAG.Networking.Packet p)
{
string user = p.ReadString();
string url = "http://" + "www.gravatar.com/avatar/" + p.ReadString() + "?s=64";
Main.CurrentPlayingGame.GetAPIConnection().Connection.Send(6, 9 + url.Length + user.Length, user, url);
}
#endregion
#region Returns
public void Packet_42(MrAG.Networking.Packet p)
{
Main.CurrentPlayingGame.GetAPIConnection().Connection.Send(3, 0, p.ReadInt());
}
#endregion
}
}