added custom error classes

Added CAPI and Login custom error classes.

Cleaned up output when error is thrown.
This commit is contained in:
onyx-and-iris 2021-04-29 20:23:02 +01:00
parent 0af51e83b3
commit 94be34b3da
3 changed files with 111 additions and 65 deletions

View File

@ -1,8 +1,9 @@
. $PSScriptRoot\errors.ps1
. $PSScriptRoot\strip.ps1 . $PSScriptRoot\strip.ps1
. $PSScriptRoot\bus.ps1 . $PSScriptRoot\bus.ps1
. $PSScriptRoot\macrobuttons.ps1 . $PSScriptRoot\macrobuttons.ps1
$Handles = @' $Signature = @'
[DllImport(@"C:\Program Files (x86)\VB\Voicemeeter\VoicemeeterRemote64.dll")] [DllImport(@"C:\Program Files (x86)\VB\Voicemeeter\VoicemeeterRemote64.dll")]
public static extern int VBVMR_Login(); public static extern int VBVMR_Login();
[DllImport(@"C:\Program Files (x86)\VB\Voicemeeter\VoicemeeterRemote64.dll")] [DllImport(@"C:\Program Files (x86)\VB\Voicemeeter\VoicemeeterRemote64.dll")]
@ -35,10 +36,11 @@ $Handles = @'
public static extern int VBVMR_SetParameters(String param); public static extern int VBVMR_SetParameters(String param);
'@ '@
$lib = Add-Type -MemberDefinition $Handles -Name VMRemote -PassThru Add-Type -MemberDefinition $Signature -Name Remote -Namespace Voicemeeter -PassThru | Out-Null
$global:layout = $null $global:layout = $null
Function Param_Set_Multi { Function Param_Set_Multi {
param( param(
[HashTable]$HASH [HashTable]$HASH
@ -59,16 +61,26 @@ Function Param_Set_Multi {
} }
[String]$cmd = $cmd.SubString(1) [String]$cmd = $cmd.SubString(1)
$retval = $lib::VBVMR_SetParameters($cmd) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_SetParameters($cmd)
if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
} }
Function Param_Set_String { Function Param_Set_String {
param( param(
[String]$PARAM, [String]$VALUE [String]$PARAM, [String]$VALUE
) )
$retval = $lib::VBVMR_SetParameterStringA($PARAM, $VALUE) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_SetParameterStringA($PARAM, $VALUE)
if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
} }
@ -80,8 +92,13 @@ Function Param_Get_String {
while(P_Dirty) { Start-Sleep -m 1 } while(P_Dirty) { Start-Sleep -m 1 }
$BYTES = [System.Byte[]]::new(512) $BYTES = [System.Byte[]]::new(512)
$retval = $lib::VBVMR_GetParameterStringA($PARAM, $BYTES) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_GetParameterStringA($PARAM, $BYTES)
if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
[System.Text.Encoding]::ASCII.GetString($BYTES).Trim([char]0) [System.Text.Encoding]::ASCII.GetString($BYTES).Trim([char]0)
} }
@ -91,8 +108,13 @@ Function Param_Set {
param( param(
[String]$PARAM, [Single]$VALUE [String]$PARAM, [Single]$VALUE
) )
$retval = $lib::VBVMR_SetParameterFloat($PARAM, $VALUE) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_SetParameterFloat($PARAM, $VALUE)
if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
} }
@ -103,10 +125,15 @@ Function Param_Get {
Start-Sleep -m 50 Start-Sleep -m 50
while(P_Dirty) { Start-Sleep -m 1 } while(P_Dirty) { Start-Sleep -m 1 }
$ptr = 0.0 New-Variable -Name ptr -Value 0.0
$retval = $lib::VBVMR_GetParameterFloat($PARAM, [ref]$ptr) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_GetParameterFloat($PARAM, [ref]$ptr)
$ptr if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
[Single]$ptr
} }
@ -114,8 +141,13 @@ Function MB_Set {
param( param(
[Int64]$ID, [Single]$SET, [Int64]$MODE [Int64]$ID, [Single]$SET, [Int64]$MODE
) )
$retval = $lib::VBVMR_MacroButton_SetStatus($ID, $SET, $MODE) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_MacroButton_SetStatus($ID, $SET, $MODE)
if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
} }
@ -126,10 +158,15 @@ Function MB_Get {
Start-Sleep -m 50 Start-Sleep -m 50
while(M_Dirty) { Start-Sleep -m 1 } while(M_Dirty) { Start-Sleep -m 1 }
$ptr = 0.0 New-Variable -Name ptr -Value 0.0
$retval = $lib::VBVMR_MacroButton_GetStatus($ID, [ref]$ptr, $MODE) try {
if($retval) { Throw "ERROR: CAPI return value: $retval" } $retval = [Int][Voicemeeter.Remote]::VBVMR_MacroButton_GetStatus($ID, [ref]$ptr, $MODE)
$ptr if($retval) { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
}
catch [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
[Int]$ptr
} }
Function DefineVersion { Function DefineVersion {
@ -164,34 +201,38 @@ Function Login {
param( param(
[String]$TYPE=$null [String]$TYPE=$null
) )
$retval = $lib::VBVMR_Login() try {
$retval = [Int][Voicemeeter.Remote]::VBVMR_Login()
if(-not $retval) { Write-Host("LOGGED IN") } if(-not $retval) { Write-Host("LOGGED IN") }
elseif($retval -eq 1) { elseif($retval -eq 1) {
Write-Host("VB NOT RUNNING") Write-Host("VB NOT RUNNING")
New-Variable -Name vbtype -Value 0
if($TYPE -eq 'basic') { Switch($TYPE) {
$retval = $lib::VBVMR_RunVoicemeeter([Int64]1) 'basic' { $vbtype = 1; Break}
if(-not $retval) { Write-Host("STARTING VB") } 'banana' { $vbtype = 2; Break}
'potato' { $vbtype = 3; Break}
Default { throw [LoginError]::new('Unknown Voicemeeter type') }
} }
elseif($TYPE -eq 'banana') {
$retval = $lib::VBVMR_RunVoicemeeter([Int64]2) $retval = [Int][Voicemeeter.Remote]::VBVMR_RunVoicemeeter([Int64]$vbtype)
if(-not $retval) { Write-Host("STARTING VB") } if(-not $retval) { Write-Host("STARTING VB") }
} else { Throw [CAPIError]::new($retval, $MyInvocation.MyCommand) }
elseif($TYPE -eq 'potato') { Start-Sleep -s 1
$retval = $lib::VBVMR_RunVoicemeeter([Int64]3)
if(-not $retval) { Write-Host("STARTING VB") }
}
} else { Exit } } else { Exit }
}
catch [LoginError], [CAPIError] {
Write-Warning $_.Exception.ErrorMessage()
}
while(P_Dirty -or M_Dirty) { Start-Sleep -m 1 } while(P_Dirty -or M_Dirty) { Start-Sleep -m 1 }
$ptr = 0 New-Variable -Name ptr -Value 0
$retval = $lib::VBVMR_GetVoicemeeterType([ref]$ptr) $retval = [Int][Voicemeeter.Remote]::VBVMR_GetVoicemeeterType([ref]$ptr)
if(-not $retval) { if(-not $retval) {
if($ptr -eq 1) { Write-Host("VERSION:[BASIC]") } if($ptr -eq 1) { Write-Host("VERSION:[BASIC]") }
elseif($ptr -eq 2) { Write-Host("VERSION:[BANANA]") } elseif($ptr -eq 2) { Write-Host("VERSION:[BANANA]") }
elseif($ptr -eq 3) { Write-Host("VERSION:[POTATO]") } elseif($ptr -eq 3) { Write-Host("VERSION:[POTATO]") }
Start-Sleep -s 1
} }
DefineVersion -TYPE $ptr DefineVersion -TYPE $ptr
@ -200,18 +241,18 @@ Function Login {
Function Logout { Function Logout {
Start-Sleep -m 20 Start-Sleep -m 20
$retval = $lib::VBVMR_Logout() $retval = [Int][Voicemeeter.Remote]::VBVMR_Logout()
if(-not $retval) { Write-Host("LOGGED OUT") } if(-not $retval) { Write-Host("LOGGED OUT") }
} }
Function P_Dirty { Function P_Dirty {
$lib::VBVMR_IsParametersDirty() [Bool][Voicemeeter.Remote]::VBVMR_IsParametersDirty()
} }
Function M_Dirty { Function M_Dirty {
$lib::VBVMR_MacroButton_IsDirty() [Bool][Voicemeeter.Remote]::VBVMR_MacroButton_IsDirty()
} }
@ -219,27 +260,7 @@ if ($MyInvocation.InvocationName -ne '.')
{ {
Login Login
$hash = @{ Param_Set -PARAM 'garbagevalue' -Value $true
"Strip[0].Mute" = $true
"Strip[1].Mute" = $true
"Strip[2].Mute" = $true
"Strip[0].Mono" = $true
"Strip[1].Mono" = $true
"Strip[2].Mono" = $true
}
0..10 | ForEach-Object {
foreach($key in $($hash.keys)){
$hash[$key] = $true
}
Param_Set_Multi -HASH $hash
foreach($key in $($hash.keys)){
$hash[$key] = $false
}
Param_Set_Multi -HASH $hash
}
Logout Logout
} }

25
lib/errors.ps1 Normal file
View File

@ -0,0 +1,25 @@
class LoginError : Exception {
[String]$msg
LoginError([String]$msg) {
$this.msg = $msg
}
[String] ErrorMessage() {
return $this.msg
}
}
class CAPIError : Exception {
[Int]$retval
[String]$caller
CAPIError([Int]$retval, [String]$caller) {
$this.retval = $retval
$this.caller = $caller
}
[String] ErrorMessage() {
return "ERROR: CAPI return value: {0} in {1}" -f $this.retval, $this.caller
}
}

View File

@ -10,10 +10,10 @@ class Remote {
Remote ([String]$type) Remote ([String]$type)
{ {
$this.type = $type $this.type = $type
$this.Login() $this.Setup()
} }
[void] Login () { [void] Setup () {
Login -TYPE $this.type Login -TYPE $this.type
$this.button = Buttons $this.button = Buttons