remove Write-Warning for CAPIErrors.

Allow them to bubble up.
(Might be worth adding a helper function to print stacktrace?)
This commit is contained in:
onyx-and-iris 2023-08-16 16:36:43 +01:00
parent 844eaeabaa
commit 9b3d9f2250
4 changed files with 74 additions and 167 deletions

View File

@ -137,28 +137,18 @@ Function Get-RemotePotato {
Function Connect-Voicemeeter { Function Connect-Voicemeeter {
param([String]$Kind) param([String]$Kind)
try { switch ($Kind) {
switch ($Kind) { "basic" {
"basic" { return Get-RemoteBasic
return Get-RemoteBasic
}
"banana" {
return Get-RemoteBanana
}
"potato" {
return Get-RemotePotato
}
default { throw [LoginError]::new("Unknown Voicemeeter kind `"$Kind`"") }
} }
} "banana" {
catch [LoginError], [CAPIError] { return Get-RemoteBanana
Write-Warning $_.Exception.ErrorMessage() }
throw "potato" {
} return Get-RemotePotato
catch [VMRemoteError] { }
$_.Exception.ErrorMessage() | Write-Warning default {
if ($_.Exception.ErrorMessage() -eq "Couldn't get Voicemeeter path") { throw [LoginError]::new("Unknown Voicemeeter kind `"$Kind`"")
Exit -1
} }
} }
} }

View File

@ -5,15 +5,9 @@ function Login {
param( param(
[string]$kindId [string]$kindId
) )
try { $retval = [int][Voicemeeter.Remote]::VBVMR_Login()
$retval = [int][Voicemeeter.Remote]::VBVMR_Login() if ($retval -notin @(0, 1, -2)) {
if ($retval -notin @(0, 1, -2)) { throw [CAPIError]::new($retval, "VBVMR_Login")
throw [CAPIError]::new($retval, "VBVMR_Login")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
switch ($retval) { switch ($retval) {
@ -21,7 +15,7 @@ function Login {
RunVoicemeeter -kindId $kindId RunVoicemeeter -kindId $kindId
} }
-2 { -2 {
throw [LoginError]::new("Login may only be called once per session. Fatal error... exiting.") throw [LoginError]::new("Login may only be called once per session.")
} }
} }
@ -31,15 +25,9 @@ function Login {
function Logout { function Logout {
Start-Sleep -m 20 Start-Sleep -m 20
try { $retval = [int][Voicemeeter.Remote]::VBVMR_Logout()
$retval = [int][Voicemeeter.Remote]::VBVMR_Logout() if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_Logout")
throw [CAPIError]::new($retval, "VBVMR_Logout")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
if ($retval -eq 0) { "Sucessfully logged out" | Write-Verbose } if ($retval -eq 0) { "Sucessfully logged out" | Write-Verbose }
} }
@ -54,59 +42,35 @@ function RunVoicemeeter {
"potato" = $(if ([Environment]::Is64BitOperatingSystem) { 6 } else { 3 }) "potato" = $(if ([Environment]::Is64BitOperatingSystem) { 6 } else { 3 })
} }
try { $retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$kinds[$kindId])
$retval = [int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([int64]$kinds[$kindId]) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
"Voicemeeter Engine running but GUI not launched. Launching GUI now." | Write-Verbose "Voicemeeter Engine running but GUI not launched. Launching GUI now." | Write-Verbose
Start-Sleep -s 1 Start-Sleep -s 1
} }
function P_Dirty { function P_Dirty {
try { $retval = [Voicemeeter.Remote]::VBVMR_IsParametersDirty()
$retval = [Voicemeeter.Remote]::VBVMR_IsParametersDirty() if ($retval -notin @(0, 1)) {
if ($retval -notin @(0, 1)) { throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
[bool]$retval [bool]$retval
} }
function M_Dirty { function M_Dirty {
try { $retval = [Voicemeeter.Remote]::VBVMR_MacroButton_IsDirty()
$retval = [Voicemeeter.Remote]::VBVMR_MacroButton_IsDirty() if ($retval -notin @(0, 1)) {
if ($retval -notin @(0, 1)) { throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
throw [CAPIError]::new($retval, "VBVMR_RunVoicemeeter")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
[bool]$retval [bool]$retval
} }
function VmType { function VmType {
New-Variable -Name ptr -Value 0 New-Variable -Name ptr -Value 0
try { $retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterType([ref]$ptr)
$retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterType([ref]$ptr) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_GetVoicemeeterType")
throw [CAPIError]::new($retval, "VBVMR_GetVoicemeeterType")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
switch ($ptr) { switch ($ptr) {
1 { return "basic" } 1 { return "basic" }
@ -117,15 +81,9 @@ function VmType {
function VmVersion { function VmVersion {
New-Variable -Name ptr -Value 0 New-Variable -Name ptr -Value 0
try { $retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterVersion([ref]$ptr)
$retval = [int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterVersion([ref]$ptr) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_GetVoicemeeterVersion")
throw [CAPIError]::new($retval, "VBVMR_GetVoicemeeterVersion")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
$v1 = ($ptr -band 0xFF000000) -shr 24 $v1 = ($ptr -band 0xFF000000) -shr 24
$v2 = ($ptr -band 0x00FF0000) -shr 16 $v2 = ($ptr -band 0x00FF0000) -shr 16
@ -142,27 +100,21 @@ function Param_Get {
Start-Sleep -m 20 Start-Sleep -m 20
while (P_Dirty) { Start-Sleep -m 1 } while (P_Dirty) { Start-Sleep -m 1 }
try { if ($IS_STRING) {
if ($IS_STRING) { $BYTES = [System.Byte[]]::new(512)
$BYTES = [System.Byte[]]::new(512) $retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterStringA($PARAM, $BYTES)
$retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterStringA($PARAM, $BYTES) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_GetParameterStringA")
throw [CAPIError]::new($retval, "VBVMR_GetParameterStringA")
}
[System.Text.Encoding]::ASCII.GetString($BYTES).Trim([char]0)
}
else {
New-Variable -Name ptr -Value 0.0
$retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterFloat($PARAM, [ref]$ptr)
if ($retval -notin @(0)) {
throw [CAPIError]::new($retval, "VBVMR_GetParameterFloat")
}
[single]$ptr
} }
[System.Text.Encoding]::ASCII.GetString($BYTES).Trim([char]0)
} }
catch [CAPIError] { else {
$_.Exception.ToString() | Write-Warning New-Variable -Name ptr -Value 0.0
throw $retval = [int][Voicemeeter.Remote]::VBVMR_GetParameterFloat($PARAM, [ref]$ptr)
if ($retval -notin @(0)) {
throw [CAPIError]::new($retval, "VBVMR_GetParameterFloat")
}
[single]$ptr
} }
} }
@ -170,23 +122,17 @@ function Param_Set {
param( param(
[string]$PARAM, [Object]$VALUE [string]$PARAM, [Object]$VALUE
) )
try { if ($VALUE -is [string]) {
if ($VALUE -is [string]) { $retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterStringA($PARAM, $VALUE)
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterStringA($PARAM, $VALUE) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_SetParameterStringA")
throw [CAPIError]::new($retval, "VBVMR_SetParameterStringA")
}
}
else {
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterFloat($PARAM, $VALUE)
if ($retval -notin @(0)) {
throw [CAPIError]::new($retval, "VBVMR_SetParameterFloat")
}
} }
} }
catch [CAPIError] { else {
$_.Exception.ToString() | Write-Warning $retval = [int][Voicemeeter.Remote]::VBVMR_SetParameterFloat($PARAM, $VALUE)
throw if ($retval -notin @(0)) {
throw [CAPIError]::new($retval, "VBVMR_SetParameterFloat")
}
} }
} }
@ -194,15 +140,9 @@ function MB_Set {
param( param(
[int64]$ID, [single]$SET, [int64]$MODE [int64]$ID, [single]$SET, [int64]$MODE
) )
try { $retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_SetStatus($ID, $SET, $MODE)
$retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_SetStatus($ID, $SET, $MODE) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_MacroButton_SetStatus")
throw [CAPIError]::new($retval, "VBVMR_MacroButton_SetStatus")
}
}
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
} }
} }
@ -214,15 +154,9 @@ function MB_Get {
while (M_Dirty) { Start-Sleep -m 1 } while (M_Dirty) { Start-Sleep -m 1 }
New-Variable -Name ptr -Value 0.0 New-Variable -Name ptr -Value 0.0
try { $retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_GetStatus($ID, [ref]$ptr, $MODE)
$retval = [int][Voicemeeter.Remote]::VBVMR_MacroButton_GetStatus($ID, [ref]$ptr, $MODE) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, $MyInvocation.MyCommand)
throw [CAPIError]::new($retval, $MyInvocation.MyCommand)
}
}
catch [CAPIError] {
Write-Warning $_.Exception.ToString()
throw
} }
[int]$ptr [int]$ptr
} }
@ -254,15 +188,9 @@ function Set_By_Script {
param( param(
[string]$script [string]$script
) )
try { $retval = [int][Voicemeeter.Remote]::VBVMR_SetParameters($script)
$retval = [int][Voicemeeter.Remote]::VBVMR_SetParameters($script) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_SetParameters")
throw [CAPIError]::new($retval, "VBVMR_SetParameters")
}
}
catch [CAPIError] {
Write-Warning $_.Exception.ToString()
throw
} }
} }
@ -271,15 +199,9 @@ function Get_Level {
[int64]$MODE, [int64]$INDEX [int64]$MODE, [int64]$INDEX
) )
New-Variable -Name ptr -Value 0.0 New-Variable -Name ptr -Value 0.0
try { $retval = [int][Voicemeeter.Remote]::VBVMR_GetLevel($MODE, $INDEX, [ref]$ptr)
$retval = [int][Voicemeeter.Remote]::VBVMR_GetLevel($MODE, $INDEX, [ref]$ptr) if ($retval -notin @(0)) {
if ($retval -notin @(0)) { throw [CAPIError]::new($retval, "VBVMR_GetLevel")
throw [CAPIError]::new($retval, "VBVMR_GetLevel")
}
}
catch [CAPIError] {
Write-Warning $_.Exception.ToString()
throw
} }
[float]$ptr [float]$ptr
} }

View File

@ -8,6 +8,6 @@ function Get_VMPath {
return $(Get-ItemPropertyValue -Path ($REG_KEY + $VM_KEY) -Name UninstallString | Split-Path -Parent) return $(Get-ItemPropertyValue -Path ($REG_KEY + $VM_KEY) -Name UninstallString | Split-Path -Parent)
} }
catch { catch {
throw [VMRemoteError]::new("Couldn't get Voicemeeter path") throw [VMRemoteError]::new("Unable to fetch Voicemeeter path from the Registry.")
} }
} }

View File

@ -24,14 +24,9 @@ function Set_Profile {
param( param(
[Object]$DATA, [string]$CONF [Object]$DATA, [string]$CONF
) )
try { if ($null -eq $DATA -or -not $DATA.$CONF) {
if ($null -eq $DATA -or -not $DATA.$CONF) { throw [VMRemoteErrors]::new("No profile named '$CONF' has been loaded into memory.")
throw [VMRemoteErrors]::new("No profile named $CONF was loaded")
}
Param_Set_Multi -HASH $DATA.$CONF
Start-Sleep -m 1
}
catch [VMRemoteErrors] {
Write-Warning $_.Exception.ErrorMessage()
} }
Param_Set_Multi -HASH $DATA.$CONF
Start-Sleep -m 1
} }