예제 #1
0
        internal static IDictionary <int, Type> GetProtocols()
        {
            IDictionary <int, Type> protocols = new Dictionary <int, Type>();

            //var subclasses = from assembly in AppDomain.CurrentDomain.GetAssemblies()
            var mod_types  = ModLoader.LoadedMods.Select(mod => mod.GetType());
            var assemblies = mod_types.Select(mod_type => mod_type.Assembly);
            var subclasses = from assembly in assemblies
                             from type in assembly.GetTypes()
                             where type.IsSubclassOf(typeof(PacketProtocol)) && !type.IsAbstract
                             select type;

            foreach (Type subclass in subclasses)
            {
                if (HamstarHelpersMod.Instance.Config.DebugModeNetInfo)
                {
                    string name = subclass.Namespace + "." + subclass.Name;
                    LogHelpers.Log("PacketProtocol.GetProtocols() - " + name);
                }

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

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

            return(protocols);
        }
        private ModPacket GetServerPacket(bool isRequest)
        {
            if (Main.netMode != 2)
            {
                throw new HamstarException("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 HamstarException("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);
        }
        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[] { typeof(PacketProtocolDataConstructorLock) }, null);

                if (ctorInfo == null)
                {
                    ctorInfo = subclassType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
                                                           new Type[] { }, null);

                    if (ctorInfo == null)
                    {
                        throw new HamstarException("Missing private constructor for " + subclassType.Name + " (" + subclassType.Namespace + ")");
                    }
                    if (ctorInfo.IsFamily)
                    {
                        throw new HamstarException("Invalid constructor for " + subclassType.Name + " (" + subclassType.Namespace + "); must be private, not protected.");
                    }
                }

                if (ModHelpersMod.Instance.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);
        }