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

Fix for hard-coded nu-get Newtonsoft usage in C# SDK causing Unity IL2CPP builds to crash #219

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
} else {
var subscribeManager = new SubscribeManager2(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, pubnubTokenMgr, PubnubInstance);
var eventEmitter = new EventEmitter(config, SubscribeListenerList, jsonLibrary, pubnubTokenMgr, pubnubLog, PubnubInstance);
subscribeEventEngine = subscribeEventEngineFactory.InitializeEventEngine(instanceId, PubnubInstance, config, subscribeManager, eventEmitter, StatusEmitter);
subscribeEventEngine = subscribeEventEngineFactory.InitializeEventEngine(instanceId, PubnubInstance, config, subscribeManager, eventEmitter, jsonLibrary, StatusEmitter);
subscribeEventEngine.OnStateTransition += SubscribeEventEngine_OnStateTransition;
subscribeEventEngine.OnEventQueued += SubscribeEventEngine_OnEventQueued;
subscribeEventEngine.OnEffectDispatch += SubscribeEventEngine_OnEffectDispatch;
Expand Down Expand Up @@ -185,7 +185,7 @@
}
}

private void MessageEmitter<T>(Pubnub pubnubInstance, PNMessageResult<T> messageResult)

Check warning on line 188 in src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs

View workflow job for this annotation

GitHub Actions / Integration and Unit tests

Type parameter 'T' has the same name as the type parameter from outer type 'SubscribeEndpoint<T>'

Check warning on line 188 in src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs

View workflow job for this annotation

GitHub Actions / Acceptance tests

Type parameter 'T' has the same name as the type parameter from outer type 'SubscribeEndpoint<T>'
{
foreach (var listener in SubscribeListenerList)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/EndPoint/PubSub/SubscribeManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task<Tuple<HandshakeResponse, PNStatus>> HandshakeRequest(PNOperati
if (!string.IsNullOrEmpty(responseTuple.Item1) && responseTuple.Item2 == null)
{
PNStatus status = new PNStatus(null, PNOperationType.PNSubscribeOperation, PNStatusCategory.PNConnectedCategory, channels, channelGroups);
HandshakeResponse handshakeResponse = JsonConvert.DeserializeObject<HandshakeResponse>(responseTuple.Item1);
HandshakeResponse handshakeResponse = jsonLibrary.DeserializeToObject<HandshakeResponse>(responseTuple.Item1);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

return new Tuple<HandshakeResponse, PNStatus>(handshakeResponse, status);
}

Expand Down Expand Up @@ -170,7 +170,7 @@ internal async Task<Tuple<ReceivingResponse<object>, PNStatus>> ReceiveRequest<T
if (!string.IsNullOrEmpty(responseTuple.Item1) && responseTuple.Item2 == null)
{
PNStatus status = new PNStatus(null, PNOperationType.PNSubscribeOperation, PNStatusCategory.PNConnectedCategory, channels, channelGroups);
ReceivingResponse<object> receiveResponse = JsonConvert.DeserializeObject<ReceivingResponse<object>>(responseTuple.Item1);
ReceivingResponse<object> receiveResponse = jsonLibrary.DeserializeToObject<ReceivingResponse<object>>(responseTuple.Item1);
return new Tuple<ReceivingResponse<object>, PNStatus>(receiveResponse, status);
}
else if (responseTuple.Item2 != null)
Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/EventEngine/Common/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ public void EmitEvent<T>(object e)
Timetoken = pnFileResult.Timetoken,
Publisher = pnFileResult.Publisher,
};
Dictionary<string, object> pnMsgObjDic = JsonDataParseInternalUtil.ConvertToDictionaryObject(pnFileResult.Message);
Dictionary<string, object> pnMsgObjDic = jsonLibrary.ConvertToDictionaryObject(pnFileResult.Message);
if (pnMsgObjDic != null && pnMsgObjDic.Count > 0) {
if (pnMsgObjDic.ContainsKey("message") && pnMsgObjDic["message"] != null) {
fileMessage.Message = pnMsgObjDic["message"];
}
if (pnMsgObjDic.ContainsKey("file")) {
Dictionary<string, object> fileObjDic = JsonDataParseInternalUtil.ConvertToDictionaryObject(pnMsgObjDic["file"]);
Dictionary<string, object> fileObjDic = jsonLibrary.ConvertToDictionaryObject(pnMsgObjDic["file"]);
if (fileObjDic != null && fileObjDic.ContainsKey("id") && fileObjDic.ContainsKey("name")) {
fileMessage.File = new PNFile { Id = fileObjDic["id"].ToString(), Name = fileObjDic["name"].ToString() };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PubnubApi.EventEngine.Common;
using PubnubApi.EventEngine.Core;
using PubnubApi.EventEngine.Subscribe.Invocations;
Expand All @@ -14,18 +12,18 @@ public class EmitMessagesHandler : EffectHandler<EmitMessagesInvocation>
{
private readonly Dictionary<string, Type> channelTypeMap;
private readonly Dictionary<string, Type> channelGroupTypeMap;
private readonly JsonSerializer serializer;
private readonly IJsonPluggableLibrary jsonPluggableLibrary;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

private readonly EventEmitter eventEmitter;

public EmitMessagesHandler(EventEmitter eventEmitter,
JsonSerializer serializer,
IJsonPluggableLibrary jsonPluggableLibrary,
Dictionary<string, Type> channelTypeMap = null,
Dictionary<string, Type> channelGroupTypeMap = null)
{
this.eventEmitter = eventEmitter;
this.channelTypeMap = channelTypeMap;
this.channelGroupTypeMap = channelGroupTypeMap;
this.serializer = serializer;
this.jsonPluggableLibrary = jsonPluggableLibrary;
}

public async override Task Run(EmitMessagesInvocation invocation)
Expand All @@ -45,17 +43,14 @@ public async override Task Run(EmitMessagesInvocation invocation)
private object DeserializePayload(string key, object rawMessage)
{
try {
if (rawMessage is JObject message) {
Type t;
if ((channelTypeMap is not null && channelTypeMap.TryGetValue(key, out t) ||
channelGroupTypeMap is not null && channelGroupTypeMap.TryGetValue(key, out t)) &&
t != typeof(string)) {
return message.ToObject(t, serializer);
} else {
return message.ToString(Formatting.None);
}
Type t;
if ((channelTypeMap is not null && channelTypeMap.TryGetValue(key, out t) ||
channelGroupTypeMap is not null && channelGroupTypeMap.TryGetValue(key, out t)) &&
t != typeof(string))
{
return jsonPluggableLibrary.DeserializeToObject(rawMessage, t);
} else {
return rawMessage;
return rawMessage.ToString();
}
} catch (Exception) {
return rawMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using PubnubApi.EventEngine.Subscribe.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using PubnubApi.EventEngine.Subscribe.Events;
using PubnubApi.EventEngine.Common;
Expand All @@ -17,11 +15,7 @@ public class SubscribeEventEngine : Engine
private SubscribeManager2 subscribeManager;
private readonly Dictionary<string, Type> channelTypeMap = new Dictionary<string, Type>();
private readonly Dictionary<string, Type> channelGroupTypeMap = new Dictionary<string, Type>();

private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings()
{ Formatting = Formatting.None, DateParseHandling = DateParseHandling.None };
private static readonly JsonSerializer Serializer = JsonSerializer.Create(SerializerSettings);

private readonly IJsonPluggableLibrary jsonPluggableLibrary;

public string[] Channels { get; set; } = new string[] {};
public string[] Channelgroups { get; set; } = new string[] {};
Expand All @@ -30,9 +24,11 @@ internal SubscribeEventEngine(Pubnub pubnubInstance,
PNConfiguration pubnubConfiguration,
SubscribeManager2 subscribeManager,
EventEmitter eventEmitter,
IJsonPluggableLibrary jsonPluggableLibrary,
Action<Pubnub, PNStatus> statusListener = null)
{
this.subscribeManager = subscribeManager;
this.jsonPluggableLibrary = jsonPluggableLibrary;
var handshakeHandler = new Effects.HandshakeEffectHandler(subscribeManager, EventQueue);
var handshakeReconnectHandler = new Effects.HandshakeReconnectEffectHandler(pubnubConfiguration, EventQueue, handshakeHandler);

Expand All @@ -49,7 +45,7 @@ internal SubscribeEventEngine(Pubnub pubnubInstance,
dispatcher.Register<Invocations.ReceiveReconnectInvocation, Effects.ReceivingReconnectEffectHandler>(receiveReconnectHandler);
dispatcher.Register<Invocations.CancelReceiveReconnectInvocation, Effects.ReceivingReconnectEffectHandler>(receiveReconnectHandler);

var emitMessageHandler = new Effects.EmitMessagesHandler(eventEmitter, Serializer, channelTypeMap, channelGroupTypeMap);
var emitMessageHandler = new Effects.EmitMessagesHandler(eventEmitter, jsonPluggableLibrary, channelTypeMap, channelGroupTypeMap);
dispatcher.Register<Invocations.EmitMessagesInvocation, Effects.EmitMessagesHandler>(emitMessageHandler);

var emitStatusHandler = new Effects.EmitStatusEffectHandler(pubnubInstance, statusListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ internal SubscribeEventEngine InitializeEventEngine(string instanceId,
PNConfiguration pubnubConfiguration,
SubscribeManager2 subscribeManager,
EventEmitter eventEmitter,
IJsonPluggableLibrary jsonPluggableLibrary,
Action<Pubnub, PNStatus> statusListener = null)
{

var subscribeEventEngine = new SubscribeEventEngine(pubnubInstance, pubnubConfiguration: pubnubConfiguration, subscribeManager,eventEmitter, statusListener);
var subscribeEventEngine = new SubscribeEventEngine(pubnubInstance, pubnubConfiguration: pubnubConfiguration, subscribeManager,eventEmitter, jsonPluggableLibrary, statusListener);
if (engineInstances.TryAdd(instanceId, subscribeEventEngine)) {
return subscribeEventEngine;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Api/PubnubApi/Interface/IJsonPluggableLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace PubnubApi
{
Expand All @@ -17,7 +19,9 @@ public interface IJsonPluggableLibrary
T DeserializeToObject<T>(string jsonString);

T DeserializeToObject<T>(List<object> listObject);


object DeserializeToObject(object rawObject, Type type);

Dictionary<string, object> DeserializeToDictionaryOfObject(string jsonString);

Dictionary<string, object> ConvertToDictionaryObject(object localContainer);
Expand Down
Loading
Loading