aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/Online/Package_Grabber.cs
blob: addf9107c8c86db4b4c20c98daf5f4e04d1a540d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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 ShiftUI;
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<string, NetObjectClient> clients = null;

        /// <summary>
        /// Connect to a ShiftOS server
        /// </summary>
        /// <param name="address">IP address</param>
        /// <param name="port">Port (typically this is 4433.)</param>
        public static void ConnectToServer(string address, int port)
        {
            if(clients == null)
            {
                clients = new Dictionary<string, NetObjectClient>();
            }
            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<NetObject> 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 = "";

        /// <summary>
        /// Send a message to a server.
        /// </summary>
        /// <param name="host">Server hostname/IP</param>
        /// <param name="text">Messge contents.</param>
        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
            {

            }
        }

        /// <summary>
        /// Send a message to a server containing a .NET object.
        /// </summary>
        /// <param name="host">Server hostname/IP</param>
        /// <param name="text">Message text.</param>
        /// <param name="optional">The object to go with it.</param>
        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
            {

            }
        }



        /// <summary>
        /// Disconnect from a server.
        /// </summary>
        /// <param name="host">Server host.</param>
        public static void Disconnect(string host)
        {
            if(clients.ContainsKey(host))
            {
                if(clients[host].IsConnected == true)
                {
                    clients[host].Disconnect();
                }
            }
        }


        /// <summary>
        /// Download a package through spkg.
        /// </summary>
        /// <param name="pkgname">Package name</param>
        /// <returns>Could it download?</returns>
        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;

        /// <summary>
        /// Extracts the last downloaded package.
        /// </summary>
        /// <returns>The temp path</returns>
        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";
            }
        }

        /// <summary>
        /// Extract a specified .stp or .pkg file.
        /// </summary>
        /// <param name="localpath">File to extract</param>
        /// <returns>The new temp path</returns>
        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";
            }
        }

        /// <summary>
        /// Install a package from a directory
        /// </summary>
        /// <param name="dir">The package directory</param>
        /// <returns>Could it install?</returns>
        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<ModApplauncherItem>(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
    {
        /// <summary>
        /// Gets a package image from the ShiftOS website
        /// </summary>
        /// <param name="ftpFilePath">Image path relative to the packages directory</param>
        /// <returns>The image</returns>
        [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; }
    }
}