Skip to content

Instantly share code, notes, and snippets.

@pjc0247
Created February 27, 2019 15:47
Show Gist options
  • Save pjc0247/7cabc699e603c2f6c529c3c4d35a7848 to your computer and use it in GitHub Desktop.
Save pjc0247/7cabc699e603c2f6c529c3c4d35a7848 to your computer and use it in GitHub Desktop.
photon.
SerializationException: Type 'Photon.Realtime.Player' in Assembly 'PhotonRealtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.FormatterServices+<>c__DisplayClass9_0.<GetSerializableMembers>b__0 (System.Runtime.Serialization.MemberHolder _) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, System.Runtime.Serialization.StreamingContext context) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo () (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize (System.Object obj, System.Runtime.Serialization.ISurrogateSelector surrogateSelector, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.Formatters.Binary.SerObjectInfoInit serObjectInfoInit, System.Runtime.Serialization.IFormatterConverter converter, System.Runtime.Serialization.Formatters.Binary.ObjectWriter objectWriter, System.Runtime.Serialization.SerializationBinder binder) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo arrayElemTypeNameInfo, System.Object data) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo memberObjectInfo) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write (System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo objectInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo memberNameInfo, System.Runtime.Serialization.Formatters.Binary.NameInfo typeNameInfo) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize (System.Object graph, System.Runtime.Remoting.Messaging.Header[] inHeaders, System.Runtime.Serialization.Formatters.Binary.__BinaryWriter serWriter, System.Boolean fCheck) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers, System.Boolean fCheck) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
Serializer.ObjectToByteArray (System.Object obj) (at Assets/Scripts/Serializer.cs:61)
Serializer.Serialize (System.Object customType) (at Assets/Scripts/Serializer.cs:51)
ExitGames.Client.Photon.Protocol18.WriteCustomTypeBody (ExitGames.Client.Photon.CustomType customType, ExitGames.Client.Photon.StreamBuffer stream, System.Object value) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.WriteCustomType (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.Write (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, ExitGames.Client.Photon.Protocol18+GpType gpType, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.Write (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.WriteHashtable (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.Write (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, ExitGames.Client.Photon.Protocol18+GpType gpType, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.Write (ExitGames.Client.Photon.StreamBuffer stream, System.Object value, System.Boolean writeType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.WriteParameterTable (ExitGames.Client.Photon.StreamBuffer stream, System.Collections.Generic.Dictionary`2[TKey,TValue] parameters) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.Protocol18.SerializeOperationRequest (ExitGames.Client.Photon.StreamBuffer stream, System.Byte operationCode, System.Collections.Generic.Dictionary`2[TKey,TValue] parameters, System.Boolean setType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.EnetPeer.SerializeOperationToMessage (System.Byte opCode, System.Collections.Generic.Dictionary`2[TKey,TValue] parameters, ExitGames.Client.Photon.EgMessageType messageType, System.Boolean encrypt) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.EnetPeer.EnqueueOperation (System.Collections.Generic.Dictionary`2[TKey,TValue] parameters, System.Byte opCode, ExitGames.Client.Photon.SendOptions sendParams, ExitGames.Client.Photon.EgMessageType messageType) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.PhotonPeer.SendOperation (System.Byte operationCode, System.Collections.Generic.Dictionary`2[TKey,TValue] operationParameters, ExitGames.Client.Photon.SendOptions sendOptions) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
Photon.Realtime.LoadBalancingPeer.OpSetPropertiesOfRoom (ExitGames.Client.Photon.Hashtable gameProperties, ExitGames.Client.Photon.Hashtable expectedProperties, Photon.Realtime.WebFlags webflags) (at Assets/Photon/PhotonRealtime/Code/LoadbalancingPeer.cs:611)
Photon.Realtime.Room.SetCustomProperties (ExitGames.Client.Photon.Hashtable propertiesToSet, ExitGames.Client.Photon.Hashtable expectedProperties, Photon.Realtime.WebFlags webFlags) (at Assets/Photon/PhotonRealtime/Code/Room.cs:385)
Room.UpdateGameContext[T] (T o) (at Assets/Scripts/Game/Room.cs:55)
GameBase`1[T].UpdateContext (T ctx) (at Assets/Scripts/Game/GameBase.cs:65)
ATMGame.RpcWithdraw (Photon.Realtime.Player sender, System.Int32 amount) (at Assets/Scripts/Game/ATM/ATMGame.cs:55)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
Photon.Pun.PhotonNetwork.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, Photon.Realtime.Player sender) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:468)
Photon.Pun.PhotonNetwork.RPC (Photon.Pun.PhotonView view, System.String methodName, Photon.Pun.RpcTarget target, Photon.Realtime.Player player, System.Boolean encrypt, System.Object[] parameters) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1060)
Photon.Pun.PhotonNetwork.RPC (Photon.Pun.PhotonView view, System.String methodName, Photon.Pun.RpcTarget target, System.Boolean encrypt, System.Object[] parameters) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetwork.cs:2723)
Photon.Pun.PhotonView.RPC (System.String methodName, Photon.Pun.RpcTarget target, System.Object[] parameters) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:410)
GameBaseNotifiable.RpcAll (System.String name, System.Object[] args) (at Assets/Scripts/Game/GameBase.cs:29)
WithdrawalUI.OnClickWithdraw () (at Assets/Scripts/Game/ATM/WithdrawalUI.cs:22)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using ExitGames.Client.Photon;
public static class Serializer
{
public static void RegisterType<T>(byte typeCode)
{
PhotonPeer.RegisterType(
typeof(T),
typeCode,
Serializer.Serialize,
Serializer.Deserialize<T>);
}
[RuntimeInitializeOnLoadMethod]
public static void RegisterPlayerSerializer()
{
Debug.Log("[RegisterPlayerSerializer]");
PhotonPeer.RegisterType(
typeof(Photon.Realtime.Player),
64,
Serializer.SerializePlayer,
Serializer.DeserializePlayer);
}
public static object DeserializePlayer(byte[] data)
{
var userId = Encoding.UTF8.GetString(data);
return Room.players
.Select(x => x.Value.owner)
.Where(x => x.UserId == userId)
.First();
}
public static byte[] SerializePlayer(object customType)
{
var p = (Photon.Realtime.Player)customType;
return Encoding.UTF8.GetBytes(p.UserId);
}
public static object Deserialize<T>(byte[] data)
{
return data.ByteArrayToObject<T>();
}
public static byte[] Serialize(object customType)
{
return (customType).ObjectToByteArray();
}
public static byte[] ObjectToByteArray(this object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
public static T ByteArrayToObject<T>(this byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
object obj = (object)binForm.Deserialize(memStream);
return (T)obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment