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.IO; using System.Windows.Shell; namespace GamerServices { public partial class FormMain : Form { #region Varibles public String GamerServicesVersion; public Networker Networker; public Downloader Downloader; public FormLogin FormLogin; public String Username = "Bromvlieg"; public String Sessionkey = ""; public bool Connected = false; public byte PopupAction = 0; public Game[] Games; public Game CurrentPlayingGame; public List Friends = new List(); public Font DefaultFont; Properties.Settings settings = new Properties.Settings(); public String Settings_Username { get { return settings.Username; } set { settings.Username = value; } } public String Settings_Password { get { return settings.Password; } set { settings.Password = value; } } #endregion public FormMain(FormLogin log) { InitializeComponent(); Networker = new GamerServices.Networker(this); Downloader = new Downloader(this); FormLogin = log; bool changed = false; MrAG.Config.Read("config.gs"); if (!MrAG.Config.Section_Exists("Main")) { System.Collections.Hashtable maintbl = new System.Collections.Hashtable(); maintbl["Version"] = "0.0.0.0"; maintbl["Language"] = "English"; MrAG.Config.Section_Set("Main", maintbl); changed = true; } if (!MrAG.Config.Section_Exists("Fonts")) { System.Collections.Hashtable maintbl = new System.Collections.Hashtable(); maintbl["General"] = "Microsoft Sans Serif"; maintbl["General_Size"] = "8.25"; maintbl["General_Style"] = "Regular"; maintbl["GameDesc"] = "Calibri"; maintbl["GameDesc_Size"] = "9.75"; maintbl["GameDesc_Style"] = "Regular"; maintbl["Chat"] = "Microsoft Sans Serif"; maintbl["Chat_Size"] = "8.25"; maintbl["Chat_Style"] = "Regular"; maintbl["Chat_Header"] = "Calibri"; maintbl["Chat_Header_Size"] = "14.25"; maintbl["Chat_Header_Style"] = "Bold"; maintbl["Chat_Sub"] = "Calibri"; maintbl["Chat_Sub_Size"] = "9"; maintbl["Chat_Sub_Style"] = "Regular"; MrAG.Config.Section_Set("Fonts", maintbl); changed = true; } if (!MrAG.Config.Section_Exists("Settings")) { System.Collections.Hashtable maintbl = new System.Collections.Hashtable(); maintbl["ChatSound"] = "true"; maintbl["ChatPopup"] = "true"; maintbl["ChatPopupIngame"] = "true"; maintbl["ChatFlash"] = "true"; maintbl["IngamePopups"] = "true"; MrAG.Config.Section_Set("Settings", maintbl); changed = true; } if (!MrAG.Config.Section_Exists("Friends_Blocked")) { MrAG.Config.Section_Set("Friends_Blocked", new System.Collections.Hashtable()); changed = true; } if (!MrAG.Config.Section_Exists("Friends_Renamed")) { MrAG.Config.Section_Set("Friends_Renamed", new System.Collections.Hashtable()); changed = true; } if (changed) MrAG.Config.Write("config.gs"); this.GamerServicesVersion = MrAG.Config.Value_Get("Main", "Version"); DefaultFont = GetFont("General"); labelGameDescription.Font = GetFont("GameDesc"); fileToolStripMenuItem.Font = DefaultFont; helpToolStripMenuItem.Font = DefaultFont; friendsToolStripMenuItem.Font = DefaultFont; tabControl1.Font = DefaultFont; listAchievements.Font = DefaultFont; listGames.Font = DefaultFont; } public Font GetFont(string configname) { string name = MrAG.Config.Value_Get("Fonts", configname, "Calibri"); float size = float.Parse(MrAG.Config.Value_Get("Fonts", configname + "_Size", "8.5"), System.Globalization.CultureInfo.InvariantCulture.NumberFormat); FontStyle style = FontStyle.Regular; switch(MrAG.Config.Value_Get("Fonts", configname + "_Style", "Regular")){ case "Bold": style = FontStyle.Bold; break; case "Italic": style = FontStyle.Italic; break; case "Regular": style = FontStyle.Regular; break; case "Strikeout": style = FontStyle.Strikeout; break; case "Underline": style = FontStyle.Underline; break; } return new Font(name, size, style); } #region Public functions public void Die(String Reason) { if (Sessionkey != "") Networker.Logout(); this.trayIcon.Dispose(); MessageBox.Show(Reason, "Fatal error!", MessageBoxButtons.OK, MessageBoxIcon.Error); System.Diagnostics.Process.GetCurrentProcess().Kill(); } public void Error(String Reason) { MessageBox.Show(Reason, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } public void Error(String title, String Reason, MessageBoxIcon icon) { MessageBox.Show(Reason, title, MessageBoxButtons.OK, icon); } public void PlaceholderText() { picGameIcon.Image = new Bitmap(32, 32); labelGameName.Text = "..."; labelGameDescription.Text = "..."; } public Game GetGame(short ID) { foreach (Game g in Games) { if (g.GameID == ID) return g; } return null; } public Friend GetFriend(string username) { foreach (Friend f in Friends) { if (f.Username == username) return f; } return null; } public Friend GetFriend(int userID) { foreach (Friend f in Friends) { if (f.UserID == userID) return f; } return null; } public void Popup(string title, string msg, byte action) { if (this.CurrentPlayingGame != null && this.CurrentPlayingGame.GetAPIConnection() != null) { this.CurrentPlayingGame.GetAPIConnection().Send(5, 9 + msg.Length + title.Length, title, msg, (byte)0); } else { this.PopupAction = action; this.trayIcon.BalloonTipTitle = title; this.trayIcon.ShowBalloonTip(2000, title, msg, ToolTipIcon.Info); } } public void Popup(string title, string msg, byte action, byte msgtype) { if (this.CurrentPlayingGame != null && this.CurrentPlayingGame.GetAPIConnection() != null) { if (msgtype == 1) { Friend f = GetFriend(title); if (f != null){ string url = "http://" + "www.gravatar.com/avatar/" + f.AvatarURL + "?s=64"; this.CurrentPlayingGame.GetAPIConnection().Send(6, 9 + url.Length + title.Length, title, url); } } this.CurrentPlayingGame.GetAPIConnection().Send(5, 9 + msg.Length + title.Length, title, msg, msgtype); } else { this.PopupAction = action; this.trayIcon.BalloonTipTitle = title; this.trayIcon.ShowBalloonTip(2000, title, msg, ToolTipIcon.Info); } } public void SetGameData(short gameID, string text, Game.GameStatus status) { for (int i = 0; i < this.listGames.Items.Count; i++) { if ((short)this.listGames.Items[i].Tag == gameID) { this.listGames.Items[i].Text = text; this.listGames.Items[i].ImageIndex = (int)status; break; } } } public void SortGames() { lock (Games) { Games = Games.OrderBy(Game => Game.Status).ToArray(); UpdateGamesList(); } } public void LoadRemainingDownloads() { if (!File.Exists("downloading.txt")) return; string[] data = File.ReadAllText("downloading.txt").Replace("\t", "").Split('\n'); if (data.Length > 0 && data[0].Length > 0) { lock (Downloader.FilesToDownload) { for (int i = 0; i < data.Length; i++) { string[] verngameid = data[i].Split(' '); short GameID = short.Parse(verngameid[0]); int actions = 0; Game g = GetGame(GameID); g.Status = Game.GameStatus.DOWNLOADING; g.Version = verngameid[1]; if (data[i + 1] == "{") { i += 2; while (data[i] != "}") { Downloader.DownloadInfo info = new Downloader.DownloadInfo(); info.Path = data[i + 0]; info.URL = "http://" + "gs.mrag.nl/download.php?gid=" + GameID + "&file=" + data[i + 0]; info.Action = byte.Parse(data[i + 1]); info.GameID = GameID; info.Version = g.Version; Downloader.FilesToDownload.Add(info); actions++; i += 2; } } for (int i2 = 0; i2 < Downloader.FilesToDownload.Count; i2++) { if (Downloader.FilesToDownload[i2].GameID == GameID) { Downloader.FilesToDownload[i2].TotalActions = actions; } } } } } } public void UpdateGamesList() { lock (this.listGames) { this.listGames.Items.Clear(); for (int i = 0; i < this.Games.Length; i++) { ListViewItem lvi = new ListViewItem(this.Games[i].Name); lvi.ImageIndex = (int)this.Games[i].Status; this.listGames.Items.Add(lvi); lvi.Tag = this.Games[i].GameID; } } } public void AddFriend(string name, int userID, bool online, string avatar, short curgame) { Friend f = new Friend(this); f.Username = name; f.Online = online; f.UserID = userID; f.AvatarURL = avatar; f.GameID = curgame; Friends.Add(f); UpdateFriendList(); } public void AddFriendRequest(string username) { ToolStripMenuItem itm = new ToolStripMenuItem(); itm.Text = username; itm.Click += new EventHandler(OpenChat); ToolStripMenuItem y = new ToolStripMenuItem(); y.Text = "Accept"; y.Tag = username; y.Click += new EventHandler(AcceptFriendRequest); ToolStripMenuItem n = new ToolStripMenuItem(); n.Text = "Decline"; n.Tag = username; n.Click += new EventHandler(RefuseFriendRequest); itm.DropDownItems.Add(y); itm.DropDownItems.Add(n); requestsToolStripMenuItem.DropDownItems.Add(itm); } public void UpdateFriendList() { onlineToolStripMenuItem.DropDownItems.Clear(); offlineToolStripMenuItem.DropDownItems.Clear(); foreach (Friend f in Friends) { ToolStripMenuItem itm = new ToolStripMenuItem(); itm.Text = f.Username; itm.Enabled = f.Online; itm.Click += new EventHandler(OpenChat); if (f.Online) onlineToolStripMenuItem.DropDownItems.Add(itm); else offlineToolStripMenuItem.DropDownItems.Add(itm); } onlineToolStripMenuItem.Text = "Online" + (onlineToolStripMenuItem.DropDownItems.Count > 0 ? " (" + onlineToolStripMenuItem.DropDownItems.Count + ")" : ""); offlineToolStripMenuItem.Text = "Offline" + (offlineToolStripMenuItem.DropDownItems.Count > 0 ? " (" + offlineToolStripMenuItem.DropDownItems.Count + ")" : ""); } public void SetFriendOnline(int userID, bool online) { GetFriend(userID).SetOnline(online); UpdateFriendList(); } public void AcceptFriendRequest(object sender, EventArgs e) { Networker.Connection.Send(6, 0, (string)(sender as ToolStripMenuItem).Tag, true); requestsToolStripMenuItem.DropDownItems.Remove((sender as ToolStripMenuItem).OwnerItem); } public void RefuseFriendRequest(object sender, EventArgs e) { Networker.Connection.Send(6, 0, (string)(sender as ToolStripMenuItem).Tag, false); requestsToolStripMenuItem.DropDownItems.Remove((sender as ToolStripMenuItem).OwnerItem); } public void OpenChat(object sender, EventArgs e) { string name = (sender as ToolStripMenuItem).Text; foreach (Friend f in Friends) { if (f.Username == name) { f.OpenChat(); break; } } } #endregion #region Form Functions private void buttonUninstall_Click(object sender, EventArgs e) { if (listGames.SelectedItems.Count == 0) return; Game_Uninstall(null, null); buttonPlay.Text = "Install"; buttonPlay.Visible = true; buttonUpdate.Visible = false; buttonUninstall.Visible = false; } private void buttonUpdate_Click(object sender, EventArgs e) { if (listGames.SelectedItems.Count == 0) return; Game_Update(null, null); buttonPlay.Visible = false; buttonUpdate.Visible = false; buttonUninstall.Visible = false; } private void buttonPlay_Click(object sender, EventArgs e) { if (listGames.SelectedItems.Count == 0) return; short id = (short)listGames.SelectedItems[0].Tag; if (buttonPlay.Text == "Play") { Game_Start(null, null); } else { Game_Install(null, null); } Game Game = GetGame(id); if (Game == null) return; buttonPlay.Text = Game.Status == GamerServices.Game.GameStatus.INSTALLED ? "Play" : Game.Status == GamerServices.Game.GameStatus.UNINSTALLED ? "Install" : ""; buttonPlay.Visible = Game.Status != GamerServices.Game.GameStatus.DOWNLOADING; buttonUpdate.Visible = Game.Status == GamerServices.Game.GameStatus.INSTALLED; buttonUninstall.Visible = Game.Status == GamerServices.Game.GameStatus.INSTALLED; } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { settings.Save(); Networker.Logout(); foreach (Friend f in Friends) { if (f.ChatWindow != null) f.ChatWindow.Close(); } foreach (Game g in Games) { g.ForceStop = true; } this.Close(); FormLogin.Close(); } private void FormMain_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; PlaceholderText(); LoadRemainingDownloads(); } private void timerUpdate_Tick(object sender, EventArgs e) { if (this.Connected) { Networker.Update(); } } private void listGames_SelectedIndexChanged(object sender, EventArgs e) { PlaceholderText(); if (listGames.SelectedItems.Count == 1) { ListViewItem lvi = listGames.SelectedItems[0]; Game Game = GetGame((short)lvi.Tag); if (Game != null) { labelGameName.Text = Game.Name; labelGameDescription.Text = Game.Description; buttonPlay.Text = Game.Status == GamerServices.Game.GameStatus.INSTALLED ? "Play" : Game.Status == GamerServices.Game.GameStatus.UNINSTALLED ? "Install" : ""; buttonPlay.Visible = Game.Status != GamerServices.Game.GameStatus.DOWNLOADING; buttonUpdate.Visible = Game.Status == GamerServices.Game.GameStatus.INSTALLED; buttonUninstall.Visible = Game.Status == GamerServices.Game.GameStatus.INSTALLED; if (Game.Status == GamerServices.Game.GameStatus.INSTALLED && File.Exists("Games/" + Game.GameID.ToString() + "/GameIcon.png")) { picGameIcon.Image = Image.FromFile("Games/" + Game.GameID.ToString() + "/GameIcon.png"); } else { if (File.Exists("Media/DefaultGameIcon.png")) picGameIcon.Image = Image.FromFile("Media/DefaultGameIcon.png"); else picGameIcon.Image = new Bitmap(32, 32); } groupDeveloperToolBox.Visible = Game.IsDevUser; if (Game.IsDevUser) { groupDeveloperToolBox.Text = "Developer Settings - " + Game.Name; textExecuteblePath.Text = Game.MainExe; labelVersion.Text = Game.Version; } listAchievements.Items.Clear(); foreach (Game.Achievement a in Game.Achievements) { ListViewItem itm = new ListViewItem(); itm.Text = a.Name; itm.BackColor = Color.GreenYellow; itm.SubItems.Add(a.Points.ToString()); if (a.CurProg < a.TargetProg) { itm.BackColor = Color.OrangeRed; } itm.SubItems.Add(a.Desc); listAchievements.Items.Add(itm); } } } else { labelGameName.Text = "..."; labelGameDescription.Text = ""; buttonPlay.Visible = false; buttonUpdate.Visible = false; buttonUninstall.Visible = false; groupDeveloperToolBox.Visible = false; listAchievements.Items.Clear(); } } private string GetSub(string line, string find_start, string find_end) { int start = line.IndexOf(find_start) + find_start.Length; int end = line.IndexOf(find_end, start) - start; return line.Substring(start, end); } private void textExecuteblePath_TextChanged(object sender, EventArgs e) { if (listGames.SelectedItems.Count == 1) { Game Game = GetGame((short)listGames.SelectedItems[0].Tag); Game.MainExe = textExecuteblePath.Text; } } private void listGames_MouseClick(object sender, MouseEventArgs e) { if (listGames.SelectedItems.Count == 0) return; if (e.Button == MouseButtons.Right) { Game Game = GetGame((short)listGames.SelectedItems[0].Tag); ContextMenu context = new ContextMenu(); if (Game.Status == Game.GameStatus.UNINSTALLED) { MenuItem InstallItem = new MenuItem("Install"); InstallItem.Click += new EventHandler(Game_Install); context.MenuItems.Add(InstallItem); }else if (Game.Status == Game.GameStatus.INSTALLED) { if (Game.IsDevUser) { MenuItem item8 = new MenuItem("Normal Options"); item8.Enabled = false; context.MenuItems.Add(item8); } MenuItem item1 = new MenuItem("Start"); item1.Click += new EventHandler(Game_Start); item1.Enabled = CurrentPlayingGame == null; context.MenuItems.Add(item1); MenuItem item3 = new MenuItem("Update"); item3.Click += new EventHandler(Game_Update); item3.Enabled = CurrentPlayingGame != Game; context.MenuItems.Add(item3); MenuItem item2 = new MenuItem("Uninstall"); item2.Click += new EventHandler(Game_Uninstall); item2.Enabled = CurrentPlayingGame != Game; context.MenuItems.Add(item2); if (Game.IsDevUser) { MenuItem item7 = new MenuItem("Developer Options"); item7.BarBreak = true; item7.Enabled = false; context.MenuItems.Add(item7); MenuItem item5 = new MenuItem("Push update"); item5.Click += new EventHandler(Game_DevUpdate); item5.Enabled = CurrentPlayingGame != Game; context.MenuItems.Add(item5); MenuItem item4 = new MenuItem("Start"); item4.Click += new EventHandler(Game_DevStart); item4.Enabled = CurrentPlayingGame == null; context.MenuItems.Add(item4); MenuItem item6 = new MenuItem("Stop"); item6.Click += new EventHandler(Game_DevStop); item6.Enabled = CurrentPlayingGame == Game && Game.Devmode; context.MenuItems.Add(item6); } } context.Show(listGames, new Point(e.X, e.Y)); } } void Game_Start(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).Start(); } void Game_DevStart(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).DevStart(); } void Game_Update(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).Update(); } void Game_Install(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).Install(); } void Game_DevStop(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).DevStop(); } void Game_DevUpdate(object sender, EventArgs e) { GetGame((short)listGames.SelectedItems[0].Tag).DevUpdate(); } void Game_Uninstall(object sender, EventArgs e) { if (MessageBox.Show("Are you sure you want to uninstall?", "Uninstall", MessageBoxButtons.YesNo, MessageBoxIcon.Error) != DialogResult.Yes) { return; } GetGame((short)listGames.SelectedItems[0].Tag).Uninstall(); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { new FormAbout().ShowDialog(); } private void PingTimer_Tick(object sender, EventArgs e) { if (this.Connected) { Networker.Connection.Send(2, 0); } } private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { Networker.Logout(); settings.Save(); this.trayIcon.Dispose(); FormLogin.Close(); if (Downloader != null && Downloader.DownloadThread != null) Downloader.DownloadThread.Abort(); } private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e) { this.Show(); } private void listAchievements_SelectedIndexChanged(object sender, EventArgs e) { foreach (ListViewItem itm in listAchievements.SelectedItems) { itm.Selected = false; } listAchievements.SelectedIndices.Clear(); } private void logoutToolStripMenuItem_Click(object sender, EventArgs e) { Logout(); } public void Logout() { Networker.Connection.Send(1, 0); Networker.Connection.Connection.Close(); Networker.Connection = null; Connected = false; settings.Username = ""; settings.Password = ""; settings.Save(); Username = ""; Sessionkey = ""; foreach (Friend f in Friends) { if (f.ChatWindow != null) f.ChatWindow.Close(); } Friends.Clear(); foreach (Game g in Games) { g.ForceStop = true; } Games = new Game[0]; this.CurrentPlayingGame = null; FormLogin.Enabled = true; FormLogin.textUsername.Text = ""; FormLogin.textPassword.Text = ""; FormLogin.connected = false; FormLogin.Show(); Hide(); } private void addToolStripMenuItem_Click(object sender, EventArgs e) { FormAddFriend f = new FormAddFriend(this); f.ShowDialog(this); } void trayIcon_BalloonTipClicked(object sender, EventArgs e) { switch (this.PopupAction) { case 0: break; case 1: foreach(Friend f in this.Friends){ if (f.Username == trayIcon.BalloonTipTitle) { if (f.ChatWindow != null && !f.ChatWindow.IsDisposed) { f.ChatWindow.FocusTextBar(); f.ChatWindow.BringToFront(); } else { f.OpenChat(); } break; } } break; case 2: foreach(Friend f in this.Friends){ if (f.Username == trayIcon.BalloonTipTitle) { if (f.ChatWindow != null && !f.ChatWindow.IsDisposed) { f.ChatWindow.FocusTextBar(); f.ChatWindow.BringToFront(); } else { f.OpenChat(); } break; } } break; } } private void buttonChecksumTool_Click(object sender, EventArgs e) { new FormChecksum() { GameID = (short)listGames.SelectedItems[0].Tag }.Show(); } #endregion } }