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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
using System.Threading;
using static ShiftOS.Objects.ShiftFS.Utils;
using Newtonsoft.Json;
namespace ShiftOS.WinForms.Applications
{
[WinOpen("{WO_INSTALLER}")]
[RequiresUpgrade("installer")]
[MultiplayerOnly]
[DefaultTitle("{TITLE_INSTALLER}")]
[Launcher("{TITLE_INSTALLER}", true, "al_installer", "{AL_UTILITIES}")]
[FileHandler("Name Pack", ".names", "fileiconnames")]
public partial class Installer : UserControl, IShiftOSWindow, IFileHandler
{
public Installer()
{
InitializeComponent();
lbtitle.Text = "Select file";
}
public void OpenFile(string path)
{
var stpInstall = new StpInstallation(path);
AppearanceManager.SetupWindow(this);
InitiateInstall(stpInstall);
}
public void InitiateInstall(Installation install)
{
pnlselectfile.Hide();
pginstall.Show();
lbprogress.Show();
lbtitle.Text = install.Name;
install.ProgressReported += (p) =>
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
pginstall.Value = p;
}));
};
install.StatusReported += (s) =>
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
lbprogress.Text = s;
}));
};
install.InstallCompleted += () =>
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
lbtitle.Text = "Select file";
pnlselectfile.Show();
}));
isInstalling = false;
InstallCompleted?.Invoke();
};
while (!this.Visible)
{
}
isInstalling = true;
install.Install();
}
public void OnLoad()
{
btnstart.Hide();
pginstall.Hide();
lbprogress.Hide();
}
private bool isInstalling = false;
public void OnSkinLoad()
{
}
public bool OnUnload()
{
return !isInstalling; //Don't close if an install is running.
}
public void OnUpgrade()
{
}
private void pnlselectfile_VisibleChanged(object sender, EventArgs e)
{
if(this.ParentForm != null)
{
this.ParentForm.Height = (pnlselectfile.Visible == true) ? this.ParentForm.Height + pnlselectfile.Height : this.ParentForm.Height - pnlselectfile.Height;
}
}
public event Action InstallCompleted;
private void btnbrowse_Click(object sender, EventArgs e)
{
FileSkimmerBackend.GetFile(new[] { ".stp" }, FileOpenerStyle.Open, (file) =>
{
txtfilepath.Text = file;
btnstart.Show();
});
}
private void btnstart_Click(object sender, EventArgs e)
{
if (FileExists(txtfilepath.Text))
{
var install = new StpInstallation(txtfilepath.Text);
InitiateInstall(install);
}
else
{
Infobox.Show("File not found.", "The file you requested was not found on the system.");
}
}
}
public abstract class Installation
{
/// <summary>
/// The display name of the installation.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Occurs when the installation updates its status.
/// </summary>
public event Action<string> StatusReported;
/// <summary>
/// Occurs when the installation updates its progress percentage.
/// </summary>
public event Action<int> ProgressReported;
/// <summary>
/// Occurs when the installation completes.
/// </summary>
public event Action InstallCompleted;
/// <summary>
/// Start the installation.
/// </summary>
public void Install()
{
var t = new System.Threading.Thread(() =>
{
ProgressReported?.Invoke(0);
StatusReported?.Invoke("");
Run();
ProgressReported?.Invoke(100);
StatusReported?.Invoke("Installation completed.");
InstallCompleted?.Invoke();
});
t.IsBackground = true;
t.Start();
}
/// <summary>
/// Sets the install progress percentage.
/// </summary>
/// <param name="value">The installation percentage.</param>
protected void SetProgress(int value)
{
if (value < 0 || value > 100)
throw new ArgumentOutOfRangeException("value", "A percentage is typically between 0 and 100.... derp...");
ProgressReported?.Invoke(value);
}
/// <summary>
/// Sets the install status text.
/// </summary>
/// <param name="status">Text to display as status.</param>
protected void SetStatus(string status)
{
StatusReported?.Invoke(status);
}
/// <summary>
/// User-defined code to run during install. Once this code is ran, the installation is complete.
/// </summary>
protected abstract void Run();
}
public class StpInstallation : Installation
{
public StpInstallation(string stpfile) : base()
{
if (!FileExists(stpfile))
throw new System.IO.FileNotFoundException("An attempt to install a ShiftOS setup package failed because the package was not found.", stpfile);
string json = ReadAllText(stpfile);
var contents = JsonConvert.DeserializeObject<StpContents>(json);
this.Name = contents.Name;
Contents = contents;
}
public StpContents Contents { get; set; }
protected override void Run()
{
if (Shiftorium.UpgradeInstalled(Contents.Upgrade))
{
Infobox.Show("Installation failed.", "This package has already been installed.");
return;
}
if (!string.IsNullOrWhiteSpace(Contents.Dependencies))
{
SetStatus("Checking Shiftorium for dependencies...");
string[] dependencies = Contents.Dependencies.Split(';');
for (int i = 0; i < dependencies.Length; i++)
{
if (Shiftorium.UpgradeInstalled(dependencies[i]))
{
double percent = (i / dependencies.Length) * 100;
SetProgress((int)percent);
}
else
{
var upg = Shiftorium.GetDefaults().FirstOrDefault(x => x.Id == dependencies[i]);
Infobox.Show("Missing dependency!", $"You are missing the following Shiftorium Upgrade: {upg.Name}\r\n\r\nThe installation cannot continue.");
return;
}
Thread.Sleep(250);
}
}
SetStatus("Installing...");
SetProgress(0);
for(int i = 0; i < 100; i++)
{
SetProgress(i);
Thread.Sleep(50);
}
Desktop.InvokeOnWorkerThread(() =>
{
Shiftorium.Buy(Contents.Upgrade, 0);
Infobox.Show("Install complete.", "The installation has completed successfully.");
SaveSystem.SaveGame();
});
}
}
public class StpContents : RequiresUpgradeAttribute
{
public StpContents(string name, string author, string dependencies = "") : base(name.ToLower().Replace(" ", "_"))
{
Name = name;
Author = author;
Dependencies = dependencies;
}
public string Name { get; set; }
public string Author { get; set; }
public string Dependencies { get; set; }
}
}
|