Skip to content

Commit

Permalink
Completely reworked the Tick system, cuz why not make it overdone? Al…
Browse files Browse the repository at this point in the history
…so added a new AutoArray collection which should in theory be faster than anything else.
  • Loading branch information
marchellc committed Aug 27, 2024
1 parent 6091535 commit 3b9e66b
Show file tree
Hide file tree
Showing 58 changed files with 1,445 additions and 926 deletions.
469 changes: 469 additions & 0 deletions LabExtended/API/Collections/AutoArray.cs

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions LabExtended/API/CustomCommands/Tick/Status/TickStatusCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using LabExtended.Commands;
using LabExtended.Commands.Arguments;
using LabExtended.Commands.Interfaces;

using LabExtended.Core.Ticking;

namespace LabExtended.API.CustomCommands.Tick.Status
{
public class TickStatusCommand : CustomCommand
{
public override string Command => "status";
public override string Description => "Shows the status of tick distribution.";

public override void OnCommand(ExPlayer sender, ICommandContext ctx, ArgumentCollection args)
{
base.OnCommand(sender, ctx, args);

if (TickDistribution.AllDistributors.Count() < 1)
{
ctx.RespondOk("There aren't any tick distributors.");
return;
}

var str = $"Tick Distributors ({TickDistribution.AllDistributors.Count()}):\n";

foreach (var distributor in TickDistribution.AllDistributors)
str += $"[{distributor.GetType().Name}]: {distributor.HandleCount} handle(s), {distributor.TickRate} TPS\n";

ctx.RespondOk(str);
}
}
}
22 changes: 22 additions & 0 deletions LabExtended/API/CustomCommands/Tick/TickCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CommandSystem;

using LabExtended.API.CustomCommands.Tick.Status;
using LabExtended.Commands;

namespace LabExtended.API.CustomCommands.Tick
{
[CommandHandler(typeof(RemoteAdminCommandHandler))]
[CommandHandler(typeof(GameConsoleCommandHandler))]
public class TickCommand : VanillaParentCommandBase
{
public override string Command => "tick";
public override string Description => "Commands for tick distribution management.";

public override void LoadGeneratedCommands()
{
base.LoadGeneratedCommands();

RegisterCommand(new TickStatusCommand());
}
}
}
4 changes: 2 additions & 2 deletions LabExtended/API/CustomItems/CustomItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
using LabExtended.API.CustomItems.Interfaces;

using LabExtended.Core;
using LabExtended.Core.Ticking;
using LabExtended.Events.Player;

using LabExtended.Extensions;
using LabExtended.Core.Ticking;

using PluginAPI.Events;

Expand All @@ -21,7 +21,7 @@ namespace LabExtended.API.CustomItems
public class CustomItem
{
static CustomItem()
=> TickManager.SubscribeTick(TickItems, TickTimer.None, "Custom Item Tick");
=> TickDistribution.UnityTick.CreateHandle(TickDistribution.CreateWith(TickItems));

private static readonly List<ICustomItemInfo> _registeredTypes = new List<ICustomItemInfo>();
private static readonly LockedDictionary<ushort, CustomItem> _items = new LockedDictionary<ushort, CustomItem>();
Expand Down
2 changes: 1 addition & 1 deletion LabExtended/API/CustomItems/Usables/CustomUsable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace LabExtended.API.CustomItems.Usables
public class CustomUsable : CustomItem
{
static CustomUsable()
=> TickManager.OnTick += UpdateUsables;
=> TickDistribution.UnityTick.CreateHandle(TickDistribution.CreateWith(UpdateUsables));

public UsableItem UsableItem
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using LabExtended.API.Collections.Locked;
using LabExtended.API.Modules;

using LabExtended.Core;
using LabExtended.Core.Ticking.Distributors.Unity;

using LabExtended.Extensions;
using LabExtended.Core.Ticking;

using UnityEngine;
using LabExtended.API.Modules;

namespace LabExtended.API.CustomModules.PositionTracking
{
Expand All @@ -17,7 +19,7 @@ public class PositionTrackingModule : GenericModule<ExPlayer>
private readonly LockedHashSet<PositionTrackingEntry> _removeNextTick = new LockedHashSet<PositionTrackingEntry>(); // Used to avoid collection exceptions.

/// <inheritdoc/>
public override TickTimer TickTimer { get; } = TickTimer.None;
public override Type TickType { get; } = typeof(UnityTickDistributor);

/// <summary>
/// Gets a list of all active entries.
Expand Down
7 changes: 2 additions & 5 deletions LabExtended/API/ExTeslaGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

using MapGeneration;

using Mirror;

using PlayerRoles;
using PlayerStatsSystem;

Expand All @@ -30,8 +28,7 @@ public class ExTeslaGate :

IDamageObject
{
static ExTeslaGate()
=> TickManager.SubscribeTick(TickGates, TickTimer.NoneProfiled, "Tesla Gate Update");
internal static TickHandle _tickHandle;

internal ExTeslaGate(TeslaGate baseValue) : base(baseValue) { }

Expand Down Expand Up @@ -320,7 +317,7 @@ internal void InternalTick()
}
}

private static void TickGates()
internal static void TickGates()
{
foreach (var pair in ExMap._gates)
{
Expand Down
10 changes: 8 additions & 2 deletions LabExtended/API/Hints/GlobalHintModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using LabExtended.API.Enums;
using LabExtended.API.Modules;

using LabExtended.Core.Ticking;
using LabExtended.Core.Ticking.Distributors.Unity;
using LabExtended.Core.Ticking.Interfaces;
using LabExtended.Core.Ticking.Timers;

using LabExtended.Extensions;
using LabExtended.Utilities;
Expand All @@ -18,7 +20,11 @@ public class GlobalHintModule : Module

public IReadOnlyList<HintElement> Elements => _activeElements;

public override TickTimer TickTimer { get; } = TickTimer.GetStatic(500f, false, true);
/// <inheritdoc/>
public override Type TickType { get; } = typeof(UnityTickDistributor);

/// <inheritdoc/>
public override ITickTimer TickTimer { get; } = new StaticTickTimer(600);

public override bool ValidateAdd(Module module)
=> base.ValidateAdd(module) && Instance is null;
Expand Down
47 changes: 38 additions & 9 deletions LabExtended/API/Modules/Module.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LabExtended.Core.Ticking;
using LabExtended.Core.Ticking.Interfaces;
using LabExtended.Extensions;

namespace LabExtended.API.Modules
Expand All @@ -11,17 +12,37 @@ public class Module
internal readonly Dictionary<Type, Module> _modules = new Dictionary<Type, Module>();
internal readonly HashSet<Type> _cache = new HashSet<Type>();

internal TickInfo _tickHandler;
private TickHandle _handle;

/// <summary>
/// Gets all submodules.
/// </summary>
public IReadOnlyCollection<Module> Modules => _modules.Values;

/// <summary>
/// Gets all cached module types.
/// </summary>
public IReadOnlyCollection<Type> Cache => _cache;

public TickInfo TickInfo => _tickHandler;
/// <summary>
/// Gets the handle for the <see cref="OnTick"/> method.
/// </summary>
public TickHandle TickHandle => _handle;

/// <summary>
/// Gets or sets the options for the <see cref="OnTick"/> method.
/// </summary>
public virtual ITickOptions TickOptions { get; }

/// <summary>
/// Gets or sets the timer for the <see cref="OnTick"/> method.
/// </summary>
public virtual ITickTimer TickTimer { get; }

/// <summary>
/// When overriden, retrieves this module's tick settings.
/// Gets or sets the tick distributor type.
/// </summary>
public virtual TickTimer TickTimer { get; }
public virtual Type TickType { get; }

/// <summary>
/// Gets or sets a value indicating whether or not this module is active.
Expand All @@ -42,8 +63,13 @@ public void StartModule()
{
IsActive = true;

if (TickTimer != null)
_tickHandler = TickManager.SubscribeTick(TickModule, TickTimer);
if (TickType != null)
{
var distributor = TickDistribution.GetDistributor(TickType);
var handle = TickDistribution.CreateWith(TickModule, TickOptions, TickTimer);

_handle = distributor.CreateHandle(handle);
}

OnStarted();
}
Expand All @@ -52,8 +78,11 @@ public void StopModule()
{
IsActive = false;

_tickHandler?.Unsubscribe();
_tickHandler = null;
if (_handle.IsActive)
{
_handle.Destroy();
_handle = default;
}

foreach (var subModule in _modules)
{
Expand Down Expand Up @@ -247,7 +276,7 @@ public virtual bool ValidateAdd(Module module)

internal void TickModule()
{
if (TickTimer is null || TickInfo is null || !IsActive || IsPaused)
if (!IsActive || IsPaused)
return;

OnTick();
Expand Down
2 changes: 1 addition & 1 deletion LabExtended/API/Modules/TransientModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace LabExtended.API.Modules
public class TransientModule : GenericModule<ExPlayer>
{
static TransientModule()
=> TickManager.SubscribeTick(UpdateModules, TickTimer.NoneProfiled, "Transient Modules Tick");
=> TickDistribution.UnityTick.CreateHandle(TickDistribution.CreateWith(UpdateModules));

/// <summary>
/// The reason for a module's removal.
Expand Down
10 changes: 8 additions & 2 deletions LabExtended/API/Npcs/Navigation/NavigationModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Interactables;

using LabExtended.API.Modules;

using LabExtended.Core;
using LabExtended.Core.Ticking;
using LabExtended.Core.Ticking.Distributors.Unity;
using LabExtended.Core.Ticking.Interfaces;
using LabExtended.Core.Ticking.Timers;

using Mirror;

Expand All @@ -29,7 +32,10 @@ public class NavigationModule : Module
public NpcHandler Npc { get; internal set; }

/// <inheritdoc/>
public override TickTimer TickTimer { get; } = TickTimer.GetStatic(50f);
public override ITickTimer TickTimer { get; } = new StaticTickTimer(500);

/// <inheritdoc/>
public override Type TickType { get; } = typeof(UnityTickDistributor);

/// <summary>
/// Whether or not to allow the NPC to interact.
Expand Down
13 changes: 8 additions & 5 deletions LabExtended/API/RemoteAdmin/RemoteAdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

using LabExtended.Core.Hooking;
using LabExtended.Core.Ticking;

using LabExtended.Core.Ticking.Distributors.Unity;
using LabExtended.Core.Ticking.Interfaces;
using LabExtended.Core.Ticking.Timers;
using LabExtended.Events.Player;
using LabExtended.Extensions;
using LabExtended.Patches.Functions.RemoteAdmin;
Expand All @@ -29,15 +31,16 @@ public class RemoteAdminModule : GenericModule<ExPlayer>
private DateTime _lastListRequestTime = DateTime.MinValue;
private bool _wasOpen = false;

public override TickTimer TickTimer { get; } = TickTimer.GetStatic(800f);
/// <inheritdoc/>
public override Type TickType { get; } = typeof(UnityTickDistributor);

/// <inheritdoc/>
public override ITickTimer TickTimer { get; } = new StaticTickTimer(8000);

public bool IsRemoteAdminOpen { get; private set; }

public IReadOnlyList<IRemoteAdminObject> Objects => _objects;

public static int SpaceCount = 0;
public static int TotalLines = 0;

public override void OnStarted()
{
base.OnStarted();
Expand Down
28 changes: 10 additions & 18 deletions LabExtended/API/Voice/Threading/ThreadedVoiceChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using LabExtended.Events;

using LabExtended.Core;
using LabExtended.Core.Ticking;
using LabExtended.Core.Profiling;

using System.Collections.Concurrent;
Expand All @@ -12,13 +11,16 @@
using Mirror;

using LabExtended.API.Pooling;
using LabExtended.Core.Ticking;

namespace LabExtended.API.Voice.Threading
{
public static class ThreadedVoiceChat
{
private static volatile bool m_RunThread;

private static TickHandle _handle;

private static volatile ProfilerMarker m_ProcessMarker;
private static volatile ProfilerMarker m_ReceiveMarker;
private static volatile ProfilerMarker m_OutputMarker;
Expand All @@ -32,13 +34,6 @@ public static class ThreadedVoiceChat

public static bool IsRunning => m_RunThread;

public static void PrintStats()
{
m_ProcessMarker.LogStats();
m_ReceiveMarker.LogStats();
m_OutputMarker.LogStats();
}

public static void Clean()
{
m_ProcessMarker.Clear();
Expand All @@ -51,8 +46,10 @@ public static void Clean()

public static void Dispose()
{
RoundEvents.OnWaitingForPlayers -= InternalClean;
TickManager.OnTick -= ProcessQueue;
RoundEvents.OnWaitingForPlayers -= Clean;

_handle.Destroy();
_handle = default;

m_RunThread = false;

Expand Down Expand Up @@ -102,8 +99,9 @@ public static void Start()
return;
}

RoundEvents.OnWaitingForPlayers += InternalClean;
TickManager.OnTick += ProcessOutput;
RoundEvents.OnWaitingForPlayers += Clean;

_handle = TickDistribution.UnityTick.CreateHandle(TickDistribution.CreateWith(ProcessOutput));

m_RunThread = true;

Expand Down Expand Up @@ -187,12 +185,6 @@ private static void ProcessQueue()
}
}

private static void InternalClean()
{
PrintStats();
Clean();
}

private static void Info(object msg, string segment = null)
=> ApiLoader.Info($"Threaded Voice Chat{(segment != null ? $" / {segment}" : "")}", msg);

Expand Down
Loading

0 comments on commit 3b9e66b

Please sign in to comment.