private ModPacket GetServerPacket(bool isRequest)
        {
            if (Main.netMode != 2)
            {
                throw new ModHelpersException("Not a server");
            }

            string name   = this.GetPacketName();
            var    packet = ModHelpersMod.Instance.GetPacket();

            packet.Write((int)PacketProtocol.GetPacketCode(name));
            packet.Write(isRequest);

            return(packet);
        }
        private ModPacket GetClientPacket(bool isRequest, bool syncToAll)
        {
            if (Main.netMode != 1)
            {
                throw new ModHelpersException("Not a client");
            }

            string name   = this.GetPacketName();
            var    packet = ModHelpersMod.Instance.GetPacket();

            packet.Write((int)PacketProtocol.GetPacketCode(name));
            packet.Write(isRequest);
            packet.Write(syncToAll);

            return(packet);
        }
Пример #3
0
        internal static IDictionary <int, Type> GetProtocolTypes()
        {
            IEnumerable <Type>      protocolTypes   = ReflectionHelpers.GetAllAvailableSubTypesFromMods(typeof(PacketProtocol));
            IDictionary <int, Type> protocolTypeMap = new Dictionary <int, Type>();

            foreach (Type subclassType in protocolTypes)
            {
                ConstructorInfo ctorInfo = subclassType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
                                                                       new Type[] { }, null);
                if (ctorInfo == null)
                {
                    throw new ModHelpersException("Missing private constructor for " + subclassType.Name + " (" + subclassType.Namespace + ")");
                }
                if (ctorInfo.IsFamily)
                {
                    throw new ModHelpersException("Invalid constructor for " + subclassType.Name + " (" + subclassType.Namespace + "); must be private, not protected.");
                }

                if (ModHelpersMod.Config.DebugModeNetInfo)
                {
                    string name = subclassType.Namespace + "." + subclassType.Name;
                    LogHelpers.Alert(name);
                }

                try {
                    string name = subclassType.Namespace + "." + subclassType.Name;
                    int    code = PacketProtocol.GetPacketCode(name);

                    protocolTypeMap[code] = subclassType;
                } catch (Exception e) {
                    LogHelpers.Log(subclassType.Name + " - " + e.Message);
                }
            }

            return(protocolTypeMap);
        }
Пример #4
0
 /// <summary>
 /// Shorthand to send a default instance of this protocol's data to everyone. Requires `SetClientDefaults()`
 /// to be implemented.
 /// </summary>
 protected static void QuickSyncToServerAndClients <T>()
     where T : PacketProtocol                        //, new()
 {
     PacketProtocol.QuickSendToServerBase <T>(true);
 }
Пример #5
0
        ////////////////


        /// <summary>
        /// Shorthand to send a default instance of this protocol's data to the server. Requires `SetClientDefaults()`
        /// to be implemented.
        /// </summary>
        protected static void QuickSendToServer <T>()
            where T : PacketProtocol                        //, new()
        {
            PacketProtocol.QuickSendToServerBase <T>(false);
        }
        ////////////////

        internal void OnPostModsLoad()
        {
            this.PacketProtocolTypesByCode = PacketProtocol.GetProtocolTypes();
        }