Exemplo n.º 1
0
        public NetManager()
        {
            statistics             = new NetStatistics(this);
            netManagerCreationTime = DateTime.UtcNow;
            netMessageHandler      = new NetMessageHandler(this);
            netWorld = new NetWorld(this);

            p2pSessionRequestCallback =
                Steamworks.Callback <Steamworks.P2PSessionRequest_t> .Create(
                    OnP2PSessionRequest);

            p2pConnectFailCallback =
                Steamworks.Callback <Steamworks.P2PSessionConnectFail_t> .Create(
                    OnP2PConnectFail);

            gameLobbyJoinRequestedCallback =
                Steamworks.Callback <Steamworks.GameLobbyJoinRequested_t> .Create(
                    OnGameLobbyJoinRequested);

            lobbyCreatedCallResult =
                new Steamworks.CallResult <Steamworks.LobbyCreated_t>(OnLobbyCreated);
            lobbyEnterCallResult =
                new Steamworks.CallResult <Steamworks.LobbyEnter_t>(OnLobbyEnter);

            Instance = this;

            RegisterProtocolMessagesHandlers();
        }
Exemplo n.º 2
0
        public NetManager()
        {
            Instance               = this;
            statistics             = new NetStatistics(this);
            netManagerCreationTime = DateTime.UtcNow;
            netMessageHandler      = new NetMessageHandler(this);
            netWorld               = new NetWorld(this);

            // Hopefully this will fix people playing with different mod versions!
            string versionFull = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            int    buildNumber = Convert.ToInt32(versionFull.Substring(versionFull.LastIndexOf('.') + 1));

            PROTOCOL_VERSION = buildNumber;
            Logger.Log("Build version is: " + PROTOCOL_VERSION);

            p2pSessionRequestCallback = Steamworks.Callback <Steamworks.P2PSessionRequest_t> .Create(OnP2PSessionRequest);

            p2pConnectFailCallback = Steamworks.Callback <Steamworks.P2PSessionConnectFail_t> .Create(OnP2PConnectFail);

            gameLobbyJoinRequestedCallback = Steamworks.Callback <Steamworks.GameLobbyJoinRequested_t> .Create(OnGameLobbyJoinRequested);

            lobbyCreatedCallResult = new Steamworks.CallResult <Steamworks.LobbyCreated_t>(OnLobbyCreated);
            lobbyEnterCallResult   = new Steamworks.CallResult <Steamworks.LobbyEnter_t>(OnLobbyEnter);
            lobbyListResult        = new Steamworks.CallResult <Steamworks.LobbyMatchList_t>(OnLobbyList);
            lobbyDataResult        = new Steamworks.CallResult <Steamworks.LobbyDataUpdate_t>(OnGetLobbyInfo);

            RegisterProtocolMessagesHandlers();

            RequestLobbies();
        }
Exemplo n.º 3
0
        public NetManager()
        {
            this.netManagerCreationTime = DateTime.UtcNow;
            netMessageHandler           = new NetMessageHandler(this);
            netWorld = new NetWorld(this);

            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);

                // Setup local player.
                players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

                mode           = Mode.Host;
                state          = State.Playing;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);
            });

            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;
                }

                // Setup local player.
                players[0] = new NetLocalPlayer(this, netWorld, Steamworks.SteamUser.GetSteamID());

                Logger.Log("Oh hello! " + result.m_ulSteamIDLobby);

                mode           = Mode.Player;
                state          = State.LoadingGameWorld;
                currentLobbyId = new Steamworks.CSteamID(result.m_ulSteamIDLobby);

                ShowLoadingScreen(true);
                SendHandshake();
            });

            RegisterProtocolMessagesHandlers();
        }
Exemplo n.º 4
0
        /// <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);
            });
        }
Exemplo n.º 5
0
        /// <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);
                    }
                }
            });
        }
Exemplo n.º 6
0
        /// <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);
                }
            });
        }