Serialization\Deserialization problem

Options
I have a Team class that has an LauncherPlayer[] array which stores info about the players. I have serialization and deserialization methods that I have tested without any problems.

The problem is that in my Team class I am serializing the array, with no problems (I guess, at least I get no errors), but upon deserialization I get an object of type string instead of LauncherPlayer[] type.

Team class:
public class Team
    {
    private int teamID;
    private LauncherPlayer[] players;
    private int playersJoined = 0;
    }
     
    public static byte[] serializeTeam(object o)
    {
    Team team = (Team)o;
    var playerBytes = ExitGames.Client.Photon.Protocol.Serialize(team.players);
    //Lenght = playerBytes.Lenght + (4 * 3) (4 bytes in size, 3 ints)
    byte[] bytes = new byte[playerBytes.Length + 12];
    int index = 0;
    ExitGames.Client.Photon.Protocol.Serialize(team.teamID, bytes, ref index);
    //We need to store the lenght for deserialization
    ExitGames.Client.Photon.Protocol.Serialize(playerBytes.Length, bytes, ref index);
    System.Array.Copy(playerBytes, 0, bytes, index, playerBytes.Length);
    index += playerBytes.Length;
    ExitGames.Client.Photon.Protocol.Serialize(team.playersJoined, bytes, ref index);
    return bytes;
    }
     
    public static object deserializeTeam(byte[] bytes)
    {
    Team team = new Team();
    int index = 0;
    int playerBytesLength;
    ExitGames.Client.Photon.Protocol.Deserialize(out team.teamID, bytes, ref index);
    ExitGames.Client.Photon.Protocol.Deserialize(out playerBytesLength, bytes, ref index);
    var playerBytes = new byte[playerBytesLength];
    System.Array.Copy(bytes, index, playerBytes, 0, playerBytesLength);
    index += playerBytes.Length;
    Debug.Log(ExitGames.Client.Photon.Protocol.Deserialize(playerBytes).GetType());
    Debug.Log((string)ExitGames.Client.Photon.Protocol.Deserialize(playerBytes)+",");
    //team.players = (LauncherPlayer[]) ExitGames.Client.Photon.Protocol.Deserialize(playerBytes);
    ExitGames.Client.Photon.Protocol.Deserialize(out team.playersJoined, bytes, ref index);
    return team;
    }

LauncherPlayer class:
    public class LauncherPlayer {
     
    private string playerName;
    private int teamID = -1;
    private int pos = -1;
    private bool ready = false;
    }
     
    public static byte[] serializePlayer(object o)
    {
    LauncherPlayer player = (LauncherPlayer)o;
    var nameBytes = ExitGames.Client.Photon.Protocol.Serialize(player.playerName);
    //Lenght playerBytes.Lenght + (4 * 3) (4 bytes in size, 3 ints) + (1 byte from bool)
    byte[] bytes = new byte[nameBytes.Length + 13];
    int index = 0;
    //We need to store the lenght for deserialization
    ExitGames.Client.Photon.Protocol.Serialize(nameBytes.Length, bytes, ref index);
    System.Array.Copy(nameBytes, 0, bytes, index, nameBytes.Length);
    index += nameBytes.Length;
    ExitGames.Client.Photon.Protocol.Serialize(player.teamID, bytes, ref index);
    ExitGames.Client.Photon.Protocol.Serialize(player.pos, bytes, ref index);
    bytes[index] = player.ready ? (byte) 1 : (byte) 0;
    return bytes;
    }
     
    public static object deserializePlayer(byte[] bytes)
    {
    LauncherPlayer player = new LauncherPlayer();
    int index = 0;
    int nameBytesLenght;
    ExitGames.Client.Photon.Protocol.Deserialize(out nameBytesLenght, bytes, ref index);
    var nameBytes = new byte[nameBytesLenght];
    System.Array.Copy(bytes, index, nameBytes, 0, nameBytesLenght);
    player.playerName = (string)ExitGames.Client.Photon.Protocol.Deserialize(nameBytes);
    index += nameBytes.Length;
    ExitGames.Client.Photon.Protocol.Deserialize(out player.teamID, bytes, ref index);
    ExitGames.Client.Photon.Protocol.Deserialize(out player.pos, bytes, ref index);
    byte b = bytes [index];
    player.ready = b == (byte)1 ? true : false;
    return player;
    }

Console output:
System.String
    ,

Thanks in advance!

Comments

  • Hi - I think the link to the original question and my answer would help ExitGames' developers to better handle the issue: http://answers.unity3d.com/questions/87 ... oblem.html
  • I completely forgot to post the link. I have been really busy but as soon as I have some time I will implement your solution. Once again, thanks very much for all your help.
  • I'm really sorry to be a pain in the ass, but this subject goes by far out of my knowledge.

    I have implemented your code. Now the objects type comes out right - LauncherPlayer[].
    The problem is that the reference comes back null. Should the deserialize method be modified aswell?

    I'm really lost here.
  • I'm now at work and using another computer, but I'll try to look at my code later, to see if I made any other changes.

    But just to be sure - did you uncomment the line:

    team.players = (LauncherPlayer[]) ExitGames.Client.Photon.Protocol.Deserialize(playerBytes);

    ?
  • Gosh that was it...
  • :P

    If this completely solves the problem, then accepting the answer at Unity Answers would be most welcome ;)