From 3b1e469d2dc3bf2d7e0e85ad7a6a6f632f8aa35c Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 18 Dec 2022 04:29:41 +0000 Subject: [PATCH] Login refactor. Return Exit value -2 for multiple login attempts. --- lib/base.ps1 | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/base.ps1 b/lib/base.ps1 index 5e08680..c621e84 100644 --- a/lib/base.ps1 +++ b/lib/base.ps1 @@ -11,41 +11,40 @@ function Login { param( - [string]$KIND = $null + [string]$kindId ) try { $retval = [int][Voicemeeter.Remote]::VBVMR_Login() - if (-not $retval) { "LOGGED IN" | Write-Verbose } + if (-not $retval) { + "LOGGED IN" | Write-Verbose + } elseif ($retval -eq 1) { "VM NOT RUNNING" | Write-Verbose - New-Variable -Name vm_exe -Value 0 + New-Variable -Name vmExe -Value 0 - switch ($KIND) { - 'basic' { $vm_exe = 1; break } - 'banana' { $vm_exe = 2; break } - 'potato' { - if ([Environment]::Is64BitOperatingSystem) { - $vm_exe = 6 - } - else { $vm_exe = 3 } - break - } - default { throw [LoginError]::new('Unknown Voicemeeter type') } + if ( $kindId -eq "basic" ) { $vmExe = 1 } + elseif ( $kindId -eq "banana" ) { $vmExe = 2 } + elseif ( $kindId -eq "potato" ) { + $vmExe = $(if ([Environment]::Is64BitOperatingSystem) { 6 } else { 3 }) } - $retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$vm_exe) - if (-not $retval) { "STARTING VOICEMEETER" | Write-Verbose } - else { throw [CAPIError]::new($retval, $MyInvocation.MyCommand) } - Start-Sleep -s 1 + $retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$vmExe) + if (-not $retval) { + "STARTING VOICEMEETER" | Write-Verbose + Start-Sleep -s 1 + } + else { + throw [CAPIError]::new($retval, $MyInvocation.MyCommand) + } } 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) } } - catch [LoginError], [CAPIError] { - Write-Warning $_.Exception.ErrorMessage() - throw + catch [LoginError] { + Write-Warning "$($_.Exception.ErrorMessage()). Fatal error, exiting..." + exit -2 } while (P_Dirty -or M_Dirty) { Start-Sleep -m 1 } @@ -55,7 +54,7 @@ function Login { function Logout { Start-Sleep -m 20 $retval = [int][Voicemeeter.Remote]::VBVMR_Logout() - if (-not $retval) { "LOGGED OUT" | Write-Verbose } + if (-not $retval) { "LOGGED OUT" | Write-Verbose } } function P_Dirty {