Skip to content
This repository has been archived by the owner on Jan 19, 2025. It is now read-only.

Commit

Permalink
Merge branch 'apis-rework' into role-api
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 authored Mar 24, 2024
2 parents 0ae0c3f + 2b09db2 commit 8694393
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Exiled.API/Extensions/MirrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public static void ChangeAppearance(this Player player, RoleTypeId type, IEnumer
else
fpc = playerfpc;

fpc.FpcModule.MouseLook.GetSyncValues(0, out ushort value, out ushort _);
ushort value = 0;
fpc?.FpcModule.MouseLook.GetSyncValues(0, out value, out ushort _);
writer.WriteRelativePosition(player.RelativePosition);
writer.WriteUShort(value);
}
Expand Down
14 changes: 12 additions & 2 deletions Exiled.API/Extensions/RoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public static class RoleExtensions
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase(this RoleTypeId roleType, out PlayerRoleBase roleBase) => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);

/// <summary>
/// Tries to get the base <see cref="PlayerRoleBase"/> of the given <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <param name="roleBase">The <see cref="PlayerRoleBase"/> to return.</param>
/// <typeparam name="T">The type to cast the <see cref="PlayerRoleBase"/> to.</typeparam>
/// <returns>The <see cref="PlayerRoleBase"/>.</returns>
public static bool TryGetRoleBase<T>(this RoleTypeId roleType, out T roleBase)
where T : PlayerRoleBase => PlayerRoleLoader.TryGetRoleTemplate(roleType, out roleBase);

/// <summary>
/// Gets the <see cref="LeadingTeam"/>.
/// </summary>
Expand Down Expand Up @@ -120,11 +130,11 @@ public static class RoleExtensions
/// <returns>Returns a <see cref="SpawnLocation"/> representing the spawn, or <see langword="null"/> if no spawns were found.</returns>
public static SpawnLocation GetRandomSpawnLocation(this RoleTypeId roleType)
{
if (roleType.GetRoleBase() is IFpcRole fpcRole &&
if (roleType.TryGetRoleBase(out FpcStandardRoleBase fpcRole) &&
fpcRole.SpawnpointHandler != null &&
fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRotation))
{
return new SpawnLocation(roleType, position, horizontalRotation);
return new(roleType, position, horizontalRotation);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion Exiled.API/Features/Doors/Door.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal Door(DoorVariant door, List<Room> rooms)
/// <summary>
/// Gets a value indicating whether or not the door is currently moving.
/// </summary>
public virtual bool IsMoving => ExactState is not(0 or 1);
public virtual bool IsMoving => !(IsFullyOpen || IsFullyClosed);

/// <summary>
/// Gets a value indicating the precise state of the door, from <c>0-1</c>. A value of <c>0</c> indicates the door is fully closed, while a value of <c>1</c> indicates the door is fully open. Values in-between represent the door's animation progress.
Expand Down
2 changes: 1 addition & 1 deletion Exiled.API/Features/Doors/Gate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Gate(PryableDoor door, List<Room> room)
/// <summary>
/// Gets a value indicating whether the door is fully open.
/// </summary>
public override bool IsFullyOpen => base.IsFullyOpen;
public override bool IsFullyOpen => base.IsFullyOpen || (Base is Timed173PryableDoor && ExactState is 0.5845918f);

/// <summary>
/// Gets a value indicating whether or not the door is currently moving.
Expand Down
19 changes: 18 additions & 1 deletion Exiled.Events/Features/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,31 @@ internal void InvokeAsync()

foreach (CustomAsyncEventHandler handler in InnerAsyncEvent.GetInvocationList().Cast<CustomAsyncEventHandler>())
{
Timing.RunCoroutine(SafeCoroutineEnumerator(handler(), handler));
}
}

/// <summary>
/// Runs the coroutine manualy so exceptions can be caught and logged.
/// </summary>
private IEnumerator<float> SafeCoroutineEnumerator(IEnumerator<float> coroutine, CustomAsyncEventHandler handler)
{
while (true)
{
float current;
try
{
Timing.RunCoroutine(handler());
if (!coroutine.MoveNext())
break;
current = coroutine.Current;
}
catch (Exception ex)
{
Log.Error($"Method \"{handler.Method.Name}\" of the class \"{handler.Method.ReflectedType.FullName}\" caused an exception when handling the event \"{GetType().FullName}\"\n{ex}");
yield break;
}

yield return current;
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion Exiled.Events/Features/Event{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,31 @@ internal void InvokeAsync(T arg)

foreach (CustomAsyncEventHandler<T> handler in InnerAsyncEvent.GetInvocationList().Cast<CustomAsyncEventHandler<T>>())
{
Timing.RunCoroutine(SafeCoroutineEnumerator(handler(arg), handler));
}
}

/// <summary>
/// Runs the coroutine manualy so exceptions can be caught and logged.
/// </summary>
private IEnumerator<float> SafeCoroutineEnumerator(IEnumerator<float> coroutine, CustomAsyncEventHandler<T> handler)
{
while (true)
{
float current;
try
{
Timing.RunCoroutine(handler(arg));
if (!coroutine.MoveNext())
break;
current = coroutine.Current;
}
catch (Exception ex)
{
Log.Error($"Method \"{handler.Method.Name}\" of the class \"{handler.Method.ReflectedType.FullName}\" caused an exception when handling the event \"{GetType().FullName}\"\n{ex}");
yield break;
}

yield return current;
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions Exiled.Events/Patches/Events/Player/InteractingDoor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
0,
new CodeInstruction[]
{
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(InteractingDoor), nameof(InteractingDoor.CanStateChange))),
new(OpCodes.Brfalse_S, retLabel),

// InteractingDoorEventArgs ev = new(Player.Get(ply), __instance, false);
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
Expand Down Expand Up @@ -115,10 +111,5 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

private static bool CanStateChange(DoorVariant variant)
{
return !(variant.GetExactState() > 0f && variant.GetExactState() < 1f);
}
}
}
47 changes: 21 additions & 26 deletions Exiled.Events/Patches/Events/Player/Spawning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ namespace Exiled.Events.Patches.Events.Player
using System.Reflection;

using API.Features;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;

using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using PlayerRoles.FirstPersonControl.NetworkMessages;
using PlayerRoles.FirstPersonControl.Spawnpoints;

using UnityEngine;
Expand All @@ -27,8 +25,8 @@ namespace Exiled.Events.Patches.Events.Player
/// <summary>
/// Patches <see cref="RoleSpawnpointManager.Init"/> delegate.
/// Adds the <see cref="Handlers.Player.Spawning"/> event.
/// Fix for spawning in void.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Spawning))]
[HarmonyPatch]
internal static class Spawning
{
Expand All @@ -39,36 +37,33 @@ private static MethodInfo TargetMethod()

private static bool Prefix(ReferenceHub hub, PlayerRoleBase prevRole, PlayerRoleBase newRole)
{
if (newRole.ServerSpawnReason != RoleChangeReason.Destroyed && Player.TryGet(hub, out Player player))
{
Vector3 oldPosition = hub.transform.position;
float oldRotation = (prevRole as IFpcRole)?.FpcModule.MouseLook.CurrentVertical ?? 0;

if (newRole is IFpcRole fpcRole)
{
if (newRole.ServerSpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint) && fpcRole.SpawnpointHandler != null && fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRot))
{
oldPosition = position;
oldRotation = horizontalRot;
}
if (newRole.ServerSpawnReason == RoleChangeReason.Destroyed || Player.TryGet(hub, out Player player))
return true;

SpawningEventArgs ev = new(player, oldPosition, oldRotation, prevRole);
Vector3 oldPosition = hub.transform.position;
float oldRotation = (prevRole as IFpcRole)?.FpcModule.MouseLook.CurrentVertical ?? 0;

Handlers.Player.OnSpawning(ev);

hub.transform.position = ev.Position;
fpcRole.FpcModule.MouseLook.CurrentHorizontal = ev.HorizontalRotation;
hub.connectionToClient.Send(new FpcOverrideMessage(ev.Position, ev.HorizontalRotation), 0);
}
else
if (newRole is IFpcRole fpcRole)
{
if (newRole.ServerSpawnFlags.HasFlag(RoleSpawnFlags.UseSpawnpoint) && fpcRole.SpawnpointHandler != null && fpcRole.SpawnpointHandler.TryGetSpawnpoint(out Vector3 position, out float horizontalRot))
{
Handlers.Player.OnSpawning(new(player, oldPosition, oldRotation, prevRole));
oldPosition = position;
oldRotation = horizontalRot;
}

return false;
SpawningEventArgs ev = new(player, oldPosition, oldRotation, prevRole);

Handlers.Player.OnSpawning(ev);

player.Position = ev.Position;
fpcRole.FpcModule.MouseLook.CurrentHorizontal = ev.HorizontalRotation;
}
else
{
Handlers.Player.OnSpawning(new(player, oldPosition, oldRotation, prevRole));
}

return true;
return false;
}
}
}

0 comments on commit 8694393

Please sign in to comment.