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 1 commit
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
21 changes: 6 additions & 15 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 @@ -49,7 +50,8 @@ public OscAvatarParametorContainer(IEnumerable<OscAvatarParameter> parameters)

#region Datas
public ImmutableArray<OscAvatarParameter> Items { get; }
public OscAvatarParameter? Get(string name) => Items.FirstOrDefault(p => p.Name == name);
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 @@ -146,11 +148,6 @@ public object? this[string name]
public T? GetAs<T>(string name) where T : notnull
{
var param = Get(name);
if (param == null)
{
return default;
}

var allParams = OscParameter.Parameters;
if (allParams.TryGetValue(param.ReadableAddress, out var value))
{
Expand All @@ -161,13 +158,7 @@ public object? this[string name]

public void SetAs<T>(string name, T value)
{
var param = Get(name);
if (param == null)
{
throw new InvalidOperationException($"{name} does not exist.");
}

var inputInterface = param.Input;
var inputInterface = Get(name).Input;
if (inputInterface == null)
{
throw new InvalidOperationException($"{name} dosen't has a input interface.");
Expand Down Expand Up @@ -213,7 +204,7 @@ public bool TryGetValue(string key,
private void GetValueCallback(IReadOnlyOscParameterCollection sender, ParameterChangedEventArgs e)
{
var name = e.Address.Substring(OscConst.AvatarParameterAddressSpace.Length);
var param = Get(name);
OscAvatarParameter? param = TryGet(name);
if (param == null)
{
return;
Expand All @@ -226,7 +217,7 @@ private void GetValueCallback(IReadOnlyOscParameterCollection sender, ParameterC

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