public void PopulateKeyFunctionsMap()
        {
            if (KeyFunctionsMap.Count != 0)
            {
                throw new InvalidOperationException("Cannot run this method twice.");
            }

            foreach (var type in reflectionManager.FindTypesWithAttribute <KeyFunctionsAttribute>())
            {
                foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy))
                {
                    // This check makes sure we ONLY get readonly fields with the type we want.
                    if (field.IsLiteral || !field.IsInitOnly || field.FieldType != typeof(BoundKeyFunction))
                    {
                        continue;
                    }

                    KeyFunctionsList.Add((BoundKeyFunction)field.GetValue(null));
                }
            }

            KeyFunctionsList.Sort();

            for (var i = 0; i < KeyFunctionsList.Count; i++)
            {
                KeyFunctionsMap.Add(KeyFunctionsList[i], i);
            }
        }
        public void DoAutoRegistrations()
        {
            var iComponent = typeof(IComponent);

            foreach (var type in _reflectionManager.FindTypesWithAttribute <RegisterComponentAttribute>())
            {
                if (!iComponent.IsAssignableFrom(type))
                {
                    Logger.Error("Type {0} has RegisterComponentAttribute but does not implement IComponent.", type);
                    continue;
                }

                Register(type);

                foreach (var attribute in Attribute.GetCustomAttributes(type, typeof(ComponentReferenceAttribute)))
                {
                    var cast = (ComponentReferenceAttribute)attribute;

                    var refType = cast.ReferenceType;

                    if (!refType.IsAssignableFrom(type))
                    {
                        Logger.Error("Type {0} has reference for type it does not implement: {1}.", type, refType);
                        continue;
                    }

                    RegisterReference(type, refType);
                }
            }
        }
示例#3
0
        public void Initialize()
        {
            var types = _reflectionManager.FindTypesWithAttribute <NetSerializableAttribute>().ToList();

#if !FULL_RELEASE
            // confirm only shared types are marked for serialization, no client & server only types
            foreach (var type in types)
            {
                if (type.Assembly.FullName !.Contains("Server"))
                {
                    throw new InvalidOperationException($"Type {type} is server specific but has a NetSerializableAttribute!");
                }

                if (type.Assembly.FullName.Contains("Client"))
                {
                    throw new InvalidOperationException($"Type {type} is client specific but has a NetSerializableAttribute!");
                }
            }
#endif

            types.AddRange(AlwaysNetSerializable);

            _mappedStringSerializer.Initialize();

            var settings = new Settings
            {
                CustomTypeSerializers = new[] { _mappedStringSerializer.TypeSerializer }
            };
            _serializer        = new Serializer(types, settings);
            _serializableTypes = new HashSet <Type>(_serializer.GetTypeMap().Keys);
            LogSzr.Info($"Serializer Types Hash: {_serializer.GetSHA256()}");
        }
        public void AllNetSerializableObjectsHaveSerializableAttribute()
        {
            var types = _reflection.FindTypesWithAttribute <NetSerializableAttribute>();

            foreach (var type in types)
            {
                Assert.IsTrue(Attribute.IsDefined(type, typeof(NetSerializableAttribute), true),
                              $"{type.FullName} has {nameof(NetSerializableAttribute)}, but not the required {nameof(SerializableAttribute)}.");
            }
        }
示例#5
0
        public void Initialize()
        {
            var types    = reflectionManager.FindTypesWithAttribute <NetSerializableAttribute>();
            var settings = new Settings()
            {
                CustomTypeSerializers = new ITypeSerializer[] { new OpenTKTypeSerializer() }
            };

            Serializer = new Serializer(types, settings);
        }
示例#6
0
        public void DoAutoRegistrations()
        {
            var iComponent = typeof(ITextMacro);

            foreach (var type in _reflectionManager.FindTypesWithAttribute <RegisterTextMacroAttribute>())
            {
                if (!iComponent.IsAssignableFrom(type))
                {
                    Logger.Error("Type {0} has RegisterTextMacroAttribute but does not implement ITextMacro.", type);
                    continue;
                }

                RegisterTextMacroAttribute registerAttribute = (RegisterTextMacroAttribute)type.GetCustomAttributes(typeof(RegisterTextMacroAttribute), false)[0];
                Register(registerAttribute.MacroName, registerAttribute.LanguageTag, type);
            }
        }
示例#7
0
        public void Initialize()
        {
            var types = reflectionManager.FindTypesWithAttribute <NetSerializableAttribute>().ToList();

            #if DEBUG
            foreach (var type in types)
            {
                if (type.Assembly.FullName.Contains("Server") || type.Assembly.FullName.Contains("Client"))
                {
                    throw new InvalidOperationException($"Type {type} is server/client specific but has a NetSerializableAttribute!");
                }
            }
            #endif

            var settings = new Settings();
            Serializer = new Serializer(types, settings);
        }
示例#8
0
        public void Initialize()
        {
            IoCManager.RegisterInstance <IRobustMappedStringSerializer>(_mappedStringSerializer);

            var types = _reflectionManager.FindTypesWithAttribute <NetSerializableAttribute>().ToList();

#if !FULL_RELEASE
            // confirm only shared types are marked for serialization, no client & server only types
            foreach (var type in types)
            {
                if (type.Assembly.FullName !.Contains("Server"))
                {
                    throw new InvalidOperationException($"Type {type} is server specific but has a NetSerializableAttribute!");
                }

                if (type.Assembly.FullName.Contains("Client"))
                {
                    throw new InvalidOperationException($"Type {type} is client specific but has a NetSerializableAttribute!");
                }
            }
#endif

            var settings = new Settings
            {
                CustomTypeSerializers = new ITypeSerializer[] { _mappedStringSerializer }
            };
            _serializer        = new Serializer(types, settings);
            _serializableTypes = new HashSet <Type>(_serializer.GetTypeMap().Keys);
            LogSzr.Info($"Serializer Types Hash: {_serializer.GetSHA256()}");

            if (_netManager.IsClient)
            {
                _mappedStringSerializer.LockMappedStrings = true;
            }
            else
            {
                var defaultAssemblies = AssemblyLoadContext.Default.Assemblies;
                var gameAssemblies    = _reflectionManager.Assemblies;
                var robustShared      = defaultAssemblies
                                        .First(a => a.GetName().Name == "Robust.Shared");
                _mappedStringSerializer.AddStrings(robustShared);

                // TODO: Need to add a GetSharedAssemblies method to the reflection manager

                var contentShared = gameAssemblies
                                    .FirstOrDefault(a => a.GetName().Name == "Content.Shared");
                if (contentShared != null)
                {
                    _mappedStringSerializer.AddStrings(contentShared);
                }

                // TODO: Need to add a GetServerAssemblies method to the reflection manager

                var contentServer = gameAssemblies
                                    .FirstOrDefault(a => a.GetName().Name == "Content.Server");
                if (contentServer != null)
                {
                    _mappedStringSerializer.AddStrings(contentServer);
                }
            }

            _mappedStringSerializer.NetworkInitialize(_netManager);
        }