First, the code:
Add-Type -AssemblyName 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
class Preferences {
hidden [string]$_PrefPath
hidden [string]$_installLocation
hidden [int]$_maxAttempts
hidden [boolean]$_silent
hidden [string]$_skippedVersion
Preferences(
[string]$p
){
$this._PrefPath = $p
If (-not ([preferences]::PrefsExistAndNotNull($p))) {
[preferences]::InitPref($p)
}
$this.LoadVals()
}
Preferences (
[string]$p,
[boolean]$s
){
If (-not ([preferences]::PrefsExistAndNotNull($p)) -and $s) {
throw "No registry config found, silence precludes requesting install location"
} elseif (-not ([preferences]::PrefsExistAndNotNull($p)) -and -not $s) {
[preferences]::InitPref($p)
}
$this._silent = $s
$this._PrefPath = $p
$this.LoadVals()
}
hidden static [boolean]PrefsExistAndNotNull([string]$p){
if (-not (Test-Path $p)) {
return $false
}
if (-not (Get-ItemPropertyValue -Path $p -Name PathToKoL -ErrorAction SilentlyContinue)) {
return $false
}
if ( [string]::IsNullOrEmpty((Get-ItemPropertyValue -Path $p -Name PathToKoL)) ) {
return $false
}
return $true
}
hidden static [void]InitPref([string]$p){
if (-not
(Test-Path $p) -and -not
(Test-Path ($p | Split-Path))
) {
New-Item ($p | Split-Path)
Write-Verbose "Creating key at $($p | Split-Path)"
}
if (-not (Test-Path $p)) {
New-Item $p
Write-Verbose "Creating key at $p"
}
try {
$needtoask = [string]::IsNullOrEmpty((Get-ItemPropertyValue -Path $p -Name PathToKoL -ErrorAction SilentlyContinue))
} catch {
$needtoask = $true
}
if ($needtoask) {
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $env:USERPROFILE
$OpenFileDialog.filter = “JAR files (*.jar)| *.jar”
$OpenFileDialog.Title = "Select Location of KolMafia JAR"
$OpenFileDialog.ShowDialog() | Out-Null
$askfordir = [string]::IsNullOrEmpty($OpenFileDialog.FileName)
if (-not $askfordir) {
$installLocation = $OpenFileDialog.filename | Split-Path -Parent
New-ItemProperty -Path $p -Name 'PathToKoL' -Value $installLocation -PropertyType String
Write-Verbose "Creating value PathToKoL at $p"
}
} else {
$askfordir = $false
}
if ($askfordir) {
$msg = "No file was selected. Should Launcher assume you don't have KoLMafia installed and ask where you want it? (Selecting No will exit immediately)"
$title = 'No file selected'
$buttons = 4 #[System.Windows.Forms.MessageBoxButtons]::YesNo
$nobutton = 7 #[System.Windows.Forms.DialogResult]::No
$icon = 32 #[System.Windows.Forms.MessageBoxIcon]::Question
$default = 0 #[System.Windows.Forms.MessageBoxDefaultButton]::Button1
$options = 0
$answer = [System.Windows.Forms.MessageBox]::Show($msg,$title,$buttons,$icon,$default,$options)
if ($answer -eq $nobutton) {
EXIT 0
} else {
$SelectFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$SelectFolderDialog.SelectedPath = $env:USERPROFILE
$SelectFolderDialog.Description = "Select where you want to install KoLMafia"
$SelectFolderDialog.ShowNewFolderButton = $true
$SelectFolderDialog.ShowDialog() | Out-Null
if ([string]::IsNullOrEmpty($SelectFolderDialog.SelectedPath)) {
EXIT 0
} else {
$installLocation = $SelectFolderDialog.SelectedPath
New-ItemProperty -Path $p -Name 'PathToKoL' -Value $installLocation -PropertyType String
Write-Verbose "Creating value PathToKoL at $p"
}
}
}
try {
Get-ItemPropertyValue -Path $p -Name MaxDownloadAttempts | Out-Null
} catch {
New-ItemProperty -Path $p -Name 'MaxDownloadAttempts' -Value 3 -PropertyType Dword
Write-Verbose "Creating value MaxDownloadAttempts at $p"
}
try {
Get-ItemPropertyValue -Path $p -Name SkippedVersion | Out-Null
} catch {
New-ItemProperty -Path $p -Name 'SkippedVersion' -Value '' -PropertyType String
Write-Verbose "Creating value SkippedVersion at $p"
}
}
hidden [void]LoadVals(){
If (-not ([preferences]::PrefsExistAndNotNull($this._PrefPath))) {
[preferences]::InitPref($this._PrefPath)
}
$this._installLocation = Get-ItemPropertyValue -Path $this._PrefPath -Name PathToKoL
$this._maxAttempts = Get-ItemPropertyValue -Path $this._PrefPath -Name MaxDownloadAttempts
try {
Get-ItemPropertyValue -Path $this._PrefPath -Name SkippedVersion | Out-Null
} catch {
New-ItemProperty -Path $this._PrefPath -Name 'SkippedVersion' -Value '' -PropertyType String
Write-Verbose "Creating value SkippedVersion at $($this._PrefPath)"
} finally {
$this._skippedVersion = Get-ItemPropertyValue -Path $this._PrefPath -Name SkippedVersion
}
}
[string]GetLocation(){
return $this._installLocation
}
[void]SetLocation([string]$p){
Set-ItemProperty -Path $this._PrefPath -Name PathToKoL -Value $p
$this._installLocation = $p
}
[int]GetMaxAttempts(){
return $this._maxAttempts
}
[void]SetMaxAttempts([int]$m){
Set-ItemProperty -Path $this._PrefPath -Name MaxDownloadAttempts -Value $m
$this._maxAttempts = $m
}
[string]GetSkippedVersion(){
return $this._skippedVersion
}
[void]SetSkippedVersion([string]$v){
Set-ItemProperty -Path $this._PrefPath -Name SkippedVersion -Value $v
$this._skippedVersion = $v
}
}
Function Get-ShellOpenFromExtention {
param(
[Parameter(Mandatory=$true)]
[string]$Extension
)
begin {
if ($Extension -notmatch '\.?(?<Extension>[a-z0-9]{1,3}$)') {
return $null
}
$Extension = -join('.', $Matches.Extension)
}
process {
try {
$RegisteredApplication = (Get-ItemProperty "Registry::HKEY_CLASSES_ROOT\$Extension" -ErrorAction SilentlyContinue).'(default)'
if ($null -eq $RegisteredApplication) {return $null}
$ShellOpen = (Get-ItemProperty "Registry::HKEY_CLASSES_ROOT\$RegisteredApplication\shell\open\command" -ErrorAction SilentlyContinue).'(default)'
} catch {
return $null
}
if ($null -eq $ShellOpen) {return $null}
$ShellOpen = $ShellOpen.Replace(' %*','')
if ($shellOpen -match '(?(^")(?<path>"[^"]*")|(?<path>[^ ]*)) (?<params>.*)') {
return @{
appPath = $Matches.path
arguments = $Matches.params
}
}
}
end {
return $null
}
}
So that code has been working for weeks. Suddenly, it has stopped working, and I'm not sure why.
Specifically, I get errors at
At C:\Users\XXXX\Documents\GitHub\KoLMafia-Launcher\SOURCE\Launch-KoLMafia.ps1:90 char:25
+ ... $buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBoxButtons].
At C:\Users\XXXX\Documents\GitHub\KoLMafia-Launcher\SOURCE\Launch-KoLMafia.ps1:91 char:26
+ $nobutton = [System.Windows.Forms.DialogResult]::No
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.DialogResult].
At C:\Users\XXXX\Documents\GitHub\KoLMafia-Launcher\SOURCE\Launch-KoLMafia.ps1:92 char:22
+ $icon = [System.Windows.Forms.MessageBoxIcon]::Question
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBoxIcon].
At C:\Users\XXXX\Documents\GitHub\KoLMafia-Launcher\SOURCE\Launch-KoLMafia.ps1:93 char:25
+ ... $default = [System.Windows.Forms.MessageBoxDefaultButton]::Butto ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBoxDefaultButton].
At C:\Users\XXXX\Documents\GitHub\KoLMafia-Launcher\SOURCE\Launch-KoLMafia.ps1:95 char:24
+ $answer = [System.Windows.Forms.MessageBox]::Show($msg,$t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.MessageBox].
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TypeNotFound
I call MessageBox elsewhere, and it works, so it has to have something to do with trying to call it within the class above. And I call OpenFileDialog within the Class, so it has the System.Windows.Forms assembly available within the class - its just the MessageBox class that seems to be problematic.