Esempio n. 1
0
 public void OnPeerDisconnected(P2PPeer peer)
 {
     if (!ReplTools.AmIMaster())
     {
         return;
     }
     this.DestroyPeerRepresentationRelatedObjects(peer);
     this.m_GameObjectMapLock = true;
     Dictionary <GameObject, ReplicationComponent> .Enumerator enumerator = this.m_GameObjectMap.GetEnumerator();
     while (enumerator.MoveNext())
     {
         KeyValuePair <GameObject, ReplicationComponent> keyValuePair = enumerator.Current;
         ReplicationComponent value = keyValuePair.Value;
         if (!(value == null) && value.ReplGetOwner() == peer)
         {
             if (value.ReplCanChangeOwnership())
             {
                 value.ReplRequestOwnership();
             }
             else
             {
                 UnityEngine.Object.Destroy(value.gameObject);
             }
         }
     }
     enumerator.Dispose();
     this.m_GameObjectMapLock = false;
 }
Esempio n. 2
0
 public void Update()
 {
     this.m_GameObjectMapLock = true;
     Dictionary <GameObject, ReplicationComponent> .Enumerator enumerator = this.m_GameObjectMap.GetEnumerator();
     while (enumerator.MoveNext())
     {
         KeyValuePair <GameObject, ReplicationComponent> keyValuePair = enumerator.Current;
         ReplicationComponent value = keyValuePair.Value;
         if (value != null && !this.m_DelayedSpawnObjects.Contains(value.gameObject))
         {
             value.OnReplicatorUpdate(false);
         }
     }
     enumerator.Dispose();
     this.m_GameObjectMapLock = false;
     if (this.m_DelayedRegisterObjects.Count > 0)
     {
         foreach (GameObject key in this.m_DelayedUnregisterObjects)
         {
             this.m_GameObjectMap.Remove(key);
         }
         foreach (Tuple <GameObject, ReplicationComponent> tuple in this.m_DelayedRegisterObjects)
         {
             this.m_GameObjectMap[tuple.Item1] = tuple.Item2;
         }
         this.m_DelayedUnregisterObjects.Clear();
         this.m_DelayedRegisterObjects.Clear();
     }
 }
Esempio n. 3
0
    public void OnNeedFullObjectInfo(P2PNetworkMessage netMsg)
    {
        byte[]     array      = netMsg.m_Reader.ReadGuidBytesTemporary();
        GameObject gameObject = GuidManager.ResolveGuid(array);

        if (!gameObject)
        {
            if (P2PLogFilter.logError)
            {
                Debug.LogError("Cant send full object info - no object! " + new Guid(array));
            }
            return;
        }
        ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true);

        if (P2PLogFilter.logPedantic)
        {
            Debug.Log(string.Format("OnNeedFullObjectInfo received for {0} guid: {1}", gameObject.name, replComponentForGameObject.GetGuidComponent.GetGuid()));
        }
        if (replComponentForGameObject != null && replComponentForGameObject.IsTransferringOwnershipTo(netMsg.m_Connection.m_Peer))
        {
            this.SpawnForPeerAndGiveOwnership(gameObject, netMsg.m_Connection.m_Peer);
            return;
        }
        this.SpawnForPeer(gameObject, netMsg.m_Connection.m_Peer);
    }
Esempio n. 4
0
 public void UnregisterReplicationComponent(ReplicationComponent component)
 {
     DebugUtils.Assert(!this.m_GameObjectMapLock, "Removing replicated object inside internal update!", true, DebugUtils.AssertType.Info);
     if (!this.m_GameObjectMapLock)
     {
         this.m_GameObjectMap.Remove(component.gameObject);
     }
 }
Esempio n. 5
0
    public static void ReplSendAsap(this MonoBehaviour component)
    {
        ReplicationComponent replComponentForGameObject = Replicator.Singleton.GetReplComponentForGameObject(component.gameObject, false);

        if (replComponentForGameObject)
        {
            replComponentForGameObject.ReplSendAsap();
        }
    }
Esempio n. 6
0
    public static void ReplSendAsap(this GameObject game_object)
    {
        ReplicationComponent replComponentForGameObject = Replicator.Singleton.GetReplComponentForGameObject(game_object, false);

        if (replComponentForGameObject)
        {
            replComponentForGameObject.ReplSendAsap();
        }
    }
Esempio n. 7
0
    public static void ReplBlockChangeOwnership(this IReplicatedBehaviour repl_behaviour, bool block)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return;
        }
        replicationComponent.ReplBlockChangeOwnership(block);
    }
Esempio n. 8
0
    public static P2PPeer ReplGetOwner(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return(null);
        }
        return(replicationComponent.ReplGetOwner());
    }
Esempio n. 9
0
    public static float ReplGetReplicationInterval(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return(0f);
        }
        return(replicationComponent.ReplGetReplicationInterval(ReplTools.GetLocalPeer()));
    }
Esempio n. 10
0
    public static void ReplClearDirty(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return;
        }
        replicationComponent.ReplSetDirty(repl_behaviour, false);
    }
Esempio n. 11
0
    public static void ReplGiveOwnership(this IReplicatedBehaviour repl_behaviour, P2PPeer peer)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        if (replicationComponent == null)
        {
            return;
        }
        replicationComponent.ReplGiveOwnership(peer);
    }
Esempio n. 12
0
    private static bool ReplRequestOwnershipInternal(GameObject game_object, bool search_parent = false)
    {
        ReplicationComponent replComponentForGameObject = Replicator.Singleton.GetReplComponentForGameObject(game_object, false);

        if (replComponentForGameObject != null)
        {
            replComponentForGameObject.ReplRequestOwnership();
            return(true);
        }
        return(search_parent && game_object.transform.parent != null && ReplTools.ReplRequestOwnershipInternal(game_object.transform.parent.gameObject, search_parent));
    }
Esempio n. 13
0
 public void RegisterReplicationComponent(ReplicationComponent component)
 {
     if (component)
     {
         if (this.m_GameObjectMapLock)
         {
             this.m_DelayedRegisterObjects.Add(Tuple.Create <GameObject, ReplicationComponent>(component.gameObject, component));
             return;
         }
         this.m_GameObjectMap[component.gameObject] = component;
     }
 }
Esempio n. 14
0
    public bool Despawn(ReplicationComponent obj)
    {
        if (ReplTools.IsPlayingAlone())
        {
            return(false);
        }
        P2PNetworkWriter p2PNetworkWriter = this.BuildDespawnWriter(obj);

        if (p2PNetworkWriter != null)
        {
            P2PSession.Instance.SendWriterToAll(p2PNetworkWriter, 0);
        }
        return(p2PNetworkWriter != null);
    }
Esempio n. 15
0
    public void OnSpawnMessage(P2PNetworkMessage net_msg, bool take_ownership)
    {
        net_msg.ReadMessage <P2PObjectSpawnMessage>(Replicator.s_ObjectSpawnMessage);
        GameObject gameObject = GuidManager.ResolveGuid(Replicator.s_ObjectSpawnMessage.guid_bytes);

        if (P2PLogFilter.logDev)
        {
            Debug.Log(string.Format("OnSpawnMessage {0} channel: {1} take_ownership: {2}", new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes), net_msg.m_ChannelId, take_ownership));
        }
        if (!gameObject)
        {
            GameObject prefabById = P2PAssetIdCache.Instance.GetPrefabById(Replicator.s_ObjectSpawnMessage.assetId);
            if (prefabById)
            {
                bool activeSelf = prefabById.activeSelf;
                prefabById.SetActive(false);
                gameObject = UnityEngine.Object.Instantiate <GameObject>(prefabById, Replicator.s_ObjectSpawnMessage.position, Quaternion.identity);
                ReplicationComponent component = gameObject.GetComponent <ReplicationComponent>();
                component.ReplOnChangedOwner(net_msg.m_Connection.m_Peer);
                gameObject.GetComponent <GuidComponent>().ForceGuid(Replicator.s_ObjectSpawnMessage.guid_bytes);
                prefabById.SetActive(activeSelf);
                gameObject.SetActive(activeSelf);
                component.ReplOnSpawned();
                component.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true);
                if (take_ownership)
                {
                    component.ReplRequestOwnership();
                    return;
                }
            }
            else if (P2PLogFilter.logError)
            {
                Debug.LogError(string.Format("OnSpawnMessage no asset found {0}", Replicator.s_ObjectSpawnMessage.assetId));
                return;
            }
        }
        else
        {
            if (P2PLogFilter.logWarn)
            {
                Debug.LogWarning(string.Format("OnSpawnMessage found target for supposedly new object to spawn {0} {1}", gameObject.name, new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes)));
            }
            ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true);
            replComponentForGameObject.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true);
            if (take_ownership)
            {
                replComponentForGameObject.ReplRequestOwnership();
            }
        }
    }
Esempio n. 16
0
    public bool DespawnForPeer(ReplicationComponent obj, P2PPeer peer)
    {
        P2PNetworkWriter p2PNetworkWriter = this.BuildDespawnWriter(obj);

        if (p2PNetworkWriter != null)
        {
            if (P2PLogFilter.logPedantic)
            {
                Debug.Log(string.Format("DespawnForPeer [{0}] called for {1} guid: {2}", peer.GetHostId(), obj.name, obj.GetGuidComponent.GetGuid()), obj);
            }
            P2PSession.Instance.SendWriterTo(peer, p2PNetworkWriter, 0);
        }
        return(p2PNetworkWriter != null);
    }
Esempio n. 17
0
    private P2PNetworkWriter BuildSpawnWriter(GameObject obj, bool give_ownership)
    {
        ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(obj, true);

        if (replComponentForGameObject == null)
        {
            if (P2PLogFilter.logError)
            {
                Debug.LogError("Replicator::Spawn called for non-replicable object " + obj.name + "!");
            }
            return(null);
        }
        if (!replComponentForGameObject.enabled)
        {
            return(null);
        }
        GuidComponent component = obj.GetComponent <GuidComponent>();

        if (component == null)
        {
            if (P2PLogFilter.logError)
            {
                Debug.LogError("Replicator::Spawn called for object " + obj.name + " without guid!", obj);
            }
            return(null);
        }
        if (P2PLogFilter.logPedantic)
        {
            Debug.Log(string.Concat(new string[]
            {
                "Replicator::BuildSpawnWriter called for ",
                obj.name,
                " with asset id ",
                replComponentForGameObject.AssetId.ToString(),
                "!"
            }), obj);
        }
        Replicator.s_ObjectSpawnMessage.assetId    = replComponentForGameObject.AssetId;
        Replicator.s_ObjectSpawnMessage.guid_bytes = component.GetGuidBytes();
        Replicator.s_ObjectSpawnMessage.position   = obj.transform.position;
        Replicator.s_ObjectSpawnMessage.payload    = replComponentForGameObject.Serialize(true);
        P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter();

        p2PNetworkWriter.StartMessage(give_ownership ? 9 : 2);
        Replicator.s_ObjectSpawnMessage.Serialize(p2PNetworkWriter);
        p2PNetworkWriter.FinishMessage();
        return(p2PNetworkWriter);
    }
Esempio n. 18
0
    private P2PNetworkWriter BuildDespawnWriter(ReplicationComponent obj)
    {
        if (obj.GetGuidComponent == null)
        {
            if (P2PLogFilter.logError)
            {
                Debug.LogError("Replicator::BuildDespawnWriter called for object without guid!");
            }
            return(null);
        }
        P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter();

        p2PNetworkWriter.StartMessage(3);
        p2PNetworkWriter.Write(obj.gameObject);
        p2PNetworkWriter.FinishMessage();
        return(p2PNetworkWriter);
    }
Esempio n. 19
0
    public void OnRequestOwnership(P2PNetworkMessage netMsg)
    {
        GameObject gameObject = netMsg.m_Reader.ReadGameObject();

        if (gameObject)
        {
            ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true);
            if (P2PLogFilter.logPedantic)
            {
                Debug.Log(string.Format("OnRequestOwnership called for {0} guid: {1}", replComponentForGameObject.name, replComponentForGameObject.GetGuidComponent.GetGuid()), gameObject);
            }
            ReplicationComponent.s_DeserializedComponent = replComponentForGameObject;
            P2PPeer peerById = ReplTools.GetPeerById(netMsg.m_Reader.ReadInt16());
            replComponentForGameObject.ReplOnChangedOwner(peerById);
            ReplicationComponent.s_DeserializedComponent = null;
        }
    }
Esempio n. 20
0
 public void OnPeerConnected(P2PPeer peer)
 {
     if (peer.GetHostId() == P2PPeer.s_InvalidId)
     {
         return;
     }
     this.m_GameObjectMapLock = true;
     Dictionary <GameObject, ReplicationComponent> .Enumerator enumerator = this.m_GameObjectMap.GetEnumerator();
     while (enumerator.MoveNext())
     {
         KeyValuePair <GameObject, ReplicationComponent> keyValuePair = enumerator.Current;
         ReplicationComponent value = keyValuePair.Value;
         if (!(value == null) && !(value.GetRelevanceComponent != null) && (value.ReplIsOwner() || (ReplTools.AmIMaster() && value.ReplGetOwner() == P2PPeer.s_Invalid)))
         {
             this.SpawnForPeer(value.gameObject, peer);
         }
     }
     enumerator.Dispose();
     this.m_GameObjectMapLock = false;
 }
Esempio n. 21
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);
    }
Esempio n. 22
0
    public static P2PPeer ReplGetOwner(this GameObject game_object)
    {
        ReplicationComponent replComponentForGameObject = Replicator.Singleton.GetReplComponentForGameObject(game_object, false);

        return(((replComponentForGameObject != null) ? replComponentForGameObject.ReplGetOwner() : null) ?? null);
    }
    public void OnPlantDestroy()
    {
        base.enabled = true;
        if (!this.m_DontDestroy)
        {
            base.gameObject.AddComponent <TinyPhysicsObject>();
        }
        Rigidbody rigidbody = base.gameObject.AddComponentWithEvent <Rigidbody>();

        if (rigidbody)
        {
            rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
        }
        Collider collider = base.gameObject.GetComponent <Collider>();

        if (collider == null)
        {
            if (this.m_UseApproxBoxCollider)
            {
                BoxCollider boxCollider = base.gameObject.AddComponent <BoxCollider>();
                boxCollider.size    = base.GetComponent <Renderer>().bounds.size * 0.5f;
                boxCollider.center -= new Vector3(0f, boxCollider.size.y * 0.5f, 0f);
                collider            = boxCollider;
            }
            else
            {
                MeshCollider meshCollider = base.gameObject.AddComponent <MeshCollider>();
                meshCollider.convex = true;
                collider            = meshCollider;
                PhysicsDampener physicsDampener = base.gameObject.AddComponent <PhysicsDampener>();
                physicsDampener.m_MaxYUpVelocity       = 1f;
                physicsDampener.m_MaxVelocityMagnitude = 2f;
            }
        }
        this.ReplaceShader(base.gameObject);
        ObjectMaterial objectMaterial = base.GetComponent <ObjectMaterial>();

        if (objectMaterial == null)
        {
            objectMaterial = base.gameObject.AddComponent <ObjectMaterial>();
        }
        objectMaterial.m_ObjectMaterial = EObjectMaterial.Bush;
        base.gameObject.layer           = this.m_Layer;
        if (this.m_ActivateScripts)
        {
            MonoBehaviour[] components = base.GetComponents <MonoBehaviour>();
            for (int i = 0; i < components.Length; i++)
            {
                components[i].enabled = true;
            }
        }
        else
        {
            ReplicationComponent component = base.GetComponent <ReplicationComponent>();
            if (component)
            {
                component.enabled = true;
            }
        }
        base.transform.parent = null;
        this.DisableCollisionWithPlayer(collider);
        this.DisableCollisionWithOtherChunks(collider);
        base.gameObject.SetActive(true);
        if (base.ReplIsOwner())
        {
            this.ReplSetDirty();
        }
        this.m_PlantDestroyed = true;
    }
Esempio n. 24
0
    public static bool ReplIsDirty(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        return(replicationComponent != null && replicationComponent.ReplIsDirty(repl_behaviour));
    }
Esempio n. 25
0
    public static bool ReplIsTransferringOwnership(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        return(replicationComponent != null && replicationComponent.IsTransferringOwnership());
    }
Esempio n. 26
0
    public static bool ReplCanChangeOwnership(this IReplicatedBehaviour repl_behaviour)
    {
        ReplicationComponent replicationComponent = repl_behaviour.GetReplicationComponent();

        return(replicationComponent != null && replicationComponent.ReplCanChangeOwnership());
    }