ToArray() public method

Turns the stream into a new object[].
public ToArray ( ) : object[]
return object[]
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private Hashtable OnSerializeWrite(PhotonView view)
    {
        object[] dataArray;

        // 1=Specific data
        if (view.observed is MonoBehaviour)
        {
            MonoBehaviour monob = (MonoBehaviour)view.observed;
            PhotonStream pStream = new PhotonStream(true, null);
            PhotonMessageInfo info = new PhotonMessageInfo(this.mLocalActor, this.ServerTimeInMilliSeconds, view);

            this.ExecuteOnSerialize(monob, pStream, info);
            if (pStream.Count == 0)
            {
                // if an observed script didn't write any data, we don't send anything
                return null;
            }

            dataArray = pStream.ToArray();
        }
        else if (view.observed is Transform)
        {
            Transform trans = (Transform)view.observed;
            List<object> data = new List<object>();

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyPosition
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add(trans.localPosition);
            else
                data.Add(null);

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyRotation
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add(trans.localRotation);
            else
                data.Add(null);

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyScale
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add(trans.localScale);

            dataArray = data.ToArray();
        }
        else if (view.observed is Rigidbody)
        {
            Rigidbody rigidB = (Rigidbody)view.observed;
            List<object> data = new List<object>();

            if (view.onSerializeRigidBodyOption != OnSerializeRigidBody.OnlyAngularVelocity)
                data.Add(rigidB.velocity);
            else
                data.Add(null);

            if (view.onSerializeRigidBodyOption != OnSerializeRigidBody.OnlyVelocity)
                data.Add( rigidB.angularVelocity);

            dataArray = data.ToArray();
        }
        else
        {
            Debug.LogError("Observed type is not serializable: " + view.observed.GetType());
            return null;
        }

        // HEADER
        // 0=View ID
        // 1=View Prefix
        // 2=Timestamp of this msg
        Hashtable messageData = new Hashtable();
        messageData[(byte)0] = (int)view.viewID.ID; // LIMITS NETWORKVIEWS&PLAYERS
        if (view.prefix > 0)
        {
            messageData[(byte)1] = view.prefix;
        }
        messageData[(byte)2] = this.ServerTimeInMilliSeconds;

        // EVDATA:
        // 0=MessageData is the header
        // 1=data of observed type (different per type of observed object)
        Hashtable evData = new Hashtable();
        evData[(byte)0] = messageData;
        evData[(byte)1] = dataArray;    // this is the actual data (script or observed object)

        if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
        {
            // copy the full data set
            Hashtable copy = new Hashtable();
            copy.Merge(evData);

            // compress content of data set (by comparing to view.lastOnSerializeDataSent)
            this.DeltaCompressionWrite(view, evData);

            // buffer the full data set (for next compression)
            view.lastOnSerializeDataSent = copy;
        }

        return evData;
    }
Exemplo n.º 2
0
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private Hashtable WriteSerializeData(PhotonView view)
    {
        //EVDATA:
        //0=MessageData is the header
        //1=data of observed type (different per
        Hashtable evData = new Hashtable();

        //0=Header
        Hashtable messageData = new Hashtable();
        messageData[(byte)0] = (int)view.viewID.ID; // LIMITS NETWORKVIEWS&PLAYERS
        if (view.prefix > 0)
        {
            messageData[(byte)1] = view.prefix;
        }
        messageData[(byte)2] = this.ServerTimeInMilliSeconds;
        evData[(byte)0] = messageData;

        //1=Specific data
        Type type = view.observed.GetType();

        if (typeof(MonoBehaviour).IsInstanceOfType(view.observed))
        {
            MonoBehaviour monob = (MonoBehaviour)view.observed;
            PhotonStream pStream = new PhotonStream(true, null);
            PhotonMessageInfo info = new PhotonMessageInfo(this.mLocalActor, this.ServerTimeInMilliSeconds, view);

            this.ExecuteOnSerialize(monob, pStream, info);
            evData[(byte)1] = pStream.ToArray();
        }
        else if (type == typeof(Transform))
        {
            Transform trans = (Transform)view.observed;
            List<object> data = new List<object>();
            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyPosition
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add(trans.localPosition);
            else
                data.Add(null);

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyRotation
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add(trans.localRotation);
            else
                data.Add(null);

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyScale
                || view.onSerializeTransformOption == OnSerializeTransform.All)
                data.Add( trans.localScale);

            evData[(byte)1] = data.ToArray();
        }
        else if (type == typeof(Rigidbody))
        {
            Rigidbody rigidB = (Rigidbody)view.observed;
            List<object> data = new List<object>();
            if (view.onSerializeRigidBodyOption != OnSerializeRigidBody.OnlyAngularVelocity)
                data.Add(rigidB.velocity);
            else
                data.Add(null);

            if (view.onSerializeRigidBodyOption != OnSerializeRigidBody.OnlyVelocity)
                data.Add( rigidB.angularVelocity);

            evData[(byte)1] = data.ToArray();
        }
        else
        {
            Debug.LogError("Type of observed is unknown! " + type + "  and " + type.BaseType);
        }

        Hashtable copy = new Hashtable();
        copy.Merge(evData);

        if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
        {
            DeltaCompressionWrite(view, evData);
        }

        // Save the full data set
        view.lastOnSerializeDataSent = copy;

        return evData;
    }
Exemplo n.º 3
0
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private Hashtable WriteSerializeData(PhotonView view)
    {
        Hashtable evData = new Hashtable();
        Hashtable messageData = new Hashtable();

        messageData[(byte)0] = (int)view.viewID.ID; // LIMITS NETWORKVIEWS&PLAYERS
        if (view.prefix > 0)
        {
            messageData[(byte)1] = view.prefix;
        }

        messageData[(byte)2] = this.ServerTimeInMilliSeconds;

        evData[(byte)0] = messageData;

        Type type = view.observed.GetType();

        if (typeof(MonoBehaviour).IsInstanceOfType(view.observed))
        {
            MonoBehaviour monob = (MonoBehaviour)view.observed;
            PhotonStream pStream = new PhotonStream(true, null);
            PhotonMessageInfo info = new PhotonMessageInfo(this.mLocalActor, this.ServerTimeInMilliSeconds, view);

            this.ExecuteOnSerialize(monob, pStream, info);
            evData[(byte)1] = pStream.ToArray();
        }
        else if (type == typeof(Transform))
        {
            Transform trans = (Transform)view.observed;
            Hashtable data = new Hashtable();
            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyPosition
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
            {
                data[(byte)0] = trans.localPosition;
            }

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyRotation
                || view.onSerializeTransformOption == OnSerializeTransform.PositionAndRotation
                || view.onSerializeTransformOption == OnSerializeTransform.All)
            {
                data[(byte)1] = trans.localRotation;
            }

            if (view.onSerializeTransformOption == OnSerializeTransform.OnlyScale
                || view.onSerializeTransformOption == OnSerializeTransform.All)
            {
                data[(byte)2] = trans.localScale;
            }

            evData[(byte)1] = data;
        }
        else if (type == typeof(Rigidbody))
        {
            Rigidbody rigidB = (Rigidbody)view.observed;
            Hashtable data = new Hashtable();
            if (view.onSerializeRigidBodyOption != OnSerializeRigidBody.OnlyAngularVelocity)
            {
                data[(byte)0] = rigidB.velocity;
            }

            if (view.onSerializeRigidBodyOption == OnSerializeRigidBody.OnlyVelocity)
            {
                data[(byte)1] = rigidB.angularVelocity;
            }

            evData[(byte)1] = data;
        }
        //else if (type == typeof(Animation))
        //{
        //    // TODO ADD ANIMATION
        //}
        else
        {
            Debug.LogError("Type of observed is unknown! " + type + "  and " + type.BaseType);
        }

        return evData;
    }
Exemplo n.º 4
0
    public void SerializeUnreliable(PhotonMessageInfo info)
    {
        PhotonStream stream = new PhotonStream(true, null);

        SerializeComponent(this.observed, stream, info, false, false);

        if (this.ObservedComponents != null && this.ObservedComponents.Count > 0)
        {
            for (int i = 0; i < this.ObservedComponents.Count; ++i)
            {
                if(ObservedComponents[i])
                    SerializeComponent(this.ObservedComponents[i], stream, info, false, false);
            }
        }

        object[] data = stream.ToArray();

        unreliableSerializedData[(byte)0] = viewID;
        unreliableSerializedData[(byte)1] = data;
    }
Exemplo n.º 5
0
    // calls OnPhotonSerializeView (through ExecuteOnSerialize)
    // the content created here is consumed by receivers in: ReadOnSerialize
    private object[] OnSerializeWrite(PhotonView view)
    {
        if (view.synchronization == ViewSynchronization.Off)
        {
            return null;
        }

        // each view creates a list of values that should be sent
        PhotonMessageInfo info = new PhotonMessageInfo(this.mLocalActor, this.ServerTimeInMilliSeconds, view);
        PhotonStream pStream = new PhotonStream(true, null);
        pStream.SendNext((int)view.viewID);
        pStream.SendNext(false);
        pStream.SendNext(null);
        view.SerializeView(pStream, info);

        // check if there are actual values to be sent (after the "header" of viewId, (bool)compressed and (int[])nullValues)
        if (pStream.Count <= SyncFirstValue)
        {
            return null;
        }
        if (view.synchronization == ViewSynchronization.Unreliable)
        {
            return pStream.ToArray();
        }

        // ViewSynchronization: Off, Unreliable, UnreliableOnChange, ReliableDeltaCompressed

        object[] currentValues = pStream.ToArray();
        if (view.synchronization == ViewSynchronization.UnreliableOnChange)
        {
            if (AlmostEquals(currentValues, view.lastOnSerializeDataSent))
            {
                if (view.mixedModeIsReliable)
                {
                    return null;
                }

                view.mixedModeIsReliable = true;
                view.lastOnSerializeDataSent = currentValues;
            }
            else
            {
                view.mixedModeIsReliable = false;
                view.lastOnSerializeDataSent = currentValues;
            }

            return currentValues;
        }

        if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
        {
            // compress content of data set (by comparing to view.lastOnSerializeDataSent)
            // the "original" dataArray is NOT modified by DeltaCompressionWrite
            object[] dataToSend = this.DeltaCompressionWrite(view.lastOnSerializeDataSent, currentValues);

            // cache the values that were written this time (not the compressed values)
            view.lastOnSerializeDataSent = currentValues;

            return dataToSend;
        }

        return null;
    }