Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. public class EntityAction {
  2. public int actorID;
  3. public enum actionType { Movement, Skill }
  4. public int actorAction;
  5. public List<int> arguments;
  6.  
  7. public static void Register() {
  8. MonoBehaviour.print("Registering EntityAction Serialization: " + PhotonPeer.RegisterType(typeof(EntityAction), (byte)'E', EntityAction.SerializeEntityAction, EntityAction.DeserializeEntityAction));
  9. }
  10.  
  11. public EntityAction() {
  12. arguments = new List<int>();
  13. }
  14.  
  15. private static short SerializeEntityAction(StreamBuffer outStream, object customobject)
  16. {
  17. BinaryFormatter format = new BinaryFormatter();
  18. MemoryStream argBytes = new MemoryStream();
  19. EntityAction o = (EntityAction)customobject;
  20. format.Serialize(argBytes, o.arguments);
  21.  
  22. byte[] bytes = new byte[argBytes.Length + sizeof(int)*4];
  23. int objectSize = (int)argBytes.Length + sizeof(int)*3;
  24. int index = 0;
  25.  
  26. Protocol.Serialize(objectSize, bytes, ref index);
  27. Protocol.Serialize(o.actorID, bytes, ref index);
  28. Protocol.Serialize(o.actorAction, bytes, ref index);
  29. Protocol.Serialize((int)argBytes.Length, bytes, ref index);
  30.  
  31. argBytes.ToArray().CopyTo(bytes, index);
  32. index += (int)argBytes.Length;
  33.  
  34. outStream.Write(bytes, 0, (int)bytes.Length);
  35. return (short)index;
  36. }
  37.  
  38. private static object DeserializeEntityAction(StreamBuffer inStream, short length)
  39. {
  40.  
  41. BinaryFormatter format = new BinaryFormatter();
  42. MemoryStream argBytes = new MemoryStream();
  43. EntityAction o = new EntityAction();
  44.  
  45. int index = 0;
  46. int argBytesLength;
  47. int objSize;
  48.  
  49. byte[] objBuf = new byte[sizeof(int)]; // Create a buffer to read in a single int.
  50. inStream.Read(objBuf, 0, sizeof(int)); // Read in one int worth of bytes
  51. Protocol.Deserialize(out objSize, objBuf, ref index); // Deserialize that int as the size of the whole remaining object
  52.  
  53. byte[] mem = new byte[objSize]; // Read in that object
  54. inStream.Read(mem, 0, objSize); //
  55.  
  56. index = 0;
  57. Protocol.Deserialize(out o.actorID, mem, ref index);
  58. Protocol.Deserialize(out o.actorAction, mem, ref index);
  59. Protocol.Deserialize(out argBytesLength, mem, ref index);
  60.  
  61. byte[] argArray = new byte[argBytesLength]; // Create a buffer the size of the list
  62. Array.Copy(mem, index, argArray, 0, argBytesLength); // Copy the memory representing the list into that buffer
  63.  
  64. argBytes.Write(argArray, 0, argBytesLength); // Write that buffer into the memory stream to be deserialized
  65.  
  66. argBytes.Position = 0;
  67. if(argBytes.Length == 0) o.arguments = new List<int>();
  68. else o.arguments = format.Deserialize(argBytes) as List<int>;
  69.  
  70.  
  71. return o;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement