示例#1
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;
                }
            }
        }