Skip to content
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
1 change: 1 addition & 0 deletions apps/counterstrikesharp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ ENV DEFAULT_MAP="de_inferno"
ENV STEAM_RELAY="false"

ENV SERVER_TYPE="Ranked"
ENV HIBERNATE_WHEN_EMPTY="false"

ENV METAMOD_URL=https://github.com/alliedmodders/metamod-source/releases/download/2.0.0.1473/mmsource-2.0.0-git1473-linux.tar.gz
ENV COUNTER_STRIKE_SHARP_URL=https://github.com/roflmuffin/CounterStrikeSharp/releases/download/v1.0.376/counterstrikesharp-with-runtime-linux-1.0.376.zip
Expand Down
3 changes: 3 additions & 0 deletions apps/counterstrikesharp/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ echo "---Create Symbolic Links---"

if [ "$SERVER_TYPE" = "Ranked" ]; then
cp "/opt/server-cfg/ranked.server.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg/server.cfg"
if [ "$HIBERNATE_WHEN_EMPTY" = "true" ]; then
sed -i 's/^sv_hibernate_when_empty.*/sv_hibernate_when_empty 1/' "$INSTANCE_SERVER_DIR/game/csgo/cfg/server.cfg"
fi
cp "/opt/server-cfg/5stack.competitive.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
cp "/opt/server-cfg/5stack.duel.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
cp "/opt/server-cfg/5stack.knife.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
Expand Down
3 changes: 2 additions & 1 deletion apps/counterstrikesharp/src/FiveStack.Events/MapChange.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using FiveStack.Enums;
using FiveStack.Utilities;
using Microsoft.Extensions.Logging;

namespace FiveStack;
Expand All @@ -19,7 +20,7 @@ public void ListenForMapChange()

// code smell: we have to wait till server exec's default cfgs
await Task.Delay(1000 * 5);
Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
MatchManager? match = _matchService.GetCurrentMatch();
match?.SyncActiveMapAfterMapStart();
Expand Down
4 changes: 2 additions & 2 deletions apps/counterstrikesharp/src/FiveStack.Services/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ MatchService matchService

public void SendCommands(string[] commands)
{
Server.NextFrame(() => Server.ExecuteCommand(string.Join(";", commands)));
HibernationUtility.NextFrame(() => Server.ExecuteCommand(string.Join(";", commands)));
}

public void Message(
Expand Down Expand Up @@ -88,7 +88,7 @@ public void Ping(string pluginVersion)
string? serverId = _environmentService.GetServerId();
string? apiPassword = _environmentService.GetServerApiPassword();

Server.NextFrame(async () =>
Server.NextWorldUpdate(async () =>
{
if (serverId == null || apiPassword == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public void SetupMatch(MatchData match)
ApplyWorkshopBlockedCvars();
}

Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
if (!wasAlreadySetup)
{
Expand Down Expand Up @@ -866,7 +866,7 @@ private void StartWarmup()
return;
}

Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
bool isInWarmup = MatchUtility.Rules()?.WarmupPeriod ?? false;

Expand Down Expand Up @@ -1581,7 +1581,7 @@ public void SetupBroadcast()
.Find("tv_broadcast_origin_auth")
?.SetValue($"{_matchData.id}:{_matchData.password}");

Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
_gameServer.SendCommands(["tv_broadcast 1"]);
_logger.LogInformation("Sending TV Broadcast command");
Expand Down
44 changes: 42 additions & 2 deletions apps/counterstrikesharp/src/FiveStack.Services/MatchService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Cvars;
using FiveStack.Entities;
using FiveStack.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -89,7 +90,7 @@ public async void GetMatchFromApi()
if (serverId == null || apiPassword == null)
{
await Task.Delay(1000 * 5);
Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
GetMatchFromApi();
});
Expand All @@ -113,7 +114,7 @@ public async void GetMatchFromApi()
httpResponse.EnsureSuccessStatusCode();
string? response = await httpResponse.Content.ReadAsStringAsync();

Server.NextFrame(() =>
HibernationUtility.NextFrame(() =>
{
Guid? previousMatchId = _currentMatch?.GetMatchData()?.id;

Expand All @@ -126,6 +127,8 @@ public async void GetMatchFromApi()

_currentMatch = null;

StayAwake(false);

_logger.LogWarning(
$"No match assigned — clearing match {previousMatchId?.ToString() ?? "none"}"
);
Expand All @@ -139,6 +142,8 @@ public async void GetMatchFromApi()
return;
}

StayAwake(true);

if (previousMatchId == matchData.id)
{
_logger.LogInformation(
Expand Down Expand Up @@ -178,6 +183,41 @@ public async void GetMatchFromApi()
}
}

private bool _heldAwake;

// A hibernating server runs no frames, so timers and the end-of-map work
// would stall once everyone left. It only hibernates while it has nothing
// to do: it is held awake from the moment it is given a match until that
// match is gone.
public void StayAwake(bool awake)
{
ConVar? hibernate = ConVar.Find("sv_hibernate_when_empty");

if (hibernate == null)
{
return;
}

if (awake)
{
if (hibernate.GetPrimitiveValue<bool>())
{
hibernate.SetValue(false);
_heldAwake = true;
_logger.LogInformation("holding the server awake for its match");
}

return;
}

if (_heldAwake)
{
hibernate.SetValue(true);
_heldAwake = false;
_logger.LogInformation("no match left, the server may hibernate again");
}
}

public void GetMatchFromOffline()
{
string? offlineMatchData = _environmentService.GetOfflineMatchData();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CounterStrikeSharp.API;

namespace FiveStack.Utilities;

// A hibernating server stops running frames but keeps updating the world, and
// it only wakes once somebody is let in. Whatever has to be in place before
// that (the match, its password, the map) is queued through here: on the next
// frame while the server is awake, exactly as before, and on the next world
// update while it hibernates.
public static class HibernationUtility
{
private static volatile bool _hibernating;

public static void SetHibernating(bool hibernating)
{
_hibernating = hibernating;
}

public static void NextFrame(Action callback)
{
if (_hibernating)
{
Server.NextWorldUpdate(callback);
return;
}

Server.NextFrame(callback);
}
}
2 changes: 2 additions & 0 deletions apps/counterstrikesharp/src/FiveStackPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public override void Load(bool hotReload)
);

RegisterListener<Listeners.OnTick>(_rankSystem.OnTick);
RegisterListener<Listeners.OnServerHibernationUpdate>(HibernationUtility.SetHibernating);

TimerUtility.AddTimer(
RankSystem.RosterRefreshInterval,
Expand Down Expand Up @@ -140,6 +141,7 @@ public override void Unload(bool hotReload)
);

_matchService.GetCurrentMatch()?.Reset();
_matchService.StayAwake(false);

_ = _matchEvents.Disconnect();
}
Expand Down
1 change: 1 addition & 0 deletions apps/swiftly/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ ENV DEFAULT_MAP="de_inferno"
ENV STEAM_RELAY="false"

ENV SERVER_TYPE="Ranked"
ENV HIBERNATE_WHEN_EMPTY="false"

ENV SWIFTLYS2_VERSION="v1.4.13"
ENV SWIFTLYS2_URL=https://github.com/swiftly-solution/swiftlys2/releases/download/${SWIFTLYS2_VERSION}/swiftlys2-linux-${SWIFTLYS2_VERSION}-with-runtimes.zip
Expand Down
3 changes: 3 additions & 0 deletions apps/swiftly/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ echo "---Create Symbolic Links---"

if [ "$SERVER_TYPE" = "Ranked" ]; then
cp "/opt/server-cfg/ranked.server.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg/server.cfg"
if [ "$HIBERNATE_WHEN_EMPTY" = "true" ]; then
sed -i 's/^sv_hibernate_when_empty.*/sv_hibernate_when_empty 1/' "$INSTANCE_SERVER_DIR/game/csgo/cfg/server.cfg"
fi
cp "/opt/server-cfg/5stack.base.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
cp "/opt/server-cfg/5stack.competitive.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
cp "/opt/server-cfg/5stack.duel.cfg" "$INSTANCE_SERVER_DIR/game/csgo/cfg"
Expand Down
2 changes: 1 addition & 1 deletion apps/swiftly/src/FiveStack.Events/MapChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async void OnMapLoad(IOnMapLoadEvent @event)
_logger.LogInformation("map changed: precaching models");

await Task.Delay(1000 * 5);
_core.Scheduler.NextTick(() =>
FiveStack.Utilities.HibernationUtility.NextTick(() =>
{
MatchManager? match = _matchService.GetCurrentMatch();
match?.SyncActiveMapAfterMapStart();
Expand Down
4 changes: 2 additions & 2 deletions apps/swiftly/src/FiveStack.Services/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void SendCommands(string[] commands)
);
}

_core.Scheduler.NextTick(() => _core.Engine.ExecuteCommand(string.Join(";", commands)));
HibernationUtility.NextTick(() => _core.Engine.ExecuteCommand(string.Join(";", commands)));
}

public void Message(
Expand Down Expand Up @@ -108,7 +108,7 @@ public void Ping(string pluginVersion)
string? serverId = _environmentService.GetServerId();
string? apiPassword = _environmentService.GetServerApiPassword();

_core.Scheduler.NextTick(() =>
_core.Scheduler.NextWorldUpdate(() =>
{
if (serverId == null || apiPassword == null)
{
Expand Down
6 changes: 3 additions & 3 deletions apps/swiftly/src/FiveStack.Services/MatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public void SetupMatch(MatchData match)
ApplyWorkshopBlockedCvars();
}

_core.Scheduler.NextTick(() =>
HibernationUtility.NextTick(() =>
{
if (!wasAlreadySetup)
{
Expand Down Expand Up @@ -859,7 +859,7 @@ private void StartWarmup()
return;
}

_core.Scheduler.NextTick(() =>
HibernationUtility.NextTick(() =>
{
bool isInWarmup = MatchUtility.Rules()?.WarmupPeriod ?? false;

Expand Down Expand Up @@ -1572,7 +1572,7 @@ public void SetupBroadcast()
SetConVar("tv_broadcast_url", $"{_environmentService.GetRelayUrl()}/{_matchData.id}");
SetConVar("tv_broadcast_origin_auth", $"{_matchData.id}:{_matchData.password}");

_core.Scheduler.NextTick(() =>
HibernationUtility.NextTick(() =>
{
_gameServer.SendCommands(["tv_broadcast 1"]);
_logger.LogInformation("Sending TV Broadcast command");
Expand Down
51 changes: 49 additions & 2 deletions apps/swiftly/src/FiveStack.Services/MatchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async void GetMatchFromApi()
if (serverId == null || apiPassword == null)
{
await Task.Delay(1000 * 5);
_core.Scheduler.NextTick(() =>
HibernationUtility.NextTick(() =>
{
GetMatchFromApi();
});
Expand All @@ -113,7 +113,7 @@ public async void GetMatchFromApi()
httpResponse.EnsureSuccessStatusCode();
string? response = await httpResponse.Content.ReadAsStringAsync();

_core.Scheduler.NextTick(() =>
HibernationUtility.NextTick(() =>
{
Guid? previousMatchId = _currentMatch?.GetMatchData()?.id;

Expand All @@ -126,6 +126,8 @@ public async void GetMatchFromApi()

_currentMatch = null;

StayAwake(false);

_logger.LogWarning(
$"No match assigned — clearing match {previousMatchId?.ToString() ?? "none"}"
);
Expand All @@ -139,6 +141,8 @@ public async void GetMatchFromApi()
return;
}

StayAwake(true);

if (previousMatchId == matchData.id)
{
_logger.LogInformation(
Expand Down Expand Up @@ -178,6 +182,49 @@ public async void GetMatchFromApi()
}
}

private bool _heldAwake;

// A hibernating server runs no ticks, so timers and the end-of-map work
// would stall once everyone left. It only hibernates while it has nothing
// to do: it is held awake from the moment it is given a match until that
// match is gone.
public void StayAwake(bool awake)
{
try
{
var hibernate = _core.ConVar.Find<bool>("sv_hibernate_when_empty");

if (hibernate == null)
{
return;
}

if (awake)
{
if (hibernate.Value)
{
// SetInternal applies now; a queued set waits for a tick
hibernate.SetInternal(false);
_heldAwake = true;
_logger.LogInformation("holding the server awake for its match");
}

return;
}

if (_heldAwake)
{
hibernate.SetInternal(true);
_heldAwake = false;
_logger.LogInformation("no match left, the server may hibernate again");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "unable to change sv_hibernate_when_empty");
}
}

public void GetMatchFromOffline()
{
string? offlineMatchData = _environmentService.GetOfflineMatchData();
Expand Down
Loading
Loading