public void RegisterNetworkableEntities(TypeRegistryBuilder typeRegistryBuilder) { //Register all entity classes that have been marked networkable //This includes classes not marked with LinkEntityToClass since base types can be mapped to foreach (var type in typeof(ClientEntities) .Assembly .GetTypes() .ToList() .Where(type => typeof(BaseEntity).IsAssignableFrom(type) && type.GetCustomAttribute <NetworkableAttribute>() != null)) { var networkableAttr = type.GetCustomAttribute <NetworkableAttribute>(); //All entities use this because they don't share a base class //Make sure not to replace words in names, just the namespace var mapsFromType = networkableAttr.MapsFromType ?? type.FullName.Replace(".Client.", ".Server."); typeRegistryBuilder .NewBuilder(type) .AddNetworkedFields() .AddNetworkedProperties() .MapsFromType(mapsFromType) .WithFactory(EntityFactory) .Build(); } }
public void RegisterNetworkableEntities(TypeRegistryBuilder typeRegistryBuilder) { var registeredTypes = new HashSet <Type>(); void AddType(Type type, NetworkableAttribute networkableAttr) { //MapsFromType is not required on the server side typeRegistryBuilder .NewBuilder(type) .AddNetworkedFields() .AddNetworkedProperties() .Build(); registeredTypes.Add(type); } bool AddBaseType(Type type) { if (type != null) { //Already registered, we're done if (registeredTypes.Contains(type)) { return(true); } var networkableAttr = type.GetCustomAttribute <NetworkableAttribute>(); if (networkableAttr != null) { if (!networkableAttr.UseBaseType) { AddType(type, networkableAttr); return(true); } //Base class also depends on its base, try that //This will recursively register all base types var registered = AddBaseType(type.BaseType); if (!registered) { throw new NotSupportedException($"The type {type.FullName} is declared as using a base class for networking, but no base class is configured for networking"); } typeRegistryBuilder.RegisterRemappedType(type, type.BaseType); //Register the type as networked so any subclasses will know registeredTypes.Add(type); return(registered); } } return(false); } foreach (var entityMetaData in EntityDictionary.Types) { //Don't need to check return value because entities can be server only AddBaseType(entityMetaData.Type); } }