Exemplo n.º 1
0
 public override void ReplOnChangedOwner(bool was_owner)
 {
     if (was_owner)
     {
         float relevance = this.GetRelevance(ReplTools.GetLocalPeer());
         this.m_Relevance.Clear();
         this.m_Relevance[ReplTools.GetLocalPeer()] = relevance;
     }
     else
     {
         foreach (P2PPeer key in ReplTools.GetRemotePeers())
         {
             this.m_Relevance[key] = 0.2f;
         }
     }
     foreach (P2PPeer p2PPeer in ReplTools.GetRemotePeers())
     {
         if (P2PLogFilter.logPedantic)
         {
             Debug.Log(string.Format("ReplOnChangedOwner is owner {0} {1} info {2} {3}", new object[]
             {
                 !was_owner,
                 base.GetComponent <GuidComponent>().GetGuid(),
                 p2PPeer.GetHostId(),
                 this.GetRelevance(p2PPeer)
             }));
         }
     }
 }
Exemplo n.º 2
0
 public static float GetDistToLocalPlayer(GameObject obj)
 {
     if (ReplTools.GetLocalPeer().m_Representation != null)
     {
         return(ReplTools.GetLocalPeer().m_Representation.GetWorldPosition().Distance(obj.transform.position));
     }
     return(-1f);
 }
Exemplo n.º 3
0
    public static float ReplGetReplicationInterval(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return(0f);
        }
        return(replicationComponent.ReplGetReplicationInterval(ReplTools.GetLocalPeer()));
    }
Exemplo n.º 4
0
 private void OnRelevanceActivated(P2PPeer peer)
 {
     if (this.ReplIsOwner() && peer != ReplTools.GetLocalPeer())
     {
         if (P2PLogFilter.logPedantic)
         {
             Debug.Log(string.Format("OnRelevanceActivated called for peer {0} {1} guid {2}", peer.GetHostId(), base.name, this.GetGuidComponent.GetGuid()));
         }
         Replicator.Singleton.SpawnForPeer(base.gameObject, peer);
     }
 }
Exemplo n.º 5
0
 private void Awake()
 {
     if (Application.isPlaying)
     {
         Replicator.Singleton.RegisterReplicationComponent(this);
         if (!this.ReplGetOwner().IsValid() && this.ReplWasSpawned())
         {
             this.m_OwnerPeer = ReplTools.GetLocalPeer();
         }
         this.m_CustomReplicationIntervalComponents = base.GetComponents <ICustomReplicationInterval>();
     }
 }
Exemplo n.º 6
0
    public float GetMaxRemoteRelevance()
    {
        float num = 0f;

        foreach (KeyValuePair <P2PPeer, float> keyValuePair in this.m_Relevance)
        {
            if (keyValuePair.Key != ReplTools.GetLocalPeer() && keyValuePair.Value > num)
            {
                num = keyValuePair.Value;
            }
        }
        return(num);
    }
 public static void UnregisterForPeer(this IPeerWorldRepresentation representation, P2PPeer peer)
 {
     if (peer.m_Representation == representation)
     {
         peer.m_Representation = null;
     }
     if (peer == ReplTools.GetLocalPeer())
     {
         if (representation.IsReplicated())
         {
             IPeerWorldRepresentationHelper.s_LocalNetPlayer = null;
             return;
         }
         IPeerWorldRepresentationHelper.s_LocalOfflinePlayer = null;
     }
 }
Exemplo n.º 8
0
 public float ReplGetReplicationInterval(P2PPeer peer)
 {
     if (!(this.GetRelevanceComponent != null))
     {
         return(this.GetReplicationIntervalMin());
     }
     if (peer == ReplTools.GetLocalPeer())
     {
         if (this.m_LocalReplicationIntervalUpdateTime < Time.time)
         {
             this.m_LocalReplicationIntervalNow        = this.CalculateReplicationInterval(peer);
             this.m_LocalReplicationIntervalUpdateTime = Time.time;
         }
         return(this.m_LocalReplicationIntervalNow);
     }
     return(this.CalculateReplicationInterval(peer));
 }
Exemplo n.º 9
0
 public void ReplRequestOwnership()
 {
     if (this.ReplIsOwner() || (!this.ReplCanChangeOwnership() && this.ReplGetOwner() != P2PPeer.s_Invalid))
     {
         return;
     }
     this.m_TransferringOwnershipTo = null;
     this.ReplOnChangedOwner(ReplTools.GetLocalPeer());
     if (!ReplTools.IsPlayingAlone())
     {
         ReplicationComponent.s_Writer.StartMessage(6);
         ReplicationComponent.s_Writer.Write(base.gameObject);
         ReplicationComponent.s_Writer.Write(this.m_OwnerPeer.GetHostId());
         ReplicationComponent.s_Writer.FinishMessage();
         if (P2PLogFilter.logPedantic)
         {
             Debug.Log(string.Format("ReplRequestOwnership called for {0} guid: {1}", base.name, this.GetGuidComponent.GetGuid()), base.gameObject);
         }
         P2PSession.Instance.SendWriterToAll(ReplicationComponent.s_Writer, 0);
     }
 }
Exemplo n.º 10
0
    public static float GetDistToClosestPlayer2D(GameObject obj)
    {
        float num = float.MaxValue;

        if (ReplTools.GetLocalPeer().m_Representation != null)
        {
            num = ReplTools.GetLocalPeer().m_Representation.GetWorldPosition().Distance2D(obj.transform.position);
        }
        for (int i = 0; i < ReplTools.GetRemotePeers().Count; i++)
        {
            P2PPeer p2PPeer = ReplTools.GetRemotePeers()[i];
            if (p2PPeer != null && p2PPeer.m_Representation != null)
            {
                float num2 = p2PPeer.m_Representation.GetWorldPosition().Distance2D(obj.transform.position);
                if (num2 < num)
                {
                    num = num2;
                }
            }
        }
        return(num);
    }
Exemplo n.º 11
0
 private void OnRelevanceDeactivated(P2PPeer peer)
 {
     if (this.ReplIsOwner() && peer != ReplTools.GetLocalPeer())
     {
         if (P2PLogFilter.logPedantic)
         {
             Debug.Log(string.Format("OnRelevanceDeactivated called for peer {0} {1} guid {2} CanBeRemovedByRelevance {3}", new object[]
             {
                 peer.GetHostId(),
                 base.name,
                 this.GetGuidComponent.GetGuid(),
                 this.GetRelevanceComponent.CanBeRemovedByRelevance(false)
             }));
         }
         if (this.GetRelevanceComponent.CanBeRemovedByRelevance(false))
         {
             Replicator.Singleton.DespawnForPeer(this, peer);
             return;
         }
         ReplicationComponent.s_SendToPeers.Reset();
         ReplicationComponent.s_SendToPeers.Add(peer);
         this.ReplicationSend(false, ReplicationComponent.s_SendToPeers);
     }
 }
Exemplo n.º 12
0
    public void OnGiveOwnership(P2PNetworkMessage netMsg)
    {
        byte[]     array;
        GameObject gameObject = netMsg.m_Reader.ReadGameObjectAndGuid(out array);

        if (gameObject)
        {
            ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true);
            if (P2PLogFilter.logPedantic)
            {
                Debug.Log(string.Format("OnGiveOwnership called for {0} guid: {1}", replComponentForGameObject.name, replComponentForGameObject.GetGuidComponent.GetGuid()), gameObject);
            }
            ReplicationComponent.s_DeserializedComponent = replComponentForGameObject;
            P2PPeer peerById = ReplTools.GetPeerById(netMsg.m_Reader.ReadInt16());
            if (peerById == ReplTools.GetLocalPeer())
            {
                replComponentForGameObject.ReplRequestOwnership();
            }
            else
            {
                replComponentForGameObject.ReplOnChangedOwner(peerById);
            }
            ReplicationComponent.s_DeserializedComponent = null;
            return;
        }
        P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter();

        p2PNetworkWriter.StartMessage(8);
        p2PNetworkWriter.Write(array, GuidComponent.GUID_BYTES_CNT);
        p2PNetworkWriter.FinishMessage();
        if (P2PLogFilter.logWarn)
        {
            Debug.LogWarning("Did not find target for give ownership message for " + new Guid(array));
        }
        P2PSession.Instance.SendWriterTo(netMsg.m_Connection.m_Peer, p2PNetworkWriter, 0);
    }
Exemplo n.º 13
0
 public bool ReplIsOwner()
 {
     return((this.m_OwnerPeer == null && ReplTools.IsPlayingAlone()) || this.m_OwnerPeer == ReplTools.GetLocalPeer());
 }
Exemplo n.º 14
0
    public void ReplicationReceive(P2PNetworkReader reader, bool initial_state)
    {
        if (P2PLogFilter.logPedantic)
        {
            Debug.Log(string.Format("ReplicationReceive called for {0} with guid {1}", base.name, this.GetGuidComponent.GetGuid()), this);
        }
        int num = reader.ReadInt32();

        if (num <= this.m_Revision && !initial_state)
        {
            return;
        }
        ReplicationComponent.s_DeserializedComponent = this;
        this.m_Revision                = num;
        this.m_LastReplicationTime     = Time.time;
        this.m_LastReplicationTimeReal = Time.realtimeSinceStartup;
        short num2 = reader.ReadInt16();

        if (this.m_OwnerPeer.GetHostId() != num2)
        {
            this.ReplOnChangedOwner(ReplTools.GetPeerById(num2));
            if (this.m_OwnerPeer == ReplTools.GetLocalPeer())
            {
                this.ReplRequestOwnership();
            }
        }
        this.ReplBlockChangeOwnership(!reader.ReadBoolean());
        for (int i = 0; i < this.GetReplBehaviours.Length; i++)
        {
            IReplicatedBehaviour replicatedBehaviour = this.GetReplBehaviours[i];
            if (replicatedBehaviour as UnityEngine.Object == null)
            {
                if (reader.ReadBoolean())
                {
                    ReplicationComponent.s_DeserializedComponent = null;
                    return;
                }
                this.m_ReplBehavioursFlag[i] = false;
            }
            else if (reader.ReadBoolean())
            {
                int uniqueIdForType = replicatedBehaviour.GetUniqueIdForType();
                int num3            = reader.ReadInt32();
                if (uniqueIdForType != num3)
                {
                    ReplicationComponent.s_DeserializedComponent = null;
                    return;
                }
                replicatedBehaviour.OnReplicationDeserialize(reader, initial_state);
                this.CallReplicationDeserialize_Gen(replicatedBehaviour, reader, initial_state);
                this.m_ReplBehavioursFlag[i] = true;
            }
            else
            {
                this.m_ReplBehavioursFlag[i] = false;
            }
        }
        for (int j = 0; j < this.GetReplBehaviours.Length; j++)
        {
            if (this.m_ReplBehavioursFlag[j])
            {
                IReplicatedBehaviour replicatedBehaviour2 = this.GetReplBehaviours[j];
                replicatedBehaviour2.OnReplicationResolve();
                this.CallReplicationResolve_Gen(replicatedBehaviour2);
            }
        }
        if (P2PConnection.s_Size > reader.Position && !initial_state)
        {
            Debug.LogError(string.Format("Didn't read whole buffer! (number of bytes unread: {0})", P2PConnection.s_Size - reader.Position), this);
        }
        ReplicationComponent.s_DeserializedComponent = null;
    }
Exemplo n.º 15
0
 private void LateUpdate()
 {
     if (!RelevanceSystem.ENABLED)
     {
         return;
     }
     Dictionary <GameObject, Relevance> .Enumerator enumerator = this.m_Components.GetEnumerator();
     while (enumerator.MoveNext())
     {
         KeyValuePair <GameObject, Relevance> keyValuePair = enumerator.Current;
         Relevance value = keyValuePair.Value;
         if (value && value.enabled && value.LastUpdate <= Time.time - 0.5f)
         {
             value.LastUpdate = Time.time + UnityEngine.Random.Range(0f, 0.2f);
             Relevance.ERelevanceCalculationResult erelevanceCalculationResult;
             float num = value.CalculateRelevance(ReplTools.GetLocalPeer(), out erelevanceCalculationResult);
             if (num >= 0.2f && !value.gameObject.activeSelf)
             {
                 value.gameObject.SetActive(true);
                 value.OnRelevanceActivated(ReplTools.GetLocalPeer());
             }
             bool flag = false;
             if (value.ReplIsOwner())
             {
                 bool    flag2   = value.ReplCanChangeOwnership() && !value.ReplIsTransferringOwnership() && value.m_CreationTime < Time.time - 1f;
                 P2PPeer p2PPeer = null;
                 float   num2    = 0f;
                 for (int i = 0; i < ReplTools.GetRemotePeers().Count; i++)
                 {
                     P2PPeer p2PPeer2 = ReplTools.GetRemotePeers()[i];
                     float   num3     = value.CalculateRelevance(p2PPeer2, out erelevanceCalculationResult);
                     if (erelevanceCalculationResult == Relevance.ERelevanceCalculationResult.ToActivate)
                     {
                         value.OnRelevanceActivated(p2PPeer2);
                     }
                     else if (erelevanceCalculationResult == Relevance.ERelevanceCalculationResult.ToDeactivate)
                     {
                         value.OnRelevanceDeactivated(p2PPeer2);
                     }
                     if (flag2 && num3 > num2 && (num <= 0f || num3 > num + 0.1f))
                     {
                         num2    = num3;
                         p2PPeer = p2PPeer2;
                     }
                 }
                 if (p2PPeer != null)
                 {
                     value.ReplGiveOwnership(p2PPeer);
                 }
                 if (num <= 0f && value.CanBeRemovedByRelevance(true) && !value.ReplIsTransferringOwnership())
                 {
                     UnityEngine.Object.Destroy(value.gameObject);
                     flag = true;
                 }
             }
             if (!flag && num <= 0.1f && value.gameObject.activeSelf)
             {
                 value.gameObject.SetActive(false);
                 value.OnRelevanceDeactivated(ReplTools.GetLocalPeer());
             }
         }
     }
     enumerator.Dispose();
 }