aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/Apps/AppscapeUploader.cs
blob: 8b5045056d93f71f6bcabc3e2dcab9f8470030d3 (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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using ShiftUI;

namespace ShiftOS
{
    public partial class AppscapeUploader : Form
    {
        /// <summary>
        /// Front-end to handle uploading of an Appscape package.
        /// </summary>
        /// <param name="modder">The developer of the new package.</param>
        /// <param name="repo">The client representing the repository to upload to.</param>
        public AppscapeUploader(AppscapeModder modder, AppscapeClient repo)
        {
            InitializeComponent();
            MyProfile = modder;
            Repo = repo;
        }

        private AppscapeModder MyProfile = null;
        private AppscapeClient Repo = null;

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btndone_Click(object sender, EventArgs e)
        {
            bool ContinueUpload = true;
            //Create new package and assign values
            var pkg = new AppscapePackage();
            pkg.Name = txtpackagename.Text;
            pkg.DevKey = MyProfile.DevKey;
            pkg.Description = txtpackagedescription.Text;
            if (cbsell.Checked == true)
            {
                try
                {
                    pkg.Cost = Convert.ToDecimal(Convert.ToDecimal(txtprice.Text).ToString("#.#####"));
                }
                catch
                {
                    ContinueUpload = false;
                    API.CreateInfoboxSession("Error", "You have entered an incorrect price value.", infobox.InfoboxMode.Info);
                }
            }
            else {
                pkg.Cost = 0;
            }
            pkg.SetupFile = pkg.Name.Replace(" ", "_");
            pkg.PkgIconPath = "";
            if(AppScreenshot != null)
            {
                pkg.ScreenshotPath = new FileInfo(AppScreenshot).Name.Replace(" ", "_");
            }
            else
            {
                pkg.ScreenshotPath = "none.jpg";
            }
            if (ContinueUpload == true) {
                //Upload the package meta file
                
                if (!Directory.Exists(Paths.Mod_Temp + "newapm")) {
                    Directory.CreateDirectory(Paths.Mod_Temp + "newapm");
                }
                else
                {
                    Directory.Delete(Paths.Mod_Temp + "newapm", true);
                    Directory.CreateDirectory(Paths.Mod_Temp + "newapm");
                }

                //Copy app SAA and icon to package folder
                File.Copy(SAAFile, Paths.Mod_Temp + "newapm" + OSInfo.DirectorySeparator + "app.saa");
                if (AppIcon != null)
                {
                    File.Copy(AppIcon, Paths.Mod_Temp + "newapm" + OSInfo.DirectorySeparator + "Icon.bmp");
                }
                else
                {
                    Properties.Resources.iconShiftnet.Save(Paths.Mod_Temp + "newapm" + OSInfo.DirectorySeparator + "Icon.bmp");
                }

                //Create AL meta file for package (so it will display in the app launcher)
                var al = new ModApplauncherItem();
                al.Display = true;
                al.Icon = "Icon.bmp";
                al.Name = pkg.Name;
                al.ShiftCode = "runSAA:app.saa";
                al.AppDirectory = al.Name;

                //Serialize AL meta file and copy to package
                var json = JsonConvert.SerializeObject(al);
                File.WriteAllText(Paths.Mod_Temp + "newapm" + OSInfo.DirectorySeparator + "applauncher", json);

                //Package file to .stp and upload to Appscape
                ZipFile.CreateFromDirectory(Paths.Mod_Temp + "newapm", Paths.Mod_Temp + pkg.Name.Replace(" ", "_") + ".stp");
                Repo.UploadPackage(pkg, File.ReadAllBytes(Paths.Mod_Temp + pkg.Name.Replace(" ", "_") + ".stp"));



                this.Close();
            }
        }

        
        int page = 0;

        /// <summary>
        /// Makes the specified control and ALL of it's children, grand children, great grand children, and so on, appear "dark".
        /// </summary>
        /// <param name="ctrl">The top of the hierarchy; the start of it all.</param>
        private void RecursiveWidgetSetup(Widget ctrl)
        {
            ctrl.BackColor = API.CurrentSkin.titlebarcolour;
            ctrl.ForeColor = API.CurrentSkin.titletextcolour;
            try
            {
                var pnl = (Panel)ctrl;
                foreach(Widget c in pnl.Widgets)
                {
                    RecursiveWidgetSetup(c);
                }
            }
            catch {

            }
        }

        /// <summary>
        /// Sets up the user interface.
        /// </summary>
        private void SetupUI()
        {
            RecursiveWidgetSetup(pnlmain);
            switch (page)
            {
                case 0:
                    lbtitle.Text = "Welcome!";
                    lbldescription.Text = "This wizard will guide you through the creation of an Appscape Package which will be uploaded to Appscape for all to see. First, let's start with some basics.";
                    Page0.BringToFront();
                    btndone.Hide();
                    btnback.Hide();
                    break;
                case 1:
                    lbtitle.Text = "Packaged Files";
                    lbldescription.Text = "Now that we have a name and description, it's time to get some files into your package. Namely, an icon and the app itself.";
                    btnback.Show();
                    Page1.BringToFront();
                    break;
                case 2:
                    lbtitle.Text = "Ready!";
                    lbldescription.Text = "That's all we need from you. Assuming everything is correct, as soon as you click 'Done!', the package will be generated and uploaded to Appscape quicker than you can say \"Appscape Package Manager is so freakin' awesome.\".";
                    btnnext.Hide();
                    btndone.Show();
                    Page2.BringToFront();
                    txtconfirm.Font = new Font(OSInfo.GetMonospaceFont(), 9);
                    ConfirmPKG();
                    break;
            }
        }

        /// <summary>
        /// Makes sure the package is ready for upload.
        /// </summary>
        private void ConfirmPKG()
        {
            txtconfirm.Text = "";
            WriteLine(" == Basics == ");
            WriteLine($"Name: {txtpackagename.Text}");
            WriteLine($"Description: {txtpackagedescription.Text}");
            WriteLine("");
            WriteLine(" == Files == ");
            if(SAAFile != null)
            {
                WriteLine(SAAFile);
            }
            else
            {
                WriteLine("no saa file");
            }
            WriteLine(AppIcon);
            WriteLine(AppScreenshot);
            WriteLine("");
            WriteLine(" == Package Check == ");
            CheckForErrors();
            foreach(string err in Errors)
            {
                WriteLine(" - " + err);
                btndone.Hide();
            }
            if(Errors.Count > 0)
            {
                WriteLine("Please go back and correct these errors.");
            }
        }

        private List<string> Errors = null;

        /// <summary>
        /// Checks for package errors.
        /// </summary>
        private void CheckForErrors()
        {
            Errors = new List<string>();
            if(txtpackagename.Text.Length < 1)
            {
                Errors.Add("Your package doesn't have a name.");
            }
            if(SAAFile == null)
            {
                Errors.Add("Your package won't contain a .saa file, therefore it won't be installed by ShiftOS!");
            }
        }

        /// <summary>
        /// Writes a line to the summary box.
        /// </summary>
        /// <param name="text">Text to write.</param>
        private void WriteLine(string text)
        {
            if(txtconfirm.Text == "")
            {
                txtconfirm.Text = text;
            }
            else
            {
                txtconfirm.Text += Environment.NewLine + text;
            }
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            page += 1;
            SetupUI();
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            page -= 1;
            SetupUI();
        }

        private void AppscapeUploader_Load(object sender, EventArgs e)
        {
            SetupUI();
        }

        private string SAAFile = null;
        private string AppIcon = null;
        private string AppScreenshot = null;

        private void btnsaa_Click(object sender, EventArgs e)
        {
            API.CreateFileSkimmerSession(".saa", File_Skimmer.FileSkimmerMode.Open);
            API.FileSkimmerSession.FormClosing += (object s, FormClosingEventArgs a) =>
            {
                var res = API.GetFSResult();
                if(res != "fail")
                {
                    SAAFile = res;
                    btnsaa.Text = new FileInfo(SAAFile).Name;
                }
            };
        }

        private void btnicon_Click(object sender, EventArgs e)
        {
            API.CreateFileSkimmerSession(".bmp;.png;.jpg;.pic", File_Skimmer.FileSkimmerMode.Open);
            API.FileSkimmerSession.FormClosing += (object s, FormClosingEventArgs a) =>
            {
                var res = API.GetFSResult();
                if (res != "fail")
                {
                    AppIcon = res;
                    btnicon.Text = new FileInfo(res).Name;
                }
            };
        }

        private void btnscreenshot_Click(object sender, EventArgs e)
        {
            API.CreateFileSkimmerSession(".bmp;.png;.jpg;.pic", File_Skimmer.FileSkimmerMode.Open);
            API.FileSkimmerSession.FormClosing += (object s, FormClosingEventArgs a) =>
            {
                var res = API.GetFSResult();
                if (res != "fail")
                {
                    AppScreenshot = res;
                    btnscreenshot.Text = new FileInfo(res).Name;
                }
            };
        }

        private void btncompilesaa_Click(object sender, EventArgs e)
        {
            //Source code folder finder
            API.CreateFileSkimmerSession("dir", File_Skimmer.FileSkimmerMode.OpenFolder);
            API.FileSkimmerSession.FormClosing += (object s, FormClosingEventArgs a) =>
            {
                var res = API.GetFSResult();
                if(res != "fail")
                {
                    if(File.Exists(res + OSInfo.DirectorySeparator + "main.lua")) {
                        string dir = res;
                        API.CreateFileSkimmerSession(".saa", File_Skimmer.FileSkimmerMode.Open);
                        API.FileSkimmerSession.FormClosing += (object s2, FormClosingEventArgs a2) =>
                        {
                            try
                            {
                                ZipFile.CreateFromDirectory(dir, API.GetFSResult());
                                API.CreateInfoboxSession("Success", "The SAA file has been created successfully.", infobox.InfoboxMode.Info);
                            }
                            catch
                            {
                                API.CreateInfoboxSession("Error", "Could not create the SAA file.", infobox.InfoboxMode.Info);
                            }
                        };
                    }
                    else {
                        API.CreateInfoboxSession("Invalid source", "Your source directory doesn't contain a main script (main.lua) file.", infobox.InfoboxMode.Info);
                    }
                }
            };
        }

        private void cbsell_CheckedChanged(object sender, EventArgs e)
        {
            lbprice.Visible = cbsell.Checked;
            lbaddress.Visible = cbsell.Checked;
            txtprice.Visible = cbsell.Checked;
        }

        private void lbprice_Click(object sender, EventArgs e)
        {

        }

        private void txtprice_TextChanged(object sender, EventArgs e)
        {

        }

        private void lbaddress_Click(object sender, EventArgs e)
        {

        }

        private void txtpackagedescription_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtpackagename_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}