Skip to content

Commit

Permalink
Fix Writer for older Unity version (#18)
Browse files Browse the repository at this point in the history
* Update Options.cs

* more checks
  • Loading branch information
theomonnom authored Jul 20, 2022
1 parent 20a7cdb commit d9b61e9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Runtime/Plugins/livekit-bridge.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ var NativeLib = {
PushFunction: function (ptr, fnc, labelPtr) {
var label = UTF8ToString(labelPtr);
LKBridge.Stack.push(function () {
if (!LKBridge.Data.has(ptr)) {
console.warn("Trying to fire an event on a freed object", ptr, fnc, label);
return;
}

try{
LKBridge.StackCSharp = Array.from(arguments);
LKBridge.FunctionInstance = this;
Expand Down
1 change: 0 additions & 1 deletion Runtime/Scripts/Internal/HTMLVideoElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ private static void ResizeEvent(IntPtr ptr)
catch (Exception e)
{
Log.Error($"Error happened on HTMLVideoElement.VideoReceived ( Is your listeners working correctly ? ): {Environment.NewLine} {e.Message}");
throw;
}
}

Expand Down
3 changes: 1 addition & 2 deletions Runtime/Scripts/Internal/JSNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
[assembly: InternalsVisibleTo("LiveKit.BridgeTests")]
namespace LiveKit
{
[SuppressUnmanagedCodeSecurity]
internal static class JSNative
{
// JSHandle can't be marshalled in a delegate
// JSHandle must be created with ptr when the callback is called
public delegate void JSDelegate(IntPtr ptr);

internal static JsonSerializerSettings JsonSettings = new JsonSerializerSettings()
public static JsonSerializerSettings JsonSettings = new JsonSerializerSettings()
{
Formatting = Formatting.None,
NullValueHandling = NullValueHandling.Ignore,
Expand Down
1 change: 0 additions & 1 deletion Runtime/Scripts/Internal/JSObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using UnityEngine;
using UnityEngine.Scripting;

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Scripts/Internal/JSRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal static T Acquire<T>(JSHandle handle) where T : JSRef
{
if (handle.IsClosed || handle.IsInvalid)
throw new Exception("Trying to acquire an invalid handle");

var ptr = handle.DangerousGetHandle();
if (Cache.TryGetValue(ptr, out var wRef) && wRef.TryGetTarget(out JSRef jsRef))
return jsRef as T;
Expand All @@ -62,7 +62,7 @@ internal static T Acquire<T>(JSHandle handle) where T : JSRef
if (s_TypeMap.TryGetValue(typeName, out Type correctType))
type = correctType;
}

return Activator.CreateInstance(type, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[]{handle}, null) as T;
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Room/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class Room : JSEventEmitter<RoomEvent>, IDisposable
public event TrackSubscriptionPermissionChangedDelegate TrackSubscriptionPermissionChanged;
public event AudioPlaybackChangedDelegate AudioPlaybackChanged;

[MonoPInvokeCallback(typeof(Action<IntPtr>))]
[MonoPInvokeCallback(typeof(JSNative.JSDelegate))]
private static void EventReceived(IntPtr iptr)
{
var handle = new JSHandle(iptr, true);
Expand Down
14 changes: 14 additions & 0 deletions Runtime/Scripts/Room/Track/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Newtonsoft.Json.Converters;
using System;
using System.Runtime.Serialization;
using UnityEngine.Scripting;

namespace LiveKit
{
Expand All @@ -25,6 +26,12 @@ public struct CreateLocalTracksOptions

public class CreateLocalTracksOptionsWriter : JsonConverter<CreateLocalTracksOptions>
{
[Preserve]
public CreateLocalTracksOptionsWriter()
{

}

public override bool CanRead => false;

public override CreateLocalTracksOptions ReadJson(JsonReader reader, Type objectType, CreateLocalTracksOptions existingValue, bool hasExistingValue, JsonSerializer serializer)
Expand Down Expand Up @@ -133,6 +140,13 @@ public struct ScreenShareCaptureOptions

public class ScreenShareCaptureOptionsWriter : JsonConverter<ScreenShareCaptureOptions>
{

[Preserve]
public ScreenShareCaptureOptionsWriter()
{

}

public override bool CanRead => false;

public override ScreenShareCaptureOptions ReadJson(JsonReader reader, Type objectType, ScreenShareCaptureOptions existingValue, bool hasExistingValue, JsonSerializer serializer)
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Scripts/Room/Track/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public TrackStreamState StreamState
}
}

[MonoPInvokeCallback(typeof(Action<IntPtr>))]
[MonoPInvokeCallback(typeof(JSNative.JSDelegate))]
private static void EventReceived(IntPtr iptr)
{
var handle = new JSHandle(iptr, true);
Expand Down

0 comments on commit d9b61e9

Please sign in to comment.