public static void RegisterGameObject(
            this ObjectMessageRouter router,
            GameObject g)
        {
            ComponentCache.Clear();
            g.GetComponentsInChildren(ComponentCache);

            for (int componentIndex = 0; componentIndex < ComponentCache.Count; componentIndex++)
            {
                var component     = ComponentCache[componentIndex];
                var componentType = component.GetType();
                var callSites     = GetMessageHandlerCallSite(componentType);
                for (int i = 0; i < callSites.Count; i++)
                {
                    var callSite = callSites[i];

                    if (VerifySignature(callSite.MethodInfo, new[] { typeof(IObjectMessage), typeof(MessageMetadata) }) ||
                        VerifySignature(callSite.MethodInfo, new[] { typeof(IObjectMessage) }))
                    {
                        var handlerParameters = callSite.MethodInfo.GetParameters();
                        var messageParameter  = handlerParameters[0];
                        var handlerType       = messageParameter.ParameterType;
                        // TODO Better error handling
                        if (handlerParameters.Length == 1)
                        {
                            var messageHandleCallSite = DelegateHelper.CreateDelegate <Action <IObjectMessage> >(
                                callSite.MethodInfo,
                                component);
                            router.RegisterHandler(handlerType, new ObjectMessageHandler(
                                                       metaData: callSite.MetaData,
                                                       invoke: (message, metadata) => {
                                if (component.enabled)
                                {
                                    messageHandleCallSite(message);
                                }
                            }));
                        }
                        else if (handlerParameters.Length == 2)
                        {
                            var messageHandleCallSite = DelegateHelper.CreateDelegate <Action <IObjectMessage, MessageMetadata> >(
                                callSite.MethodInfo,
                                component);
                            router.RegisterHandler(handlerType, new ObjectMessageHandler(
                                                       metaData: callSite.MetaData,
                                                       invoke: (message, metadata) => {
                                if (component.enabled)
                                {
                                    messageHandleCallSite(message, metadata);
                                }
                            }));
                        }
                    }
                    else
                    {
                        throw new Exception("Message handler has incorrect signature: " +
                                            "use (IObjectMessage, MessageMetadata) or (IObjectMessage) instead");
                    }
                }
            }
        }
Example #2
0
        void Awake()
        {
            // TODO Re-use DependencyContainer
            var diContainer = new DependencyContainer();

            diContainer.AddDependency("objectMessagePools", ObjectMessagePools.Value);
            var objectMessageRouter = new ObjectMessageRouter(MessageTypes.ObjectMessageTypes, gameObject);

            diContainer.AddDependency("localObjectMessageSender", new LocalObjectMessageSender(objectMessageRouter));
            DependencyInjector.Default.Inject(this.gameObject, diContainer);
            (ObjectRole.Authority | ObjectRole.Owner).ApplyTo(this.gameObject);
        }
 public ReplicatedObject(
     GameObject gameObject,
     ObjectMessageRouter messageHandler,
     IReplicationConstructor replicationConstructor,
     IReadOnlyList <ISpawnable> spawnables)
 {
     GlobalObjectId         = new Guid();
     GameObject             = gameObject;
     MessageHandler         = messageHandler;
     ReplicationConstructor = replicationConstructor;
     Spawnables             = spawnables;
     GameObjectNetworkInfo  = gameObject.GetComponent <GameObjectNetworkInfo>() ?? gameObject.AddComponent <GameObjectNetworkInfo>();
 }
Example #4
0
 public LocalObjectMessageSender(ObjectMessageRouter messageRouter)
 {
     _messageRouter         = messageRouter;
     _sequenceNumberCounter = new SequenceNumber(0);
 }