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); }
// One or more entities' scale have changed private void OnEntityScaleChanged(P2PNetworkMessage message) { // Don't receive the scale changes that we've transmitted ourselves if (message.m_Connection.m_Peer.IsLocalPeer()) { return; } // If we want to change scaled entities' animation speeds on all clients, then all clients must // keep track of the scaled entities (i.e. isHost check is commented out) //bool isHost = ReplTools.AmIMaster(); int count = message.m_Reader.ReadByte(); for (int i = 0; i < count; i++) { GameObject obj = message.m_Reader.ReadGameObject(); float scale = message.m_Reader.ReadFloat(); if (obj) { obj.transform.localScale = new Vector3(scale, scale, scale); //if( isHost ) AddScaledEntityToCache(obj.transform, true); } } }
private void OnDisconnectInternal(P2PNetworkMessage net_msg) { if (P2PLogFilter.logDebug) { Debug.Log("P2PSession:OnDisconnectInternal"); } Replicator.Singleton.OnPeerDisconnected(net_msg.m_Connection.m_Peer); }
// Host has activated or deactivated the mod private void OnModEnabledStateChanged(P2PNetworkMessage message) { bool modEnabled = message.m_Reader.ReadBoolean(); if (!ReplTools.AmIMaster() && (message.m_Connection.m_Peer == null || message.m_Connection.m_Peer.IsMaster())) { OnModEnabledStateChanged(modEnabled); } }
public bool InvokeHandler(P2PNetworkMessage netMsg) { if (this.m_MessageHandlersDict.ContainsKey(netMsg.m_MsgType)) { List <P2PNetworkMessageDelegate> list = this.m_MessageHandlersDict[netMsg.m_MsgType]; for (int i = 0; i < list.Count; i++) { list[i](this.m_MessageInfo); } return(true); } return(false); }
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(); } } }
// A player's scale has changed private void OnPlayerScaleChanged(P2PNetworkMessage message) { // Don't receive the scale changes that we've transmitted ourselves if (message.m_Connection.m_Peer.IsLocalPeer()) { return; } GameObject obj = message.m_Reader.ReadGameObject(); float scale = message.m_Reader.ReadFloat(); if (obj) { obj.transform.localScale = new Vector3(scale, scale, scale); } }
// A new player has joined the server and requested the scale values of entities from the host private void OnEntityScalesRequested(P2PNetworkMessage message) { if (message.m_Connection.m_Peer.IsLocalPeer()) { ModAPI.Log.Write("OnEntityScalesRequested sent to self, this shouldn't happen!"); return; } if (message.m_Connection.m_IsReady) { P2PNetworkWriter writer = CreateNetworkWriterForChangedEntities(scaledEntityCache); if (writer != null) { message.m_Connection.SendWriter(writer, 0); // 0: TCP, 1: UDP } } }
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; } }
private void OnPeerDisconnect(P2PNetworkMessage net_msg) { P2PConnection connection = net_msg.m_Connection; P2PPeer p2PPeer = (connection != null) ? connection.m_Peer : null; if (p2PPeer != null) { 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.OnPeerDisconnected(p2PPeer); } } enumerator.Dispose(); } }
private void OnConnect(P2PNetworkMessage net_msg) { if (net_msg.m_Connection.m_Peer == P2PSession.Instance.GetSessionMaster()) { ItemsManager.Get().ClearFallenObjects(); ItemsManager.Get().Preload(); MainLevel.Instance.ResetGameBeforeLoad(); Vector2 vector = UnityEngine.Random.insideUnitCircle * 2f; NavMeshHit navMeshHit; if (NavMesh.SamplePosition(P2PSession.Instance.m_SpawnRefPosition + new Vector3(vector.x, 0f, vector.y), out navMeshHit, 10f, AIManager.s_WalkableAreaMask)) { Player.Get().Reposition(navMeshHit.position, null); Player.Get().m_RespawnPosition = navMeshHit.position; } if (this.m_LogicalPlayer == null) { this.m_LogicalPlayer = UnityEngine.Object.Instantiate <GameObject>(this.m_PlayerPrefab, Player.Get().transform.position, Player.Get().transform.rotation); } SaveGame.LoadCoop(); } }
public void OnDespawnMessage(P2PNetworkMessage netMsg) { GameObject gameObject = netMsg.m_Reader.ReadGameObject(); if (gameObject) { if (this.GetReplComponentForGameObject(gameObject, true).ReplGetOwner() == netMsg.m_Connection.m_Peer) { if (P2PLogFilter.logPedantic) { Debug.Log(string.Format("OnDespawnMessage called for {0} guid: {1}", gameObject.name, gameObject.GetComponent <GuidComponent>().GetGuid()), gameObject); } UnityEngine.Object.Destroy(gameObject); return; } if (P2PLogFilter.logError) { Debug.LogError(string.Format("Removing object {0} guid: {1} not owned by peer ordering the removal!!!", gameObject.name, gameObject.GetComponent <GuidComponent>().GetGuid())); } } }
public void OnReplicationMessage(P2PNetworkMessage net_msg) { byte[] array = net_msg.m_Reader.ReadGuidBytesTemporary(); GameObject gameObject = GuidManager.ResolveGuid(array); if (gameObject) { this.GetReplComponentForGameObject(gameObject, true).ReplicationReceive(net_msg.m_Reader, false); return; } if (P2PLogFilter.logWarn) { Debug.LogWarning("Did not find target for replication update message for " + new Guid(array)); } P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter(); p2PNetworkWriter.StartMessage(8); p2PNetworkWriter.Write(array, GuidComponent.GUID_BYTES_CNT); p2PNetworkWriter.FinishMessage(); P2PSession.Instance.SendWriterTo(net_msg.m_Connection.m_Peer, p2PNetworkWriter, 0); }
// Another player has dropped some items, reset these items' scale values private void OnPeerDroppedItems(P2PNetworkMessage message) { // Ignore the items that we've dropped ourselves if (message.m_Connection.m_Peer.IsLocalPeer()) { return; } // Fetch the dropped items int count = message.m_Reader.ReadByte(); List <Item> droppedItems = new List <Item>(count); for (int i = 0; i < count; i++) { GameObject obj = message.m_Reader.ReadGameObject(); if (obj) { droppedItems.Add(obj.GetComponent <Item>()); } } StartCoroutine(ScalePeersDroppedItemsCoroutine(droppedItems)); }
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); }
private void OnGiveOwnership(P2PNetworkMessage net_msg) { Replicator.Singleton.OnGiveOwnership(net_msg); }
private void OnReplication(P2PNetworkMessage net_msg) { Replicator.Singleton.OnReplicationMessage(net_msg); }
private void OnConnectInternal(P2PNetworkMessage net_msg) { this.OnConnectInternal(net_msg.m_Connection); }
private void ObjectSpawnAndGiveOwnership(P2PNetworkMessage net_msg) { Replicator.Singleton.OnSpawnMessage(net_msg, true); }
private void OnObjectSpawn(P2PNetworkMessage net_msg) { Replicator.Singleton.OnSpawnMessage(net_msg, false); }
private void OnDisconnect(P2PNetworkMessage net_msg) { }
private void OnObjectDespawn(P2PNetworkMessage net_msg) { Replicator.Singleton.OnDespawnMessage(net_msg); }
private void OnNeedFullObjectInfo(P2PNetworkMessage net_msg) { Replicator.Singleton.OnNeedFullObjectInfo(net_msg); }
private void OnPeerInfo(P2PNetworkMessage net_msg) { }
private void OnTextChat(P2PNetworkMessage net_msg) { HUDMessages.Get().AddMessage(net_msg.m_Connection.m_Peer.GetDisplayName() + ": " + net_msg.m_Reader.ReadString(), null, HUDMessageIcon.None, "", null); }