private bool ApplyEvent(ME.ECS.StatesHistory.HistoryEvent historyEvent)
        {
            /*if (historyEvent.storeInHistory == true) {
             *
             *  System.Reflection.MethodInfo methodInfo;
             *  if (this.registry.TryGetValue(historyEvent.rpcId, out methodInfo) == true) {
             *
             *      UnityEngine.Debug.LogWarning("Received. evt.objId: " + historyEvent.objId + ", evt.rpcId: " + historyEvent.rpcId + ", evt.order: " + historyEvent.order + ", method: " + methodInfo.Name);
             *
             *  }
             *
             * }*/

            if ((this.GetNetworkType() & NetworkType.RunLocal) != 0 && historyEvent.order == this.GetRPCOrder())
            {
                // Skip events from local owner is it was run already
                //UnityEngine.Debug.LogWarning("Skipped event: " + historyEvent.objId + ", " + historyEvent.rpcId);
                return(false);
            }

            if (historyEvent.storeInHistory == true)
            {
                // Run event normally on certain tick
                this.statesHistoryModule.AddEvent(historyEvent);
            }
            else
            {
                // Run event immediately
                this.statesHistoryModule.RunEvent(historyEvent);
            }

            return(true);
        }
Beispiel #2
0
        private void CallRPC(object instance, RPCId rpcId, params object[] parameters)
        {
            Key key;

            if (this.objectToKey.TryGetValue(instance, out key) == true)
            {
                var evt = new ME.ECS.StatesHistory.HistoryEvent();
                evt.tick       = this.world.GetTick() + 1UL; // Call RPC on next tick
                evt.order      = this.GetRPCOrder();
                evt.localOrder = ++this.localOrderIndex;
                evt.parameters = parameters;
                evt.rpcId      = rpcId;
                evt.objId      = key.objId;
                evt.groupId    = key.groupId;

                //UnityEngine.Debug.Log("CallRPC: " + evt);

                if ((this.GetNetworkType() & NetworkType.RunLocal) != 0)
                {
                    this.statesHistoryModule.AddEvent(evt);
                }

                if ((this.GetNetworkType() & NetworkType.SendToNet) != 0)
                {
                    if (this.transporter != null && this.serializer != null)
                    {
                        this.transporter.Send(this.serializer.Serialize(evt));
                    }
                }
            }
        }
Beispiel #3
0
    public byte[] Serialize(ME.ECS.StatesHistory.HistoryEvent historyEvent)
    {
        FullSerializer.fsData data;
        this.fsSerializer.TrySerialize(typeof(ME.ECS.StatesHistory.HistoryEvent), historyEvent, out data).AssertSuccessWithoutWarnings();
        var str = FullSerializer.fsJsonPrinter.CompressedJson(data);

        return(System.Text.Encoding.UTF8.GetBytes(str));
    }
 void StatesHistory.IEventRunner.RunEvent(StatesHistory.HistoryEvent historyEvent)
 {
     System.Reflection.MethodInfo methodInfo;
     if (this.registry.TryGetValue(historyEvent.rpcId, out methodInfo) == true)
     {
         var key = MathUtils.GetKey(historyEvent.groupId, historyEvent.objId);
         if (this.keyToObjects.TryGetValue(key, out var instance) == true)
         {
             this.runCurrentEvent = historyEvent;
             methodInfo.Invoke(instance, historyEvent.parameters);
             this.runCurrentEvent = default;
         }
     }
 }
Beispiel #5
0
        private void CallRPC(object instance, RPCId rpcId, bool storeInHistory, params object[] parameters)
        {
            Key key;

            if (this.objectToKey.TryGetValue(instance, out key) == true)
            {
                if (this.transporter.IsConnected() == false)
                {
                    return;
                }

                var evt = new ME.ECS.StatesHistory.HistoryEvent();
                evt.tick           = this.world.GetStateTick() + Tick.One; // Call RPC on next tick
                evt.order          = this.GetRPCOrder();
                evt.localOrder     = ++this.localOrderIndex;
                evt.parameters     = parameters;
                evt.rpcId          = rpcId;
                evt.objId          = key.objId;
                evt.groupId        = key.groupId;
                evt.storeInHistory = storeInHistory;

                var storedInHistory = false;
                if (storeInHistory == true && (this.GetNetworkType() & NetworkType.RunLocal) != 0)
                {
                    this.statesHistoryModule.AddEvent(evt);
                    storedInHistory = true;
                }

                if ((this.GetNetworkType() & NetworkType.SendToNet) != 0)
                {
                    if (this.transporter != null && this.serializer != null)
                    {
                        this.transporter.Send(this.serializer.Serialize(evt));
                    }
                }

                if (storedInHistory == false && parameters != null)
                {
                    // Return parameters into pool if we are not storing them locally
                    PoolArray <object> .Recycle(ref parameters);
                }
            }
            else
            {
                throw new RegisterObjectMissingException(instance, rpcId);
            }
        }
        protected virtual void SendSync(float deltaTime)
        {
            this.syncTime += deltaTime;
            if (this.syncTime >= 2f)
            {
                if (this.syncTickSent != this.syncedTick)
                {
                    this.SystemRPC(this, NetworkModule <TState> .SYNC_RPC_ID, this.syncedTick, this.syncHash);
                    this.runCurrentEvent = new ME.ECS.StatesHistory.HistoryEvent()
                    {
                        order = this.GetRPCOrder(),
                    };
                    this.Sync_RPC(this.syncedTick, this.syncHash);
                    this.syncTickSent = this.syncedTick;
                }

                this.syncTime -= 2f;
            }
        }
Beispiel #7
0
        private bool ApplyEvent(ME.ECS.StatesHistory.HistoryEvent historyEvent)
        {
            /*if (historyEvent.storeInHistory == true) {
             *
             *  System.Reflection.MethodInfo methodInfo;
             *  if (this.registry.TryGetValue(historyEvent.rpcId, out methodInfo) == true) {
             *
             *      UnityEngine.Debug.LogWarning("Received. evt.objId: " + historyEvent.objId + ", evt.rpcId: " + historyEvent.rpcId + ", evt.order: " + historyEvent.order + ", method: " + methodInfo.Name);
             *
             *  }
             *
             * }*/

            if ((this.GetNetworkType() & NetworkType.RunLocal) != 0 && historyEvent.order == this.GetRPCOrder())
            {
                // Skip events from local owner is it was run already
                //UnityEngine.Debug.LogWarning("Skipped event: " + historyEvent.objId + ", " + historyEvent.rpcId);
                return(false);
            }

            if (historyEvent.storeInHistory == true)
            {
                // Run event normally on certain tick
                this.statesHistoryModule.AddEvent(historyEvent);
            }
            else
            {
                // Run event immediately
                this.statesHistoryModule.RunEvent(historyEvent);
            }

            var st = this.statesHistoryModule.GetStateBeforeTick(historyEvent.tick, out var syncTick);

            if (st == null || syncTick == Tick.Invalid)
            {
                st = this.world.GetResetState <TState>();
            }
            this.syncedTick = st.tick;
            this.syncHash   = this.statesHistoryModule.GetStateHash(st);

            return(true);
        }
        public object Unpack(Packer packer)
        {
            var pack = new ME.ECS.StatesHistory.HistoryEvent();

            pack.tick           = Int64Serializer.UnpackDirect(packer);
            pack.order          = Int32Serializer.UnpackDirect(packer);
            pack.rpcId          = Int32Serializer.UnpackDirect(packer);
            pack.localOrder     = Int32Serializer.UnpackDirect(packer);
            pack.objId          = Int32Serializer.UnpackDirect(packer);
            pack.groupId        = Int32Serializer.UnpackDirect(packer);
            pack.storeInHistory = BooleanSerializer.UnpackDirect(packer);

            var hasParams = packer.ReadByte();

            if (hasParams == 1)
            {
                var serializer = new ObjectArraySerializer();
                pack.parameters = (object[])serializer.Unpack(packer);
            }

            return(pack);
        }
 protected void CancelEvent(ME.ECS.StatesHistory.HistoryEvent historyEvent)
 {
     this.statesHistoryModule.CancelEvent(historyEvent);
 }
        private void CallRPC(object instance, RPCId rpcId, bool storeInHistory, object[] parameters)
        {
            if (this.world.HasStep(WorldStep.LogicTick) == true)
            {
                InStateException.ThrowWorldStateCheck();
            }

            if (this.objectToKey.TryGetValue(instance, out var key) == true)
            {
                var evt = new ME.ECS.StatesHistory.HistoryEvent();
                evt.tick       = this.world.GetStateTick() + this.statesHistoryModule.GetEventForwardTick(); // Call RPC on next N tick
                evt.parameters = parameters;
                evt.rpcId      = rpcId;
                evt.objId      = key.objId;
                evt.groupId    = key.groupId;

                // If event run only on local machine
                // we need to write data through all states and run this event immediately on each
                // then return current state back
                // so we don't need to store this event in history, we just need to rewrite reset data
                var runLocalOnly = this.runLocalOnly.Contains(rpcId);
                if (runLocalOnly == true)
                {
                    { // Apply data to current state
                        this.statesHistoryModule.RunEvent(evt);
                    }

                    var currentState = this.world.GetState();
                    var resetState   = this.world.GetResetState();
                    this.world.SetStateDirect(resetState);
                    {
                        this.statesHistoryModule.RunEvent(evt);
                    }

                    foreach (var entry in this.statesHistoryModule.GetDataStates().GetEntries())
                    {
                        if (entry.isEmpty == false)
                        {
                            this.world.SetStateDirect(entry.state);
                            this.statesHistoryModule.RunEvent(evt);
                        }
                    }

                    this.world.SetStateDirect(currentState);
                    return;
                }

                // Set up other event data
                evt.order          = this.GetRPCOrder();
                evt.localOrder     = ++this.localOrderIndex;
                evt.storeInHistory = storeInHistory;

                var storedInHistory = false;
                if (this.GetNetworkType() == NetworkType.RunLocal && storeInHistory == true)
                {
                    this.statesHistoryModule.AddEvent(evt);
                    storedInHistory = true;
                }

                if (storedInHistory == false && storeInHistory == true && (this.GetNetworkType() & NetworkType.RunLocal) != 0)
                {
                    //var dEvt = this.serializer.Deserialize(this.serializer.Serialize(evt));
                    this.statesHistoryModule.AddEvent(evt);
                    storedInHistory = true;
                }

                if (this.transporter != null && this.transporter.IsConnected() == true)
                {
                    if (runLocalOnly == false && (this.GetNetworkType() & NetworkType.SendToNet) != 0)
                    {
                        if (this.transporter != null && this.serializer != null)
                        {
                            if (storeInHistory == false)
                            {
                                this.transporter.SendSystem(this.serializer.Serialize(evt));
                            }
                            else
                            {
                                this.transporter.Send(this.serializer.Serialize(evt));
                            }
                        }
                    }
                }

                if (storedInHistory == false && parameters != null)
                {
                    // Return parameters into pool if we are not storing them locally
                    PoolArray <object> .Recycle(ref parameters);
                }
            }
            else
            {
                throw new RegisterObjectMissingException(instance, rpcId);
            }
        }
Beispiel #11
0
 public byte[] Serialize(ME.ECS.StatesHistory.HistoryEvent historyEvent)
 {
     return(ME.ECS.Serializer.Serializer.Pack(historyEvent));
 }