public static void UpdateNetmessageTypes() { Type typea = typeof(Network); FieldInfo info2 = typea.GetField("_typeToMessageID", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); dynamic _typeToMessageID = info2.GetValue(Level.current) as Map <ushort, System.Type>; IEnumerable <System.Type> subclasses = Editor.GetSubclasses(typeof(NetMessage)); _typeToMessageID.Clear(); ushort key = 1; foreach (System.Type type in subclasses) { if (type.GetCustomAttributes(typeof(FixedNetworkID), false).Length != 0) { FixedNetworkID customAttribute = (FixedNetworkID)type.GetCustomAttributes(typeof(FixedNetworkID), false)[0]; if (customAttribute != null) { _typeToMessageID.Add(type, customAttribute.FixedID); } } } foreach (System.Type type in subclasses) { if (!_typeToMessageID.ContainsValue(type)) { while (_typeToMessageID.ContainsKey(key)) { ++key; } _typeToMessageID.Add(type, key); ++key; } } }
// The netmessages are enumerated on startup and if you send one which isn't enumerated, duck game crashes or at least I remember it doing that // We simply run this method to re-enumerate the netmessages after our mod has loaded. public static void AddNewNetmessageTypes() { FieldInfo typeToMessageIDField = typeof(Network).GetField("_typeToMessageID", BindingFlags.Static | BindingFlags.NonPublic); dynamic _typeToMessageID = typeToMessageIDField.GetValue(null) as Map <ushort, Type>; IEnumerable <Type> subclasses = Editor.GetSubclasses(typeof(NetMessage)); _typeToMessageID.Clear(); ushort key = 1; foreach (Type type in subclasses) { if (type.GetCustomAttributes(typeof(FixedNetworkID), false).Length != 0) { FixedNetworkID customAttribute = (FixedNetworkID)type.GetCustomAttributes(typeof(FixedNetworkID), false)[0]; if (customAttribute != null) { _typeToMessageID.Add(type, customAttribute.FixedID); } } } foreach (Type type in subclasses) { if (!_typeToMessageID.ContainsValue(type)) { while (_typeToMessageID.ContainsKey(key)) { ++key; } _typeToMessageID.Add(type, key); ++key; } } }