aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/Apps/Installer.cs
blob: 816e9b549e3aebd674ea888ed78b7fc327dd0867 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;
using ShiftOS.Frontend.GUI;
using Newtonsoft.Json;
using static ShiftOS.Objects.ShiftFS.Utils;
using Microsoft.Xna.Framework;

namespace ShiftOS.Frontend.Apps
{
    [DefaultTitle("Installer")]
    [FileHandler("Setup file", ".stp", "")]
    public class Installer : Control, IShiftOSWindow, IFileHandler
    {
        private SetupFile _setup = null;
        private TextControl _header = null;
        private TextControl _body = null;
        private Button _cancel = null;
        private Button _install = null;

        public Installer()
        {
            Width = 600;
            Height = 400;
            _header = new GUI.TextControl();
            _body = new GUI.TextControl();
            _cancel = new Button();
            _install = new Button();

            _install.Text = "Install";
            _cancel.Text = "Close";

            _install.AutoSize = true;
            _cancel.AutoSize = true;

            AddControl(_header);
            AddControl(_body);
            AddControl(_install);
            AddControl(_cancel);

            _install.Click += () =>
            {
                Install();
                
            };
            _cancel.Click += () =>
            {
                AppearanceManager.Close(this);
            };

        }

        public void Install()
        {
            switch (_setup.SourceType)
            {
                case SetupSource.ShiftoriumUpgrade:
                    if (Shiftorium.UpgradeInstalled(_setup.Source))
                    {
                        Engine.Infobox.Show("Upgrade installed.", "This upgrade has already been installed either through the Shiftorium or through another setup file.");
                        return;
                    }
                    Shiftorium.Buy(_setup.Source, 0);
                    Engine.Infobox.Show("Upgrade installed.", "The upgrade \"" + _setup.Source + "\" has been installed and is now ready to be used!");

                    break;
                case SetupSource.CowFile:
                    string cow = _setup.Source;
                    string[] split = cow.Split('\t');
                    string fname = split[0] + ".cow";
                    string ascii = split[1];
                    string csCowfiles = Paths.GetPath("data") + "/cows";
                    if (!DirectoryExists(csCowfiles))
                        CreateDirectory(csCowfiles);
                    WriteAllText(csCowfiles + "/" + fname, ascii);
                    Engine.Infobox.Show("Cowsay", "New cowfile installed! Have fun with your talking " + split[0] + "!");
                    break;
            }
        }


        protected override void OnLayout(GameTime gameTime)
        {
            _header.X = 10;
            _header.Y = 10;
            _header.Font = SkinEngine.LoadedSkin.Header3Font;
            _header.AutoSize = true;

            _body.X = 10;
            _body.Y = _header.Y + _header.Height + 5;
            _body.Width = Width - 20;

            _cancel.X = Width - _cancel.Width - 10;
            _cancel.Y = Height - _cancel.Height - 10;
            _body.Height = (_cancel.Y - _body.Y);
            _install.Y = _cancel.Y;
            _install.X = _cancel.X - _install.Width - 5;
        }


        public void OpenFile(string file)
        {
            _setup = JsonConvert.DeserializeObject<SetupFile>(ReadAllText(file));
            AppearanceManager.SetupDialog(this);
        }

        public void OnLoad()
        {
            _header.Text = _setup.Name;
            _body.Text = _setup.Description;
        }

        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            return true;
        }

        public void OnUpgrade()
        {
        }
    }

    public class SetupFile
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public SetupSource SourceType { get; set; }
        public string Source { get; set; }
    }

    public enum SetupSource
    {
        ShiftoriumUpgrade,
        CowFile
    }
}