Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More events #57

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions LC-API/CheatDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static void RunLocalCheatDetector()
public static void OtherPlayerCheatDetector()
{
Plugin.Log.LogWarning("Asking all other players for their mod list..");
GameTips.ShowTip("Mod List:", "Asking all other players for installed mods..");
GameTips.ShowTip("Mod List:", "Check the logs for more detailed results.\n<size=13>(Note that if someone doesnt show up on the list, they may not have LC_API installed)</size>");
Player.LocalPlayer.QueueTip("Mod List:", "Asking all other players for installed mods..");
Player.LocalPlayer.QueueTip("Mod List:", "Check the logs for more detailed results.\n<size=13>(Note that if someone doesnt show up on the list, they may not have LC_API installed)</size>");
Network.Broadcast(SIG_REQ_GUID);
}

Expand All @@ -48,7 +48,7 @@ internal static void ReceivedModListHandler(ulong senderId, List<string> mods)
{
Player player = Player.Get(senderId);
string data = $"{player.Username} responded with these mods:\n{string.Join("\n", mods)}";
GameTips.ShowTip("Mod List:", data);
Player.LocalPlayer.QueueTip("Mod List:", data);
Plugin.Log.LogWarning(data);
}

Expand Down
52 changes: 52 additions & 0 deletions LC-API/GameInterfaceAPI/Events/EventArgs/Player/DiedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using UnityEngine;

namespace LC_API.GameInterfaceAPI.Events.EventArgs.Player
{
/// <summary>
/// Contains all the information after a <see cref="Features.Player"/> dies.
/// </summary>
public class DiedEventArgs : System.EventArgs
{
/// <summary>
/// Gets the player that died.
/// </summary>
public Features.Player Player { get; }

/// <summary>
/// Gets the force that was added to the ragdoll.
/// </summary>
public Vector3 Force { get; }

/// <summary>
/// Gets whether or not a ragdoll was spawned.
/// </summary>
public bool SpawnBody { get; }

/// <summary>
/// Gets the cause of death.
/// </summary>
public CauseOfDeath CauseOfDeath { get; }

/// <summary>
/// Gets the death animation index.
/// </summary>
public int DeathAnimation { get; }

/// <summary>
/// Initializes a new instance of the <see cref="DiedEventArgs"/> class.
/// </summary>
/// <param name="player"><inheritdoc cref="Player" /></param>
/// <param name="force"><inheritdoc cref="Force" /></param>
/// <param name="spawnBody"><inheritdoc cref="SpawnBody" /></param>
/// <param name="causeOfDeath"><inheritdoc cref="CauseOfDeath" /></param>
/// <param name="deathAnimation"><inheritdoc cref="DeathAnimation" /></param>
public DiedEventArgs(Features.Player player, Vector3 force, bool spawnBody, CauseOfDeath causeOfDeath, int deathAnimation)
{
Player = player;
Force = force;
SpawnBody = spawnBody;
CauseOfDeath = causeOfDeath;
DeathAnimation = deathAnimation;
}
}
}
72 changes: 72 additions & 0 deletions LC-API/GameInterfaceAPI/Events/EventArgs/Player/HurtEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using UnityEngine;

namespace LC_API.GameInterfaceAPI.Events.EventArgs.Player
{
/// <summary>
/// Contains all the information after a <see cref="Features.Player"/> is hurt.
/// </summary>
public class HurtEventArgs : System.EventArgs
{
/// <summary>
/// Gets the player that is taking damage.
/// </summary>
public Features.Player Player { get; }

/// <summary>
/// Gets the amount of damage the <see cref="Player"/> took.
/// </summary>
public int Damage { get; }

/// <summary>
/// Gets or sets whether or not this damage will play sfx, if it has any.
/// </summary>
public bool HasSFX { get; set; }

/// <summary>
/// Gets or sets the cause of death.
/// </summary>
public CauseOfDeath CauseOfDeath { get; set; }

/// <summary>
/// Gets or sets the death animation index.
/// </summary>
public int DeathAnimation { get; set; }

/// <summary>
/// Gets or sets whether or not this damage is considered fall damage.
/// </summary>
public bool FallDamage { get; set; }

/// <summary>
/// Gets or sets the force to add to the ragdoll if the player is killed.
/// </summary>
public Vector3 Force { get; set; }

/// <summary>
/// Gets whether or not this damage will kill the player.
/// </summary>
public bool Killing => Player.Health <= 0;

/// <summary>
/// Initializes a new instance of the <see cref="HurtingEventArgs"/> class.
/// </summary>
/// <param name="player"><inheritdoc cref="Player" /></param>
/// <param name="damage"><inheritdoc cref="Damage" /></param>
/// <param name="hasSFX"><inheritdoc cref="HasSFX" /></param>
/// <param name="causeOfDeath"><inheritdoc cref="CauseOfDeath" /></param>
/// <param name="deathAnimation"><inheritdoc cref="DeathAnimation" /></param>
/// <param name="fallDamage"><inheritdoc cref="FallDamage" /></param>
/// <param name="force"><inheritdoc cref="Force" /></param>
public HurtEventArgs(Features.Player player, int damage, bool hasSFX, CauseOfDeath causeOfDeath, int deathAnimation,
bool fallDamage, Vector3 force)
{
Player = player;
Damage = damage;
HasSFX = hasSFX;
CauseOfDeath = causeOfDeath;
DeathAnimation = deathAnimation;
FallDamage = fallDamage;
Force = force;
}
}
}
24 changes: 23 additions & 1 deletion LC-API/GameInterfaceAPI/Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ public static class Player
/// </summary>
public static event CustomEventHandler<HurtingEventArgs> Hurting;

/// <summary>
/// Invoked after a <see cref="Features.Player"/> is hurt.
/// </summary>
public static event CustomEventHandler<HurtEventArgs> Hurt;

/// <summary>
/// Invoked before a <see cref="Features.Player"/> dies.
/// </summary>
public static event CustomEventHandler<DyingEventArgs> Dying;

/// <summary>
/// Invoked after a <see cref="Features.Player"/> dies.
/// </summary>
public static event CustomEventHandler<DiedEventArgs> Died;

/// <summary>
/// Called after a <see cref="Features.Player"/> joined the server, including the host.
/// </summary>
Expand All @@ -46,10 +56,22 @@ public static class Player
/// <param name="ev">The <see cref="HurtingEventArgs"/> event arguments.</param>
public static void OnHurting(HurtingEventArgs ev) => Hurting.InvokeSafely(ev);

/// <summary>
/// Called after a <see cref="Features.Player"/> is hurt.
/// </summary>
/// <param name="ev">The <see cref="HurtEventArgs"/> event arguments.</param>
public static void OnHurt(HurtEventArgs ev) => Hurt.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="Features.Player"/> dies.
/// </summary>
/// <param name="ev">The <see cref="DyingEventArgs"/> event arguments.</param>
public static void OnDying(DyingEventArgs ev) => Dying.InvokeSafely(ev);

/// <summary>
/// Called after a <see cref="Features.Player"/> dies.
/// </summary>
/// <param name="ev">The <see cref="DiedEventArgs"/> event arguments.</param>
public static void OnDied(DiedEventArgs ev) => Died.InvokeSafely(ev);
}
}
}
39 changes: 39 additions & 0 deletions LC-API/GameInterfaceAPI/Events/Patches/Internal/DisplayTipPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using HarmonyLib;
using LC_API.GameInterfaceAPI.Features;
using System;
using System.Collections.Generic;
using System.Text;

namespace LC_API.GameInterfaceAPI.Events.Patches.Internal
{
[HarmonyPatch(typeof(HUDManager), nameof(HUDManager.DisplayTip))]
class DisplayTipPatch
{
// Normally we wouldn't prefix return false, however, we need all uses of `DisplayTip` to go through
// our system in order to not cause improper timing. All uses of base game's `DisplayTip` will bypass queue
// and immediately be shown, but preserving the currently showing tip, if there is one, and continuing it after
// the tip is complete.
private static bool Prefix(string headerText, string bodyText, bool isWarning, bool useSave, string prefsKey)
{
Features.Player player = Features.Player.LocalPlayer;

Tip tip = new Tip(headerText, bodyText, 5, 0, 0);

if (player.CurrentTip != null)
{
// Ensures the current tip will continue afterwards
player.CurrentTip.TipId = int.MinValue;
player.TipQueue.Insert(0, player.CurrentTip);
}

player.CurrentTip = tip;

HUDManager.Instance.tipsPanelAnimator.speed = 1;
HUDManager.Instance.tipsPanelAnimator.ResetTrigger("TriggerHint");

Features.Player.DisplayTip(tip.Header, tip.Message, isWarning, useSave, prefsKey);

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
namespace LC_API.GameInterfaceAPI.Events.Patches.Player
{
[HarmonyPatch(typeof(PlayerControllerB), nameof(PlayerControllerB.DamagePlayer))]
internal class Hurting
internal class Hurt
{
private static HurtingEventArgs CallEvent(PlayerControllerB playerController, int damage, bool hasSFX, CauseOfDeath causeOfDeath,
private static HurtingEventArgs CallHurtingEvent(PlayerControllerB playerController, int damage, bool hasSFX, CauseOfDeath causeOfDeath,
int deathAnimation, bool fallDamage, Vector3 force)
{
HurtingEventArgs ev = new HurtingEventArgs(Features.Player.GetOrAdd(playerController), damage, hasSFX,
Expand All @@ -21,6 +21,17 @@ private static HurtingEventArgs CallEvent(PlayerControllerB playerController, in
return ev;
}

private static HurtEventArgs CallHurtEvent(PlayerControllerB playerController, int damage, bool hasSFX, CauseOfDeath causeOfDeath,
int deathAnimation, bool fallDamage, Vector3 force)
{
HurtEventArgs ev = new HurtEventArgs(Features.Player.GetOrAdd(playerController), damage, hasSFX,
causeOfDeath, deathAnimation, fallDamage, force);

Handlers.Player.OnHurt(ev);

return ev;
}

private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = new List<CodeInstruction>(instructions);
Expand All @@ -34,15 +45,15 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

CodeInstruction[] inst = new CodeInstruction[]
{
// HurtingEventArgs ev = Hurting.CallEvent(PlayerControllerB, int, bool, CauseOfDeath, int, bool, Vector3)
// HurtingEventArgs ev = Hurt.CallHurtingEvent(PlayerControllerB, int, bool, CauseOfDeath, int, bool, Vector3)
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Ldarg, 4),
new CodeInstruction(OpCodes.Ldarg, 5),
new CodeInstruction(OpCodes.Ldarg, 6),
new CodeInstruction(OpCodes.Ldarg, 7),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Hurting), nameof(Hurting.CallEvent))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Hurt), nameof(Hurt.CallHurtingEvent))),
new CodeInstruction(OpCodes.Dup),
// if (!ev.IsAllwed) return
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.IsAllowed))),
Expand Down Expand Up @@ -87,7 +98,51 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

newInstructions[index + inst.Length].labels.Add(skipLabel);

const int offset2 = 1;

index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Stfld
&& i.OperandIs(AccessTools.Field(typeof(PlayerControllerB), nameof(PlayerControllerB.health)))) + offset2;

newInstructions.InsertRange(index, new CodeInstruction[]
{
// HurtEventArgs ev = Hurt.CallHurtEvent(PlayerControllerB, int, bool, CauseOfDeath, int, bool, Vector3)
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new CodeInstruction(OpCodes.Ldarg_1),
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Ldarg, 4),
new CodeInstruction(OpCodes.Ldarg, 5),
new CodeInstruction(OpCodes.Ldarg, 6),
new CodeInstruction(OpCodes.Ldarg, 7),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Hurt), nameof(Hurt.CallHurtEvent))),

// Duplicating the stack is more memory efficient than making a local
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Dup),

// hasDamageSFX = ev.HasSFX
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.HasSFX))),
new CodeInstruction(OpCodes.Starg_S, 2),

// causeOfDeath = ev.CauseOfDeath
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.CauseOfDeath))),
new CodeInstruction(OpCodes.Starg_S, 4),

// deathAnimation = ev.DeathAnimation
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.DeathAnimation))),
new CodeInstruction(OpCodes.Starg_S, 5),

// fallDamage = ev.FallDamage
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.FallDamage))),
new CodeInstruction(OpCodes.Starg_S, 6),

// force = ev.Force
new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(HurtingEventArgs), nameof(HurtingEventArgs.Force))),
new CodeInstruction(OpCodes.Starg_S, 7),
});

for (int i = 0; i < newInstructions.Count; i++) yield return newInstructions[i];
}
}
}
}
Loading
Loading