DeltaCompressionRead









How do I use Delta Compression Read
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
3339     private void OnSerializeRead(Hashtable data, PhotonPlayer sender, int networkTime, short correctPrefix)
3340     {
3341         // read view ID from key (byte)0: a int-array (PUN 1.17++)
3342         int viewID = (int)data[(byte)0];
3343
3344
3345         PhotonView view = this.GetPhotonView(viewID);
3346         if (view == null)
3347         {
3348             Debug.LogWarning("Received OnSerialization for view ID " + viewID + ". We have no such PhotonView! Ignored this if you're leaving a room. State: " + this.State);
3349             return;
3350         }
3351
3352         if (view.prefix > 0 && correctPrefix != view.prefix)
3353         {
3354             Debug.LogError("Received OnSerialization for view ID " + viewID + " with prefix " + correctPrefix + ". Our prefix is " + view.prefix);
3355             return;
3356         }
3357
3358         // SetReceiving filtering
3359         if (view.group != 0 && !this.allowedReceivingGroups.Contains(view.group))
3360         {
3361             return; // Ignore group
3362         }
3363
3364
3365         if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3366         {
3367             if (!this.DeltaCompressionRead(view, data))
3368             {
3369                 // Skip this packet as we haven't got received complete-copy of this view yet.
3370                 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
3371                     Debug.Log("Skipping packet for " + view.name + " [" + view.viewID + "] as we haven't received a full packet for delta compression yet. This is OK if it happens for the first few frames after joining a game.");
3372                 return;
3373             }
3374
3375             // store last received for delta-compression usage
3376             view.lastOnSerializeDataReceived = data[(byte)1] as object[];
3377         }
3378
3379         if (sender.ID != view.ownerId)
3380         {
3381             if (!view.isSceneView || !sender.isMasterClient)
3382             {
3383                 // obviously the owner changed and we didn't yet notice.
3384                 Debug.Log("Adjusting owner to sender of updates. From: " + view.ownerId + " to: " + sender.ID);
3385                 view.ownerId = sender.ID;
3386             }
3387         }
3388
3389         object[] contents = data[(byte)1] as object[];
3390         PhotonStream pStream = new PhotonStream(false, contents);
3391         PhotonMessageInfo info = new PhotonMessageInfo(sender, networkTime, view);
3392
3393         view.DeserializeView( pStream, info );
3394     }
File name: NetworkingPeer.cs Copy
3499     private bool DeltaCompressionRead(PhotonView view, Hashtable data)
3500     {
3501         if (data.ContainsKey((byte)1))
3502         {
3503             // we have a full list of data (cause key 1 is used), so return "we have uncompressed all"
3504             return true;
3505         }
3506
3507         // Compression was applied as data[(byte)2] exists (this is the data with some fields being compressed to null)
3508         // now we also need a previous "full" list of values to restore values that are null in this msg
3509         if (view.lastOnSerializeDataReceived == null)
3510         {
3511             return false; // We dont have a full match yet, we cannot work with missing values: skip this message
3512         }
3513
3514         object[] compressedContents = data[(byte)2] as object[];
3515         if (compressedContents == null)
3516         {
3517             // despite expectation, there is no compressed data in this msg. shouldn't happen. just a null check
3518             return false;
3519         }
3520
3521         int[] indexesThatAreChangedToNull = data[(byte)3] as int[];
3522         if (indexesThatAreChangedToNull == null)
3523         {
3524             indexesThatAreChangedToNull = new int[0];
3525         }
3526
3527         object[] lastReceivedData = view.lastOnSerializeDataReceived;
3528         for (int index = 0; index < compressedContents.Length; index++)
3529         {
3530             if (compressedContents[index] == null && !indexesThatAreChangedToNull.Contains(index))
3531             {
3532                 // we replace null values in this received msg unless a index is in the "changed to null" list
3533                 object lastValue = lastReceivedData[index];
3534                 compressedContents[index] = lastValue;
3535             }
3536         }
3537
3538         data[(byte)1] = compressedContents; // compressedContents are now uncompressed...
3539         return true;
3540     }

DeltaCompressionRead 88 lượt xem

Gõ tìm kiếm nhanh...