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); } }
private static void WriteStreamObjectValue(BinaryWriter writer, Type fieldType, object rawVal) { bool isEnumerable = false, isDictionary = false; string[] dataTypeNameChunks = null; if (fieldType.IsInterface) { dataTypeNameChunks = fieldType.Name.Split(new char[] { '`' }, 2); switch (dataTypeNameChunks[0]) { case "ISet": case "IList": isEnumerable = true; break; case "IDictionary": isDictionary = true; break; } } if (fieldType.IsSubclassOf(typeof(StreamProtocol))) { ((StreamProtocol)rawVal).WriteStream(writer); } else if ((isEnumerable || typeof(IEnumerable).IsAssignableFrom(fieldType)) && (!isDictionary && !typeof(IDictionary).IsAssignableFrom(fieldType))) { Type[] innerTypes = fieldType.GetGenericArguments(); Type innerType; if (innerTypes.Length == 0) { innerType = fieldType.GetElementType(); } else { innerType = innerTypes[0]; } IEnumerable <object> collection = ((IEnumerable)rawVal).Cast <object>(); writer.Write((ushort)collection.Count()); if (innerType.IsSubclassOf(typeof(StreamProtocol))) { foreach (object item in collection) { ((StreamProtocol)item).WriteStream(writer); } } else { foreach (object item in collection) { StreamProtocol.WriteStreamValue(writer, innerType, item); } } } else { string jsonEncVal = JsonConvert.SerializeObject(rawVal); writer.Write((string)jsonEncVal); if (ModHelpersMod.Config.DebugModePacketInfo) { LogHelpers.Log(" - WriteStreamObjectValue - type: " + fieldType.Name + ", raw value (" + jsonEncVal.Length + "): \n " + jsonEncVal); } } }