// Create a P2PNetworkWriter from a list of entities. This P2PNetworkWriter can then be sent to peers private P2PNetworkWriter CreateNetworkWriterForChangedEntities(List <EntityData> changedEntities) { for (int i = changedEntities.Count - 1; i >= 0; i--) { if (!changedEntities[i].transform) { changedEntities.RemoveAt(i); } } if (changedEntities.Count == 0) { return(null); } P2PNetworkWriter writer = new P2PNetworkWriter(); writer.StartMessage(ENTITY_SCALE_SYNC_MESSAGE_TYPE); writer.Write((byte)changedEntities.Count); for (int i = 0; i < changedEntities.Count; i++) { writer.Write(changedEntities[i].transform.gameObject); writer.Write(changedEntities[i].transform.localScale.x); } writer.FinishMessage(); return(writer); }
public void SendTextChatMessage(string message) { P2PNetworkWriter p2PNetworkWriter = new P2PNetworkWriter(); p2PNetworkWriter.StartMessage(10); p2PNetworkWriter.Write(message); p2PNetworkWriter.FinishMessage(); this.SendWriterToAll(p2PNetworkWriter, 1); }
// Synchronize IsEnabled's value with peers private void SendEnabledStateToPeers() { enabledStateNextSyncTime = Time.realtimeSinceStartup + ENABLED_STATE_SYNC_INTERVAL; // Master (host) determines whether or not the mod is enabled if (ReplTools.AmIMaster() && !ReplTools.IsPlayingAlone()) { P2PNetworkWriter writer = new P2PNetworkWriter(); writer.StartMessage(ENABLED_STATE_SYNC_MESSAGE_TYPE); writer.Write(IsEnabled); writer.FinishMessage(); P2PSession.Instance.SendWriterToAll(writer, 0); // 0: TCP, 1: UDP } }
private void SyncScale(bool tcp) { if (!ReplTools.IsPlayingAlone()) { float scale = targetTransform.localScale.x; remoteScale = scale; P2PNetworkWriter writer = new P2PNetworkWriter(); writer.StartMessage(GulliverMod.PLAYER_SCALE_SYNC_MESSAGE_TYPE); writer.Write(gameObject); writer.Write(scale); writer.FinishMessage(); P2PSession.Instance.SendWriterToAll(writer, tcp ? 0 : 1); // 0: TCP, 1: UDP } }
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); }
private IEnumerator SendDroppedItemsToPeersCoroutine(GameObject[] droppedItems) { yield return(new WaitForSecondsRealtime(0.2f)); if (droppedItems != null && droppedItems.Length > 0) { P2PNetworkWriter writer = new P2PNetworkWriter(); writer.StartMessage(DROPPED_ITEMS_SYNC_MESSAGE_TYPE); writer.Write((byte)droppedItems.Length); for (int i = 0; i < droppedItems.Length; i++) { writer.Write(droppedItems[i].gameObject); } writer.FinishMessage(); P2PSession.Instance.SendWriterToAll(writer, 0); // 0: TCP, 1: UDP } }
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); }
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); }
// Add a PlayerScaleHandler component to this Player's P2PPlayer object (the Player representation that is // synchronized on all peers). This component notifies peers about changes to Player's scale and adjusts the Player's // animation speed, CharacterController values and etc. when the Player's scale changes private IEnumerator AddPlayerScaleHandlerComponentCoroutine() { while (!ReplicatedLogicalPlayer.s_LocalLogicalPlayer) { yield return(null); } if (!ReplicatedLogicalPlayer.s_LocalLogicalPlayer.GetComponent <PlayerScaleHandler>()) { ReplicatedLogicalPlayer.s_LocalLogicalPlayer.gameObject.AddComponent <PlayerScaleHandler>(); } yield return(new WaitForSecondsRealtime(1.5f)); // Request scale values of entities from host if (!ReplTools.AmIMaster()) { P2PNetworkWriter writer = new P2PNetworkWriter(); writer.StartMessage(ENTITY_SCALE_CACHE_REQUEST_MESSAGE_TYPE); writer.FinishMessage(); P2PSession.Instance.SendWriterTo(P2PSession.Instance.GetSessionMaster(false), writer, 0); // 0: TCP, 1: UDP } }
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); }