示例#1
0
        protected override void ReadStream(BinaryReader reader)
        {
            int    typeId      = (ushort)reader.ReadUInt16();
            byte   ownerWho    = (byte)reader.ReadByte();
            int    who         = (ushort)reader.ReadUInt16();
            string displayName = (string)reader.ReadString();
            var    pos         = new Vector2 {
                X = (float)reader.ReadSingle(),
                Y = (float)reader.ReadSingle()
            };
            int dir = (short)reader.ReadInt16();
            int wid = (ushort)reader.ReadUInt16();
            int hei = (ushort)reader.ReadUInt16();
            var vel = new Vector2 {
                X = (float)reader.ReadSingle(),
                Y = (float)reader.ReadSingle()
            };

            Type entType = CustomEntityManager.GetTypeById(typeId);

            if (entType == null)
            {
                //throw new HamstarException( "!ModHelpers.CustomEntity.ReadStream - Invalid entity type id " + typeId );
                throw new HamstarException("Invalid entity type id " + typeId);
            }

            Player plr = ownerWho == (byte)255 ? null : Main.player[ownerWho];

            var myentTemplate = (CustomEntity)PacketProtocolData.CreateInstance(entType);
            //var myentTemplate = (CustomEntity)Activator.CreateInstance( entType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            CustomEntityCore core = myentTemplate.CreateCoreTemplate();
            IList <CustomEntityComponent> components = myentTemplate.CreateComponentsTemplate();

            core.WhoAmI      = who;
            core.DisplayName = displayName;
            core.Width       = wid;
            core.Height      = hei;
            core.Position    = pos;
            core.Velocity    = vel;
            core.Direction   = dir;

            for (int i = 0; i < components.Count; i++)
            {
                components[i].ReadStreamForwarded(reader);
                components[i].InternalOnClone();
            }

            this.MyTypeName = SerializableCustomEntity.GetTypeName(myentTemplate);
            this.CopyChangesFrom(core, components, plr);
//LogHelpers.Log( "READ "+this );
        }
示例#2
0
        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from the server.
        /// Requires `SetServerDefaults()` to be implemented.
        /// </summary>
        public static void QuickRequestToServer <T>(int retries) where T : PacketProtocol             //, new()
        {
            if (Main.netMode != 1)
            {
                throw new HamstarException("Not a client.");
            }

            T t = (T)PacketProtocolData.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToServer(retries);
        }
示例#3
0
        ////////////////

        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from a client.
        /// Requires `SetClientDefaults()` to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) being requested for this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        public static void QuickRequestToClient <T>(int toWho, int ignoreWho, int retries) where T : PacketProtocol
        {
            if (Main.netMode != 2)
            {
                throw new HamstarException("Not server.");
            }

            T t = (T)PacketProtocolData.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToClient(toWho, ignoreWho, retries);
        }
        private static void QuickSendToServerBase <T>(bool syncToClients)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 1)
            {
                throw new HamstarException("Can only send as client.");
            }

            T t = (T)PacketProtocolData.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );
            t.SetClientDefaults();
            t.OnClone();

            t.SendToServer(syncToClients);
        }
        internal static void HandlePacketOnServer(int protocolCode, BinaryReader reader, int playerWho)
        {
            var  mymod = ModHelpersMod.Instance;
            bool isRequest, isSyncedToClients;

            Type protocolType = mymod.PacketProtocolMngr.GetProtocolType(protocolCode);

            if (protocolType == null)
            {
                throw new HamstarException("Invalid protocol (hash: " + protocolCode + ")");
            }

            try {
                isRequest         = reader.ReadBoolean();
                isSyncedToClients = reader.ReadBoolean();
            } catch (Exception e) {
                throw new HamstarException("Could not read data for protocol " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }

            try {
                var protocol = (PacketProtocol)PacketProtocolData.CreateInstance(protocolType);
                //var protocol = (PacketProtocol)Activator.CreateInstance( protocolType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

                if (isRequest)
                {
                    protocol.ReceiveRequestWithServerBase(playerWho);
                    protocol.OnClone();
                }
                else
                {
                    if (protocol.IsAsync)
                    {
                        ThreadPool.QueueUserWorkItem(_ => {
                            protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                        });
                    }
                    else
                    {
                        protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                    }
                }
            } catch (Exception e) {
                throw new HamstarException("Error handling " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }
        }
        /// <summary>
        /// Shorthand to send a default instance of this protocol's data to a client. Requires `SetServerDefaults()`
        /// to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) receiving this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        public static void QuickSendToClient <T>(int toWho, int ignoreWho)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 2)
            {
                throw new HamstarException("Can only send as client.");
            }

            T t = (T)PacketProtocolData.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            try {
                t.SetServerDefaults(toWho);
            } catch (NotImplementedException) {
                t.SetServerDefaults();
            }
            t.OnClone();

            t.SendToClient(toWho, ignoreWho);
        }