Skip to content
Merged
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
18 changes: 18 additions & 0 deletions TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
_hookManager = hookManager;

HookEvents.Terraria.Main.startDedInput += Main_startDedInput;
HookEvents.Terraria.Main.ReadLineInput += Main_ReadLineInput;
HookEvents.Terraria.RemoteClient.Reset += RemoteClient_Reset;
Hooks.Main.CommandProcess += OnProcess;
}

static void Main_startDedInput(object? sender, HookEvents.Terraria.Main.startDedInputEventArgs args)

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / MacOS

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / MacOS

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / Windows

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 26 in TerrariaServerAPI/TerrariaApi.Server/Hooking/ServerHooks.cs

View workflow job for this annotation

GitHub Actions / Windows

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (!args.ContinueExecution) return;
args.ContinueExecution = false;
Expand All @@ -36,6 +37,23 @@
args.OriginalMethod();
}

#nullable enable
/// <summary>
/// Hooks the default ReadLineInput method to add a small delay when Console.ReadLine() returns null.
/// For example, some docker instances may experience a 100% CPU usage due to this vanilla thread implementation.
/// </summary>
static void Main_ReadLineInput(object? sender, HookEvents.Terraria.Main.ReadLineInputEventArgs args)
{
args.ContinueExecution = false;

string? text;
while ((text = Console.ReadLine()) is null)
System.Threading.Thread.Sleep(100);

args.HookReturnValue = text;
}
#nullable disable

static void OnProcess(object sender, Hooks.Main.CommandProcessEventArgs e)
{
if (e.Result == HookResult.Cancel)
Expand Down
Loading