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(); } 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(Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } } 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 (WebException wex) { 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 (Exception ex) { 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; } } }