Пример #1
0
        public static bool CompressedQuaternionUnitTest()
        {
            var stream = new VRage.Library.Collections.BitStream();

            stream.ResetWrite();
            Quaternion q = Quaternion.Identity;

            stream.WriteQuaternionNormCompressed(q);
            stream.ResetRead();
            var  q2   = stream.ReadQuaternionNormCompressed();
            bool fail = !q.Equals(q2, 1 / 511.0f);

            stream.ResetWrite();
            q = Quaternion.CreateFromAxisAngle(Vector3.Forward, (float)Math.PI / 3.0f);
            stream.WriteQuaternionNormCompressed(q);
            stream.ResetRead();
            q2    = stream.ReadQuaternionNormCompressed();
            fail |= !q.Equals(q2, 1 / 511.0f);

            stream.ResetWrite();
            var v = new Vector3(1, -1, 3);

            v.Normalize();
            q = Quaternion.CreateFromAxisAngle(v, (float)Math.PI / 3.0f);
            stream.WriteQuaternionNormCompressed(q);
            stream.ResetRead();
            q2    = stream.ReadQuaternionNormCompressed();
            fail |= !q.Equals(q2, 1 / 511.0f);
            return(fail);
        }
        private void ProcessEvent( MyPacket packet )
        {
            if ( _networkHandlers.Count == 0 )
            {
                ((MyReplicationLayer)MyMultiplayer.ReplicationLayer).ProcessEvent(packet);
                return;
            }

            //magic: DO NOT TOUCH
            BitStream stream = new BitStream();
            stream.ResetRead( packet );

            NetworkId networkId = stream.ReadNetworkId();
            //this value is unused, but removing this line corrupts the rest of the stream
            NetworkId blockedNetworkId = stream.ReadNetworkId();
            uint eventId = (uint)stream.ReadByte();

            CallSite site;
            IMyNetObject sendAs;
            object obj;
            if ( networkId.IsInvalid ) // Static event
            {
                site = m_typeTable.StaticEventTable.Get( eventId );
                sendAs = null;
                obj = null;
            }
            else // Instance event
            {
                sendAs = ( (MyReplicationLayer)MyMultiplayer.ReplicationLayer ).GetObjectByNetworkId( networkId );
                if ( sendAs == null )
                {
                    return;
                }
                var typeInfo = m_typeTable.Get( sendAs.GetType() );
                int eventCount = typeInfo.EventTable.Count;
                if ( eventId < eventCount ) // Directly
                {
                    obj = sendAs;
                    site = typeInfo.EventTable.Get( eventId );
                }
                else // Through proxy
                {
                    obj = ( (IMyProxyTarget)sendAs ).Target;
                    typeInfo = m_typeTable.Get( obj.GetType() );
                    site = typeInfo.EventTable.Get( eventId - (uint)eventCount ); // Subtract max id of Proxy
                }
            }

            #if DEBUG
            if ( ExtenderOptions.IsDebugging )
            {
                if ( !site.MethodInfo.Name.Contains( "SyncPropertyChanged" ) && !site.MethodInfo.Name.Contains( "OnSimulationInfo" ) )
                    ApplicationLog.Error( $"Caught event {site.MethodInfo.Name} from user {PlayerMap.Instance.GetFastPlayerNameFromSteamId( packet.Sender.Value )}:{packet.Sender.Value}" );
            }
            #endif
            //we're handling the network live in the game thread, this needs to go as fast as possible
            bool handled = false;
            Parallel.ForEach( _networkHandlers, handler =>
                                                {
                                                    if ( handler.CanHandle( site ) )
                                                        handled |= handler.Handle( packet.Sender.Value, site, stream, obj );
                                                } );

            if ( !handled )
                ( (MyReplicationLayer)MyMultiplayer.ReplicationLayer ).ProcessEvent( packet );
        }