/// <summary> /// Send player joined message to connected players. /// </summary> /// <param name="player">Player who joined.</param> private void SendPlayerJoined(int playerID, NetPlayer player) { Messages.PlayerJoinMessage msg = new Messages.PlayerJoinMessage(); msg.playerID = playerID; msg.steamID = player.SteamId.m_SteamID; BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }
/// <summary> /// Sends handshake to the connected player. /// </summary> private void SendHandshake(NetPlayer player) { Messages.HandshakeMessage message = new Messages.HandshakeMessage(); message.protocolVersion = PROTOCOL_VERSION; message.clock = GetNetworkClock(); SendMessage(player, message, Steamworks.EP2PSend.k_EP2PSendReliable); }
/// <summary> /// Process handshake message received from the given steam id. /// </summary> /// <param name="senderSteamId">The steam id of the sender.</param> /// <param name="msg">Hand shake message.</param> private void HandleHandshake(Steamworks.CSteamID senderSteamId, Messages.HandshakeMessage msg) { if (IsHost) { if (players[1] != null) { Logger.Log("Received handshake from player but player is already here."); LeaveLobby(); return; } // Setup THE PLAYER timeSinceLastHeartbeat = 0.0f; players[1] = new NetPlayer(this, netWorld, senderSteamId); // Player can be spawned here safely. Host is already in game and all game objects are here. players[1].Spawn(); SendHandshake(); } else { if (players[1] == null) { Logger.Log("Received handshake from host but host is not here."); LeaveLobby(); return; } Logger.Log("CONNECTION ESTABLISHED!"); MPController.Instance.LoadLevel("GAME"); // Host will be spawned when game will be loaded and OnGameWorldLoad callback will be called. } // Set player state. players[1].Teleport(Utils.NetVec3ToGame(msg.spawnPosition), Utils.NetQuatToGame(msg.spawnRotation)); if (msg.occupiedVehicleId != NetVehicle.INVALID_ID) { var vehicle = netWorld.GetVehicle(msg.occupiedVehicleId); Client.Assert(vehicle != null, $"Player {players[1].GetName()} ({players[1].SteamId}) you tried to join reported that he drives car that does not exists in your game. Vehicle id: {msg.occupiedVehicleId}, passenger: {msg.passenger}"); players[1].EnterVehicle(vehicle, msg.passenger); } if (msg.pickedUpObject != NetPickupable.INVALID_ID) { players[1].PickupObject(msg.pickedUpObject); } players[1].hasHandshake = true; }
/// <summary> /// Set player inside vehicle. /// </summary> /// <param name="player">The player to set.</param> /// <param name="passenger">Whether to set player as passenger or as driver.</param> public void SetPlayer(NetPlayer player, bool passenger) { if (passenger) { passengerPlayer = player; } else { driverPlayer = player; } }
/// <summary> /// Set player inside vehicle. /// </summary> /// <param name="player">The player to set.</param> /// <param name="passenger">Whether to set player as passenger or as driver.</param> public void SetPlayer(NetPlayer player, bool passenger) { if (passenger) { passengerPlayer = player; } else { driverPlayer = player; ApplyRemoteSteering(); } }
/// <summary> /// Send message to given player. /// </summary> /// <typeparam name="T">The type of the message to broadcast.</typeparam> /// <param name="player">Player to who message should be send.</param> /// <param name="message">The message to broadcast.</param> /// <param name="sendType">The send type.</param> /// <param name="channel">The channel used to deliver message.</param> /// <returns>true if message was sent false otherwise</returns> public bool SendMessage <T>(NetPlayer player, T message, Steamworks.EP2PSend sendType, int channel = 0) where T : INetMessage { if (player == null) { return(false); } MemoryStream stream = new MemoryStream(); if (!WriteMessage(message, stream)) { return(false); } return(player.SendPacket(stream.GetBuffer(), sendType, channel)); }
/// <summary> /// Callback called when client accepts lobby join request from other steam user. /// </summary> /// <param name="request">The request.</param> private void OnGameLobbyJoinRequested(Steamworks.GameLobbyJoinRequested_t request) { Steamworks.SteamAPICall_t apiCall = Steamworks.SteamMatchmaking.JoinLobby(request.m_steamIDLobby); if (apiCall == Steamworks.SteamAPICall_t.Invalid) { Logger.Log("Unable to join lobby."); return; } Logger.Log("Setup player."); // Setup remote player. The HOST. timeSinceLastHeartbeat = 0.0f; players[1] = new NetPlayer(this, netWorld, request.m_steamIDFriend); lobbyEnterCallResult.Set(apiCall); }
/// <summary> /// Callback called when client accepts lobby join request from other steam user. /// </summary> /// <param name="request">The request.</param> private void OnGameLobbyJoinRequested(Steamworks.GameLobbyJoinRequested_t request) { Steamworks.SteamAPICall_t apiCall = Steamworks.SteamMatchmaking.JoinLobby(request.m_steamIDLobby); if (apiCall == Steamworks.SteamAPICall_t.Invalid) { Logger.Error($"Unable to join lobby {request.m_steamIDLobby}. JoinLobby call failed."); MPGUI.Instance.ShowMessageBox($"Failed to join lobby.\nPlease try again later."); return; } Logger.Debug("Setup player."); // Setup remote player. The HOST. timeSinceLastHeartbeat = 0.0f; players[1] = new NetPlayer(this, netWorld, request.m_steamIDFriend); lobbyEnterCallResult.Set(apiCall); }
/// <summary> /// Process handshake message received from the given steam id. /// </summary> /// <param name="senderSteamId">The steam id of the sender.</param> /// <param name="msg">Hand shake message.</param> private void HandleHandshake(Steamworks.CSteamID senderSteamId, Messages.HandshakeMessage msg) { if (IsHost) { if (players[1] != null) { Logger.Log("Received handshake from player but player is already here."); LeaveLobby(); return; } // Setup THE PLAYER timeSinceLastHeartbeat = 0.0f; players[1] = new NetPlayer(this, netWorld, senderSteamId); // Player can be spawned here safely. Host is already in game and all game objects are here. players[1].Spawn(); SendHandshake(); } else { if (players[1] == null) { Logger.Log("Received handshake from host but host is not here."); LeaveLobby(); return; } Logger.Log("CONNECTION ESTABLISHED!"); MPController.Instance.LoadLevel("GAME"); // Host will be spawned when game will be loaded and OnGameWorldLoad callback will be called. } players[1].hasHandshake = true; }
/// <summary> /// Register world related network message handlers. /// </summary> /// <param name="netMessageHandler">The network message handler to register messages to.</param> void RegisterNetworkMessagesHandlers(NetMessageHandler netMessageHandler) { netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableSetPositionMessage msg) => { Client.Assert(netPickupables.ContainsKey(msg.id), $"Tried to move pickupable that is not spawned {msg.id}."); GameObject gameObject = netPickupables[msg.id].gameObject; gameObject.transform.position = Utils.NetVec3ToGame(msg.position); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableActivateMessage msg) => { GameObject gameObject = null; if (netPickupables.ContainsKey(msg.id)) { gameObject = netPickupables[msg.id].gameObject; } if (msg.activate) { Client.Assert(gameObject != null, "Tried to activate pickupable but its not spawned!"); gameObject.SetActive(true); } else { if (gameObject != null) { gameObject.SetActive(false); } } }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableSpawnMessage msg) => { SpawnPickupable(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableDestroyMessage msg) => { if (!netPickupables.ContainsKey(msg.id)) { return; } NetPickupable pickupable = netPickupables[msg.id]; GameObject.Destroy(pickupable.gameObject); netPickupables.Remove(msg.id); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.WorldPeriodicalUpdateMessage msg) => { // Game reports 'next hour' - we want to have transition so correct it. Game.GameWorld.Instance.WorldTime = (float)msg.sunClock - 2.0f; Game.GameWorld.Instance.WorldDay = (int)msg.worldDay; Game.GameWeatherManager.Instance.SetWeather(msg.currentWeather); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.RemoveBottleMessage msg) => { GameObject beerGO = GetPickupableGameObject(msg.netId); Game.Objects.BeerCase beer = Game.BeerCaseManager.Instance.FindBeerCase(beerGO); if (beer == null) { Logger.Log($"Player tried to drink beer, however, the beercase cannot be found."); return; } beer.RemoveBottles(1); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PlayerSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Log($"Received synchronization packet from {sender} but there is not player registered using this id."); return; } player.HandleSynchronize(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.OpenDoorsMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Log($"Received OpenDoorsMessage however there is no matching player {sender}! (open: {msg.open}"); return; } Game.Objects.GameDoor doors = Game.GameDoorsManager.Instance.FindGameDoors(Utils.NetVec3ToGame(msg.position)); if (doors == null) { Logger.Log($"Player tried to open door, however, the door could not be found!"); return; } doors.Open(msg.open); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.FullWorldSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); // This one should never happen - if happens there is something done miserably wrong. Client.Assert(player != null, $"There is no player matching given steam id {sender}."); // Handle full world state synchronization. HandleFullWorldSync(msg); // Spawn host character. player.Spawn(); // Set player state. player.Teleport(Utils.NetVec3ToGame(msg.spawnPosition), Utils.NetQuatToGame(msg.spawnRotation)); if (msg.occupiedVehicleId != NetVehicle.INVALID_ID) { var vehicle = GetVehicle(msg.occupiedVehicleId); Client.Assert(vehicle != null, $"Player {player.GetName()} ({player.SteamId}) you tried to join reported that he drives car that does not exists in your game. Vehicle id: {msg.occupiedVehicleId}, passenger: {msg.passenger}"); player.EnterVehicle(vehicle, msg.passenger); } if (msg.pickedUpObject != NetPickupable.INVALID_ID) { player.PickupObject(msg.pickedUpObject); } // World is loaded! Notify network manager about that. netManager.OnNetworkWorldLoaded(); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.AskForWorldStateMessage msg) => { var msgF = new Messages.FullWorldSyncMessage(); WriteFullWorldSync(msgF); netManager.BroadcastMessage(msgF, Steamworks.EP2PSend.k_EP2PSendReliable); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleEnterMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } NetVehicle vehicle = GetVehicle(msg.vehicleId); if (vehicle == null) { Logger.Error("Player " + player.SteamId + " tried to enter vehicle " + msg.vehicleId + " but there is no vehicle with such id."); return; } player.EnterVehicle(vehicle, msg.passenger); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleLeaveMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.LeaveVehicle(); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.HandleVehicleSync(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupObjectMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.PickupObject(msg.netId); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ReleaseObjectMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.ReleaseObject(msg.drop); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.LightSwitchMessage msg) => { Game.Objects.LightSwitch light = Game.LightSwitchManager.Instance.FindLightSwitch(Utils.NetVec3ToGame(msg.pos)); light.TurnOn(msg.toggle); }); }
/// <summary> /// Process handshake message received from the given steam id. /// </summary> /// <param name="senderSteamId">The steam id of the sender.</param> /// <param name="msg">Hand shake message.</param> private void HandleHandshake(Steamworks.CSteamID senderSteamId, Messages.HandshakeMessage msg) { if (IsHost) { if (players[1] != null) { Logger.Log("Received handshake from player but player is already here."); LeaveLobby(); return; } // Setup THE PLAYER timeSinceLastHeartbeat = 0.0f; players[1] = new NetPlayer(this, netWorld, senderSteamId); // Check if version matches - if not ignore this player. if (msg.protocolVersion != PROTOCOL_VERSION) { RejectPlayer($"Mod version mismatch."); return; } // Player can be spawned here safely. Host is already in game and all game objects are here. players[1].Spawn(); SendHandshake(players[1]); MessagesList.AddMessage($"Player {players[1].GetName()} joined.", MessageSeverity.Info); } else { if (players[1] == null) { Logger.Log("Received handshake from host but host is not here."); LeaveLobby(); return; } // Check if protocol version matches. if (msg.protocolVersion != PROTOCOL_VERSION) { string message; if (msg.protocolVersion > PROTOCOL_VERSION) { message = "Host has newer version of the mod."; } else { message = "Host has older version of the mod."; } AbortJoining($"{message}\n(Your mod version: {PROTOCOL_VERSION}, Host mod version: {msg.protocolVersion})"); return; } // All is fine - load game world. MessagesList.AddMessage($"Connection established!", MessageSeverity.Info); MPController.Instance.LoadLevel("GAME"); // Host will be spawned when game will be loaded and OnGameWorldLoad callback will be called. } remoteClock = msg.clock; players[1].hasHandshake = true; }
/// <summary> /// Register world related network message handlers. /// </summary> /// <param name="netMessageHandler">The network message handler to register /// messages to.</param> void RegisterNetworkMessagesHandlers(NetMessageHandler netMessageHandler) { netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.PickupableSetPositionMessage msg) => { Client.Assert(ObjectSyncManager.Instance.ObjectIDs.ContainsKey(msg.id), $"Tried to move pickupable that is not spawned {msg.id}."); GameObject gameObject = ObjectSyncManager.Instance.ObjectIDs[msg.id].gameObject; gameObject.transform.position = Utils.NetVec3ToGame(msg.position); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.PickupableActivateMessage msg) => { GameObject gameObject = null; if (ObjectSyncManager.Instance.ObjectIDs.ContainsKey(msg.id)) { gameObject = ObjectSyncManager.Instance.ObjectIDs[msg.id].gameObject; } Client.Assert(gameObject != null, "Tried to activate pickupable but its not spawned!"); if (msg.activate) { gameObject.SetActive(true); } else { if (gameObject != null) { gameObject.SetActive(false); } } }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.PickupableSpawnMessage msg) => { SpawnPickupable(msg); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.PickupableDestroyMessage msg) => { if (!ObjectSyncManager.Instance.ObjectIDs.ContainsKey(msg.id)) { return; } GameObject go; try { go = ObjectSyncManager.Instance.ObjectIDs[msg.id].gameObject; } catch { Logger.Error( "Failed to remove object: OSC found but can't get GameObject."); return; } GameObject.Destroy( ObjectSyncManager.Instance.ObjectIDs[msg.id].gameObject); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.WorldPeriodicalUpdateMessage msg) => { // Game reports 'next hour' - we want to have transition so correct it. GameWorld.Instance.WorldTime = (float)msg.sunClock - 2.0f; GameWorld.Instance.WorldDay = (int)msg.worldDay; GameWeatherManager.Instance.SetWeather(msg.currentWeather); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PlayerSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Log( $"Received synchronization packet from {sender} but there is not player registered using this id."); return; } player.HandleSynchronize(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.AnimSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Log( $"Received animation synchronization packet from {sender} but there is not player registered using this id."); return; } player.HandleAnimSynchronize(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.OpenDoorsMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Log( $"Received OpenDoorsMessage however there is no matching player {sender}! (open: {msg.open}"); return; } GameDoor doors = GameDoorsManager.Instance.FindGameDoors( Utils.NetVec3ToGame(msg.position)); if (doors == null) { Logger.Log( $"Player tried to open door, however, the door could not be found!"); return; } doors.Open(msg.open); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.FullWorldSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); // This one should never happen - if happens there is something done // miserably wrong. Client.Assert(player != null, $"There is no player matching given steam id {sender}."); // Handle full world state synchronization. HandleFullWorldSync(msg); // Spawn host character. player.Spawn(); // Set player state. player.Teleport(Utils.NetVec3ToGame(msg.spawnPosition), Utils.NetQuatToGame(msg.spawnRotation)); if (msg.pickedUpObject != NetPickupable.INVALID_ID) { player.PickupObject(msg.pickedUpObject); } // World is loaded! Notify network manager about that. netManager.OnNetworkWorldLoaded(); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.AskForWorldStateMessage msg) => { var msgF = new Messages.FullWorldSyncMessage(); WriteFullWorldSync(msgF); netManager.BroadcastMessage( msgF, Steamworks.EP2PSend.k_EP2PSendReliable); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleEnterMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error( $"Steam user of id {sender} send message however there is no active player matching this id."); return; } ObjectSyncComponent vehicle = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; if (vehicle == null) { Logger.Error("Player " + player.SteamId + " tried to enter vehicle with Object ID " + msg.objectID + " but there is no vehicle with such id."); return; } player.EnterVehicle(vehicle, msg.passenger); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleLeaveMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error( $"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.LeaveVehicle(); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.VehicleStateMessage msg) => { float startTime = -1; ObjectSyncComponent vehicle = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; if (vehicle == null) { Logger.Log("Remote player tried to set state of vehicle " + msg.objectID + " but there is no vehicle with such id."); return; } if (msg.HasStartTime) { startTime = msg.StartTime; } PlayerVehicle subType = vehicle.GetObjectSubtype() as PlayerVehicle; subType.SetEngineState((PlayerVehicle.EngineStates)msg.state, (PlayerVehicle.DashboardStates)msg.dashstate, startTime); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.VehicleSwitchMessage msg) => { float newValueFloat = -1; PlayerVehicle vehicle = ObjectSyncManager.Instance.ObjectIDs[msg.objectID].GetObjectSubtype() as PlayerVehicle; if (vehicle == null) { Logger.Log("Remote player tried to change a switch in vehicle " + msg.objectID + " but there is no vehicle with such id."); return; } if (msg.HasSwitchValueFloat) { newValueFloat = msg.SwitchValueFloat; } vehicle.SetVehicleSwitch((PlayerVehicle.SwitchIDs)msg.switchID, msg.switchValue, newValueFloat); }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.LightSwitchMessage msg) => { LightSwitch light = Game.LightSwitchManager.Instance.FindLightSwitch( Utils.NetVec3ToGame(msg.pos)); light.TurnOn(msg.toggle); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ObjectSyncMessage msg) => { ObjectSyncComponent osc; ObjectSyncManager.SyncTypes type = (ObjectSyncManager.SyncTypes)msg.SyncType; try { osc = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; } catch { Logger.Log( $"Specified object is not yet added to the ObjectID's Dictionary! (Object ID: {msg.objectID})"); return; } if (osc != null) { // Set owner. if (type == ObjectSyncManager.SyncTypes.SetOwner) { if (osc.Owner == ObjectSyncManager.NO_OWNER || osc.Owner == sender.m_SteamID) { osc.OwnerSetToRemote(sender.m_SteamID); netManager.GetLocalPlayer().SendObjectSyncResponse(osc.ObjectID, true); } else { Logger.Debug( $"Set owner request rejected for object: {osc.transform.name} (Owner: {osc.Owner} Sender: {sender.m_SteamID})"); } } // Remove owner. else if (type == ObjectSyncManager.SyncTypes.RemoveOwner) { if (osc.Owner == sender.m_SteamID) { osc.OwnerRemoved(); } } // Force set owner. else if (type == ObjectSyncManager.SyncTypes.ForceSetOwner) { osc.Owner = sender.m_SteamID; netManager.GetLocalPlayer().SendObjectSyncResponse(osc.ObjectID, true); osc.SyncTakenByForce(); osc.SyncEnabled = false; } // Set object's position and variables if (osc.Owner == sender.m_SteamID || type == ObjectSyncManager.SyncTypes.PeriodicSync) { if (msg.HasSyncedVariables == true) { osc.HandleSyncedVariables(msg.SyncedVariables); } osc.SetPositionAndRotation(Utils.NetVec3ToGame(msg.position), Utils.NetQuatToGame(msg.rotation)); } } }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.ObjectSyncResponseMessage msg) => { ObjectSyncComponent osc = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; if (msg.accepted) { osc.SyncEnabled = true; osc.Owner = Steamworks.SteamUser.GetSteamID().m_SteamID; } }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ObjectSyncRequestMessage msg) => { try { ObjectSyncComponent osc = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; osc.SendObjectSync(ObjectSyncManager.SyncTypes.GenericSync, true, true); } catch { Logger.Error( $"Remote client tried to request object sync of an unknown object, Object ID: {msg.objectID}"); } }); netMessageHandler.BindMessageHandler( (Steamworks.CSteamID sender, Messages.EventHookSyncMessage msg) => { if (msg.request) { EventHook.SendSync(msg.fsmID); } else { if (msg.HasFsmEventName) { EventHook.HandleEventSync( msg.fsmID, msg.fsmEventID, msg.FsmEventName); } else { EventHook.HandleEventSync(msg.fsmID, msg.fsmEventID); } } }); }
public NetManager() { this.netManagerCreationTime = DateTime.UtcNow; netWorld = new NetWorld(this); // Setup local player. players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID()); p2pSessionRequestCallback = Steamworks.Callback <Steamworks.P2PSessionRequest_t> .Create((Steamworks.P2PSessionRequest_t result) => { if (!Steamworks.SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote)) { Logger.Log("Accepted p2p session with " + result.m_steamIDRemote.ToString()); } }); gameLobbyJoinRequestedCallback = Steamworks.Callback <Steamworks.GameLobbyJoinRequested_t> .Create(OnGameLobbyJoinRequested); lobbyCreatedCallResult = new Steamworks.CallResult <Steamworks.LobbyCreated_t>((Steamworks.LobbyCreated_t result, bool ioFailure) => { if (result.m_eResult != Steamworks.EResult.k_EResultOK) { Logger.Log("Oh my f*****g god i failed to create a lobby for you. Please forgive me. (result: " + result.m_eResult + ")"); MPGUI.Instance.ShowMessageBox("Failed to create lobby due to steam error.\n" + result.m_eResult, () => { MPController.Instance.LoadLevel("MainMenu"); }); return; } Logger.Log("Hey you! I have lobby id for you! " + result.m_ulSteamIDLobby); mode = Mode.Host; state = State.Playing; currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby); netWorld.RegisterPickupables(); }); lobbyEnterCallResult = new Steamworks.CallResult <Steamworks.LobbyEnter_t>((Steamworks.LobbyEnter_t result, bool ioFailure) => { if (result.m_EChatRoomEnterResponse != (uint)Steamworks.EChatRoomEnterResponse.k_EChatRoomEnterResponseSuccess) { Logger.Log("Oh my f*****g god i failed to join the lobby for you. Please forgive me. (reponse: " + result.m_EChatRoomEnterResponse + ")"); players[1] = null; return; } Logger.Log("Oh hello! " + result.m_ulSteamIDLobby); mode = Mode.Player; state = State.LoadingGameWorld; currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby); SendHandshake(); }); // TODO: Move message handlers to some class. BindMessageHandler((Steamworks.CSteamID sender, Messages.HandshakeMessage msg) => { remoteClock = msg.clock; HandleHandshake(sender, msg); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.PlayerSyncMessage msg) => { if (players[1] == null) { Logger.Log("Received synchronization packet but no remote player is currently connected."); return; } players[1].HandleSynchronize(msg); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.HeartbeatMessage msg) => { var message = new Messages.HeartbeatResponseMessage(); message.clientClock = msg.clientClock; message.clock = GetNetworkClock(); BroadcastMessage(message, Steamworks.EP2PSend.k_EP2PSendReliable); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.HeartbeatResponseMessage msg) => { ping = (uint)(GetNetworkClock() - msg.clientClock); // TODO: Some smart lag compensation. remoteClock = msg.clock; timeSinceLastHeartbeat = 0.0f; }); BindMessageHandler((Steamworks.CSteamID sender, Messages.DisconnectMessage msg) => { HandleDisconnect(false); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.OpenDoorsMessage msg) => { NetPlayer player = players[1]; // 1.5 is a length of the ray used to check interaction with doors in game scripts. Vector3 interactionPosition = player.GetPosition() + player.GetRotation() * Vector3.forward * 1.5f; Game.Objects.GameDoor doors = Game.GameDoorsManager.Instance.FindGameDoors(interactionPosition); if (doors == null) { Logger.Log($"Player tried to open doors however he is not close to any: {interactionPosition}."); return; } doors.Open(msg.open); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.FullWorldSyncMessage msg) => { netWorld.HandleFullWorldSync(msg); // Now spawn player. if (players[1] != null) { players[1].Spawn(); } // World loaded we are playing! state = State.Playing; }); BindMessageHandler((Steamworks.CSteamID sender, Messages.AskForWorldStateMessage msg) => { var msgF = new Messages.FullWorldSyncMessage(); netWorld.WriteFullWorldSync(msgF); BroadcastMessage(msgF, Steamworks.EP2PSend.k_EP2PSendReliable); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleEnterMessage msg) => { NetPlayer player = players[1]; if (player == null) { return; } NetVehicle vehicle = netWorld.GetVehicle(msg.vehicleId); if (vehicle == null) { Logger.Log("Player " + player.SteamId + " tried to enter vehicle " + msg.vehicleId + " but there is no vehicle with such id."); return; } player.EnterVehicle(vehicle, msg.passenger); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleLeaveMessage msg) => { NetPlayer player = players[1]; if (player == null) { return; } player.LeaveVehicle(); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleSyncMessage msg) => { NetPlayer player = players[1]; if (player == null) { return; } player.HandleVehicleSync(msg); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupObjectMessage msg) => { NetPlayer player = players[1]; if (player == null) { return; } player.PickupObject(msg.netId); }); BindMessageHandler((Steamworks.CSteamID sender, Messages.ReleaseObjectMessage msg) => { NetPlayer player = players[1]; if (player == null) { return; } player.ReleaseObject(msg.drop); }); }
/// <summary> /// Register world related network message handlers. /// </summary> /// <param name="netMessageHandler">The network message handler to register messages to.</param> void RegisterNetworkMessagesHandlers(NetMessageHandler netMessageHandler) { netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableSetPositionMessage msg) => { Client.Assert(ObjectSyncManager.GetObjectByID(msg.id), $"Tried to move pickupable that is not spawned {msg.id}."); GameObject gameObject = ObjectSyncManager.GetObjectByID(msg.id); gameObject.transform.position = Utils.NetVec3ToGame(msg.position); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableActivateMessage msg) => { GameObject gameObject = null; if (ObjectSyncManager.GetObjectByID(msg.id)) { gameObject = ObjectSyncManager.GetObjectByID(msg.id); } Client.Assert(gameObject != null, "Tried to activate a pickupable but it's not spawned! Does any connected client or you have other mods beside MSC:MP installed? Try uninstalling them!"); if (msg.activate) { gameObject.SetActive(true); } else { if (gameObject != null) { gameObject.SetActive(false); } } }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableSpawnMessage msg) => { SpawnPickupable(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PickupableDestroyMessage msg) => { if (!ObjectSyncManager.GetSyncComponentByID(msg.id)) { return; } ObjectSyncComponent osc = ObjectSyncManager.GetSyncComponentByID(msg.id); GameObject.Destroy(ObjectSyncManager.GetObjectByID(msg.id)); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.WorldPeriodicalUpdateMessage msg) => { // Game reports 'next hour' - we want to have transition so correct it. GameWorld.Instance.WorldTime = (float)msg.sunClock - 2.0f; GameWorld.Instance.WorldDay = (int)msg.worldDay; }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.PlayerSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Received synchronization packet from {sender} but there is not player registered using this id."); return; } player.HandleSynchronize(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.AnimSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Received animation synchronization packet from {sender} but there is not player registered using this id."); return; } player.HandleAnimSynchronize(msg); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.OpenDoorsMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Received OpenDoorsMessage however there is no matching player {sender}! (open: {msg.open}"); return; } GameDoor doors = GameDoorsManager.Instance.FindGameDoors(Utils.NetVec3ToGame(msg.position)); if (doors == null) { Logger.Error($"Player tried to open door, however, the door could not be found!"); return; } doors.Open(msg.open); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.FullWorldSyncMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); // This one should never happen - if happens there is something done miserably wrong. Client.Assert(player != null, $"There is no player matching given steam id {sender}."); // Handle full world state synchronization. HandleFullWorldSync(msg); // Spawn host character. player.Spawn(); // Set player state. player.Teleport(Utils.NetVec3ToGame(msg.spawnPosition), Utils.NetQuatToGame(msg.spawnRotation)); if (msg.pickedUpObject != NetPickupable.INVALID_ID) { player.PickupObject(msg.pickedUpObject); } // World is loaded! Notify network manager about that. netManager.OnNetworkWorldLoaded(); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.AskForWorldStateMessage msg) => { var msgF = new Messages.FullWorldSyncMessage(); WriteFullWorldSync(msgF); netManager.SendMessage(netManager.GetPlayer(sender), msgF, Steamworks.EP2PSend.k_EP2PSendReliable); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleEnterMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } ObjectSyncComponent vehicle = ObjectSyncManager.Instance.ObjectIDs[msg.objectID]; if (vehicle == null) { Logger.Error("Player " + player.SteamId + " tried to enter vehicle with Object ID " + msg.objectID + " but there is no vehicle with such id."); return; } player.EnterVehicle(vehicle, msg.passenger); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleLeaveMessage msg) => { NetPlayer player = netManager.GetPlayer(sender); if (player == null) { Logger.Error($"Steam user of id {sender} send message however there is no active player matching this id."); return; } player.LeaveVehicle(); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.VehicleSwitchMessage msg) => { float newValueFloat = -1; PlayerVehicle vehicle = ObjectSyncManager.Instance.ObjectIDs[msg.objectID].GetObjectSubtype() as PlayerVehicle; if (vehicle == null) { Logger.Debug("Remote player tried to change a switch in vehicle " + msg.objectID + " but there is no vehicle with such id."); return; } if (msg.HasSwitchValueFloat) { newValueFloat = msg.SwitchValueFloat; } vehicle.SetVehicleSwitch((PlayerVehicle.SwitchIDs)msg.switchID, msg.switchValue, newValueFloat); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.LightSwitchMessage msg) => { LightSwitch light = LightSwitchManager.Instance.FindLightSwitch(Utils.NetVec3ToGame(msg.pos)); light.TurnOn(msg.toggle); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ObjectSyncMessage msg) => { ObjectSyncComponent osc; ObjectSyncManager.SyncTypes type = (ObjectSyncManager.SyncTypes)msg.SyncType; if (ObjectSyncManager.GetObjectByID(msg.objectID)) { osc = ObjectSyncManager.GetSyncComponentByID(msg.objectID); } else { Logger.Error($"Specified object is not yet added to the ObjectID's Dictionary! (Object ID: {msg.objectID})"); return; } // This *should* never happen, but apparently it's possible. Client.Assert(osc != null, $"Object Sync Component wasn't found for object with ID {msg.objectID}, however, the object had a dictionary entry!"); // Host controls who owns an object. if (NetManager.Instance.IsHost) { // Set owner on host. if (type == ObjectSyncManager.SyncTypes.SetOwner) { ObjectSyncManager.SetOwnerHandler(msg, osc, sender); } // Remove owner on host. if (type == ObjectSyncManager.SyncTypes.RemoveOwner) { if (osc.Owner == netManager.GetLocalPlayer()) { ObjectSyncManager.RemoveOwnerHandler(msg, osc, sender); } } // Sync taken by force on host. if (type == ObjectSyncManager.SyncTypes.ForceSetOwner) { ObjectSyncManager.SyncTakenByForceHandler(msg, osc, sender); } } // Set ownership info on clients. else { NetPlayer player = netManager.GetPlayerByPlayerID(msg.OwnerPlayerID); // Check if player exists. if (player == null) { return; } // Set owner. if (type == ObjectSyncManager.SyncTypes.SetOwner) { if (osc.Owner != netManager.GetLocalPlayer()) { osc.OwnerSetToRemote(player); } osc.Owner = player; } // Remove owner. else if (type == ObjectSyncManager.SyncTypes.RemoveOwner) { if (osc.Owner != netManager.GetLocalPlayer()) { osc.OwnerRemoved(); } osc.Owner = null; } // Force set owner. else if (type == ObjectSyncManager.SyncTypes.ForceSetOwner) { if (osc.Owner != netManager.GetLocalPlayer()) { osc.SyncTakenByForce(); osc.SyncEnabled = false; } osc.Owner = player; if (osc.Owner == netManager.GetLocalPlayer()) { osc.SyncEnabled = true; } } } // Set object's position and variables. if ((osc.Owner == netManager.GetPlayer(sender)) || type == ObjectSyncManager.SyncTypes.PeriodicSync) { // Send synced variables, or variables only sync in some cases. if (msg.HasSyncedVariables) { osc.HandleSyncedVariables(msg.SyncedVariables); } // Full sync. if (msg.HasPosition && msg.HasRotation) { osc.SetPositionAndRotation(Utils.NetVec3ToGame(msg.Position), Utils.NetQuatToGame(msg.Rotation)); } // Position only sync. else if (msg.HasPosition) { Quaternion zero = new Quaternion(0, 0, 0, 0); osc.SetPositionAndRotation(Utils.NetVec3ToGame(msg.Position), zero); } // Rotation only sync. else if (msg.HasRotation) { osc.SetPositionAndRotation(Vector3.zero, Utils.NetQuatToGame(msg.Rotation)); } } }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ObjectSyncResponseMessage msg) => { ObjectSyncComponent osc = ObjectSyncManager.GetSyncComponentByID(msg.objectID); if (msg.accepted) { osc.SyncEnabled = true; osc.Owner = NetManager.Instance.GetLocalPlayer(); } }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.ObjectSyncRequestMessage msg) => { Client.Assert(ObjectSyncManager.GetObjectByID(msg.objectID), $"Remote client tried to request object sync of an unknown object, remote ObjectID was: {msg.objectID}"); ObjectSyncComponent osc = ObjectSyncManager.GetSyncComponentByID(msg.objectID); osc.SendObjectSync(ObjectSyncManager.SyncTypes.GenericSync, true, true); }); netMessageHandler.BindMessageHandler((Steamworks.CSteamID sender, Messages.EventHookSyncMessage msg) => { if (msg.request) { EventHook.SendSync(msg.fsmID); return; } if (msg.HasFsmEventName) { EventHook.HandleEventSync(msg.fsmID, msg.fsmEventID, msg.FsmEventName); } else { EventHook.HandleEventSync(msg.fsmID, msg.fsmEventID); } }); }