public static IEnumerable <CodeInstruction> GameTick_Transpiler(IEnumerable <CodeInstruction> instructions)
        {
            // Send PlayerUseWarper(bool) whenever warpCommand is toggled between true or false
            var codeMatcher = new CodeMatcher(instructions)
                              .MatchForward(true,
                                            new CodeMatch(i => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "warpCommand")
                                            );

            if (codeMatcher.IsInvalid)
            {
                NebulaModel.Logger.Log.Error("PlayerMoveSail_Transpiler.GameTick failed. Mod version not compatible with game version.");
                return(instructions);
            }

            return(codeMatcher
                   .Repeat(matcher =>
            {
                var warpCommand = matcher.InstructionAt(-1).opcode == OpCodes.Ldc_I4_1;
                matcher
                .Advance(1)
                .InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate <Action>(() =>
                {
                    // send to host / clients
                    if (!SimulatedWorld.Initialized)
                    {
                        return;
                    }

                    if (LocalPlayer.IsMasterClient)
                    {
                        PlayerUseWarper packet = new PlayerUseWarper(warpCommand)
                        {
                            PlayerId = LocalPlayer.PlayerId
                        };
                        LocalPlayer.SendPacket(packet);
                    }
                    else
                    {
                        LocalPlayer.SendPacket(new PlayerUseWarper(warpCommand));
                    }

                    return;
                }));
            })
                   .InstructionEnumeration());
        }
Esempio n. 2
0
        public static IEnumerable <CodeInstruction> GameTick_Transpiler(IEnumerable <CodeInstruction> instructions)
        {
            // Send PlayerUseWarper(bool) whenever warpCommand is toggled between true or false
            CodeMatcher codeMatcher = new CodeMatcher(instructions)
                                      .MatchForward(true,
                                                    new CodeMatch(i => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "warpCommand")
                                                    );

            if (codeMatcher.IsInvalid)
            {
                NebulaModel.Logger.Log.Error("PlayerMoveSail_Transpiler.GameTick failed. Mod version not compatible with game version.");
                return(instructions);
            }

            return(codeMatcher
                   .Repeat(matcher =>
            {
                bool warpCommand = matcher.InstructionAt(-1).opcode == OpCodes.Ldc_I4_1;
                matcher
                .Advance(1)
                .InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate <Action>(() =>
                {
                    if (Multiplayer.IsActive)
                    {
                        if (Multiplayer.Session.LocalPlayer.IsHost)
                        {
                            PlayerUseWarper packet = new PlayerUseWarper(warpCommand)
                            {
                                PlayerId = Multiplayer.Session.LocalPlayer.Id
                            };
                            Multiplayer.Session.Network.SendPacket(packet);
                        }
                        else
                        {
                            Multiplayer.Session.Network.SendPacket(new PlayerUseWarper(warpCommand));
                        }
                    }
                }));
            })
                   .InstructionEnumeration());
        }
Esempio n. 3
0
 public static void UpdateRemotePlayerWarpState(PlayerUseWarper packet)
 {
     using (GetRemotePlayersModels(out var remotePlayersModels))
     {
         if (packet.PlayerId == 0)
         {
             packet.PlayerId = 1;                       // host sends himself as PlayerId 0 but clients see him as id 1
         }
         if (remotePlayersModels.TryGetValue(packet.PlayerId, out RemotePlayerModel player))
         {
             if (packet.WarpCommand)
             {
                 player.Effects.StartWarp();
             }
             else
             {
                 player.Effects.StopWarp();
             }
         }
     }
 }