private static void ReadStreamIntoContainer(BinaryReader reader, StreamProtocol fieldContainer)
        {
            IOrderedEnumerable <FieldInfo> orderedFields = fieldContainer.OrderedFields;
            int i = 0;

            if (ModHelpersMod.Config.DebugModePacketInfo)
            {
                LogHelpers.Log("  Begun reading packet " + fieldContainer.GetType().Name + " (" + fieldContainer.FieldCount + " fields)");
            }

            foreach (FieldInfo field in orderedFields)
            {
                i++;

                Type   fieldType = field.FieldType;
                object fieldData = StreamProtocol.ReadStreamValue(reader, fieldType);

                if (Main.netMode == 1)
                {
                    if (Attribute.IsDefined(field, typeof(ProtocolWriteIgnoreServerAttribute)))
                    {
                        continue;
                    }
                }
                else if (Main.netMode == 2)
                {
                    if (Attribute.IsDefined(field, typeof(ProtocolWriteIgnoreClientAttribute)))
                    {
                        continue;
                    }
                }

                if (ModHelpersMod.Config.DebugModePacketInfo)
                {
                    LogHelpers.Log("  * Reading packet " + fieldContainer.GetType().Name
                                   + " field (" + i + " of " + fieldContainer.FieldCount + ") " + field.Name
                                   + ": " + DotNetHelpers.Stringify(fieldData, 32));
                }

//LogHelpers.Log( "READ "+ fieldContainer.GetType().Name + " FIELD " + field + " VALUE " + fieldData );
                field.SetValue(fieldContainer, fieldData);
            }
        }
Ejemplo n.º 2
0
        private static void WriteStreamFromContainer(BinaryWriter writer, StreamProtocol fieldContainer)
        {
            IOrderedEnumerable <FieldInfo> orderedFields = fieldContainer.OrderedFields;
            int i = 0;

            if (!StreamProtocol.ValidateConstructor(fieldContainer.GetType()))
            {
                throw new ModHelpersException("Invalid default constructor for " + fieldContainer.GetType().Name);
            }

            if (ModHelpersMod.Config.DebugModePacketInfo)
            {
                LogHelpers.Log("  Begun writing packet " + fieldContainer.GetType().Name + " (" + fieldContainer.FieldCount + " fields)");
            }

            foreach (FieldInfo field in orderedFields)
            {
                i++;

                if (Main.netMode == 1 && Attribute.IsDefined(field, typeof(ProtocolWriteIgnoreClientAttribute)))
                {
                    continue;
                }
                else if (Main.netMode == 2 && Attribute.IsDefined(field, typeof(ProtocolWriteIgnoreServerAttribute)))
                {
                    continue;
                }

                object rawFieldVal = field.GetValue(fieldContainer);
                //LogHelpers.Log( "WRITE "+ data.GetType().Name+ " FIELD " + field + " VALUE "+(rawFieldVal??"null"));

                if (ModHelpersMod.Config.DebugModePacketInfo)
                {
                    LogHelpers.Log("  * Writing packet " + fieldContainer.GetType().Name
                                   + " field (" + i + " of " + fieldContainer.FieldCount + ") "
                                   + field.Name + ": " + DotNetHelpers.Stringify(rawFieldVal, 32));
                }

                StreamProtocol.WriteStreamValue(writer, field.FieldType, rawFieldVal);
            }
        }