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

CustomRole Fix #2763

Merged
merged 5 commits into from
Jul 30, 2024
Merged
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
15 changes: 10 additions & 5 deletions Exiled.API/Features/Core/EActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ protected EActor()
{
IsEditable = true;
fixedTickRate = DEFAULT_FIXED_TICK_RATE;
PostInitialize();
Timing.CallDelayed(fixedTickRate, OnBeginPlay);
Timing.CallDelayed(fixedTickRate * 2, () => serverTick = Timing.RunCoroutine(ServerTick()));
}

/// <summary>
Expand Down Expand Up @@ -462,12 +459,21 @@ public bool HasComponent(Type type, bool depthInheritance = false) => depthInher
? ComponentsInChildren.Any(type.IsInstanceOfType)
: ComponentsInChildren.Any(comp => type == comp.GetType());

/// <summary>
/// Called when the <see cref="EActor"/> is initialized.
/// </summary>
public void ComponentInitialize()
{
PostInitialize();
Timing.CallDelayed(fixedTickRate, OnBeginPlay);
Timing.CallDelayed(fixedTickRate * 2, () => serverTick = Timing.RunCoroutine(ServerTick()));
}

/// <summary>
/// Fired after the <see cref="EActor"/> instance is created.
/// </summary>
protected virtual void PostInitialize()
{
Log.InfoWithContext(nameof(PostInitialize));
}

/// <summary>
Expand All @@ -486,7 +492,6 @@ protected virtual void Tick()
if (DestroyNextTick)
{
Destroy();
return;
}
}

Expand Down
18 changes: 8 additions & 10 deletions Exiled.API/Features/Core/EObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ protected EObject(GameObject gameObject = null)
/// <summary>
/// Gets or sets the name of the <see cref="EObject"/> instance.
/// </summary>
public string Name { get; set; }
public string Name { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the tag of the <see cref="EObject"/> instance.
/// </summary>
public string Tag { get; set; }
public string Tag { get; set; } = string.Empty;

/// <summary>
/// Gets or sets a value indicating whether the <see cref="EObject"/> values can be edited.
Expand Down Expand Up @@ -357,17 +357,14 @@ public static Type GetObjectTypeFromRegisteredTypes(Type type, string name)
public static EObject CreateDefaultSubobject(Type type, params object[] parameters)
{
const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
EObject @object = null;
if (type.GetConstructors().Length == 0)
{
@object = InitializeBaseType(type, parameters);
}
else
EObject @object = Activator.CreateInstance(type, flags, null, null, null) as EObject;

if (@object is not null && Player.DEFAULT_ROLE_BEHAVIOUR is not null && type.BaseType == Player.DEFAULT_ROLE_BEHAVIOUR)
{
@object = Activator.CreateInstance(type, flags, null, parameters, null) as EObject;
@object.Base = parameters[0] as GameObject;
@object.Cast<EActor>().ComponentInitialize();
}


// Do not use implicit bool conversion as @object may be null
if (@object != null)
{
Expand Down Expand Up @@ -918,3 +915,4 @@ protected virtual void OnDestroyed()
}
}
}

1 change: 1 addition & 0 deletions Exiled.API/Features/Core/GameEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public T AddComponent<T>(string name = "")
public EActor AddComponent(Type type, string name = "")
{
EActor component = EObject.CreateDefaultSubobject(type, GameObject).Cast<EActor>();
component.Base = GameObject;

if (!component)
return null;
Expand Down
6 changes: 0 additions & 6 deletions Exiled.API/Features/Core/Generic/EBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,10 @@ protected override void PostInitialize()
{
base.PostInitialize();

Log.InfoWithContext($"EBehaviour::{nameof(PostInitialize)}");

FindOwner();

Log.InfoWithContext($"EBehaviour::{nameof(FindOwner)}");

if (!Owner && DisposeOnNullOwner)
{
Log.InfoWithContext($"Destroying Behaviour");

Destroy();
return;
}
Expand Down
5 changes: 5 additions & 0 deletions Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public class Player : GameEntity
public const string INFO_CATEGORY = "Player_Info";

#pragma warning disable SA1401
/// <summary>
/// The default role behaviour class.
/// </summary>
public static Type DEFAULT_ROLE_BEHAVIOUR = null;

/// <summary>
/// The default player class.
/// </summary>
Expand Down
31 changes: 2 additions & 29 deletions Exiled.CustomModules/API/Features/CustomRoles/CustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,10 @@ public static void Remove<T>(IEnumerable<Pawn> players)
/// </remarks>
public static void EnableAll(Assembly assembly)
{
if (!CustomModules.Instance.Config.Modules.Contains(UUModuleType.CustomRoles.Name))
if (!CustomModules.Instance.Config.Modules.Contains("CustomRoles"))
throw new Exception("ModuleType::CustomRoles must be enabled in order to load any custom roles");

Player.DEFAULT_ROLE_BEHAVIOUR = typeof(RoleBehaviour);
List<CustomRole> customRoles = new();
foreach (Type type in assembly.GetTypes())
{
Expand Down Expand Up @@ -743,66 +744,40 @@ public void ForceSpawn(Pawn player)
ChangingCustomRoleEventArgs ev = new(player, Id);
ChangingCustomRoleDispatcher.InvokeAll(ev);

Log.InfoWithContext("ForceSpawn 1");

if (!ev.IsAllowed)
return;


Log.InfoWithContext("ForceSpawn 2");

player = ev.Player.Cast<Pawn>();
if (ev.Role is RoleTypeId rId)
{
Log.InfoWithContext("ForceSpawn 2.1");

player.SetRole(rId);
return;
}

Log.InfoWithContext("ForceSpawn 3");

if (!TryGet(ev.Role, out CustomRole role))
return;

Log.InfoWithContext("ForceSpawn 4");


if (role.Id != Id)
{
Log.InfoWithContext("ForceSpawn 4.1");

role.ForceSpawn(player);
return;
}

Log.InfoWithContext("ForceSpawn 5");


object prevRole = player.CustomRole ? player.CustomRole.Id : player.Role.Type;
Remove(player);
PlayersValue.Add(player, this);

Log.InfoWithContext("ForceSpawn 6");


if (!player.IsAlive)
{
Log.InfoWithContext("ForceSpawn 7");

ForceSpawn_Internal(player, false);
ChangedCustomRoleEventArgs @event = new(player, prevRole);
ChangedCustomRoleDispatcher.InvokeAll(@event);
return;
}

Log.InfoWithContext("ForceSpawn 8");

player.Role.Set(RoleTypeId.Spectator, SpawnReason.Respawn);
Timing.CallDelayed(0.1f, () =>
{
Log.InfoWithContext("ForceSpawn 9");

ForceSpawn_Internal(player, false);
ChangedCustomRoleEventArgs @event = new(player, prevRole);
ChangedCustomRoleDispatcher.InvokeAll(@event);
Expand Down Expand Up @@ -999,7 +974,6 @@ private void ForceSpawn_Internal(Pawn player, bool preservePosition, SpawnReason
{
Instances += 1;
RoleBehaviour roleBehaviour = EObject.CreateDefaultSubobject<EActor>(BehaviourComponent, $"ECS-{Name}").Cast<RoleBehaviour>();
Log.InfoWithContext($"RB is null? {roleBehaviour is null}");
roleBehaviour.Settings.PreservePosition = preservePosition;

spawnReason ??= SpawnReason.ForceClass;
Expand All @@ -1010,7 +984,6 @@ private void ForceSpawn_Internal(Pawn player, bool preservePosition, SpawnReason
roleBehaviour.Settings.SpawnFlags = roleSpawnFlags;

EActor ea = player.AddComponent(roleBehaviour);
Log.InfoWithContext($"ea is null? {ea is null}");
}
}
}
16 changes: 1 addition & 15 deletions Exiled.CustomModules/API/Features/CustomRoles/RoleBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public virtual void AdjustAdditivePipe()
}

Owner.UniqueRole = CustomRole.Name;
Owner.TryAddCustomRoleFriendlyFire(Name, Settings.FriendlyFireMultiplier);
//Owner.TryAddCustomRoleFriendlyFire(Name, Settings.FriendlyFireMultiplier);

if (CustomRole.EscapeBehaviourComponent is not null)
{
Expand All @@ -275,12 +275,8 @@ protected override void PostInitialize()
{
base.PostInitialize();

Log.InfoWithContext("Pre-Adjust");

AdjustAdditivePipe();

Log.InfoWithContext("Post-Adjust");

wasNoClipPermitted = Owner.IsNoclipPermitted;
isHuman = !CustomRole.IsScp;

Expand All @@ -290,23 +286,17 @@ protected override void PostInitialize()

if (Role is RoleTypeId.None)
{
Log.InfoWithContext("Role is none");

Destroy();
return;
}
}

if (Settings.SpawnFlags is not RoleSpawnFlags.All)
{
Log.InfoWithContext("Is not RSF ALL");

Owner.Role.Set(Role, Settings.SpawnReason, Settings.SpawnFlags);
}
else
{
Log.InfoWithContext("Is RSF ALL");

switch (Settings.PreservePosition)
{
case true when Settings.PreserveInventory:
Expand Down Expand Up @@ -384,8 +374,6 @@ protected override void OnBeginPlay()

if (!Owner)
{
Log.InfoWithContext("Owner is null RB");

Destroy();
return;
}
Expand All @@ -407,8 +395,6 @@ protected override void Tick()
// Must be refactored (performance issues)
if ((Settings.UseDefaultRoleOnly && (Owner.Role != Role)) || (!Settings.DynamicRoles.IsEmpty() && !Settings.DynamicRoles.Contains(Owner.Role)))
{
Log.InfoWithContext("Tick check failed");

Destroy();
return;
}
Expand Down
22 changes: 22 additions & 0 deletions Exiled.Example/TestRole/CustomRoleType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -----------------------------------------------------------------------
// <copyright file="CustomRoleType.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Example.TestRole
{
using Exiled.CustomModules.API.Enums;

/// <summary>
/// The custom role type.
/// </summary>
public class CustomRoleType : UUCustomRoleType
{
/// <summary>
/// Initializes a new custom role id.
/// </summary>
public static readonly CustomRoleType Scp999 = new();
}
}
21 changes: 21 additions & 0 deletions Exiled.Example/TestRole/Scp999Behaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="Scp999Behaviour.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Example.TestRole
{
using Exiled.CustomModules.API.Features.CustomRoles;

/// <inheritdoc />
public class Scp999Behaviour : RoleBehaviour
{
/// <inheritdoc />
protected override void PostInitialize()
{
base.PostInitialize();
}
}
}
21 changes: 21 additions & 0 deletions Exiled.Example/TestRole/Scp999Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="Scp999Config.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Example.TestRole
{
using Exiled.CustomModules.API.Features.Attributes;
using Exiled.CustomModules.API.Features.CustomRoles;
using Exiled.CustomModules.API.Features.Generic;

/// <inheritdoc />
[ModuleIdentifier]
public class Scp999Config : ModulePointer<CustomRole>
{
/// <inheritdoc />
public override uint Id { get; set; } = CustomRoleType.Scp999;
}
}
Loading
Loading