Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for null parameter #22

Merged
merged 3 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree on that ;) just eat it

}
}
}

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
}
}
}
}