示例#1
0
        public IEnumerator PhysicSyncLoad()
        {
            ObjectIDManager.objects.Clear();

            yield return(new WaitForSeconds(3));

            MelonModLogger.Log("Getting and adding all Rigidbodies");
            var rbs = FindObjectsOfType <Rigidbody>();

            foreach (var rb in rbs)
            {
                if (rb.gameObject.transform.root.gameObject.name.Contains("HMD") ||
                    rb.isKinematic == true ||
                    rb.gameObject.GetComponent <ButtonController>() ||
                    rb.gameObject.GetComponent <MechanicalPushButtonController>())
                {
                    Features.PatchTools.blacklist.Add(rb.GetInstanceID());
                    continue;
                }

                var sso      = rb.gameObject.AddComponent <ServerSyncedObject>();
                var idHolder = rb.gameObject.AddComponent <IDHolder>();

                idHolder.ID = ObjectIDManager.GenerateID(sso);
                ObjectIDManager.AddObject(idHolder.ID, sso);

                // MelonModLogger.Log($"added {rb.gameObject.name} with generated id {idHolder.ID.ToString()}");
            }
        }
示例#2
0
        public void Update()
        {
            while (SteamNetworking.IsP2PPacketAvailable(0))
            {
                P2Packet?packet = SteamNetworking.ReadP2PPacket(0);

                if (packet.HasValue)
                {
                    P2PMessage msg = new P2PMessage(packet.Value.Data);

                    MessageType type = (MessageType)msg.ReadByte();

                    switch (type)
                    {
                    case MessageType.Join:
                    {
                        if (msg.ReadByte() != JobSimulatorMultiplayer.PROTOCOL_VERSION)
                        {
                            // Somebody tried to join with an incompatible verison
                            P2PMessage m2 = new P2PMessage();
                            m2.WriteByte((byte)MessageType.JoinRejected);
                            SteamNetworking.SendP2PPacket(packet.Value.SteamId, m2.GetBytes(), -1, 0, P2PSend.Reliable);
                            SteamNetworking.CloseP2PSessionWithUser(packet.Value.SteamId);
                        }
                        else
                        {
                            MelonModLogger.Log("Player joined with SteamID: " + packet.Value.SteamId);
                            players.Add(packet.Value.SteamId);
                            MelonModLogger.Log("Player count: " + players.Count);
                            byte newPlayerId = smallIdCounter;
                            smallPlayerIds.Add(packet.Value.SteamId, newPlayerId);
                            largePlayerIds.Add(newPlayerId, packet.Value.SteamId);
                            smallIdCounter++;

                            string name = msg.ReadUnicodeString();
                            MelonModLogger.Log("Name: " + name);

                            foreach (var smallId in playerNames.Keys)
                            {
                                ClientJoinMessage cjm = new ClientJoinMessage
                                {
                                    playerId = smallId,
                                    name     = playerNames[smallId],
                                    steamId  = largePlayerIds[smallId]
                                };
                                SteamNetworking.SendP2PPacket(packet.Value.SteamId, cjm.MakeMsg().GetBytes(), -1, 0, P2PSend.Reliable);
                            }

                            ClientJoinMessage cjm2 = new ClientJoinMessage
                            {
                                playerId = 0,
                                name     = SteamClient.Name,
                                steamId  = SteamClient.SteamId
                            };
                            SteamNetworking.SendP2PPacket(packet.Value.SteamId, cjm2.MakeMsg().GetBytes(), -1, 0, P2PSend.Reliable);

                            playerNames.Add(newPlayerId, name);

                            ClientJoinMessage cjm3 = new ClientJoinMessage
                            {
                                playerId = newPlayerId,
                                name     = name,
                                steamId  = packet.Value.SteamId
                            };
                            ServerSendToAllExcept(cjm3, P2PSend.Reliable, packet.Value.SteamId);

                            playerObjects.Add(newPlayerId, new PlayerRep(name, packet.Value.SteamId));

                            RichPresence.SetActivity(
                                new Activity()
                                {
                                    State   = "Hosting a server",
                                    Assets  = { LargeImage = "jobsim" },
                                    Secrets = new ActivitySecrets()
                                    {
                                        Join = SteamClient.SteamId.ToString()
                                    },
                                    Party = new ActivityParty()
                                    {
                                        Id   = partyId,
                                        Size = new PartySize()
                                        {
                                            CurrentSize = players.Count + 1,
                                            MaxSize     = JobSimulatorMultiplayer.MAX_PLAYERS
                                        }
                                    }
                                });

                            SceneTransitionMessage stm = new SceneTransitionMessage()
                            {
                                sceneName = SceneManager.GetActiveScene().name
                            };
                            SendToId(stm, P2PSend.Reliable, packet.Value.SteamId);

                            SetPartyIdMessage spid = new SetPartyIdMessage()
                            {
                                partyId = partyId
                            };
                            SendToId(spid, P2PSend.Reliable, packet.Value.SteamId);
                        }
                        break;
                    }

                    case MessageType.Disconnect:
                    {
                        MelonModLogger.Log("Player left with SteamID: " + packet.Value.SteamId);
                        byte smallId = smallPlayerIds[packet.Value.SteamId];

                        P2PMessage disconnectMsg = new P2PMessage();
                        disconnectMsg.WriteByte((byte)MessageType.Disconnect);
                        disconnectMsg.WriteByte(smallId);
                        foreach (SteamId p in players)
                        {
                            SteamNetworking.SendP2PPacket(p, disconnectMsg.GetBytes(), -1, 0, P2PSend.Reliable);
                        }

                        playerObjects[smallId].Destroy();
                        playerObjects.Remove(smallId);
                        players.RemoveAll((ulong val) => val == packet.Value.SteamId);
                        smallPlayerIds.Remove(packet.Value.SteamId);
                        break;
                    }

                    case MessageType.PlayerPosition:
                    {
                        if (smallPlayerIds.ContainsKey(packet.Value.SteamId))
                        {
                            byte      playerId = smallPlayerIds[packet.Value.SteamId];
                            PlayerRep pr       = GetPlayerRep(playerId);

                            PlayerPositionMessage ppm = new PlayerPositionMessage(msg);
                            pr.head.transform.position  = ppm.headPos;
                            pr.handL.transform.position = ppm.lHandPos;
                            pr.handR.transform.position = ppm.rHandPos;

                            pr.head.transform.rotation  = ppm.headRot;
                            pr.handL.transform.rotation = ppm.lHandRot;
                            pr.handR.transform.rotation = ppm.rHandRot;

                            /*MelonModLogger.Log($@"---------------------
                             * SteamID: {pr.steamId.ToString()}
                             * LeftHand: {ppm.lHandPos.ToString()}
                             * RightHand: {ppm.rHandPos.ToString()}
                             * Head: {ppm.headPos.ToString()}
                             * ---------------------");*/

                            OtherPlayerPositionMessage relayOPPM = new OtherPlayerPositionMessage
                            {
                                headPos  = ppm.headPos,
                                lHandPos = ppm.lHandPos,
                                rHandPos = ppm.rHandPos,

                                headRot  = ppm.headRot,
                                lHandRot = ppm.lHandRot,
                                rHandRot = ppm.rHandRot,
                                playerId = ppm.playerId
                            };
                            ServerSendToAllExcept(relayOPPM, P2PSend.Unreliable, packet.Value.SteamId);
                        }
                        break;
                    }

                    case MessageType.ObjectSync:
                    {
                        ObjectSyncMessage osm = new ObjectSyncMessage(msg);

                        for (int i = 0; i < osm.objectsToSync.Count; i++)
                        {
                            GameObject obj = ObjectIDManager.GetObject(osm.objectsToSync.Keys.ToList()[i]).gameObject;

                            if (!obj)
                            {
                                MelonModLogger.LogError($"Couldn't find object with ID {obj.name}");
                            }
                            else
                            {
                                obj.transform.position = osm.objectsToSync.Values.ToList()[i].Item1;
                                obj.transform.rotation = osm.objectsToSync.Values.ToList()[i].Item2;
                            }
                            MelonModLogger.Log($"got sync message with id: {obj.name}");
                        }
                        break;
                    }

                    default:
                        MelonModLogger.Log("Unknown message type: " + type.ToString());
                        break;
                    }
                }
            }

            /*
             * foreach (var pair in ObjectIDManager.objects)
             * {
             *  ServerSyncedObject sso = pair.Value;
             *  if (sso.NeedsSync())
             *  {
             *      // Sync it
             *      pair.Value.lastSyncedPos = pair.Value.transform.position;
             *      pair.Value.lastSyncedRotation = pair.Value.transform.rotation;
             *
             *      ObjectSyncMessage osm = new ObjectSyncMessage
             *      {
             *          ID = sso.IDHolder.ID,
             *          position = pair.Value.transform.position,
             *          rotation = pair.Value.transform.rotation
             *      };
             *
             *      //ServerSendToAll(osm, P2PSend.Unreliable);
             *  }
             * }
             */

            SendSync();

            if (GlobalStorage.Instance.MasterHMDAndInputController != null)
            {
                OtherPlayerPositionMessage ppm = new OtherPlayerPositionMessage
                {
                    headPos  = GlobalStorage.Instance.MasterHMDAndInputController.camTransform.position,
                    lHandPos = GlobalStorage.Instance.MasterHMDAndInputController.LeftHand.cfjTransform.position,
                    rHandPos = GlobalStorage.Instance.MasterHMDAndInputController.RightHand.cfjTransform.position,

                    headRot  = GlobalStorage.Instance.MasterHMDAndInputController.camTransform.rotation,
                    lHandRot = GlobalStorage.Instance.MasterHMDAndInputController.LeftHand.cfjTransform.rotation,
                    rHandRot = GlobalStorage.Instance.MasterHMDAndInputController.RightHand.cfjTransform.rotation,
                };

                ServerSendToAll(ppm, P2PSend.Unreliable);
            }
        }
示例#3
0
        public void Update()
        {
            while (SteamNetworking.IsP2PPacketAvailable(0))
            {
                P2Packet?packet = SteamNetworking.ReadP2PPacket(0);

                if (packet.HasValue)
                {
                    P2PMessage msg = new P2PMessage(packet.Value.Data);

                    MessageType type = (MessageType)msg.ReadByte();

                    switch (type)
                    {
                    case MessageType.OtherPlayerPosition:
                    {
                        OtherPlayerPositionMessage oppm = new OtherPlayerPositionMessage(msg);

                        if (playerObjects.ContainsKey(oppm.playerId))
                        {
                            PlayerRep pr = GetPlayerRep(oppm.playerId);

                            pr.head.transform.position  = oppm.headPos;
                            pr.handL.transform.position = oppm.lHandPos;
                            pr.handR.transform.position = oppm.rHandPos;

                            pr.head.transform.rotation  = oppm.headRot;
                            pr.handL.transform.rotation = oppm.lHandRot;
                            pr.handR.transform.rotation = oppm.rHandRot;

                            /*MelonModLogger.Log($@"oppm-----------------
                             * SteamID: {oppm.playerId}
                             * LeftHand: {oppm.lHandPos.ToString()}
                             * RightHand: {oppm.rHandPos.ToString()}
                             * Head: {oppm.headPos.ToString()}
                             * ---------------------");*/
                        }

                        break;
                    }

                    case MessageType.PlayerPosition:
                    {
                        PlayerPositionMessage ppm = new PlayerPositionMessage(msg);

                        if (playerObjects.ContainsKey(ppm.playerId))
                        {
                            PlayerRep pr = GetPlayerRep(ppm.playerId);

                            pr.head.transform.position  = ppm.headPos;
                            pr.handL.transform.position = ppm.lHandPos;
                            pr.handR.transform.position = ppm.rHandPos;

                            pr.head.transform.rotation  = ppm.headRot;
                            pr.handL.transform.rotation = ppm.lHandRot;
                            pr.handR.transform.rotation = ppm.rHandRot;

                            MelonModLogger.Log($@"ppm------------------    
                                    SteamID: {ppm.playerId}
                                    LeftHand: {ppm.lHandPos.ToString()}    
                                    RightHand: {ppm.rHandPos.ToString()}    
                                    Head: {ppm.headPos.ToString()}    
                                    ---------------------");
                        }

                        break;
                    }

                    case MessageType.ServerShutdown:
                    {
                        foreach (PlayerRep pr in playerObjects.Values)
                        {
                            pr.Destroy();
                        }
                        break;
                    }

                    case MessageType.Disconnect:
                    {
                        byte pid = msg.ReadByte();
                        playerObjects[pid].Destroy();
                        playerObjects.Remove(pid);
                        largePlayerIds.Remove(pid);
                        playerNames.Remove(pid);
                        break;
                    }

                    case MessageType.JoinRejected:
                    {
                        MelonModLogger.LogError("Join rejected - you are using an incompatible version of the mod!");
                        Disconnect();
                        break;
                    }

                    case MessageType.Join:
                    {
                        ClientJoinMessage cjm = new ClientJoinMessage(msg);
                        largePlayerIds.Add(cjm.playerId, cjm.steamId);
                        playerNames.Add(cjm.playerId, cjm.name);
                        playerObjects.Add(cjm.playerId, new PlayerRep(cjm.name, cjm.steamId));
                        break;
                    }

                    case MessageType.ObjectSync:
                    {
                        ObjectSyncMessage osm = new ObjectSyncMessage(msg);
                        MelonModLogger.Log($"Received object sync");

                        for (int i = 0; i < osm.objectsToSync.Count; i++)
                        {
                            GameObject obj = ObjectIDManager.GetObject(osm.objectsToSync.Keys.ToList()[i]).gameObject;

                            if (!obj)
                            {
                                MelonModLogger.LogError($"Couldn't find object with ID {obj.name}");
                            }
                            else
                            {
                                obj.transform.position = osm.objectsToSync.Values.ToList()[i].Item1;
                                obj.transform.rotation = osm.objectsToSync.Values.ToList()[i].Item2;
                            }

                            MelonModLogger.Log($"got sync message with id: {obj.name}");
                        }         //oh, yeah
                        // but that's in the loop and won't bring if it doesn't deserialize the objects correctly
                        break;
                    }

                    case MessageType.SetPartyId:
                    {
                        SetPartyIdMessage spid = new SetPartyIdMessage(msg);
                        RichPresence.SetActivity(
                            new Activity()
                            {
                                State   = "Connected to a server",
                                Assets  = { LargeImage = "jobsim" },
                                Secrets = new ActivitySecrets()
                                {
                                    Join = ServerId.ToString()
                                },
                                Party = new ActivityParty()
                                {
                                    Id   = spid.partyId,
                                    Size = new PartySize()
                                    {
                                        CurrentSize = 1,
                                        MaxSize     = JobSimulatorMultiplayer.MAX_PLAYERS
                                    }
                                }
                            });
                        break;
                    }
                    }
                }
            }
            {
                if (GlobalStorage.Instance.MasterHMDAndInputController != null)
                {
                    PlayerPositionMessage ppm = new PlayerPositionMessage
                    {
                        headPos  = GlobalStorage.Instance.MasterHMDAndInputController.camTransform.position,
                        lHandPos = GlobalStorage.Instance.MasterHMDAndInputController.LeftHand.cfjTransform.position,
                        rHandPos = GlobalStorage.Instance.MasterHMDAndInputController.RightHand.cfjTransform.position,

                        headRot  = GlobalStorage.Instance.MasterHMDAndInputController.camTransform.rotation,
                        lHandRot = GlobalStorage.Instance.MasterHMDAndInputController.LeftHand.cfjTransform.rotation,
                        rHandRot = GlobalStorage.Instance.MasterHMDAndInputController.RightHand.cfjTransform.rotation,
                    };

                    SendToServer(ppm.MakeMsg(), P2PSend.Unreliable);
                }

                foreach (var id in ObjectIDManager.objects.Keys)
                {
                    ObjectIDManager.GetObject(id).gameObject.GetComponent <Rigidbody>().isKinematic = true;
                }
            }
        }