Exemple #1
0
        public bool ShouldPullDataFromMecanim(NetworkState state)
        {
#if DEBUG
            if (
#else
            return
                (#endif
                MecanimDirection == MecanimDirection.UsingAnimatorMethods && (state.Entity.IsOwner || state.Entity.HasPredictedControl)

#if DEBUG
                )
            {
                if (state.Animators.Count > 1)
                {
                    NetLog.Warn("Property '{0}' set to 'UsingAnimatorMethods' but several animators have been specified, only the first one added will be used.");
                }

                return true;
            }

                return false);
#else
                ;
#endif
        }
        public bool QueueInput(Command cmd)
        {
            if (canQueueCommands)
            {
                NetAssert.True(HasControl);

                if (CommandQueue.Count < Core.Config.commandQueueSize)
                {
                    cmd.ServerFrame = Core.ServerFrame;
                    cmd.Sequence    = CommandSequence = NetMath.SeqNext(CommandSequence, Command.SEQ_MASK);
                }
                else
                {
                    NetLog.Error("Input queue for {0} is full", this);
                    return(false);
                }

                CommandQueue.AddLast(cmd);
                return(true);
            }
            else
            {
                NetLog.Error("You can only queue commands to the host in the 'SimulateController' callback");
                return(false);
            }
        }
        public void TakeControl(IMessageRider token)
        {
            if (IsOwner)
            {
                if (HasControl)
                {
                    NetLog.Warn("You already have control of {0}", this);
                }
                else
                {
                    // revoke any existing control
                    RevokeControl(token);

                    // take control locally
                    TakeControlInternal(token);


                    // de-freeze
                    Freeze(false);
                }
            }
            else
            {
                NetLog.Error("Only the owner of {0} can take control of it", this);
            }
        }
        public void AssignControl(Connection connection, IMessageRider token)
        {
            if (IsOwner)
            {
                if (HasControl)
                {
                    ReleaseControl(token);
                }

                EntityProxy proxy;

                CommandLastExecuted = null;
                CommandSequence     = 0;
                CommandQueue.Clear();

                Controller = connection;
                Controller.controlling.Add(this);
                Controller.entityChannel.CreateOnRemote(this, out proxy);
                Controller.entityChannel.ForceSync(this);

                // set token
                proxy.ControlTokenLost   = null;
                proxy.ControlTokenGained = token;

                Freeze(false);
            }
            else
            {
                NetLog.Error("You can not assign control of {0}, you are not the owner", this);
            }
        }
        public void RevokeControl(IMessageRider token)
        {
            if (IsOwner)
            {
                if (Controller)
                {
                    EntityProxy proxy;

                    // force a replication of this
                    Controller.controlling.Remove(this);
                    Controller.entityChannel.ForceSync(this, out proxy);
                    Controller = null;

                    // clear out everything
                    CommandLastExecuted = null;
                    CommandSequence     = 0;
                    CommandQueue.Clear();

                    // set token
                    if (proxy != null)
                    {
                        proxy.ControlTokenLost   = token;
                        proxy.ControlTokenGained = null;
                    }
                }

                Freeze(false);
            }
            else
            {
                NetLog.Error("You can not revoke control of {0}, you are not the owner", this);
                return;
            }
        }
Exemple #6
0
        void InvokeCallbacksForProperty(int propertyIndex)
        {
            try
            {
                NetworkPropertyInfo pi = Meta.Properties[propertyIndex];

                List <PropertyCallback>       callbacks;
                List <PropertyCallbackSimple> callbacksSimple;

                for (int i = 0; i < pi.Paths.Length; ++i)
                {
                    if (Callbacks.TryGetValue(pi.Paths[i], out callbacks))
                    {
                        for (int c = 0; c < callbacks.Count; ++c)
                        {
                            callbacks[c](this, pi.Paths[pi.Paths.Length - 1], new ArrayIndices(pi.Indices));
                        }
                    }

                    if (CallbacksSimple.TryGetValue(pi.Paths[i], out callbacksSimple))
                    {
                        for (int c = 0; c < callbacksSimple.Count; ++c)
                        {
                            callbacksSimple[c]();
                        }
                    }
                }
            }
            catch (Exception exn)
            {
                NetLog.Exception(exn);
            }
        }
        static void Entity_Only_Controller(Event ev)
        {
            if (ev.TargetEntity)
            {
                if (ev.TargetEntity.HasControl)
                {
                    RaiseLocal(ev);

                    // we can free this (never used after this)
                    ev.FreeStorage();
                }
                else
                {
                    if (ev.TargetEntity.IsOwner)
                    {
                        if (ev.TargetEntity.Controller != null)
                        {
                            ev.TargetEntity.Controller.eventChannel.Queue(ev);
                        }
                        else
                        {
                            NetLog.Warn("NetworkEvent sent to controller but no controller exists, event will NOT be raised");
                        }
                    }
                    else
                    {
                        ev.TargetEntity.Source.eventChannel.Queue(ev);
                    }
                }
            }
            else
            {
                NetLog.Warn("NetworkEvent with NULL target, event will NOT be forwarded or raised");
            }
        }
        bool PackEvent(Event ev, Packet stream, uint sequence)
        {
            NetLog.Debug("sending event {0}", ev);

            stream.WriteContinueMarker();

            // type id of this event
            stream.WriteTypeId(ev.Meta.TypeId);

            // targets of this event
            stream.WriteInt(ev.Targets, 5);

            if (stream.WriteBool(ev.Reliability == ReliabilityModes.ReliableOrdered))
            {
                // write sequence number for reliable events
                stream.WriteUInt(sequence, Event.RELIABLE_SEQUENCE_BITS);
            }
            else
            {
                if (ev.IsEntityEvent)
                {
                    // write network id for entity events
                    stream.WriteEntity(ev.TargetEntity);
                }
            }

            stream.WriteByteArrayLengthPrefixed(ev.BinaryData, Core.Config.packetSize / 2);
            return(ev.Pack(connection, stream));
        }
        public static void Enqueue(SceneLoadState scene)
        {
            NetLog.Debug("Loading {0} ({1})", scene, AscensionNetworkInternal.GetSceneName(scene.Scene.Index));

            delay = 0;
            LoadOps.AddLast(new LoadOp {
                scene = scene
            });
        }
Exemple #10
0
        public override void SetDynamic(NetworkObj obj, object value)
        {
            if (MecanimDirection == MecanimDirection.UsingAnimatorMethods)
            {
                NetLog.Error("Can't call SetDynamic on a trigger in 'UsingAnimatorMethods' mode");
                return;
            }

            obj.Storage.Values[obj[this]].TriggerLocal.Update(Core.Frame, true);
        }
Exemple #11
0
        bool VerifyCallbackPath(string path)
        {
            if (Meta.CallbackPaths.Contains(path))
            {
                return(true);
            }

            NetLog.Error("No callback path '{0}' available on {1}", path, this);
            return(false);
        }
Exemple #12
0
        public void SetIdle(Connection connection, bool idle)
        {
            if (idle && IsController(connection))
            {
                NetLog.Error("You can not idle {0} on {1}, as it is the controller for this entity", this, connection);
                return;
            }

            connection.entityChannel.SetIdle(this, idle);
        }
        /// <summary>
        /// Get the state if this entity
        /// </summary>
        public TState GetState <TState>()
        {
            if (AEntity.Serializer is TState)
            {
                return((TState)(object)AEntity.Serializer);
            }

            NetLog.Error("You are trying to access the state of {0} as '{1}'", AEntity, typeof(TState));
            return(default(TState));
        }
Exemple #14
0
 public void SetParent(Entity entity)
 {
     if (IsOwner || HasPredictedControl)
     {
         SetParentInternal(entity);
     }
     else
     {
         NetLog.Error("You are not allowed to assign the parent of this entity, only the owner or a controller with local prediction can");
     }
 }
Exemple #15
0
        static object Create(UniqueId id)
        {
#if DEBUG
            if (_factoriesByKey.ContainsKey(id) == false)
            {
                NetLog.Error("Unknown {0}", id);
            }
#endif

            return(_factoriesByKey[id].Create());
        }
Exemple #16
0
        public void InvokeOnce(Command command, CommandCallback callback, int delay)
        {
            NetAssert.True(delay > 0);

            if (!canQueueCallbacks)
            {
                NetLog.Error("Can only queue callbacks when commands with 'IsFirstExecution' set to true are executing");
                return;
            }

            //CommandCallbacks.Add(new CommandCallbackItem { Command = command, Callback = callback, Start = -1, End = command.Number + delay, Mode = CommandCallbackModes.InvokeOnce });
        }
        private static void Entity_Only_Controller_And_Owner(Event ev)
        {
            if (ev.TargetEntity)
            {
                if (ev.TargetEntity.HasControl)
                {
                    RaiseLocal(ev);

                    if (ev.TargetEntity.IsOwner)
                    {
                        // if we're also owner, free this
                        ev.FreeStorage();
                    }
                    else
                    {
                        // check so this was not received from source, and if it wasn't then send it to it
                        if (ev.SourceConnection != ev.TargetEntity.Source)
                        {
                            ev.TargetEntity.Source.eventChannel.Queue(ev);
                        }
                    }
                }
                else
                {
                    if (ev.TargetEntity.IsOwner)
                    {
                        // raise this locally for owner
                        RaiseLocal(ev);

                        if (ev.TargetEntity.Controller != null)
                        {
                            // check so we didn't receive this from owner
                            if (ev.SourceConnection != ev.TargetEntity.Controller)
                            {
                                ev.TargetEntity.Controller.eventChannel.Queue(ev);
                            }
                        }
                        else
                        {
                            NetLog.Warn("NetworkEvent sent to controller but no controller exists, event will NOT be raised");
                        }
                    }
                    else
                    {
                        ev.TargetEntity.Source.eventChannel.Queue(ev);
                    }
                }
            }
            else
            {
                NetLog.Warn("NetworkEvent with NULL target, event will NOT be forwarded or raised");
            }
        }
Exemple #18
0
        public static IFactory GetFactory(UniqueId id)
        {
#if DEBUG
            if (!_factoriesByKey.ContainsKey(id))
            {
                NetLog.Error("Unknown factory {0}", id);
                return(null);
            }
#endif

            return(_factoriesByKey[id]);
        }
Exemple #19
0
        public void InvokeRepeating(Command command, CommandCallback callback, int period)
        {
            NetAssert.True(period > 0);

            if (!canQueueCallbacks)
            {
                NetLog.Error("Can only queue callbacks when commands with 'IsFirstExecution' set to true are executing");
                return;
            }

            //CommandCallbacks.Add(new CommandCallbackItem { Command = command, Callback = callback, Start = command.Number + 1, End = command.Number + period, Mode = CommandCallbackModes.InvokeRepeating });
        }
        public void Remove(MonoBehaviour behaviour)
        {
            for (int i = 0; i < targets.Count; ++i)
            {
                if (ReferenceEquals(targets[i].Behaviour, behaviour))
                {
                    targets.RemoveAt(i);
                    return;
                }
            }

            NetLog.Warn("Behaviour not available in this dispatcher, ignoring call to Remove.");
        }
Exemple #21
0
 public override bool Write(Connection connection, NetworkObj obj, NetworkStorage storage, Packet packet)
 {
     try
     {
         packet.WriteToken(storage.Values[obj[this]].ProtocolToken);
         return(true);
     }
     catch (Exception exn)
     {
         NetLog.Error("User code threw exception while serializing protocol token");
         NetLog.Exception(exn);
         return(false);
     }
 }
        public void SetScope(Entity entity, bool inScope)
        {
            if (Core.Config.scopeMode == ScopeMode.Automatic)
            {
                NetLog.Error("SetScope has no effect when Scope Mode is set to Automatic");
                return;
            }

            if (ReferenceEquals(entity.Source, connection))
            {
                return;
            }

            if (inScope)
            {
                if (_incommingDict.ContainsKey(entity.NetworkId))
                {
                    return;
                }

                EntityProxy proxy;

                if (_outgoingDict.TryGetValue(entity.NetworkId, out proxy))
                {
                    if (proxy.Flags & ProxyFlags.DESTROY_REQUESTED)
                    {
                        if (proxy.Flags & ProxyFlags.DESTROY_PENDING)
                        {
                            proxy.Flags |= ProxyFlags.DESTROY_IGNORE;
                        }
                        else
                        {
                            proxy.Flags &= ~ProxyFlags.DESTROY_IGNORE;
                            proxy.Flags &= ~ProxyFlags.DESTROY_REQUESTED;
                        }
                    }
                }
                else
                {
                    CreateOnRemote(entity);
                }
            }
            else
            {
                if (_outgoingDict.ContainsKey(entity.NetworkId))
                {
                    DestroyOnRemote(entity);
                }
            }
        }
Exemple #23
0
        public static void RegisterTokenClass(Type type)
        {
            if (_token2id.Count == 255)
            {
                throw new ArgumentException("Can only register 255 different token types");
            }

            byte id = (byte)(_token2id.Count + 1);

            _token2id.Add(type, id);
            _id2token.Add(id, type);

            NetLog.Debug("Registered token class {0} as id {1}", type, id);
        }
        public void Add(MonoBehaviour behaviour)
        {
            for (int i = 0; i < targets.Count; ++i)
            {
                if (ReferenceEquals(targets[i].Behaviour, behaviour))
                {
                    NetLog.Warn("Behaviour is already registered in this dispatcher, ignoring call to Add.");
                    return;
                }
            }

            targets.Add(new EventListener {
                Behaviour = behaviour, GameObject = behaviour.gameObject, Listener = behaviour as IEventListener
            });
        }
Exemple #25
0
 public void PacketLost(Packet packet)
 {
     for (int i = 0; i < channels.Length; ++i)
     {
         channels[i].Lost(packet);
     }
     try
     {
     }
     catch (Exception exn)
     {
         NetLog.Exception(exn);
         NetLog.Error("exception thrown while handling lost packet to {0}", sockConn.RemoteEndPoint);
     }
 }
Exemple #26
0
 public void PacketDelivered(Packet packet)
 {
     try
     {
         for (int i = 0; i < channels.Length; ++i)
         {
             channels[i].Delivered(packet);
         }
     }
     catch (Exception exn)
     {
         NetLog.Exception(exn);
         NetLog.Error("exception thrown while handling delivered packet to {0}", sockConn.RemoteEndPoint);
     }
 }
Exemple #27
0
        public static void WriteByteArraySimple(this BasePacket stream, byte[] array, int maxLength)
        {
            if (stream.WriteBool(array != null))
            {
                int length = Mathf.Min(array.Length, maxLength);

                if (length < array.Length)
                {
                    NetLog.Warn("Only sending {0}/{1} bytes from byte array", length, array.Length);
                }

                stream.WriteUShort((ushort)length);
                stream.WriteByteArray(array, 0, length);
            }
        }
Exemple #28
0
        public void WriteByteArrayLengthPrefixed(byte[] array, int maxLength)
        {
            if (WriteBool(array != null))
            {
                int length = Math.Min(array.Length, maxLength);

                if (length < array.Length)
                {
                    NetLog.Warn("Only sendig {0}/{1} bytes from byte array", length, array.Length);
                }

                WriteUShort((ushort)length);
                WriteByteArray(array, 0, length);
            }
        }
Exemple #29
0
        public override void SetDynamic(NetworkObj obj, object value)
        {
            if (MecanimDirection == MecanimDirection.UsingAnimatorMethods)
            {
                NetLog.Error("Can't call SetDynamic on a float in 'UsingAnimatorMethods' mode");
                return;
            }

            var v = (float)value;

            if (NetworkValue.Diff(obj.Storage.Values[obj[this]].Float0, v))
            {
                obj.Storage.Values[obj[this]].Float0 = v;
                obj.Storage.PropertyChanged(obj.OffsetProperties + this.OffsetProperties);
            }
        }
Exemple #30
0
        public void WriteEndPoint(IPEndPoint endpoint)
        {
            var parts = endpoint.Address.ToString().Split('.');

            if (parts.Length != 4)
            {
                NetLog.Warn(string.Format("IP address: {0} was malformed, could not write.", endpoint.Address));
                return;
            }

            WriteByte(Convert.ToByte(parts[0]));
            WriteByte(Convert.ToByte(parts[1]));
            WriteByte(Convert.ToByte(parts[2]));
            WriteByte(Convert.ToByte(parts[3]));
            WriteUShort((ushort)endpoint.Port);
        }