using System; using System.Diagnostics; using System.Collections.Generic; namespace MrAG { public class Gamerservices { public class GamerServicesException : Exception{ public GamerServicesException(string msg): base(msg){ } } private struct AvatarRequest{ public string user; public Action cb; } public static Action> ServerListCallback; public static Action IncommingTCPPacket; public static Dictionary Avatars = new Dictionary(); private static string User = ""; private static string SessID = ""; private static bool LoggedIn = false; private static string GameName = ""; private static string APIKey = ""; private static bool DebugMode = false; private static Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice; private static System.Collections.Generic.List AvatarRequests = new System.Collections.Generic.List(); private static MrAG.Networking.TCPClient APIConnection; public static MrAG.Networking.TCPClient GetAPIConnection() { return APIConnection; } public static bool GetDebug(){ return DebugMode; } public static void SetDebug(bool debug){ DebugMode = debug; } public static string GetSessID(){ return SessID; } public static string GetAPIKey(){ return APIKey; } public static string GetUser(){ return User; } public static void SetAPIKey(string key){ APIKey = key; } public static string GetIdentifier(){ return GameName; } public static void SetIdentifier(string id){ GameName = id; } public static void SetUsername(string user) { User = user; } public static bool IsLoggedIn(){ return LoggedIn; } public static void SetGraphicsDevice(Microsoft.Xna.Framework.Graphics.GraphicsDevice gs){ GraphicsDevice = gs; } public static Microsoft.Xna.Framework.Graphics.Texture2D GetAvatar(string user) { if (Avatars.ContainsKey(user)) return Avatars[user]; else return null; } public static void RequestAvatar(string user, Action Callback) { AvatarRequests.Add(new AvatarRequest(){user = user, cb = Callback}); MrAG.Gamerservices.APIConnection.Send(6, 0, user); } public static void Print(string head, string[] keys, string[] values){ string remotetext = head + "\r\n"; System.Diagnostics.Debug.Print(head); for (int i = 0; i < keys.Length; i++){ System.Diagnostics.Debug.Print("\t" + keys[i] + ": " + values[i]); remotetext += "\t" + keys[i] + ": " + values[i] + "\r\n"; } System.Diagnostics.Debug.Print(""); GetAPIConnection().Send(5, remotetext.Length + 4, remotetext.Substring(0, remotetext.Length - 1)); } public static void Print(string text){ System.Diagnostics.Debug.Print(text); GetAPIConnection().Send(5, text.Length + 5, text); } public static bool Login(bool EnableDrawNoticeSystem){ if (LoggedIn) return false; string[] args = Environment.GetCommandLineArgs(); if (args.Length > 1) { string sessID = args[args.Length - 2]; int APIPort = int.Parse(args[args.Length - 1]); try { APIConnection = new MrAG.Networking.TCPClient("127.0.0.1", APIPort); } catch { throw new GamerServicesException("Failed to connect to the API service!"); } APIConnection.Send(0, sessID.Length + APIKey.Length + GameName.Length + 13, sessID, APIKey, GameName); APIConnection.Packets[0] = new Action(Packet_00); APIConnection.Packets[1] = new Action(Packet_01); APIConnection.Packets[2] = new Action(Packet_02); APIConnection.Packets[3] = new Action(Packet_03); APIConnection.Packets[5] = new Action(Packet_05); APIConnection.Packets[6] = new Action(Packet_06); APIConnection.Packets[7] = new Action(Packet_07); while (User.Length == 0) { APIConnection.Update(); } LoggedIn = true; Achievements.Load(); Overlay.InitilizeSystem(EnableDrawNoticeSystem); } else { return false; } return true; } public static void Update() { if (APIConnection != null) { APIConnection.OnRecieve = IncommingTCPPacket; APIConnection.Update(); } } public static void LogOut(){ if (User != null && User.Length > 0) GetAPIConnection().Send(4, 1); User = ""; SessID = ""; LoggedIn = false; Overlay.Close(); } private static void Packet_00(MrAG.Networking.Packet p){ User = p.ReadString(); } private static void Packet_01(MrAG.Networking.Packet p){ p.Send(1, 1); } private static void Packet_02(MrAG.Networking.Packet p){ byte type = p.ReadByte(); switch (type) { case 0: short loops = p.ReadShort(); for (short i = 0; i < loops; i++) { string name = p.ReadString(); Achievements.AchievementList[name] = new Achievements.Achievement(); Achievements.AchievementList[name].Name = name; Achievements.AchievementList[name].Hash = p.ReadString(); Achievements.AchievementList[name].Desc = p.ReadString(); Achievements.AchievementList[name].CurrentValue = p.ReadInt(); Achievements.AchievementList[name].TargetValue = p.ReadInt(); Achievements.AchievementList[name].Points = p.ReadByte(); Achievements.AchievementList[name].Completed = Achievements.AchievementList[name].CurrentValue >= Achievements.AchievementList[name].TargetValue; Achievements.AchievementList[name].ID = i; } break; } } private static void Packet_03(MrAG.Networking.Packet p){ MrAG.Leaderboard.SubmittedEntry.Rank = p.ReadInt(); MrAG.Gamerservices.GetAPIConnection().Send(5, 0, "Recieved leaderboard pos " + MrAG.Leaderboard.SubmittedEntry.Rank); } private static void Packet_05(MrAG.Networking.Packet p){ string title = p.ReadString(); string msg = p.ReadString(); byte icon = p.ReadByte(); Overlay.AddNotice(icon, title, msg); } private static void Packet_06(MrAG.Networking.Packet p){ string user = p.ReadString(); string url = p.ReadString(); if (GraphicsDevice == null) return; System.Net.WebClient wc = new System.Net.WebClient(); wc.Proxy = null; System.IO.MemoryStream ms = new System.IO.MemoryStream(wc.DownloadData(url)); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); System.IO.MemoryStream ms2 = new System.IO.MemoryStream(); img.Save(ms2, System.Drawing.Imaging.ImageFormat.Png); Microsoft.Xna.Framework.Graphics.Texture2D tex = Microsoft.Xna.Framework.Graphics.Texture2D.FromStream(GraphicsDevice, ms2); ms.Close(); Avatars[user] = tex; for (int i = 0; i < AvatarRequests.Count; i++) { if (AvatarRequests[i].user == user) { AvatarRequests[i].cb.DynamicInvoke(user, tex); AvatarRequests.RemoveAt(i); break; } } } private static void Packet_07(MrAG.Networking.Packet p){ int count = p.ReadInt(); List servers = new List(); for (int i = 0; i < count; i++) servers.Add(new MrAG.Serverlist.ServerInfo(){IP = p.ReadString(), Port = p.ReadInt(), Servername = p.ReadString(), Meta = p.ReadString(), Users = p.ReadInt(), MaxUsers = p.ReadInt()}); ServerListCallback.DynamicInvoke(servers); } } }