void EventConnectionRequest.IEv.Callback(EventConnectionRequest.Arguments args) { var caller = args.Caller; var request = args.Request; if (caller != NetInstance || !NetInstance.SelfHost) { return; } // TODO: This is only temporary, this should be moved to somewhere else var playerId = request.Data.GetIdent128(); var em = MainWorld.GetOrCreateManager <EntityManager>(); var plBank = MainWorld.GetOrCreateManager <GamePlayerBank>(); var player = plBank.GetPlayerFromIdent(playerId); if (!player.IsCreated) { player = new GamePlayer(em.CreateEntity()); var masterServerPlayerId = new MasterServerPlayerId() { Id = playerId }; em.SetOrAddSharedComponentData(player.WorldPointer, masterServerPlayerId); plBank.AddPlayer(masterServerPlayerId, player); } else { var masterServerPlayerId = new MasterServerPlayerId() { Id = playerId }; em.SetOrAddSharedComponentData(player.WorldPointer, masterServerPlayerId); } m_ConnectionPlayerBank.RegisterPlayer(StMath.DoubleIntToLong(player.WorldPointer.Index, player.WorldPointer.Version), player); var peer = request.Accept(); em.SetOrAddSharedComponentData(player.WorldPointer, new PlayerPeerLink(caller.PeerInstance, peer)); }
void EventReceiveData.IEv.Callback(EventReceiveData.Arguments args) { var caller = args.Caller; var peerInstance = args.PeerInstance; var reader = args.Reader; if (caller != NetInstance || reader.Type != MessageType.Pattern) { return; } var peerPatternMgr = peerInstance.GetPatternManager(); var msg = peerPatternMgr.GetPattern(reader); var peerNetId = peerInstance.Global.Id; var conPlayerBank = peerInstance.Get <ConnectionPlayerBank>(); var plBank = MainWorld.GetOrCreateManager <GamePlayerBank>(); var em = MainWorld.GetOrCreateManager <EntityManager>(); if (msg == m_MsgUpdatePlayer) { var playerId = reader.Data.GetEntity(); var userIndex = reader.Data.GetULong(); var masterId = reader.Data.GetIdent128(); var owned = reader.Data.GetBool(); Debug.Log($"Update player ({playerId.Index}) {userIndex} {masterId}."); var player = plBank.GetPlayerFromIdent(masterId); if (!player.IsCreated) { player = new GamePlayer(em.CreateEntity()); var masterServerPlayerId = new MasterServerPlayerId() { Id = masterId }; player.WorldPointer.SetOrAddSharedComponentData(masterServerPlayerId, MainWorld); plBank.AddPlayer(masterServerPlayerId, player); } else { var masterServerPlayerId = new MasterServerPlayerId() { Id = masterId }; player.WorldPointer.SetOrAddSharedComponentData(masterServerPlayerId, MainWorld); } conPlayerBank.RegisterPlayer(StMath.DoubleIntToLong(playerId.Index, playerId.Version), player); player.WorldPointer.SetOrAddSharedComponentData ( new PlayerPeerLink(caller.PeerInstance, peerInstance.Peer), MainWorld ); player.WorldPointer.SetOrAddSharedComponentData ( new PlayerUserLink(peerInstance.Id, peerNetId, userIndex), MainWorld ); player.WorldPointer.SetOrAddComponentData(new ConnectedPlayerEntity(), MainWorld); } else if (msg == m_MsgRemovePlayer) { var playerId = reader.Data.GetInt(); var player = m_ConnectionPlayerBank.Get(playerId); if (player.IsCreated) { em.RemoveComponent <PlayerPeerLink>(player.WorldPointer); em.RemoveComponent <PlayerUserLink>(player.WorldPointer); em.RemoveComponent <ConnectedPlayerEntity>(player.WorldPointer); conPlayerBank.UnregisterPlayer(playerId); Debug.Log("player disconnected!"); } else { Debug.LogError("Data incoherence (player.IsCreated == false)"); } } }