Exemplo n.º 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);
        }
Exemplo n.º 5
0
 public static void QuickRequestToServer <T>() where T : PacketProtocol           //, new()
 {
     PacketProtocol.QuickRequestToServer <T>(0);
 }
Exemplo n.º 6
0
 public static void QuickRequestToClient <T>(int toWho, int ignoreWho) where T : PacketProtocol             //, new()
 {
     PacketProtocol.QuickRequestToClient <T>(toWho, ignoreWho, 0);
 }
 public static void QuickSend <T>(int toWho, int ignoreWho) where T : PacketProtocolSendToClient
 {
     PacketProtocol.QuickSendToClient <T>(toWho, ignoreWho);
 }
 public static void QuickSend <T>() where T : PacketProtocolSendToServer
 {
     PacketProtocol.QuickSendToServer <T>();
 }
 public static void QuickRequest <T>(int toWho, int ignoreWho, int retries) where T : PacketProtocolRequestToClient
 {
     PacketProtocol.QuickRequestToClient <T>(toWho, ignoreWho, retries);
 }
Exemplo n.º 10
0
        ////////////////

        internal void OnPostSetupContent()
        {
            this.PacketProtocolTypesByCode = PacketProtocol.GetProtocolTypes();
        }
 /// <summary>
 /// Shorthand to send a default instance of this protocol's data to everyone. Requires `SetClientDefaults()`
 /// to be implemented.
 /// </summary>
 public static void QuickSyncToServerAndClients <T>()
     where T : PacketProtocol                        //, new()
 {
     PacketProtocol.QuickSendToServerBase <T>(true);
 }
        ////////////////


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