/// <summary> /// Handle full world sync message. /// </summary> /// <param name="msg">The message to handle.</param> public void HandleFullWorldSync(Messages.FullWorldSyncMessage msg) { Logger.Debug("Handling full world synchronization message."); var watch = System.Diagnostics.Stopwatch.StartNew(); // Read time Game.GameWorld gameWorld = GameWorld.Instance; gameWorld.WorldTime = msg.dayTime; gameWorld.WorldDay = msg.day; // Read mailbox name gameWorld.PlayerLastName = msg.mailboxName; // Doors. foreach (Messages.DoorsInitMessage door in msg.doors) { Vector3 position = Utils.NetVec3ToGame(door.position); GameDoor doors = GameDoorsManager.Instance.FindGameDoors(position); Client.Assert(doors != null, $"Unable to find doors at: {position}."); if (doors.IsOpen != door.open) { doors.Open(door.open); } } // Lights. foreach (Messages.LightSwitchMessage light in msg.lights) { Vector3 position = Utils.NetVec3ToGame(light.pos); LightSwitch lights = LightSwitchManager.Instance.FindLightSwitch(position); Client.Assert(lights != null, $"Unable to find light switch at: {position}."); if (lights.SwitchStatus != light.toggle) { lights.TurnOn(light.toggle); } } // Pickupables foreach (Messages.PickupableSpawnMessage pickupableMsg in msg.pickupables) { SpawnPickupable(pickupableMsg); } // Remove spawned (and active) pickupables that we did not get info about. foreach (var kv in GamePickupableDatabase.Instance.Pickupables) { if (kv.Value.GetComponent <ObjectSyncComponent>() == null) { GameObject.Destroy(kv.Value); } } // Connected players. int i = 0; foreach (int newPlayerID in msg.connectedPlayers.playerIDs) { Steamworks.CSteamID localSteamID = Steamworks.SteamUser.GetSteamID(); Steamworks.CSteamID newPlayerSteamID = new Steamworks.CSteamID(msg.connectedPlayers.steamIDs[i]); // If player is not host or local player, setup new player. if (newPlayerSteamID != netManager.GetHostPlayer().SteamId&& newPlayerSteamID != localSteamID) { netManager.players.Add(newPlayerID, new NetPlayer(netManager, this, newPlayerSteamID)); netManager.players[newPlayerID].Spawn(); Logger.Debug("Setup new player at ID: " + newPlayerID); } i++; } // Object owners. foreach (Messages.ObjectOwnerSync syncMsg in msg.objectOwners) { if (syncMsg.ownerPlayerID != -1) { ObjectSyncManager.Instance.ObjectIDs[syncMsg.objectID].Owner = netManager.GetPlayerByPlayerID(syncMsg.ownerPlayerID); } } GamePickupableDatabase.Instance.Pickupables.Clear(); playerIsLoading = false; watch.Stop(); Logger.Debug("Full world synchronization message has been handled. Took " + watch.ElapsedMilliseconds + "ms"); }
/// <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); } } }); }
/// <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); } }); }