-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
BhudModuleLib.ps1
218 lines (180 loc) · 8.05 KB
/
BhudModuleLib.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
#Requires -Version 7.0
Add-Type -AssemblyName System.IO.Compression
enum TestResult {
na
ok
warn
fail
}
class BhudUtil {
static [void] WriteTestStart([String] $test) {
Write-Host $test.PadRight(32, '.') -NoNewLine
}
static [void] WriteTestOk([string] $value) {
[BhudUtil]::WriteTestResult("OK [$($value)]", [TestResult]::ok)
}
static [void] WriteTestResult([String] $details, [TestResult] $result) {
$foregroundLookup = @{
[TestResult]::na = "Blue";
[TestResult]::ok = "Green";
[TestResult]::warn = "Yellow";
[TestResult]::fail = "Red"
}
Write-Host $details -ForegroundColor $foregroundLookup[$result]
}
}
class BhudModule {
[int] $MANIFEST_VERSION = 1
[String] $MANIFEST_NAME = "manifest.json"
[System.IO.Compression.ZipArchive] $Archive
[string] $Checksum
BhudModule([System.IO.Compression.ZipArchive] $ModuleArchive) {
$this.MANIFEST_VERSION = 1
$this.MANIFEST_NAME = "manifest.json"
$this.Archive = $ModuleArchive
}
[Object] GetManifest() {
$manifestEntry = $this.Archive.GetEntry($this.MANIFEST_NAME)
$manifestReader = New-Object System.IO.StreamReader($manifestEntry.Open())
return ($manifestReader.ReadToEnd() | ConvertFrom-Json -AsHashtable)
}
[bool] Validate() {
$isValid = $true
# check manifest exists
[BhudUtil]::WriteTestStart("Checking $($this.MANIFEST_NAME) exists")
if (!$this.Archive.GetEntry($this.MANIFEST_NAME)) {
[BhudUtil]::WriteTestResult("$($this.MANIFEST_NAME) is missing from archive!", [TestResult]::fail)
Write-Host "Unable to continue validation!" -ForegroundColor Red
return $false; # can't continue validating
} else {
[BhudUtil]::WriteTestResult("OK", [TestResult]::ok)
}
$manifest = $this.GetManifest();
# check module name
[BhudUtil]::WriteTestStart("Checking name")
if (!$manifest.name) {
[BhudUtil]::WriteTestResult("Mandatory field 'name' missing from manifest!", [TestResult]::fail)
$isValid = $false
} else {
[BhudUtil]::WriteTestOk($manifest.name)
}
# check manifest version
[BhudUtil]::WriteTestStart("Checking manifest version")
if (!$manifest.manifest_version) {
[BhudUtil]::WriteTestResult("Mandatory field 'manifest_version' missing from manifest!", [TestResult]::fail)
$isValid = $false
} elseif ($manifest.manifest_version -ne $this.MANIFEST_VERSION) {
[BhudUtil]::WriteTestResult("Provided version '$($manifest.manifest_version)' is invalid (must be '$($this.MANIFEST_VERSION)').", [TestResult]::fail)
$isValid = $false
} else {
[BhudUtil]::WriteTestOk($manifest.manifest_version)
}
# check primary assembly name
[BhudUtil]::WriteTestStart("Checking package reference")
if (!$manifest.package) {
[BhudUtil]::WriteTestResult("Mandatory field 'package' missing from manifest!", [TestResult]::fail)
$isValid = $false
} elseif (!$this.Archive.GetEntry($manifest.package)) {
[BhudUtil]::WriteTestResult("Provided '$($manifest.package)' does not reference a valid module DLL!", [TestResult]::fail)
$isValid = $false
} else {
[BhudUtil]::WriteTestOk($manifest.package)
}
# check module version
[BhudUtil]::WriteTestStart("Checking version")
if (!$manifest.version) {
[BhudUtil]::WriteTestResult("Mandatory field 'version' missing from manifest!", [TestResult]::fail)
$isValid = $false
} elseif ($manifest.version -notmatch "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$") {
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
[BhudUtil]::WriteTestResult("Provided '$($manifest.version)' is invalid (must be SemVer).", [TestResult]::fail)
$isValid = $false
} else {
[BhudUtil]::WriteTestOk($manifest.version)
}
# check namespace
[BhudUtil]::WriteTestStart("Checking namespace")
if (!$manifest.namespace) {
[BhudUtil]::WriteTestResult("Mandatory field 'namespace' missing from manifest!", [TestResult]::fail)
$isValid = $false
} elseif ($manifest.namespace -notmatch "^([a-zA-Z_][\w_]*\.)*[a-zA-Z_][\w_]*$") {
# https://stackoverflow.com/a/5205467
[BhudUtil]::WriteTestResult("Provided '$($manifest.namespace)' is invalid.", [TestResult]::fail)
$isValid = $false
} else {
[BhudUtil]::WriteTestOk($manifest.namespace)
}
### optional fields
# api permission check
[BhudUtil]::WriteTestStart("Checking API permissions")
if (!$manifest.api_permissions) {
[BhudUtil]::WriteTestResult("NA", [TestResult]::na)
} else {
$refPermissions = @("account", "inventories", "characters", "tradingpost", "wallet", "unlocks", "pvp", "builds", "progression", "guilds")
$reqPermissions = $($manifest.api_permissions.Keys)
$badPermissions = ((Compare-Object -ReferenceObject $refPermissions -DifferenceObject $reqPermissions) | Where-Object {$_.SideIndicator -eq '=>'}) | Select-Object -ExpandProperty InputObject
if ($badPermissions.Length -gt 0) {
[BhudUtil]::WriteTestResult("Invalid permissions [$($badPermissions -join ', ')]", [TestResult]::fail)
$isValid = $false;
} else {
[BhudUtil]::WriteTestOk($reqPermissions -join ', ')
}
}
# directory check
[BhudUtil]::WriteTestStart("Checking directories")
if (!$manifest.directories) {
[BhudUtil]::WriteTestResult("NA", [TestResult]::na)
} else {
$badDirs = $manifest.directories -notmatch "^[^<>:""\/\\|\?\*\r\n]+$"
if ($badDirs.Length -gt 0) {
[BhudUtil]::WriteTestResult("Invalid dirs [$($badDirs -join ', ')]", [TestResult]::fail)
$isValid = $false;
} else {
[BhudUtil]::WriteTestOk($manifest.directories -join ', ')
}
}
# checking enabled with without Blish HUD
[BhudUtil]::WriteTestStart("Checking enabled without game")
if (!$manifest.enable_without_gw2) {
[BhudUtil]::WriteTestResult("FALSE", [TestResult]::na)
} else {
if ($manifest.enable_without_gw2 -eq $true) {
[BhudUtil]::WriteTestResult("TRUE", [TestResult]::warn)
} else {
[BhudUtil]::WriteTestResult("FALSE", [TestResult]::ok)
}
}
return $isValid;
}
}
function Get-BhudModule {
[OutputType([BhudModule])]
param(
[System.IO.MemoryStream] $InputStream = $null,
[String] $Path = "",
[String] $Url = ""
)
$moduleStream = New-Object System.IO.MemoryStream
if ($InputStream) {
$moduleStream = $InputStream
}
if ($Path.Length -gt 0) {
(Get-Item -Path $Path).OpenRead().CopyTo($moduleStream)
} elseif ($Url.Length -gt 0) {
write-host "Downloading module from URL. This could take a while..." -ForeGroundColor Blue -NoNewline
$webClient = New-Object System.Net.WebClient
try {
$webClient.OpenRead($URL).CopyTo($moduleStream)
Write-Host "OK" -ForegroundColor Green
} catch {
write-host "Error downloading module." -ForeGroundColor red
Write-Host "FAILED" -ForegroundColor Red
return $null
}
}
$module = [BhudModule]::new($moduleStream)
# Consider moving to the ctor
$moduleStream.Position = 0
$module.Checksum = (Get-FileHash -InputStream $moduleStream).Hash
return $module
}