Lachee / discord-rpc-unity

C# custom implementation for Discord Rich Presence. Unity package to easy add to your own games.
MIT License
43 stars 2 forks source link

Newtonsoft.JSON error on IL2CPP #6

Closed reithegoat closed 2 years ago

reithegoat commented 4 years ago

I downloaded and currently using a fork of Newtonsoft.JSON that has compatibility with Unity's IL2CPP (the original does not have this kind of support). However, I am getting this error and DiscordRPC is stuck in an endless loop. It is repeating this error.

Unity version: 2019.4 LTS Newtonsoft.JSON Fork: https://github.com/jilleJr/Newtonsoft.Json-for-Unity

TRCE: Connecting to the pipe through the DiscordRPC.Unity.UnityNamedPipe
TRCE: PIPE WIN
INFO: Connecting to discord-ipc-0 ()
INFO: Connected
TRCE: Connected to the pipe. Attempting to establish handshake...
TRCE: Enqueue Message: ConnectionEstablished
TRCE: Attempting to establish a handshake...
TRCE: Sending Handshake...
TRCE: Writing frame
TRCE: Setting the connection state to CONNECTING
TRCE: Connection Established. Starting reading loop...
TRCE: Read 0 bytes
TRCE: Read 54 bytes
TRCE: Read pipe frame!
TRCE: Read Payload: Close
ERR : Unhandled Exception: Newtonsoft.Json.JsonSerializationException
ERR : Unable to find a constructor to use for type DiscordRPC.RPC.Payload.ClosePayload. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'code', line 1, position 8.
ERR :   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <00000000000000000000000000000000>:0 
  at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <00000000000000000000000000000000>:0 
  at CodeStage.AntiCheat.Storage.ObscuredPrefs.DecryptValue[T] (System.String key, System.String encryptedKey, T defaultValue, System.String encryptedInput) [0x00000] in <00000000000000000000000000000000>:0 
  at DiscordRPC.RPC.RpcConnection.MainLoop () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadStart.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadHelper.ThreadStart () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadStart.Invoke () [0x00000] in <00000000000000000000000000000000>:0 
TRCE: Closing the named pipe.
TRCE: Closing stream
TRCE: Setting the connection state to DISCONNECTED
Lachee commented 4 years ago

Hmm, this is not something I was expecting. When I originally wrote this library, IL2CPP were only for game consoles which couldn't obviously run Discord.

I will look at this over the weekend.

reithegoat commented 4 years ago

When I originally wrote this library, IL2CPP were only for game consoles which couldn't obviously run Discord. Oh? It's available for Windows which increases performance and code security, which is why I am trying to use it.

reithegoat commented 4 years ago

UPDATE!! IL2CPP seems to strip the coding for DiscordRPC and adding an exception within a link.xml file, you could get a different and less worse (assumption) error code.

WARN: Client has not yet initialized, but events are being subscribed too. Storing them as state instead.
WARN: The client is not yet initialized, storing the presence as a state instead.
TRCE: Enqueue Command: DiscordRPC.RPC.Commands.PresenceCommand
INFO: Attempting a new connection
INFO: RPC Connection Started
TRCE: ============================
TRCE: Assembly:             DiscordRPC, Version=1.0.152.0, Culture=neutral, PublicKeyToken=null
TRCE: Pipe:                 DiscordRPC.Unity.UnityNamedPipe
TRCE: Platform:             Microsoft Windows NT 10.0.18363.0
TRCE: applicationID:        673382310820839434
TRCE: targetPipe:           -1
TRCE: POLL_RATE:            1000
TRCE: _maxRtQueueSize:      512
TRCE: _maxRxQueueSize:      128
TRCE: ============================

\/\/\/\/\/ LOOP BEGINS \/\/\/\/\/
TRCE: Connecting to the pipe through the DiscordRPC.Unity.UnityNamedPipe
TRCE: PIPE WIN
INFO: Connecting to discord-ipc-0 ()
INFO: Connected
TRCE: Connected to the pipe. Attempting to establish handshake...
TRCE: Enqueue Message: ConnectionEstablished
TRCE: Attempting to establish a handshake...
TRCE: Sending Handshake...
TRCE: Writing frame
TRCE: Setting the connection state to CONNECTING
TRCE: Connection Established. Starting reading loop...
TRCE: Read 0 bytes
TRCE: Enqueue Command: DiscordRPC.RPC.Commands.PresenceCommand
TRCE: Read 54 bytes
TRCE: Read pipe frame!
TRCE: Read Payload: Close
WARN: We have been told to terminate by discord: (0) 
TRCE: Enqueue Message: Close
TRCE: Left main read loop for some reason. Aborting: False, Shutting Down: False
TRCE: Waiting 1095ms before attempting to connect again
TRCE: Closing the named pipe.
TRCE: Closing stream
TRCE: Setting the connection state to DISCONNECTED

EDIT: This is where I found the workaround: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-link.xml

Lachee commented 2 years ago

This issue has been fixed with v1.0.2