From 84f689b91a73e512b035df40bbcf556b008a3b81 Mon Sep 17 00:00:00 2001 From: MichaelTheShifter Date: Sat, 25 Jun 2016 08:10:03 -0400 Subject: Sort source code into folders. It feels better to know what's responsible for what... Plus I removed some un-needed C# stuff. --- .../Online/ConnectionManager.Designer.cs | 129 +++++++ .../Online/ConnectionManager.cs | 91 +++++ .../Online/ConnectionManager.resx | 120 ++++++ .../Online/Package_Grabber.cs | 402 +++++++++++++++++++++ 4 files changed, 742 insertions(+) create mode 100644 source/WindowsFormsApplication1/Online/ConnectionManager.Designer.cs create mode 100644 source/WindowsFormsApplication1/Online/ConnectionManager.cs create mode 100644 source/WindowsFormsApplication1/Online/ConnectionManager.resx create mode 100644 source/WindowsFormsApplication1/Online/Package_Grabber.cs (limited to 'source/WindowsFormsApplication1/Online') diff --git a/source/WindowsFormsApplication1/Online/ConnectionManager.Designer.cs b/source/WindowsFormsApplication1/Online/ConnectionManager.Designer.cs new file mode 100644 index 0000000..89abcca --- /dev/null +++ b/source/WindowsFormsApplication1/Online/ConnectionManager.Designer.cs @@ -0,0 +1,129 @@ +namespace ShiftOS +{ + partial class ConnectionManager + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.panel1 = new System.Windows.Forms.Panel(); + this.lbconnections = new System.Windows.Forms.ListBox(); + this.flbuttons = new System.Windows.Forms.FlowLayoutPanel(); + this.btnadd = new System.Windows.Forms.Button(); + this.btnconnect = new System.Windows.Forms.Button(); + this.btndisconnect = new System.Windows.Forms.Button(); + this.panel1.SuspendLayout(); + this.flbuttons.SuspendLayout(); + this.SuspendLayout(); + // + // panel1 + // + this.panel1.Controls.Add(this.flbuttons); + this.panel1.Controls.Add(this.lbconnections); + this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel1.Location = new System.Drawing.Point(0, 0); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(684, 531); + this.panel1.TabIndex = 0; + // + // lbconnections + // + this.lbconnections.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lbconnections.FormattingEnabled = true; + this.lbconnections.Location = new System.Drawing.Point(13, 13); + this.lbconnections.Name = "lbconnections"; + this.lbconnections.Size = new System.Drawing.Size(659, 472); + this.lbconnections.TabIndex = 0; + this.lbconnections.SelectedIndexChanged += new System.EventHandler(this.lbconnections_SelectedIndexChanged); + // + // flbuttons + // + this.flbuttons.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.flbuttons.Controls.Add(this.btnadd); + this.flbuttons.Controls.Add(this.btnconnect); + this.flbuttons.Controls.Add(this.btndisconnect); + this.flbuttons.Location = new System.Drawing.Point(13, 489); + this.flbuttons.Name = "flbuttons"; + this.flbuttons.Size = new System.Drawing.Size(659, 30); + this.flbuttons.TabIndex = 1; + // + // btnadd + // + this.btnadd.Location = new System.Drawing.Point(3, 3); + this.btnadd.Name = "btnadd"; + this.btnadd.Size = new System.Drawing.Size(75, 23); + this.btnadd.TabIndex = 0; + this.btnadd.Text = "Add"; + this.btnadd.UseVisualStyleBackColor = true; + this.btnadd.Click += new System.EventHandler(this.btnadd_Click); + // + // btnconnect + // + this.btnconnect.Location = new System.Drawing.Point(84, 3); + this.btnconnect.Name = "btnconnect"; + this.btnconnect.Size = new System.Drawing.Size(75, 23); + this.btnconnect.TabIndex = 1; + this.btnconnect.Text = "Connect"; + this.btnconnect.UseVisualStyleBackColor = true; + this.btnconnect.Click += new System.EventHandler(this.btnconnect_Click); + // + // btndisconnect + // + this.btndisconnect.Location = new System.Drawing.Point(165, 3); + this.btndisconnect.Name = "btndisconnect"; + this.btndisconnect.Size = new System.Drawing.Size(75, 23); + this.btndisconnect.TabIndex = 2; + this.btndisconnect.Text = "Disconnect"; + this.btndisconnect.UseVisualStyleBackColor = true; + this.btndisconnect.Click += new System.EventHandler(this.btndisconnect_Click); + // + // ConnectionManager + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(684, 531); + this.Controls.Add(this.panel1); + this.Name = "ConnectionManager"; + this.Text = "ConnectionManager"; + this.Load += new System.EventHandler(this.ConnectionManager_Load); + this.panel1.ResumeLayout(false); + this.flbuttons.ResumeLayout(false); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.FlowLayoutPanel flbuttons; + private System.Windows.Forms.Button btnadd; + private System.Windows.Forms.Button btnconnect; + private System.Windows.Forms.Button btndisconnect; + private System.Windows.Forms.ListBox lbconnections; + } +} \ No newline at end of file diff --git a/source/WindowsFormsApplication1/Online/ConnectionManager.cs b/source/WindowsFormsApplication1/Online/ConnectionManager.cs new file mode 100644 index 0000000..eaf2a24 --- /dev/null +++ b/source/WindowsFormsApplication1/Online/ConnectionManager.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ShiftOS +{ + public partial class ConnectionManager : Form + { + public ConnectionManager() + { + InitializeComponent(); + } + + string selectednet = null; + + private void lbconnections_SelectedIndexChanged(object sender, EventArgs e) + { + try + { + selectednet = lbconnections.SelectedItem.ToString(); + SetupUI(); + } + catch + { + selectednet = null; + SetupUI(); + } + } + + public void SetupUI() + { + if(selectednet != null) + { + btnconnect.Visible = !Package_Grabber.clients[selectednet].IsConnected; + btndisconnect.Visible = Package_Grabber.clients[selectednet].IsConnected; + } + else + { + btnconnect.Hide(); + btndisconnect.Hide(); + } + } + + private void ConnectionManager_Load(object sender, EventArgs e) + { + SetupUI(); + var t = new Timer(); + t.Interval = 500; + t.Tick += (object s, EventArgs a) => + { + lbconnections.Items.Clear(); + foreach(var itm in Package_Grabber.clients) + { + lbconnections.Items.Add(itm.Key); + } + }; + t.Start(); + } + + private void btndisconnect_Click(object sender, EventArgs e) + { + Package_Grabber.Disconnect(selectednet); + SetupUI(); + } + + private void btnconnect_Click(object sender, EventArgs e) + { + Package_Grabber.ConnectToServer(selectednet, 7429); + SetupUI(); + } + + private void btnadd_Click(object sender, EventArgs e) + { + API.CreateInfoboxSession("Add connection", "Please type the IP Address or hostname of the server.", infobox.InfoboxMode.TextEntry); + API.InfoboxSession.FormClosing += (o, a) => + { + var res = API.GetInfoboxResult(); + if(res != "Cancelled") + { + Package_Grabber.ConnectToServer(res, 7429); + } + }; + } + } +} diff --git a/source/WindowsFormsApplication1/Online/ConnectionManager.resx b/source/WindowsFormsApplication1/Online/ConnectionManager.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/source/WindowsFormsApplication1/Online/ConnectionManager.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/source/WindowsFormsApplication1/Online/Package_Grabber.cs b/source/WindowsFormsApplication1/Online/Package_Grabber.cs new file mode 100644 index 0000000..b6bbc61 --- /dev/null +++ b/source/WindowsFormsApplication1/Online/Package_Grabber.cs @@ -0,0 +1,402 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.IO.Compression; +using Newtonsoft.Json; +using System.Windows.Forms; +using System.Threading; +using System.Drawing; +using NetSockets; + +namespace ShiftOS +{ + [Serializable] + public class ObjectModel + { + public string SysId { get; set; } + public string Command { get; set; } + public object OptionalObject { get; set; } + } + + [Serializable] + public class ChatUser + { + public string Name { get; set; } + public event EventHandler OnJoin; + public event EventHandler OnLeave; + } + + [Serializable] + public class ChatMessage + { + public string Text { get; set; } + public ChatUser User { get; set; } + } + + class Package_Grabber + { + + + public static Dictionary clients = null; + + /// + /// Connect to a ShiftOS server + /// + /// IP address + /// Port (typically this is 4433.) + public static void ConnectToServer(string address, int port) + { + if(clients == null) + { + clients = new Dictionary(); + } + bool blacklisted = false; + string blacklist = new WebClient().DownloadString("http://playshiftos.ml/server/blacklist"); + string[] splitter = blacklist.Split(';'); + foreach (string addr in splitter) + { + try + { + string[] addSplitter = addr.Split(':'); + string host = addSplitter[0]; + int prt = Convert.ToInt32(addSplitter[1]); + if(address == host && port == prt) + { + blacklisted = true; + } + } + catch + { + + } + } + if (!blacklisted) + { + var client = new NetObjectClient(); + client.OnReceived += (object s, NetReceivedEventArgs a) => + { + try + { + var obj = (ObjectModel)a.Data.Object; + if (obj.Command == "set_ident") + { + this_id = obj.SysId; + } + } + catch + { + + } + }; + + try + { + client.Connect(address, port); + clients.Add(client.RemoteHost, client); + } + catch + { + } + } + else + { + API.CreateInfoboxSession("Server Connection Error", "The server you are trying to connect to has been blacklisted for breaking the rules of the Server Showcase, therefore you may not connect to it.", infobox.InfoboxMode.Info); + } + } + + private static string this_id = ""; + + /// + /// Send a message to a server. + /// + /// Server hostname/IP + /// Messge contents. + public static void SendMessage(string host, string text) + { + var obj = new ObjectModel(); + obj.SysId = this_id; + obj.Command = text; + try { + clients[host].Send(new NetObject("test", obj)); + } + catch + { + + } + } + + /// + /// Send a message to a server containing a .NET object. + /// + /// Server hostname/IP + /// Message text. + /// The object to go with it. + public static void SendMessage(string host, string text, object optional) + { + var obj = new ObjectModel(); + obj.SysId = this_id; + obj.Command = text; + obj.OptionalObject = optional; + try { + clients[host].Send(obj.SysId, obj); + } + catch + { + + } + } + + + + /// + /// Disconnect from a server. + /// + /// Server host. + public static void Disconnect(string host) + { + if(clients.ContainsKey(host)) + { + if(clients[host].IsConnected == true) + { + clients[host].Disconnect(); + } + } + } + + + /// + /// Download a package through spkg. + /// + /// Package name + /// Could it download? + public static bool GetPackage(string pkgname) + { + bool result = true; + try + { + string downloadpath = Paths.Mod_Temp; + if (!Directory.Exists(downloadpath)) + { + Directory.CreateDirectory(downloadpath); + } else + { + Directory.Delete(downloadpath, true); + Directory.CreateDirectory(downloadpath); + } + WebClient wc = new WebClient(); + wc.DownloadFile("http://playshiftos.ml/shiftnet/packages/" + pkgname + ".pkg", downloadpath + pkgname + ".pkg"); + LastPackage_DownloadPath = downloadpath + pkgname + ".pkg"; + } + catch + { + result = false; + } + return result; + } + + public static string LastPackage_DownloadPath = null; + + /// + /// Extracts the last downloaded package. + /// + /// The temp path + public static string ExtractPackage() + { + if (LastPackage_DownloadPath != null) + { + string packagedir = null; + if (LastPackage_DownloadPath.EndsWith(".pkg")) + { + packagedir = LastPackage_DownloadPath.Replace(".pkg", ""); + } + else + { + packagedir = LastPackage_DownloadPath.Replace(".stp", ""); + } + if (Directory.Exists(packagedir)) + { + Directory.Delete(packagedir, true); + } + ZipFile.ExtractToDirectory(LastPackage_DownloadPath, packagedir); + return packagedir; + } + else + { + return "fail"; + } + } + + /// + /// Extract a specified .stp or .pkg file. + /// + /// File to extract + /// The new temp path + public static string ExtractPackage(string localpath) + { + try { + + string packagedir = Paths.Mod_Temp + "pkg"; + if (Directory.Exists(packagedir)) + { + Directory.Delete(packagedir, true); + } + Directory.CreateDirectory(packagedir); + ZipFile.ExtractToDirectory(localpath, packagedir); + return packagedir; + } + catch + { + return "fail"; + } + } + + /// + /// Install a package from a directory + /// + /// The package directory + /// Could it install? + public static string InstallPackage(string dir) + { + try + { + string dirsepchar = "\\"; + switch (OSInfo.GetPlatformID()) + { + case "microsoft": + dirsepchar = "\\"; + break; + default: + dirsepchar = "/"; + break; + } + string alfile = null; + foreach (string file in Directory.GetFiles(dir)) + { + if (file.Contains("applauncher")) + { + alfile = file; + } + } + string json = File.ReadAllText(alfile); + if (!Directory.Exists(Paths.Mod_AppLauncherEntries)) + { + Directory.CreateDirectory(Paths.Mod_AppLauncherEntries); + } + ModApplauncherItem itm = JsonConvert.DeserializeObject(json); + File.WriteAllText(Paths.Mod_AppLauncherEntries + itm.Name, json); + //Applauncher Entry installed! + if (!Directory.Exists(Paths.Applications + itm.AppDirectory)) + { + Directory.CreateDirectory(Paths.Applications + itm.AppDirectory); + } + + Thread.Sleep(200); + if (!File.Exists(Paths.Applications + itm.AppDirectory + dirsepchar + "Icon.bmp")) + { + File.Copy(dir + "Icon.bmp", Paths.Applications + itm.AppDirectory + dirsepchar + "Icon.bmp"); + } + if (File.Exists(Paths.Applications + itm.AppDirectory + dirsepchar + "app.saa")) + { + File.Delete(Paths.Applications + itm.AppDirectory + dirsepchar + "app.saa"); + } + File.Move(dir + "app.saa", Paths.Applications + itm.AppDirectory + dirsepchar + "app.saa"); + //App installed. + foreach (string file in Directory.GetFiles(dir)) + { + if (file.EndsWith(".dll")) + { + if (!File.Exists(Paths.Applications + itm.AppDirectory + dirsepchar + new FileInfo(file).Name)) + { + //Dependencies are fucking bitches. + File.Copy(file, Paths.Applications + itm.AppDirectory + dirsepchar + new FileInfo(file).Name); + } + } + } + //Dependencies installed. + API.CurrentSession.SetupAppLauncher(); + return "success"; + } + catch (Exception ex) + { + return ex.Message; + } + } + } + + class FTP_API + { + /// + /// Gets a package image from the ShiftOS website + /// + /// Image path relative to the packages directory + /// The image + [Obsolete] + public static Image GetImage(string ftpFilePath) + { + var request = WebRequest.Create("http://playshiftos.ml/appscape/packages/" + ftpFilePath); + + using (var response = request.GetResponse()) + { + using (var str = response.GetResponseStream()) + { + return Image.FromStream(str); + } + } + } + + [Obsolete] + public static Image GetSkinImage(string ftpFilePath) + { + var request = WebRequest.Create("http://playshiftos.ml/appscape/skins/" + ftpFilePath); + + using (var response = request.GetResponse()) + { + using (var str = response.GetResponseStream()) + { + return Image.FromStream(str); + } + } + } + } + + + [Serializable] + public class AppscapeModder + { + public string DevKey { get; set; } + public string Name { get; set; } + public string Bio { get; set; } + public string BitnoteAddress { get; set; } + } + + [Serializable] + public class AppscapePackage + { + public string Name { get; set; } + public string Address { get; set; } + public string DevKey { get; set; } + public string Description { get; set; } + public string SetupFile { get; set; } + public string PkgIconPath { get; set; } + public string ScreenshotPath { get; set; } + public decimal Cost { get; set; } + public string Server { get; set; } + } + + [Serializable] + public class SkinData + { + public string Name { get; set; } + public string Author { get; set; } + public string Description { get; set; } + public string Download { get; set; } + public string Icon { get; set; } + public string Screenshot { get; set; } + public decimal Cost { get; set; } + public string Server { get; set; } + } +} -- cgit v1.2.3