Skip to content

Commit

Permalink
Fixes for null parameter (#22)
Browse files Browse the repository at this point in the history
* Fixes for null parameter

* Add test for #22.

* Fixed an issue that stopped reading parameters.

Fixed an issue that stopped reading parameters when receiving not registered avatar parameters on the avatar config file.

Co-authored-by: Marcel Kallen <[email protected]>
Co-authored-by: ChanyaKushima <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2022
1 parent 4572c2c commit c533ea6
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/vrcosclib.Test/Avatar/OscAvatarParametorContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public void OneTimeTearDown()
_client.Dispose();
}

[SetUp]
public void SetUp()
{
OscParameter.Parameters.Clear();
}


[Test]
public async Task ParameterChangedTest()
{
Expand Down Expand Up @@ -100,4 +107,42 @@ public void PhysBonesTest()
new[] { "ValidParam1", "ValidParam2", "ValidParam3", },
physbones.Select(v => v.ParamName));
}

[Test]
public void Get_ExistParameterTest()
{
var param = _parameters.Get("TestParam");
Assert.AreEqual("TestParam", param.Name);
Assert.AreEqual(OscType.Float, param.Input!.OscType);
Assert.AreEqual(OscType.Float, param.Output!.OscType);
}

[Test]
public void Get_NotExistParameterTest()
{
Assert.Throws<InvalidOperationException>(() => _parameters.Get("NotTestParam"));
}

[Test]
public async Task OnParameterChanged_NotExistParameterRecievedTest()
{
bool isCalled = false;

_parameters.ParameterChanged += ThrowExceptionHandler;
_parameters.ParameterChanged += MonitorCalledHandler;

_client.Send(OscConst.AvatarParameterAddressSpace + "TestParam", 1);
await TestUtility.LoopWhile(() => !isCalled, TestUtility.LatencyTimeout);
isCalled = false;

_client.Send(OscConst.AvatarParameterAddressSpace + "TestParam", 2);
await TestUtility.LoopWhile(() => !isCalled, TestUtility.LatencyTimeout);
isCalled = false;

_parameters.ParameterChanged -= ThrowExceptionHandler;
_parameters.ParameterChanged -= MonitorCalledHandler;

void ThrowExceptionHandler(OscAvatarParameter param, ValueChangedEventArgs e) => throw new Exception();
void MonitorCalledHandler(OscAvatarParameter param, ValueChangedEventArgs e) => isCalled = true;
}
}
18 changes: 18 additions & 0 deletions src/vrcosclib.Test/Collections/OscParameterCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private static OscParameterCollection CreateParameterCollectionForTest()
break;
}
};
parameters.ValueChanged += (sender, e) => throw new Exception();
return parameters;
}

Expand Down Expand Up @@ -273,4 +274,21 @@ public void TestLinq()
pararmeters["/address/to/parameter3"] = false;
Assert.DoesNotThrow(() => pararmeters.OrderBy(v => v.Key).ToArray());
}


[Test]
public void OnValueChangedByAddress_IgnoreExceptionTest()
{
var parameters = CreateParameterCollectionForTest();
int calledCount = 0;

const string Address = "/address/to/parameter";
parameters.AddValueChangedEventByAddress(Address, (_, _) => calledCount++);
parameters.AddValueChangedEventByAddress(Address, (_, _) => throw new Exception());
parameters.AddValueChangedEventByAddress(Address, (_, _) => calledCount++);

parameters[Address] = 1;

Assert.AreEqual(2, calledCount);
}
}
12 changes: 10 additions & 2 deletions src/vrcosclib/Avatar/OscAvatarParametorContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using BlobHandles;
using BuildSoft.OscCore;
using BuildSoft.VRChat.Osc.Delegate;

namespace BuildSoft.VRChat.Osc.Avatar;

Expand Down Expand Up @@ -50,6 +51,7 @@ public OscAvatarParametorContainer(IEnumerable<OscAvatarParameter> parameters)
#region Datas
public ImmutableArray<OscAvatarParameter> Items { get; }
public OscAvatarParameter Get(string name) => Items.First(p => p.Name == name);
internal OscAvatarParameter? TryGet(string name) => Items.FirstOrDefault(p => p.Name == name);

public IEnumerable<OscAvatarParameter> UniqueParameters => Items.Where(parm => !OscAvatarUtility.IsCommonParameter(parm.Name));
public IEnumerable<object?> UniqueParameterValues
Expand Down Expand Up @@ -202,14 +204,20 @@ public bool TryGetValue(string key,
private void GetValueCallback(IReadOnlyOscParameterCollection sender, ParameterChangedEventArgs e)
{
var name = e.Address.Substring(OscConst.AvatarParameterAddressSpace.Length);
OnParameterChanged(Get(name), e);
OscAvatarParameter? param = TryGet(name);
if (param == null)
{
return;
}

OnParameterChanged(param, e);
}

public event OscAvatarParameterChangedEventHandler? ParameterChanged;

protected internal void OnParameterChanged(OscAvatarParameter param, ValueChangedEventArgs e)
{
ParameterChanged?.Invoke(param, e);
ParameterChanged?.DynamicInvokeAllWithoutException(param, e);
}
#endregion

Expand Down
13 changes: 10 additions & 3 deletions src/vrcosclib/Collections/OscParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Text;

using BuildSoft.VRChat.Osc.Delegate;
using ParamChangedHandler = BuildSoft.VRChat.Osc.OscParameterChangedEventHandler<BuildSoft.VRChat.Osc.IReadOnlyOscParameterCollection>;

namespace BuildSoft.VRChat.Osc;
Expand Down Expand Up @@ -101,7 +101,7 @@ public bool Remove(string key)

protected void OnValueChanged(ParameterChangedEventArgs args)
{
ValueChanged?.Invoke(this, args);
ValueChanged?.DynamicInvokeAllWithoutException(this, args);
OnValueChangedByAddress(args);
}

Expand All @@ -114,7 +114,14 @@ protected void OnValueChangedByAddress(ParameterChangedEventArgs args)
var handlers = list.ToArray();
for (int i = 0; i < handlers.Length; i++)
{
handlers[i].Invoke(this, args);
try
{
handlers[i].Invoke(this, args);
}
catch (Exception)
{
// eat exception
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/vrcosclib/Delegate/EventDelegateExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BuildSoft.VRChat.Osc.Delegate;

internal static class EventDelegateExtension
{
public static void DynamicInvokeAllWithoutException<T>(this T @delegate, params object[] args) where T : System.Delegate
{
foreach (var item in @delegate.GetInvocationList())
{
try
{
item.DynamicInvoke(args);
}
catch (Exception)
{
// eat exception
}
}
}
}

0 comments on commit c533ea6

Please sign in to comment.