-
Notifications
You must be signed in to change notification settings - Fork 7
/
launcher.ps1
270 lines (227 loc) · 8.3 KB
/
launcher.ps1
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
# Launcher
# --------
#
# Author: Henrik Noerfjand Stengaard
# Date: 2017-09-18
#
# A powershell script to launch HstWB Installer and ensure HstWB Installer for first time use.
Param(
[Parameter(Mandatory=$false)]
[string]$settingsDir
)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Import-Module (Resolve-Path('modules\version.psm1')) -Force
function MessageDialog($title, $message)
{
$buttons = [System.Windows.Forms.MessageBoxButtons]::OK
$icon = [System.Windows.Forms.MessageBoxIcon]::Information
[void][System.Windows.Forms.MessageBox]::Show($message, $title, $buttons, $icon)
}
# confirm dialog
function ConfirmDialog($title, $message)
{
$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
$icon = [System.Windows.Forms.MessageBoxIcon]::Question
$result = [System.Windows.Forms.MessageBox]::Show($message, $title, $buttons, $icon)
if($result -eq 'Yes')
{
return $true
}
return $false
}
function Run($runFile, $settingsDir)
{
try
{
& $runFile -settingsDir $settingsDir
}
catch
{
$errorFormatingString = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
$errorFields = $_.InvocationInfo.MyCommand.Name,
$_.ErrorDetails.Message,
$_.InvocationInfo.PositionMessage,
$_.CategoryInfo.ToString(),
$_.FullyQualifiedErrorId
$message = $errorFormatingString -f $errorFields
$logFile = Join-Path $settingsDir -ChildPath "hstwb_installer.log"
Add-Content $logFile ("{0} | ERROR | {1}" -f (Get-Date -Format s), $message) -Encoding UTF8
Write-Host ""
Write-Error "HstWB Installer Run Failed: $message"
Write-Host ""
Write-Host "Press enter to continue"
Read-Host
}
Clear-Host
}
function Setup($setupFile, $settingsDir)
{
try
{
& $setupFile -settingsDir $settingsDir
}
catch
{
$errorFormatingString = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
$errorFields = $_.InvocationInfo.MyCommand.Name,
$_.ErrorDetails.Message,
$_.InvocationInfo.PositionMessage,
$_.CategoryInfo.ToString(),
$_.FullyQualifiedErrorId
$message = $errorFormatingString -f $errorFields
$logFile = Join-Path $settingsDir -ChildPath "hstwb_installer.log"
Add-Content $logFile ("{0} | ERROR | {1}" -f (Get-Date -Format s), $message) -Encoding UTF8
Write-Host ""
Write-Error "HstWB Installer Setup Failed: $message"
Write-Host ""
Write-Host "Press enter to continue"
Read-Host
}
Clear-Host
}
function Settings($settingsFile)
{
Start-Process "Notepad.exe" "$settingsFile" -Wait
}
function Assigns($assignsFile)
{
Start-Process "Notepad.exe" "$assignsFile" -Wait
}
function GuiMenu($title, $options)
{
$hash = [hashtable]::Synchronized(@{})
$hash.option = $null
$pfc = New-Object System.Drawing.Text.PrivateFontCollection
$pfc.AddFontFile($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('fonts\TopazPlus_a1200_v1.0.ttf'))
$buttonFont = New-Object System.Drawing.Font($pfc.Families[0],14)
$blueColor = [System.Drawing.Color]::FromArgb(0, 85, 170)
$width = 300;
$form = New-Object System.Windows.Forms.Form
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = 'None'
$form.MinimizeBox = $False
$form.MaximizeBox = $False
$form.WindowState = "Normal"
$form.SizeGripStyle = "Hide"
$form.Icon = New-Object System.Drawing.Icon ($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('hstwb_installer.ico'))
$form.ClientSize = New-Object System.Drawing.Size ($width, (100 + ($options.Count * 70)))
$form.BackColor = $blueColor
$captionLabel = New-Object System.Windows.Forms.Label
$captionLabel.AutoSize = $false
$captionLabel.TextAlign = 'MiddleCenter'
$captionLabel.Size = New-Object System.Drawing.Size ($width, 30)
$captionLabel.Location = New-Object System.Drawing.Point(0, 0)
$captionLabel.Text = "HstWB Installer v{0}" -f (HstwbInstallerVersion)
$captionLabel.Font = $buttonFont
$captionLabel.ForeColor = $blueColor
$captionLabel.BackColor = "White"
$captionLabel.Anchor = "Left", "Top", "Right"
$form.Controls.Add($captionLabel)
$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.AutoSize = $false
$titleLabel.TextAlign = 'MiddleCenter'
$titleLabel.Location = New-Object System.Drawing.Point(0, 40)
$titleLabel.Text = $title
$titleLabel.Size = New-Object System.Drawing.Size ($width, 50)
$titleLabel.Font = $buttonFont
$titleLabel.ForeColor = "White"
$titleLabel.Anchor = "Left", "Top", "Right"
$form.Controls.Add($titleLabel)
$positionY = 100
foreach ($option in $options)
{
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(20, $positionY)
$button.Text = $option
$button.Size = New-Object System.Drawing.Size (($width - 40), 50)
$button.Font = $buttonFont
$button.ForeColor = $blueColor
$button.BackColor = "White"
$button.Anchor = "Left", "Top", "Right"
$button.Add_Click({
$form.Close()
$hash.option = $option
}.GetNewClosure())
$button.Add_GotFocus({
$button.BackColor = [System.Drawing.Color]::FromArgb(210, 210, 210);
}.GetNewClosure())
$button.Add_LostFocus({
$button.BackColor = "White";
}.GetNewClosure())
$form.Controls.Add($button)
$positionY += 70
}
[void]$form.ShowDialog()
return $hash.option
}
function LauncherMenu($hstwb)
{
do
{
$option = GuiMenu "Launcher" @('Setup', 'Advanced', 'Extra', 'Help', 'Exit')
switch ($option)
{
"Setup" { Setup $hstwb.Paths.SetupFile $hstwb.Paths.SettingsDir }
"Advanced" { AdvancedMenu $hstwb }
"Extra" { ExtraMenu $hstwb }
"Help" { HelpMenu $hstwb }
}
} while ($option -ne $null -and $option -ne 'Exit')
}
function AdvancedMenu($hstwb)
{
do
{
$option = GuiMenu "Advanced" @('Run', 'Settings', 'Assigns', 'Back')
switch ($option)
{
"Run" { Run $hstwb.Paths.RunFile $hstwb.Paths.SettingsDir }
"Settings" { Settings $hstwb.Paths.SettingsFile }
"Assigns" { Assigns $hstwb.Paths.AssignsFile }
}
} while ($option -ne $null -and $option -ne 'Back')
}
function ExtraMenu($hstwb)
{
do
{
$option = GuiMenu "Extra" @('Support Files', 'Back')
} while ($option -ne $null -and $option -ne 'Back')
}
function HelpMenu($hstwb)
{
do
{
$option = GuiMenu "Help" @('Readme', 'Website', 'Source Code', 'Report Issue', 'Back')
} while ($option -ne $null -and $option -ne 'Back')
}
if (!$settingsDir)
{
$settingsDir = Join-Path $env:LOCALAPPDATA -ChildPath 'HstWB Installer'
}
$runFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('run.ps1')
$setupFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('setup.ps1')
$settingsFile = Join-Path $settingsDir -ChildPath 'hstwb-installer-settings.ini'
$assignsFile = Join-Path $settingsDir -ChildPath 'hstwb-installer-assigns.ini'
$runSetupFirstTime = $false
if (!(Test-Path $settingsFile))
{
$runSetupFirstTime = $true
MessageDialog "First time use" ("It appears this is the first time you're using HstWB Installer,{1}since settings file '{0}' doesn't exist.{1}{1}HstWB Installer will now start setup to{1}create a default settings file?" -f $settingsFile, [Environment]::NewLine)
Setup $setupFile $settingsDir
}
$hstwb = @{
'Paths' = @{
'SettingsDir' = $settingsDir;
'SetupFile' = $setupFile;
'RunFile' = $runFile;
'SettingsFile' = $settingsFile;
'AssignsFile' = $assignsFile;
};
}
LauncherMenu $hstwb