Esempio n. 1
0
        public override void OnApplicationStart()
        {
            // Setup MonoBehaviors
            ClassInjector.RegisterTypeInIl2Cpp <ServerSyncedObject>();
            ClassInjector.RegisterTypeInIl2Cpp <IDHolder>();

            // Register Prefs
            ModPrefs.RegisterCategory("MPMod", "Multiplayer Settings");
            ModPrefs.RegisterPrefString("MPMod", "HostSteamID", "0");
            ModPrefs.RegisterPrefBool("MPMod", "ForceLargePlayspace", true);

            // Start Server Stuff
            SteamClient.Init(448280);

            // MelonModLogger.LogWarning("ALPHA TESTING BUILD");
            MelonModLogger.Log($"Multiplayer initialising with protocol version {PROTOCOL_VERSION}.");

            SteamNetworking.AllowP2PPacketRelay(true);

            client = new Client();
            server = new Server();
            PlayerRep.LoadPlayer();

            // Setup Discord Presence
            RichPresence.Initialise(736050983335100436);
            client.SetupRP();

            MelonModLogger.Log("MPMod Loaded");
        }
Esempio n. 2
0
 public override void OnStartAuthority()
 {
     ins = this;
     //repui = this.gameObject.GetComponent<RepUIHandler>();
     CmdGetmyConnId();
     base.OnStartAuthority();
 }
Esempio n. 3
0
 public void SetPlayerRep(PlayerRep inrep)
 {
     prep            = inrep;
     connectedConnid = prep.myConnid;
     Init();
     prep.flagChangedEvn += GotFlagChangedEvn;
     prep.nameChangedEvn += GotNameChangedEvn;
 }
Esempio n. 4
0
        private void TransportLayer_OnMessageReceived(ITransportConnection connection, P2PMessage msg)
        {
            MessageType type = (MessageType)msg.ReadByte();

            switch (type)
            {
            case MessageType.GunFireHit:
            {
                byte playerId = smallPlayerIds[connection.ConnectedTo];
                if (playerObjects.ContainsKey(playerId))
                {
                    PlayerRep pr = playerObjects[playerId];

                    if (pr.rigTransforms.main != null)
                    {
                        GameObject instance = Instantiate(GunResources.HurtSFX, pr.rigTransforms.main);
                        Destroy(instance, 3);
                    }
                }
                GunFireHit gff = new GunFireHit()
                {
                    playerId = playerId
                };
                ServerSendToAllExcept(gff, MessageSendType.Unreliable, connection.ConnectedTo);
                break;
            }

            case MessageType.GunFire:
            {
                bool           didHit;
                GunFireMessage gfm = new GunFireMessage(msg);
                Ray            ray = new Ray(gfm.fireOrigin, gfm.fireDirection);

                if (Physics.Raycast(ray, out RaycastHit hit, int.MaxValue, ~0, QueryTriggerInteraction.Ignore))
                {
                    if (hit.transform.root.gameObject == BWUtil.RigManager)
                    {
                        MelonModLogger.Log("Hit BRETT!");
                        int random = UnityEngine.Random.Range(0, 10);
                        BWUtil.LocalPlayerHealth.TAKEDAMAGE(gfm.bulletDamage, random == 0);
                        GunFireHit gff = new GunFireHit();
                        ServerSendToAll(gff, MessageSendType.Reliable);
                    }
                    else
                    {
                        MelonModLogger.Log("Hit!");
                    }
                    didHit = true;
                }
Esempio n. 5
0
        public override void OnGUI()
        {
#if DEBUG
            GUILayout.BeginVertical(null);

            if (GUILayout.Button("Create Dummy", null))
            {
                if (dummyRep == null)
                {
                    dummyRep = new PlayerRep("Dummy", SteamClient.SteamId);
                }
                else
                {
                    dummyRep.Destroy();
                }
            }

            if (GUILayout.Button("Create Dummy Accessories", null))
            {
                zCubed.Accessories.Accessory.CreateDummies(zCubed.Accessories.Accessory.GetPlayerRoot());
            }

            if (GUILayout.Button("Create Local Accessories", null))
            {
                zCubed.Accessories.Accessory.CreateLocalAccessories(zCubed.Accessories.Accessory.GetPlayerRoot());
            }

            if (GUILayout.Button("Create Net Accessories", null))
            {
                string[] accessoryPaths = Features.NetworkedAccesories.GetLocalList();
                byte[]   rawData        = Features.NetworkedAccesories.BundleToNetBundle(accessoryPaths[0]);
            }

            if (GUILayout.Button("Test Guard Lists", null))
            {
                Features.Guard.AddUserToList(Guid.NewGuid().ToString(), Features.Guard.Lists.Blocked);
                Features.Guard.AddUserToList(Guid.NewGuid().ToString(), Features.Guard.Lists.Trusted);
            }

            if (GUILayout.Button("Create Main Panel", null))
            {
                Features.UI.CreateMainPanel();
            }

            GUILayout.EndVertical();
#endif
        }
Esempio n. 6
0
 /// <summary>
 /// Return Player Represent INSTANCE
 /// </summary>
 /// <param name="conn"></param>
 /// <returns></returns>
 public static PlayerRep RetRepIns(NetworkConnection conn = null)
 {
     if (NetworkServer.active && conn != null)
     {
         GameObject go = null;
         if (KnetMan.cliBook.TryGetValue(conn, out go))
         {
             ins = go.GetComponent <PlayerRep>();
             return(ins);
         }
         return(null);
     }
     else
     {
         return(ins);
     }
 }
Esempio n. 7
0
        public override void OnGUI()
        {
#if DEBUG
            GUILayout.BeginVertical(null);

            if (GUILayout.Button("Create Dummy", null))
            {
                if (dummyRep == null)
                {
                    dummyRep = new PlayerRep("Dummy", SteamClient.SteamId);
                }
                else
                {
                    dummyRep.Delete();
                }
            }

            if (GUILayout.Button("Create Main Panel", null))
            {
                Features.UI.CreateMainPanel();
            }

            if (GUILayout.Button("Test Object IDs", null))
            {
                var    testObj  = GameObject.Find("[RigManager (Default Brett)]/[SkeletonRig (GameWorld Brett)]/Brett@neutral");
                string fullPath = BWUtil.GetFullNamePath(testObj);

                MelonLogger.Log($"Got path {fullPath} for Brett@neutral");
                MelonLogger.Log("Trying to get object from path...");

                var gotObj = BWUtil.GetObjectFromFullPath(fullPath);

                if (gotObj == testObj)
                {
                    MelonLogger.Log("Success!!!!!");
                }
                else
                {
                    MelonLogger.Log($"Failed :( Got {gotObj.name}");
                }
            }

            GUILayout.EndVertical();
#endif
        }
Esempio n. 8
0
        public void RecreatePlayers()
        {
            List <byte>    ids      = new List <byte>();
            List <SteamId> steamIds = new List <SteamId>();

            foreach (byte id in playerObjects.Keys)
            {
                ids.Add(id);
                steamIds.Add(playerObjects[id].steamId);
            }

            int i = 0;

            foreach (byte id in ids)
            {
                playerObjects[id] = new PlayerRep(playerNames[id], steamIds[i]);
            }
        }
Esempio n. 9
0
        public unsafe override void OnApplicationStart()
        {
            SteamClient.Init(823500);

            Features.Guard.GetSteamFriends();
            Features.Guard.GetLocalGuard();

#if DEBUG
            MelonModLogger.LogWarning("Debug build!");
#endif

            MelonModLogger.Log($"Multiplayer initialising with protocol version {PROTOCOL_VERSION}.");

            // Set up prefs
            ModPrefs.RegisterCategory("MPMod", "Multiplayer Settings");
            ModPrefs.RegisterPrefBool("MPMod", "BaldFord", false, "90% effective hair removal solution");

            // Initialise transport layer
            TransportLayer = new SteamTransportLayer();

            // Create the UI and cache the PlayerRep's model
            ui     = new MultiplayerUI();
            client = new Client(ui, TransportLayer);
            server = new Server(ui, TransportLayer);
            PlayerRep.LoadFord();

            // Configures if the PlayerRep's are showing or hiding certain parts
            PlayerRep.showBody = true;
            PlayerRep.showHair = ModPrefs.GetBool("MPMod", "BaldFord");

            // Initialize Discord's RichPresence
            RichPresence.Initialise(701895326600265879);
            client.SetupRP();

            #region Unused Code
            //PlayerHooks.OnPlayerGrabObject += PlayerHooks_OnPlayerGrabObject;
            //PlayerHooks.OnPlayerLetGoObject += PlayerHooks_OnPlayerLetGoObject;
            //BWUtil.InitialiseGunPrefabs();
            #endregion
        }
Esempio n. 10
0
        public unsafe override void OnApplicationStart()
        {
            if (!SteamClient.IsValid)
            {
                SteamClient.Init(823500);
            }

#if DEBUG
            MelonModLogger.LogWarning("Debug build!");
#endif

            MelonLogger.Log($"Multiplayer initialising with protocol version {PROTOCOL_VERSION}.");

            // Set up prefs
            MelonPrefs.RegisterCategory("MPMod", "Multiplayer Settings");
            MelonPrefs.RegisterBool("MPMod", "BaldFord", false, "90% effective hair removal solution");

            // Initialise transport layer
            TransportLayer = new SteamTransportLayer();

            // Create the UI and cache the PlayerRep's model
            ui     = new MultiplayerUI();
            client = new Client(ui, TransportLayer);
            server = new Server(ui, TransportLayer);
            PlayerRep.LoadFord();

            // Configures if the PlayerRep's are showing or hiding certain parts
            PlayerRep.showBody = true;
            PlayerRep.showHair = MelonPrefs.GetBool("MPMod", "BaldFord");

            // Initialize Discord's RichPresence
            RichPresence.Initialise(701895326600265879);
            client.SetupRP();

            BWUtil.Hook();

            UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp <SyncedObject>();
        }
Esempio n. 11
0
        private void TransportLayer_OnMessageReceived(ITransportConnection arg1, P2PMessage msg)
        {
            MessageType type = (MessageType)msg.ReadByte();

            try
            {
                switch (type)
                {
                case MessageType.GunFire:
                {
                    GunFireMessageOther gfmo          = new GunFireMessageOther(msg);
                    PlayerRep           pr            = GetPlayerRep(gfmo.playerId);
                    AmmoVariables       ammoVariables = new AmmoVariables()
                    {
                        AttackDamage   = gfmo.ammoDamage,
                        AttackType     = AttackType.Piercing,
                        cartridgeType  = Cart.Cal_9mm,
                        ExitVelocity   = gfmo.exitVelocity,
                        ProjectileMass = gfmo.projectileMass,
                        Tracer         = false
                    };
                    if ((StressLevelZero.Handedness)gfmo.handedness == StressLevelZero.Handedness.RIGHT)
                    {
                        pr.rightGunScript.firePointTransform.position = gfmo.firepointPos;
                        pr.rightGunScript.firePointTransform.rotation = gfmo.firepointRotation;
                        pr.rightGunScript.muzzleVelocity   = gfmo.muzzleVelocity;
                        pr.rightBulletObject.ammoVariables = ammoVariables;
                        pr.rightGunScript.PullCartridge();
                        pr.rightGunScript.Fire();
                    }
                    if ((StressLevelZero.Handedness)gfmo.handedness == StressLevelZero.Handedness.LEFT)
                    {
                        pr.leftGunScript.firePointTransform.position = gfmo.firepointPos;
                        pr.leftGunScript.firePointTransform.rotation = gfmo.firepointRotation;
                        pr.leftGunScript.muzzleVelocity   = gfmo.muzzleVelocity;
                        pr.leftBulletObject.ammoVariables = ammoVariables;
                        pr.leftGunScript.PullCartridge();
                        pr.leftGunScript.Fire();
                    }
                    pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Angry;
                    pr.faceAnimator.faceTime  = 5;
                    break;
                }

                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.pelvis.transform.position = oppm.pelvisPos;
                        pr.ford.transform.position   = oppm.pelvisPos - new Vector3(0.0f, 0.3f, 0.0f);
                        pr.footL.transform.position  = oppm.lFootPos;
                        pr.footR.transform.position  = oppm.rFootPos;

                        pr.head.transform.rotation   = oppm.headRot;
                        pr.handL.transform.rotation  = oppm.lHandRot;
                        pr.handR.transform.rotation  = oppm.rHandRot;
                        pr.pelvis.transform.rotation = oppm.pelvisRot;
                        pr.footL.transform.rotation  = oppm.lFootRot;
                        pr.footR.transform.rotation  = oppm.rFootRot;
                    }

                    break;
                }

                case MessageType.OtherFullRig:
                {
                    OtherFullRigTransformMessage ofrtm = new OtherFullRigTransformMessage(msg);
                    byte playerId = ofrtm.playerId;

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

                        pr.ApplyTransformMessage(ofrtm);
                    }
                    break;
                }

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

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

                    foreach (PlayerRep pr in playerObjects.Values)
                    {
                        pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Sad;
                        pr.faceAnimator.faceTime  = 10;
                    }
                    break;
                }

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

                case MessageType.SceneTransition:
                {
                    SceneTransitionMessage stm = new SceneTransitionMessage(msg);
                    if (BoneworksSceneManager.GetCurrentSceneName() != stm.sceneName)
                    {
                        BoneworksSceneManager.LoadScene(stm.sceneName);
                    }
                    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));

                    foreach (PlayerRep pr in playerObjects.Values)
                    {
                        pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Happy;
                        pr.faceAnimator.faceTime  = 15;
                    }
                    break;
                }

                case MessageType.SetPartyId:
                {
                    SetPartyIdMessage spid = new SetPartyIdMessage(msg);
                    RichPresence.SetActivity(
                        new Activity()
                        {
                            Details = "Connected to a server",
                            Secrets = new ActivitySecrets()
                            {
                                Join = ServerId.ToString()
                            },
                            Party = new ActivityParty()
                            {
                                Id   = spid.partyId,
                                Size = new PartySize()
                                {
                                    CurrentSize = 1,
                                    MaxSize     = MultiplayerMod.MAX_PLAYERS
                                }
                            }
                        });
                    break;
                }

                case MessageType.EnemyRigTransform:
                {
                    enemyPoolManager.FindMissingPools();
                    EnemyRigTransformMessage ertm = new EnemyRigTransformMessage(msg);
                    Pool pool = enemyPoolManager.GetPool(ertm.enemyType);

                    // HORRID PERFORMANCE
                    Transform              enemyTf = pool.transform.GetChild(ertm.poolChildIdx);
                    GameObject             rootObj = enemyTf.Find("enemyBrett@neutral").gameObject;
                    BoneworksRigTransforms brt     = BWUtil.GetHumanoidRigTransforms(rootObj);
                    BWUtil.ApplyRigTransform(brt, ertm);
                    break;
                }

                case MessageType.IdAllocation:
                {
                    IDAllocationMessage iam = new IDAllocationMessage(msg);
                    GameObject          obj = BWUtil.GetObjectFromFullPath(iam.namePath);
                    if (!obj)
                    {
                        MelonLogger.LogWarning("Got IdAllocation for nonexistent object???");
                    }
                    ObjectIDManager.AddObject(iam.allocatedId, obj);

                    var so = obj.AddComponent <SyncedObject>();
                    so.ID    = iam.allocatedId;
                    so.owner = iam.initialOwner;
                    so.rb    = obj.GetComponent <Rigidbody>();

                    syncedObjects.Add(so);

                    if (so.owner != localSmallId)
                    {
                        so.rb.isKinematic = true;
                    }

                    MelonLogger.Log($"ID Allocation: {iam.namePath}, {so.ID}");
                    break;
                }

                case MessageType.ObjectSync:
                {
                    ObjectSyncMessage osm = new ObjectSyncMessage(msg);
                    GameObject        obj = ObjectIDManager.GetObject(osm.id);

                    if (!obj)
                    {
                        MelonLogger.LogError($"Couldn't find object with ID {osm.id}");
                    }
                    else
                    {
                        obj.transform.position = osm.position;
                        obj.transform.rotation = osm.rotation;
                    }
                    break;
                }

                case MessageType.ChangeObjectOwnership:
                {
                    var coom = new ChangeObjectOwnershipMessage(msg);
                    var obj  = ObjectIDManager.GetObject(coom.objectId);
                    var so   = obj.GetComponent <SyncedObject>();
                    so.owner = coom.ownerId;

                    if (so.owner == localSmallId)
                    {
                        so.rb.isKinematic     = false;
                        so.rb.velocity        = coom.linVelocity;
                        so.rb.angularVelocity = coom.angVelocity;
                    }
                    else
                    {
                        so.rb.isKinematic = true;
                    }

                    MelonLogger.Log($"Object {coom.objectId} is now owned by {coom.ownerId} (kinematic: {so.rb.isKinematic})");

                    break;
                }

                case MessageType.SetLocalSmallId:
                {
                    var slsi = new SetLocalSmallIdMessage(msg);
                    localSmallId = slsi.smallId;
                    break;
                }
                }
            }
            catch (Exception e)
            {
                MelonLogger.LogError($"Caught exception in message handler for message {type}: {e}");
            }
        }
Esempio n. 12
0
        private void TransportLayer_OnMessageReceived(ITransportConnection connection, P2PMessage msg)
        {
            MessageType type = (MessageType)msg.ReadByte();

            switch (type)
            {
            case MessageType.GunFire:
            {
                GunFireMessage gfm           = new GunFireMessage(msg);
                byte           LocalplayerId = smallPlayerIds[connection.ConnectedTo];
                if (playerObjects.ContainsKey(LocalplayerId))
                {
                    PlayerRep     pr            = playerObjects[LocalplayerId];
                    AmmoVariables ammoVariables = new AmmoVariables()
                    {
                        AttackDamage   = gfm.ammoDamage,
                        AttackType     = AttackType.Piercing,
                        cartridgeType  = Cart.Cal_9mm,
                        ExitVelocity   = gfm.exitVelocity,
                        ProjectileMass = gfm.projectileMass,
                        Tracer         = false
                    };
                    if ((StressLevelZero.Handedness)gfm.handedness == StressLevelZero.Handedness.RIGHT)
                    {
                        pr.rightGunScript.firePointTransform.position = gfm.firepointPos;
                        pr.rightGunScript.firePointTransform.rotation = gfm.firepointRotation;
                        pr.rightGunScript.muzzleVelocity   = gfm.muzzleVelocity;
                        pr.rightBulletObject.ammoVariables = ammoVariables;
                        pr.leftGunScript.PullCartridge();
                        pr.rightGunScript.Fire();
                    }
                    if ((StressLevelZero.Handedness)gfm.handedness == StressLevelZero.Handedness.LEFT)
                    {
                        pr.leftGunScript.firePointTransform.position = gfm.firepointPos;
                        pr.leftGunScript.firePointTransform.rotation = gfm.firepointRotation;
                        pr.leftGunScript.muzzleVelocity   = gfm.muzzleVelocity;
                        pr.leftBulletObject.ammoVariables = ammoVariables;
                        pr.leftGunScript.PullCartridge();
                        pr.leftGunScript.Fire();
                    }
                    GunFireMessageOther gfmo = new GunFireMessageOther()
                    {
                        playerId          = LocalplayerId,
                        handedness        = gfm.handedness,
                        firepointPos      = gfm.firepointPos,
                        firepointRotation = gfm.firepointRotation,
                        ammoDamage        = gfm.ammoDamage,
                        projectileMass    = gfm.projectileMass,
                        exitVelocity      = gfm.exitVelocity,
                        muzzleVelocity    = gfm.muzzleVelocity
                    };
                    pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Angry;
                    pr.faceAnimator.faceTime  = 5;
                    ServerSendToAllExcept(gfmo, MessageSendType.Reliable, connection.ConnectedTo);
                }
                break;
            }

            case MessageType.Join:
            {
                if (msg.ReadByte() != MultiplayerMod.PROTOCOL_VERSION)
                {
                    // Somebody tried to join with an incompatible verison
                    P2PMessage m2 = new P2PMessage();
                    m2.WriteByte((byte)MessageType.JoinRejected);
                    connection.SendMessage(m2, MessageSendType.Reliable);
                    connection.Disconnect();
                }
                else
                {
                    MelonLogger.Log("Player joined with ID: " + connection.ConnectedTo);
                    if (players.Contains(connection.ConnectedTo))
                    {
                        players.Remove(connection.ConnectedTo);
                    }

                    players.Add(connection.ConnectedTo);
                    MelonLogger.Log("Player count: " + players.Count);
                    byte newPlayerId = smallIdCounter;

                    if (smallPlayerIds.ContainsKey(newPlayerId))
                    {
                        smallPlayerIds.Remove(newPlayerId);
                    }

                    smallPlayerIds.Add(connection.ConnectedTo, newPlayerId);

                    if (largePlayerIds.ContainsKey(newPlayerId))
                    {
                        largePlayerIds.Remove(newPlayerId);
                    }

                    largePlayerIds.Add(newPlayerId, connection.ConnectedTo);
                    smallIdCounter++;

                    playerConnections.Add(connection.ConnectedTo, connection);

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

                    foreach (var smallId in playerNames.Keys)
                    {
                        ClientJoinMessage cjm = new ClientJoinMessage
                        {
                            playerId = smallId,
                            name     = playerNames[smallId],
                            steamId  = largePlayerIds[smallId]
                        };
                        connection.SendMessage(cjm.MakeMsg(), MessageSendType.Reliable);
                    }

                    ClientJoinMessage cjm2 = new ClientJoinMessage
                    {
                        playerId = 0,
                        name     = SteamClient.Name,
                        steamId  = SteamClient.SteamId
                    };
                    connection.SendMessage(cjm2.MakeMsg(), MessageSendType.Reliable);

                    if (playerNames.ContainsKey(newPlayerId))
                    {
                        playerNames.Remove(newPlayerId);
                    }

                    playerNames.Add(newPlayerId, name);

                    ClientJoinMessage cjm3 = new ClientJoinMessage
                    {
                        playerId = newPlayerId,
                        name     = name,
                        steamId  = connection.ConnectedTo
                    };
                    ServerSendToAllExcept(cjm3, MessageSendType.Reliable, connection.ConnectedTo);

                    if (playerObjects.ContainsKey(newPlayerId))
                    {
                        playerObjects.Remove(newPlayerId);
                    }
                    playerObjects.Add(newPlayerId, new PlayerRep(name, connection.ConnectedTo));

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

                    SceneTransitionMessage stm = new SceneTransitionMessage()
                    {
                        sceneName = BoneworksSceneManager.GetCurrentSceneName()
                    };
                    connection.SendMessage(stm.MakeMsg(), MessageSendType.Reliable);

                    SetPartyIdMessage spid = new SetPartyIdMessage()
                    {
                        partyId = partyId
                    };
                    connection.SendMessage(spid.MakeMsg(), MessageSendType.Reliable);

                    SetLocalSmallIdMessage slsi = new SetLocalSmallIdMessage()
                    {
                        smallId = newPlayerId
                    };
                    connection.SendMessage(slsi.MakeMsg(), MessageSendType.Reliable);

                    foreach (var so in syncObjs)
                    {
                        var iam = new IDAllocationMessage
                        {
                            allocatedId  = so.ID,
                            namePath     = BWUtil.GetFullNamePath(so.gameObject),
                            initialOwner = so.owner
                        };
                        connection.SendMessage(iam.MakeMsg(), MessageSendType.Reliable);
                    }

                    ui.SetPlayerCount(players.Count, MultiplayerUIState.Server);

                    foreach (PlayerRep pr in playerObjects.Values)
                    {
                        pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Happy;
                        pr.faceAnimator.faceTime  = 15;
                    }
                }
                break;
            }

            case MessageType.Disconnect:
            {
                MelonLogger.Log("Player left with ID: " + connection.ConnectedTo);
                byte smallId = smallPlayerIds[connection.ConnectedTo];

                playerObjects[smallId].Delete();
                playerObjects.Remove(smallId);
                players.RemoveAll((ulong val) => val == connection.ConnectedTo);
                smallPlayerIds.Remove(connection.ConnectedTo);

                P2PMessage disconnectMsg = new P2PMessage();
                disconnectMsg.WriteByte((byte)MessageType.Disconnect);
                disconnectMsg.WriteByte(smallId);

                foreach (SteamId p in players)
                {
                    playerConnections[p].SendMessage(disconnectMsg, MessageSendType.Reliable);
                }
                foreach (PlayerRep pr in playerObjects.Values)
                {
                    pr.faceAnimator.faceState = Source.Representations.FaceAnimator.FaceState.Sad;
                    pr.faceAnimator.faceTime  = 6;
                }
                break;
            }

            case MessageType.FullRig:
            {
                FullRigTransformMessage frtm = new FullRigTransformMessage(msg);

                byte playerId = smallPlayerIds[connection.ConnectedTo];
                if (playerObjects.ContainsKey(playerId))
                {
                    PlayerRep pr = playerObjects[playerId];

                    if (pr.rigTransforms.main != null)
                    {
                        //ApplyTransformMessage(pr, frtm);
                        pr.ApplyTransformMessage(frtm);

                        OtherFullRigTransformMessage ofrtm = new OtherFullRigTransformMessage
                        {
                            playerId = playerId,

                            posMain   = frtm.posMain,
                            posRoot   = frtm.posRoot,
                            posLHip   = frtm.posLHip,
                            posRHip   = frtm.posRHip,
                            posLKnee  = frtm.posLKnee,
                            posRKnee  = frtm.posRKnee,
                            posLAnkle = frtm.posLAnkle,
                            posRAnkle = frtm.posRAnkle,

                            posSpine1    = frtm.posSpine1,
                            posSpine2    = frtm.posSpine2,
                            posSpineTop  = frtm.posSpineTop,
                            posLClavicle = frtm.posLClavicle,
                            posRClavicle = frtm.posRClavicle,
                            posNeck      = frtm.posNeck,
                            posLShoulder = frtm.posLShoulder,
                            posRShoulder = frtm.posRShoulder,
                            posLElbow    = frtm.posLElbow,
                            posRElbow    = frtm.posRElbow,
                            posLWrist    = frtm.posLWrist,
                            posRWrist    = frtm.posRWrist,

                            rotMain      = frtm.rotMain,
                            rotRoot      = frtm.rotRoot,
                            rotLHip      = frtm.rotLHip,
                            rotRHip      = frtm.rotRHip,
                            rotLKnee     = frtm.rotLKnee,
                            rotRKnee     = frtm.rotRKnee,
                            rotLAnkle    = frtm.rotLAnkle,
                            rotRAnkle    = frtm.rotRAnkle,
                            rotSpine1    = frtm.rotSpine1,
                            rotSpine2    = frtm.rotSpine2,
                            rotSpineTop  = frtm.rotSpineTop,
                            rotLClavicle = frtm.rotLClavicle,
                            rotRClavicle = frtm.rotRClavicle,
                            rotNeck      = frtm.rotNeck,
                            rotLShoulder = frtm.rotLShoulder,
                            rotRShoulder = frtm.rotRShoulder,
                            rotLElbow    = frtm.rotLElbow,
                            rotRElbow    = frtm.rotRElbow,
                            rotLWrist    = frtm.rotLWrist,
                            rotRWrist    = frtm.rotRWrist
                        };

                        ServerSendToAllExcept(ofrtm, MessageSendType.Unreliable, connection.ConnectedTo);
                    }
                }
                break;
            }

            case MessageType.IdRequest:
            {
                var idrqm = new IDRequestMessage(msg);
                MelonLogger.Log("ID request: " + idrqm.namePath);
                var obj = BWUtil.GetObjectFromFullPath(idrqm.namePath);

                SetupSyncFor(obj, idrqm.initialOwner);
                break;
            }

            case MessageType.ChangeObjectOwnership:
            {
                var coom = new ChangeObjectOwnershipMessage(msg);

                if (coom.ownerId != smallPlayerIds[connection.ConnectedTo] && coom.ownerId != 0)
                {
                    MelonLogger.LogError("Invalid object ownership change??");
                }

                if (!ObjectIDManager.objects.ContainsKey(coom.objectId))
                {
                    MelonLogger.LogError($"Got ownership change for invalid object ID {coom.objectId}");
                }

                MelonLogger.Log($"Object {coom.objectId} is now owned by {coom.ownerId}");

                var obj = ObjectIDManager.GetObject(coom.objectId);
                var so  = obj.GetComponent <SyncedObject>();
                so.owner = coom.ownerId;

                if (so.owner != 0)
                {
                    coom.linVelocity  = so.rb.velocity;
                    coom.angVelocity  = so.rb.angularVelocity;
                    so.rb.isKinematic = true;
                }
                else if (so.owner == 0)
                {
                    so.rb.isKinematic     = false;
                    so.rb.velocity        = coom.linVelocity;
                    so.rb.angularVelocity = coom.angVelocity;
                }

                ServerSendToAll(coom, MessageSendType.Reliable);
                break;
            }

            case MessageType.ObjectSync:
            {
                ObjectSyncMessage osm = new ObjectSyncMessage(msg);
                GameObject        obj = ObjectIDManager.GetObject(osm.id);

                var so = obj.GetComponent <SyncedObject>();

                if (!obj)
                {
                    MelonLogger.LogError($"Couldn't find object with ID {osm.id}");
                }
                else
                {
                    if (so.owner != smallPlayerIds[connection.ConnectedTo])
                    {
                        MelonLogger.LogError("Got object sync from client that doesn't own the object");
                    }
                    else
                    {
                        obj.transform.position = osm.position;
                        obj.transform.rotation = osm.rotation;

                        ServerSendToAllExcept(osm, MessageSendType.Reliable, connection.ConnectedTo);
                    }
                }

                break;
            }

            default:
                MelonLogger.Log("Unknown message type: " + type.ToString());
                break;
            }
        }
Esempio n. 13
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);
            }
        }
Esempio n. 14
0
        private void TransportLayer_OnMessageReceived(ITransportConnection connection, P2PMessage msg)
        {
            MessageType type = (MessageType)msg.ReadByte();

            switch (type)
            {
            case MessageType.Join:
            {
                if (msg.ReadByte() != MultiplayerMod.PROTOCOL_VERSION)
                {
                    // Somebody tried to join with an incompatible verison
                    P2PMessage m2 = new P2PMessage();
                    m2.WriteByte((byte)MessageType.JoinRejected);
                    connection.SendMessage(m2, MessageSendType.Reliable);
                    connection.Disconnect();
                }
                else
                {
                    MelonModLogger.Log("Player joined with ID: " + connection.ConnectedTo);
                    players.Add(connection.ConnectedTo);
                    MelonModLogger.Log("Player count: " + players.Count);
                    byte newPlayerId = smallIdCounter;
                    smallPlayerIds.Add(connection.ConnectedTo, newPlayerId);
                    largePlayerIds.Add(newPlayerId, connection.ConnectedTo);
                    smallIdCounter++;

                    playerConnections.Add(connection.ConnectedTo, connection);

                    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]
                        };
                        connection.SendMessage(cjm.MakeMsg(), MessageSendType.Reliable);
                    }

                    ClientJoinMessage cjm2 = new ClientJoinMessage
                    {
                        playerId = 0,
                        name     = SteamClient.Name,
                        steamId  = SteamClient.SteamId
                    };
                    connection.SendMessage(cjm2.MakeMsg(), MessageSendType.Reliable);

                    playerNames.Add(newPlayerId, name);

                    ClientJoinMessage cjm3 = new ClientJoinMessage
                    {
                        playerId = newPlayerId,
                        name     = name,
                        steamId  = connection.ConnectedTo
                    };
                    ServerSendToAllExcept(cjm3, MessageSendType.Reliable, connection.ConnectedTo);

                    playerObjects.Add(newPlayerId, new PlayerRep(name, connection.ConnectedTo));

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

                    SceneTransitionMessage stm = new SceneTransitionMessage()
                    {
                        sceneName = BoneworksSceneManager.GetCurrentSceneName()
                    };
                    connection.SendMessage(stm.MakeMsg(), MessageSendType.Reliable);

                    SetPartyIdMessage spid = new SetPartyIdMessage()
                    {
                        partyId = partyId
                    };
                    connection.SendMessage(spid.MakeMsg(), MessageSendType.Reliable);

                    ui.SetPlayerCount(players.Count, MultiplayerUIState.Server);
                }
                break;
            }

            case MessageType.Disconnect:
            {
                MelonModLogger.Log("Player left with ID: " + connection.ConnectedTo);
                byte smallId = smallPlayerIds[connection.ConnectedTo];

                playerObjects[smallId].Destroy();
                playerObjects.Remove(smallId);
                players.RemoveAll((ulong val) => val == connection.ConnectedTo);
                smallPlayerIds.Remove(connection.ConnectedTo);

                P2PMessage disconnectMsg = new P2PMessage();
                disconnectMsg.WriteByte((byte)MessageType.Disconnect);
                disconnectMsg.WriteByte(smallId);

                foreach (SteamId p in players)
                {
                    playerConnections[p].SendMessage(disconnectMsg, MessageSendType.Reliable);
                }
                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.ford.transform.position = ppm.pelvisPos - new Vector3(0.0f, 0.3f, 0.0f);
            //            pr.pelvis.transform.position = ppm.pelvisPos;
            //            pr.footL.transform.position = ppm.lFootPos;
            //            pr.footR.transform.position = ppm.rFootPos;

            //            //pr.ford.transform.rotation = ppm.pelvisRot;
            //            pr.head.transform.rotation = ppm.headRot;
            //            pr.handL.transform.rotation = ppm.lHandRot;
            //            pr.handR.transform.rotation = ppm.rHandRot;
            //            pr.pelvis.transform.rotation = ppm.pelvisRot;
            //            pr.footL.transform.rotation = ppm.lFootRot;
            //            pr.footR.transform.rotation = ppm.rFootRot;

            //            // Send to all other players

            //            OtherPlayerPositionMessage relayOPPM = new OtherPlayerPositionMessage
            //            {
            //                headPos = ppm.headPos,
            //                lHandPos = ppm.lHandPos,
            //                rHandPos = ppm.rHandPos,
            //                pelvisPos = ppm.pelvisPos,
            //                lFootPos = ppm.lFootPos,
            //                rFootPos = ppm.rFootPos,

            //                headRot = ppm.headRot,
            //                lHandRot = ppm.lHandRot,
            //                rHandRot = ppm.rHandRot,
            //                pelvisRot = ppm.pelvisRot,
            //                lFootRot = ppm.lFootRot,
            //                rFootRot = ppm.rFootRot,
            //                playerId = smallPlayerIds[packet.Value.SteamId]
            //            };

            //            ServerSendToAllExcept(relayOPPM, P2PSend.Unreliable, packet.Value.SteamId);
            //        }
            //        break;
            //    }
            case MessageType.FullRig:
            {
                FullRigTransformMessage frtm = new FullRigTransformMessage(msg);

                byte playerId = smallPlayerIds[connection.ConnectedTo];
                if (playerObjects.ContainsKey(playerId))
                {
                    PlayerRep pr = playerObjects[playerId];

                    if (pr.rigTransforms.main != null)
                    {
                        //ApplyTransformMessage(pr, frtm);
                        pr.ApplyTransformMessage(frtm);

                        OtherFullRigTransformMessage ofrtm = new OtherFullRigTransformMessage
                        {
                            playerId = playerId,

                            posMain   = frtm.posMain,
                            posRoot   = frtm.posRoot,
                            posLHip   = frtm.posLHip,
                            posRHip   = frtm.posRHip,
                            posLKnee  = frtm.posLKnee,
                            posRKnee  = frtm.posRKnee,
                            posLAnkle = frtm.posLAnkle,
                            posRAnkle = frtm.posRAnkle,

                            posSpine1    = frtm.posSpine1,
                            posSpine2    = frtm.posSpine2,
                            posSpineTop  = frtm.posSpineTop,
                            posLClavicle = frtm.posLClavicle,
                            posRClavicle = frtm.posRClavicle,
                            posNeck      = frtm.posNeck,
                            posLShoulder = frtm.posLShoulder,
                            posRShoulder = frtm.posRShoulder,
                            posLElbow    = frtm.posLElbow,
                            posRElbow    = frtm.posRElbow,
                            posLWrist    = frtm.posLWrist,
                            posRWrist    = frtm.posRWrist,

                            rotMain      = frtm.rotMain,
                            rotRoot      = frtm.rotRoot,
                            rotLHip      = frtm.rotLHip,
                            rotRHip      = frtm.rotRHip,
                            rotLKnee     = frtm.rotLKnee,
                            rotRKnee     = frtm.rotRKnee,
                            rotLAnkle    = frtm.rotLAnkle,
                            rotRAnkle    = frtm.rotRAnkle,
                            rotSpine1    = frtm.rotSpine1,
                            rotSpine2    = frtm.rotSpine2,
                            rotSpineTop  = frtm.rotSpineTop,
                            rotLClavicle = frtm.rotLClavicle,
                            rotRClavicle = frtm.rotRClavicle,
                            rotNeck      = frtm.rotNeck,
                            rotLShoulder = frtm.rotLShoulder,
                            rotRShoulder = frtm.rotRShoulder,
                            rotLElbow    = frtm.rotLElbow,
                            rotRElbow    = frtm.rotRElbow,
                            rotLWrist    = frtm.rotLWrist,
                            rotRWrist    = frtm.rotRWrist
                        };

                        ServerSendToAllExcept(ofrtm, MessageSendType.Unreliable, connection.ConnectedTo);
                    }
                }
                break;
            }

            case MessageType.HandGunChange:
            {
                HandGunChangeMessage hgcm = new HandGunChangeMessage(msg, false)
                {
                    playerId         = smallPlayerIds[connection.ConnectedTo],
                    isForOtherPlayer = false
                };

                MelonModLogger.Log("Got HGC: " + hgcm.type.ToString() + ", destroy: " + hgcm.destroy.ToString());

                if (hgcm.destroy)
                {
                    Destroy(playerObjects[smallPlayerIds[connection.ConnectedTo]].currentGun);
                }
                else
                {
                    MelonModLogger.Log("Spawning " + hgcm.type.ToString());
                    PlayerRep pr = playerObjects[smallPlayerIds[connection.ConnectedTo]];
                    pr.currentGun = BWUtil.SpawnGun(hgcm.type);
                    if (pr.currentGun == null)
                    {
                        MelonModLogger.LogError("Failed to spawn gun");
                    }
                    pr.currentGun.transform.parent        = pr.gunParent.transform;
                    pr.currentGun.transform.localPosition = Vector3.zero;
                    pr.currentGun.transform.localRotation = Quaternion.identity;        //Quaternion.AngleAxis(90.0f, new Vector3(0.0f, 1.0f, 0.0f)) * Quaternion.AngleAxis(90.0f, new Vector3(1.0f, 0.0f, 0.0f));
                    pr.currentGun.GetComponentInChildren <Rigidbody>().isKinematic = true;
                }

                hgcm.isForOtherPlayer = true;
                ServerSendToAllExcept(hgcm, MessageSendType.Reliable, connection.ConnectedTo);
                break;
            }

            default:
                MelonModLogger.Log("Unknown message type: " + type.ToString());
                break;
            }
        }
Esempio n. 15
0
        private void TransportLayer_OnMessageReceived(ITransportConnection arg1, P2PMessage msg)
        {
            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.pelvis.transform.position = oppm.pelvisPos;
                    pr.ford.transform.position   = oppm.pelvisPos - new Vector3(0.0f, 0.3f, 0.0f);
                    pr.footL.transform.position  = oppm.lFootPos;
                    pr.footR.transform.position  = oppm.rFootPos;

                    pr.head.transform.rotation   = oppm.headRot;
                    pr.handL.transform.rotation  = oppm.lHandRot;
                    pr.handR.transform.rotation  = oppm.rHandRot;
                    pr.pelvis.transform.rotation = oppm.pelvisRot;
                    pr.footL.transform.rotation  = oppm.lFootRot;
                    pr.footR.transform.rotation  = oppm.rFootRot;
                }

                break;
            }

            case MessageType.OtherFullRig:
            {
                OtherFullRigTransformMessage ofrtm = new OtherFullRigTransformMessage(msg);
                byte playerId = ofrtm.playerId;

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

                    pr.ApplyTransformMessage(ofrtm);
                }
                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.SceneTransition:
            {
                SceneTransitionMessage stm = new SceneTransitionMessage(msg);
                if (BoneworksSceneManager.GetCurrentSceneName() != stm.sceneName)
                {
                    BoneworksSceneManager.LoadScene(stm.sceneName);
                }
                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.OtherHandGunChange:
            {
                HandGunChangeMessage hgcm = new HandGunChangeMessage(msg, true);

                if (hgcm.destroy)
                {
                    Destroy(playerObjects[hgcm.playerId].currentGun);
                }
                else
                {
                    PlayerRep pr = playerObjects[hgcm.playerId];
                    pr.currentGun = BWUtil.SpawnGun(hgcm.type);
                    pr.currentGun.transform.parent                       = pr.gunParent.transform;
                    pr.currentGun.transform.localPosition                = Vector3.zero;
                    pr.currentGun.transform.localEulerAngles             = new Vector3(0.0f, 0.0f, 90.0f);
                    pr.currentGun.GetComponent <Rigidbody>().isKinematic = true;
                }
                break;
            }

            case MessageType.SetPartyId:
            {
                SetPartyIdMessage spid = new SetPartyIdMessage(msg);
                RichPresence.SetActivity(
                    new Activity()
                    {
                        Details = "Connected to a server",
                        Secrets = new ActivitySecrets()
                        {
                            Join = ServerId.ToString()
                        },
                        Party = new ActivityParty()
                        {
                            Id   = spid.partyId,
                            Size = new PartySize()
                            {
                                CurrentSize = 1,
                                MaxSize     = MultiplayerMod.MAX_PLAYERS
                            }
                        }
                    });
                break;
            }

            case MessageType.EnemyRigTransform:
            {
                enemyPoolManager.FindMissingPools();
                EnemyRigTransformMessage ertm = new EnemyRigTransformMessage(msg);
                Pool pool = enemyPoolManager.GetPool(ertm.enemyType);
                // HORRID PERFORMANCE
                Transform              enemyTf = pool.transform.GetChild(ertm.poolChildIdx);
                GameObject             rootObj = enemyTf.Find("enemyBrett@neutral").gameObject;
                BoneworksRigTransforms brt     = BWUtil.GetHumanoidRigTransforms(rootObj);
                BWUtil.ApplyRigTransform(brt, ertm);
                break;
            }
            }
        }
Esempio n. 16
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;
                }
            }
        }