Login refactor.

Return Exit value -2 for multiple login attempts.
This commit is contained in:
onyx-and-iris 2022-12-18 04:29:41 +00:00
parent 3b1bb06c7d
commit 3b1e469d2d

View File

@ -11,41 +11,40 @@
function Login { function Login {
param( param(
[string]$KIND = $null [string]$kindId
) )
try { try {
$retval = [int][Voicemeeter.Remote]::VBVMR_Login() $retval = [int][Voicemeeter.Remote]::VBVMR_Login()
if (-not $retval) { "LOGGED IN" | Write-Verbose } if (-not $retval) {
"LOGGED IN" | Write-Verbose
}
elseif ($retval -eq 1) { elseif ($retval -eq 1) {
"VM NOT RUNNING" | Write-Verbose "VM NOT RUNNING" | Write-Verbose
New-Variable -Name vm_exe -Value 0 New-Variable -Name vmExe -Value 0
switch ($KIND) { if ( $kindId -eq "basic" ) { $vmExe = 1 }
'basic' { $vm_exe = 1; break } elseif ( $kindId -eq "banana" ) { $vmExe = 2 }
'banana' { $vm_exe = 2; break } elseif ( $kindId -eq "potato" ) {
'potato' { $vmExe = $(if ([Environment]::Is64BitOperatingSystem) { 6 } else { 3 })
if ([Environment]::Is64BitOperatingSystem) {
$vm_exe = 6
}
else { $vm_exe = 3 }
break
}
default { throw [LoginError]::new('Unknown Voicemeeter type') }
} }
$retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$vm_exe) $retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$vmExe)
if (-not $retval) { "STARTING VOICEMEETER" | Write-Verbose } if (-not $retval) {
else { throw [CAPIError]::new($retval, $MyInvocation.MyCommand) } "STARTING VOICEMEETER" | Write-Verbose
Start-Sleep -s 1 Start-Sleep -s 1
}
else {
throw [CAPIError]::new($retval, $MyInvocation.MyCommand)
}
} }
elseif ($retval -eq -2) { elseif ($retval -eq -2) {
throw [LoginError]::new('Login may only be called once per session') throw [LoginError]::new('login may only be called once per session')
} }
else { throw [CAPIError]::new($retval, $MyInvocation.MyCommand) } else { throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
} }
catch [LoginError], [CAPIError] { catch [LoginError] {
Write-Warning $_.Exception.ErrorMessage() Write-Warning "$($_.Exception.ErrorMessage()). Fatal error, exiting..."
throw exit -2
} }
while (P_Dirty -or M_Dirty) { Start-Sleep -m 1 } while (P_Dirty -or M_Dirty) { Start-Sleep -m 1 }
@ -55,7 +54,7 @@ function Login {
function Logout { function Logout {
Start-Sleep -m 20 Start-Sleep -m 20
$retval = [int][Voicemeeter.Remote]::VBVMR_Logout() $retval = [int][Voicemeeter.Remote]::VBVMR_Logout()
if (-not $retval) { "LOGGED OUT" | Write-Verbose } if (-not $retval) { "LOGGED OUT" | Write-Verbose }
} }
function P_Dirty { function P_Dirty {