Collection of CustomType (Serialization)

I was able to setup a CustomType for serialization but I've got some fairly lengthy nested classes to send when an item is initialized and at the very least, a collection of customtype would be handy. The Serialization Documentation indicated that for Array "T-type can be any of the types listed in this table except byte." ... which surely rules out custom types. ..but for Dictionary "K-type can be any of the types listed in this table." with no limit spec'd on Object. I figured I'd give it a shot.

It didn't work for me. I got two different dictionary related exceptions depending on whether the key was a string or int/byte

I thought I would ask if I did something wrong or if this is not supported.


Exceptions:
ArgumentException: An element with the same key already exists in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,ExitGames.Client.Photon.CustomType].Add (System.String key, ExitGames.Client.Photon.CustomType value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404)
System.Collections.Generic.Dictionary`2[System.String,ExitGames.Client.Photon.CustomType].System.Collections.IDictionary.Add (System.Object key, System.Object value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:671)
ExitGames.Client.Photon.Protocol.DeserializeDictionary (System.IO.MemoryStream din) (at h:/svncontent/photon-sdk-dotnet/PhotonDotNet/Protocol.cs:1812)


ArgumentException: not of type: ExitGames.Client.Photon.CustomType
Parameter name: value
System.Collections.Generic.Dictionary`2[System.Int32,ExitGames.Client.Photon.CustomType].ToTValue (System.Object value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:542)
System.Collections.Generic.Dictionary`2[System.Int32,ExitGames.Client.Photon.CustomType].System.Collections.IDictionary.Add (System.Object key, System.Object value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:164)
ExitGames.Client.Photon.Protocol.DeserializeDictionary (System.IO.MemoryStream din) (at h:/svncontent/photon-sdk-dotnet/PhotonDotNet/Protocol.cs:1812)

Comments

  • vreference
    edited May 2017
    This quote from another thread certainly makes it sound like a collection of customtypes should be possible:
    Tobias said:

    I got a fix for the next release. I missed updating the serialization methods for arrays of custom types.

    Should be out next week.

    I believe I actually started by testing with an array but when I read "can be any of the types listed in this table" in the docs for array, I moved on.


    While I'm here - and assuming collections of custom types are supported - is there an existing way to handle CustomTypes that have member collections? Are there existing serializers/deserializers I could call from my custom serialize method so I don't have to write my own handlers for functionality that photon already has?
  • vreference
    edited May 2017
    For anyone who comes looking; I'm pretty sure the photon serialization system doesn't have support for nesting. Look for ProtoBuf; use it in your Serializer/Deserializer implementations - job done.

    public static byte[] Serialize(object input)
    {
    YourType theThing = (YourType)input;
    MemoryStream stream = new MemoryStream();
    Serializer.SerializeWithLengthPrefix(stream, theThing, PrefixStyle.Fixed32);
    return ToByteArray(stream);
    }

    public static object Deserialize(byte[] data)
    {
    Stream stream = new MemoryStream(data);
    YourType output = Serializer.DeserializeWithLengthPrefix(stream, PrefixStyle.Fixed32);
    return output;
    }

    Not sure it's particularly optimized (seriously just got working 2 minutes ago after an hour or so) - but looks good so far.
  • Hey @vreference this was a very timely find for me. I'll use ProtoBuf-net as well. It seems that the fix may not be in yet or something. I was trying to send a dictionary with one float, and the other entry an array of custom types and that didn't work either.

    Looks like Protobuf for the win until they fix/implement serialization of nested classes. Thanks for the tip!
  • Could either of you send a repro case?
    The error seen in the first post looks more like the comparison of the custom type leads to duplicates, which makes the Dictionary fail adding keys.

    We can have another look then.
  • Mail the repro to: developer@photonengine.com please.
  • I'll see if I can reproduce it when I get home. It should be reproducible by setting up a custom type for serialization and then putting that type in a collection that will be sent via serialization... e.g., add a List<string, TheCustomType> to the properties collection of the mmo demo. I'm not sure what version of photon this is on but I just downloaded the library fresh a month or two ago - whenever you start seeing activity on my server license for the first time in a few years.

    The two exceptions I list in the first post are generated from the exact same code, only changing the Type of the Dictionary key. e.g., Dictionary<Int,TheCustomType> vs Dictionary<string,TheCustomType>; no other changes.

    Also wow... I just killed the comment I was editing while applying the "code" format.