Author Topic: TNet equivalent of OnSerializeNetworkView  (Read 2244 times)

tylerglaiel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
TNet equivalent of OnSerializeNetworkView
« on: January 19, 2014, 10:17:45 PM »
Hey I'm in the process of porting my game over from Unity built in networking to TNet, and the lack of an equivalent to OnSerializeNetworkView was bugging me, so I wrote one (modified from TNAutoSync), and here's the code if you want it.

I havent finished migrating my game over, so I haven't tested this very well, and it's missing a little bit of functionality, and you 100% SHOULD NOT use this for production code (it sends way too much uncompressed data without checks for redundancy), it's just to ease the process of switching off of unity built in networking so you can get it working fast before you go to optimize it.

TO USE: add TNAutoSerializer to your class, hook the monobehavior that needs to be synced into it, then in your script replace
  1. void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
with
  1. void SyncNetworkBytes(GlaielBitStream stream) {

  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4. using System.IO;
  5.  
  6. public class GlaielBitStream {
  7.     public bool isReading;
  8.     public bool isWriting;
  9.  
  10.     MemoryStream stream;
  11.  
  12.     BinaryReader reader;
  13.     BinaryWriter writer;
  14.  
  15.    
  16.  
  17.  
  18.     public byte[] getData(){
  19.         return stream.GetBuffer();
  20.     }
  21.  
  22.     public GlaielBitStream(byte[] initialData = null){
  23.         bool writing = initialData == null;
  24.  
  25.         isReading = !writing;
  26.         isWriting = writing;
  27.  
  28.         if(isReading){
  29.             stream = new MemoryStream(initialData);
  30.             reader = new BinaryReader(stream);
  31.         } else {
  32.             stream = new MemoryStream();
  33.             writer = new BinaryWriter(stream);
  34.         }
  35.  
  36.        
  37.     }
  38.  
  39.     public void Serialize(ref bool value){
  40.         if(isReading){
  41.             value = reader.ReadBoolean();
  42.         }
  43.         if(isWriting){
  44.             writer.Write(value);
  45.         }
  46.     }
  47.     public void Serialize(ref char value){
  48.         if (isReading) {
  49.             value = reader.ReadChar();
  50.         }
  51.         if (isWriting) {
  52.             writer.Write(value);
  53.         }
  54.     }
  55.     public void Serialize(ref float value){
  56.         if (isReading) {
  57.             value = reader.ReadSingle();
  58.         }
  59.         if (isWriting) {
  60.             writer.Write(value);
  61.         }
  62.     }
  63.     public void Serialize(ref int value){
  64.         if (isReading) {
  65.             value = reader.ReadInt32();
  66.         }
  67.         if (isWriting) {
  68.             writer.Write(value);
  69.         }
  70.     }
  71.     /*public void Serialize(ref NetworkPlayer value);
  72.     public void Serialize(ref NetworkViewID viewID);*/
  73.     public void Serialize(ref Quaternion value){
  74.         if (isReading) {
  75.             value.x = reader.ReadSingle();
  76.             value.y = reader.ReadSingle();
  77.             value.z = reader.ReadSingle();
  78.             value.w = reader.ReadSingle();
  79.         }
  80.         if (isWriting) {
  81.             writer.Write(value.x);
  82.             writer.Write(value.y);
  83.             writer.Write(value.z);
  84.             writer.Write(value.w);
  85.         }
  86.     }
  87.     public void Serialize(ref short value){
  88.         if (isReading) {
  89.             value = reader.ReadInt16();
  90.         }
  91.         if (isWriting) {
  92.             writer.Write(value);
  93.         }
  94.     }
  95.     public void Serialize(ref Vector3 value){
  96.         if (isReading) {
  97.             value.x = reader.ReadSingle();
  98.             value.y = reader.ReadSingle();
  99.             value.z = reader.ReadSingle();
  100.         }
  101.         if (isWriting) {
  102.             writer.Write(value.x);
  103.             writer.Write(value.y);
  104.             writer.Write(value.z);
  105.         }
  106.     }
  107.     /*public void Serialize(ref float value, float maxDelta);
  108.     public void Serialize(ref Quaternion value, float maxDelta);
  109.     public void Serialize(ref Vector3 value, float maxDelta);*/
  110. }
  111.  
  112. public class TNAutoSerializer : TNBehaviour {
  113.  
  114.     public MonoBehaviour scriptToListenOn;
  115.     bool mCanSync = false;
  116.  
  117.     void OnNetworkJoinChannel(bool success, string err) { mCanSync = success; }
  118.  
  119.  
  120.     public int updatesPerSecond = 20;
  121.     public bool isSavedOnServer = true;
  122.     public bool onlyOwnerCanSync = true;
  123.     public bool isImportant = true;
  124.  
  125.         // Use this for initialization
  126.         void Awake () {
  127.         if (TNManager.isInChannel) mCanSync = true;
  128.         StartCoroutine(PeriodicSync());
  129.         }
  130.  
  131.     IEnumerator PeriodicSync() {
  132.         for (; ; ) {
  133.             //if (!TNManager.isInChannel) break;
  134.             if (mCanSync && (!onlyOwnerCanSync || tno.isMine)) Sync();
  135.  
  136.  
  137.             yield return new WaitForSeconds(1f / updatesPerSecond);
  138.         }
  139.     }
  140.  
  141.     void Sync(){
  142.         GlaielBitStream gStream = new GlaielBitStream();
  143.         scriptToListenOn.BroadcastMessage("SyncNetworkBytes", gStream);
  144.  
  145.         if (isImportant) {
  146.             tno.Send(255, isSavedOnServer ? Target.OthersSaved : Target.Others, gStream.getData());
  147.         } else {
  148.             tno.SendQuickly(255, isSavedOnServer ? Target.OthersSaved : Target.Others, gStream.getData());
  149.         }
  150.     }
  151.  
  152.     [RFC(255)]
  153.     void Syncer(byte[] bytes){
  154.         GlaielBitStream gStream = new GlaielBitStream(bytes);
  155.         scriptToListenOn.BroadcastMessage("SyncNetworkBytes", gStream);
  156.     }
  157. }
  158.  
« Last Edit: January 19, 2014, 10:28:57 PM by tylerglaiel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet equivalent of OnSerializeNetworkView
« Reply #1 on: January 20, 2014, 01:19:39 AM »
Cool, thanks for sharing. Just keep in mind that in Unity's networking, Stream serialization is always one-way, and you can never change the direction. In your case, you can tweak it so that it can, which makes it better!