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,7 +137,6 @@ 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
@ -148,17 +147,8 @@ Function Connect-Voicemeeter {
"potato" { "potato" {
return Get-RemotePotato return Get-RemotePotato
} }
default { throw [LoginError]::new("Unknown Voicemeeter kind `"$Kind`"") } default {
} throw [LoginError]::new("Unknown Voicemeeter kind `"$Kind`"")
}
catch [LoginError], [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
throw
}
catch [VMRemoteError] {
$_.Exception.ErrorMessage() | Write-Warning
if ($_.Exception.ErrorMessage() -eq "Couldn't get Voicemeeter path") {
Exit -1
} }
} }
} }

View File

@ -5,23 +5,17 @@ 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) {
1 { 1 {
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,16 +25,10 @@ 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,60 +42,36 @@ 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" }
2 { return "banana" } 2 { return "banana" }
@ -117,16 +81,10 @@ 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
$v3 = ($ptr -band 0x0000FF00) -shr 8 $v3 = ($ptr -band 0x0000FF00) -shr 8
@ -142,7 +100,6 @@ 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)
@ -160,17 +117,11 @@ function Param_Get {
[single]$ptr [single]$ptr
} }
} }
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
}
}
function Param_Set { 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)) {
@ -184,27 +135,16 @@ function Param_Set {
} }
} }
} }
catch [CAPIError] {
$_.Exception.ToString() | Write-Warning
throw
}
}
function MB_Set { 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
}
}
function MB_Get { function MB_Get {
param( param(
@ -214,16 +154,10 @@ 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,32 +188,20 @@ 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
}
}
function Get_Level { function Get_Level {
param( param(
[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 was loaded") throw [VMRemoteErrors]::new("No profile named '$CONF' has been loaded into memory.")
} }
Param_Set_Multi -HASH $DATA.$CONF Param_Set_Multi -HASH $DATA.$CONF
Start-Sleep -m 1 Start-Sleep -m 1
} }
catch [VMRemoteErrors] {
Write-Warning $_.Exception.ErrorMessage()
}
}