Ejemplo n.º 1
0
        private bool WriteParameters(NetworkWriter writer, bool forceAll = false)
        {
            var dirtyBits = forceAll ? ~0ul : NextDirtyBits();

            writer.WritePackedUInt64(dirtyBits);
            for (var i = 0; i < parameters.Length; i++)
            {
                if ((dirtyBits & (1ul << i)) == 0)
                {
                    continue;
                }

                var par = parameters[i];
                if (par.type == AnimatorControllerParameterType.Int)
                {
                    var newIntValue = animator.GetInteger(par.nameHash);
                    writer.WritePackedInt32(newIntValue);
                }
                else if (par.type == AnimatorControllerParameterType.Float)
                {
                    var newFloatValue = animator.GetFloat(par.nameHash);
                    writer.WriteSingle(newFloatValue);
                }
                else if (par.type == AnimatorControllerParameterType.Bool)
                {
                    var newBoolValue = animator.GetBool(par.nameHash);
                    writer.WriteBoolean(newBoolValue);
                }
            }

            return(dirtyBits != 0);
        }
Ejemplo n.º 2
0
        bool WriteParameters(NetworkWriter writer)
        {
            ulong dirtyBits = NextDirtyBits();

            writer.WritePackedUInt64(dirtyBits);
            for (int i = 0; i < parameters.Length; i++)
            {
                if ((dirtyBits & (1ul << i)) == 0)
                {
                    continue;
                }

                AnimatorControllerParameter par = parameters[i];
                if (par.type == AnimatorControllerParameterType.Int)
                {
                    int newIntValue = animator.GetInteger(par.nameHash);
                    writer.WritePackedInt32(newIntValue);
                }
                else if (par.type == AnimatorControllerParameterType.Float)
                {
                    float newFloatValue = animator.GetFloat(par.nameHash);
                    writer.WriteSingle(newFloatValue);
                }
                else if (par.type == AnimatorControllerParameterType.Bool)
                {
                    bool newBoolValue = animator.GetBool(par.nameHash);
                    writer.WriteBoolean(newBoolValue);
                }
            }
            return(dirtyBits != 0);
        }
Ejemplo n.º 3
0
 public virtual bool OnSerialize(NetworkWriter writer, bool initialState)
 {
     if (!initialState)
     {
         writer.WritePackedUInt64(0);
     }
     return(false);
 }
Ejemplo n.º 4
0
        // serialize all components (or only dirty ones if not initial state)
        // -> returns TRUE if any date other than dirtyMask was written!
        internal bool OnSerializeAllSafely(NetworkBehaviour[] components, NetworkWriter writer, bool initialState)
        {
            if (components.Length > 64)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Only 64 NetworkBehaviour components are allowed for NetworkIdentity: " + name + " because of the dirtyComponentMask");
                }
                return(false);
            }

            // loop through all components only once and then write dirty+payload into the writer afterwards
            ulong         dirtyComponentsMask = 0L;
            NetworkWriter payload             = new NetworkWriter();

            for (int i = 0; i < components.Length; ++i)
            {
                // is this component dirty?
                // -> always serialize if initialState so all components are included in spawn packet
                // -> note: IsDirty() is false if the component isn't dirty or sendInterval isn't elapsed yet
                NetworkBehaviour comp = m_NetworkBehaviours[i];
                if (initialState || comp.IsDirty())
                {
                    // set bit #i to 1 in dirty mask
                    dirtyComponentsMask |= (ulong)(1L << i);

                    // serialize the data
                    if (LogFilter.logDebug)
                    {
                        Debug.Log("OnSerializeAllSafely: " + name + " -> " + comp.GetType() + " initial=" + initialState);
                    }
                    OnSerializeSafely(comp, payload, initialState);

                    // Clear dirty bits only if we are synchronizing data and not sending a spawn message.
                    // This preserves the behavior in HLAPI
                    if (!initialState)
                    {
                        comp.ClearAllDirtyBits();
                    }
                }
            }

            // did we write anything? then write dirty, payload and return true
            if (dirtyComponentsMask != 0L)
            {
                byte[] payloadBytes = payload.ToArray();
                writer.WritePackedUInt64(dirtyComponentsMask); // WritePacked64 so we don't write full 8 bytes if we don't have to
                writer.Write(payloadBytes, 0, payloadBytes.Length);
                return(true);
            }

            // didn't write anything, return false
            return(false);
        }
Ejemplo n.º 5
0
 public void Serialize(NetworkWriter writer)
 {
     writer.WritePackedUInt32(netId);
     writer.WriteBoolean(isLocalPlayer);
     writer.WritePackedUInt64(sceneId);
     if (sceneId == 0)
     {
         writer.WriteGuid(assetId);
     }
     writer.WriteVector3(position);
     writer.WriteQuaternion(rotation);
     writer.WriteVector3(scale);
     writer.WriteBytesAndSizeSegment(payload);
 }
Ejemplo n.º 6
0
 public bool SerializeObjectsDelta(NetworkWriter writer)
 {
     bool dirty = false;
     // write the mask
     writer.WritePackedUInt64(DirtyObjectBits());
     // serializable objects, such as synclists
     for (int i = 0; i < syncObjects.Count; i++)
     {
         SyncObject syncObject = syncObjects[i];
         if (syncObject.IsDirty)
         {
             syncObject.OnSerializeDelta(writer);
             dirty = true;
         }
     }
     return dirty;
 }
        bool WriteParameters(NetworkWriter writer, bool forceAll = false)
        {
            ulong dirtyBits = forceAll ? (~0ul) : NextDirtyBits();

            writer.WritePackedUInt64(dirtyBits);
            for (int i = 0; i < namesParameters.Length; i++)
            {
                if ((dirtyBits & (1ul << i)) == 0)
                {
                    continue;
                }

                if (i < lastFloatParameters.Length)
                {
                    float newFloatValue = m_Motion.motionGraph.GetFloat(Animator.StringToHash(namesParameters[i]));
                    writer.WriteSingle(newFloatValue);
                }
                else if (i >= lastFloatParameters.Length &&
                         i < (lastFloatParameters.Length + lastIntParameters.Length))
                {
                    int newIntValue = m_Motion.motionGraph.GetInt(Animator.StringToHash(namesParameters[i]));
                    writer.WritePackedInt32(newIntValue);
                }
                else if (i >= (lastFloatParameters.Length + lastIntParameters.Length) &&
                         i < (lastFloatParameters.Length + lastIntParameters.Length + lastSwitchParameters.Length))
                {
                    bool newBoolValue = m_Motion.motionGraph.GetSwitch(Animator.StringToHash(namesParameters[i]));
                    writer.WriteBoolean(newBoolValue);
                }
                else if (i >= (lastFloatParameters.Length + lastIntParameters.Length + lastSwitchParameters.Length) &&
                         i < (lastFloatParameters.Length + lastIntParameters.Length + lastSwitchParameters.Length + lastVectorParameters.Length))
                {
                    Vector3 newVectorValue = m_Motion.motionGraph.GetVector(Animator.StringToHash(namesParameters[i]));
                    writer.WriteVector3(newVectorValue);
                }
                else if (i >= (lastFloatParameters.Length + lastIntParameters.Length + lastSwitchParameters.Length + lastVectorParameters.Length) &&
                         i < (lastFloatParameters.Length + lastIntParameters.Length + lastSwitchParameters.Length + lastVectorParameters.Length + lastTransformParameters.Length))
                {
                    Transform newTransformValue = m_Motion.motionGraph.GetTransform(Animator.StringToHash(namesParameters[i]));
                    writer.WriteTransform(newTransformValue);
                }
            }
            return(dirtyBits != 0);
        }
Ejemplo n.º 8
0
        // serialize all components (or only dirty ones if not initial state)
        // -> returns serialized data of everything dirty,  null if nothing was dirty
        internal byte[] OnSerializeAllSafely(bool initialState)
        {
            if (m_NetworkBehaviours.Length > 64)
            {
                Debug.LogError("Only 64 NetworkBehaviour components are allowed for NetworkIdentity: " + name + " because of the dirtyComponentMask");
                return(null);
            }
            ulong dirtyComponentsMask = GetDirtyMask(m_NetworkBehaviours, initialState);

            if (dirtyComponentsMask == 0L)
            {
                return(null);
            }

            NetworkWriter writer = new NetworkWriter();

            writer.WritePackedUInt64(dirtyComponentsMask); // WritePacked64 so we don't write full 8 bytes if we don't have to

            foreach (NetworkBehaviour comp in m_NetworkBehaviours)
            {
                // is this component dirty?
                // -> always serialize if initialState so all components are included in spawn packet
                // -> note: IsDirty() is false if the component isn't dirty or sendInterval isn't elapsed yet
                if (initialState || comp.IsDirty())
                {
                    // serialize the data
                    if (LogFilter.Debug)
                    {
                        Debug.Log("OnSerializeAllSafely: " + name + " -> " + comp.GetType() + " initial=" + initialState);
                    }
                    OnSerializeSafely(comp, writer, initialState);

                    // Clear dirty bits only if we are synchronizing data and not sending a spawn message.
                    // This preserves the behavior in HLAPI
                    if (!initialState)
                    {
                        comp.ClearAllDirtyBits();
                    }
                }
            }

            return(writer.ToArray());
        }
Ejemplo n.º 9
0
        // zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba
        public static void WritePackedInt64(this NetworkWriter writer, long i)
        {
            ulong zigzagged = (ulong)((i >> 63) ^ (i << 1));

            writer.WritePackedUInt64(zigzagged);
        }
Ejemplo n.º 10
0
 // http://sqlite.org/src4/doc/trunk/www/varint.wiki
 public static void WritePackedUInt32(this NetworkWriter writer, uint value)
 {
     // for 32 bit values WritePackedUInt64 writes the
     // same exact thing bit by bit
     writer.WritePackedUInt64(value);
 }