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
95 changes: 95 additions & 0 deletions Game.Server/EventLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Game.Shared;

using Source.Common;

namespace Game.Server;

public class EventLog : BaseGameSystem, IGameEventListener2
{
public void FireGameEvent(IGameEvent ev) => PrintEvent(ev);

protected virtual bool PrintEvent(IGameEvent ev) {
ReadOnlySpan<char> name = ev.GetName();

if (name.StartsWith("server_"))
return true;
else if (name.StartsWith("player_"))
return PrintPlayerEvent(ev);
else if (name.StartsWith("team_"))
return PrintTeamEvent(ev);
else if (name.StartsWith("game_"))
return PrintGameEvent(ev);
else
return PrintOtherEvent(ev);
}

protected virtual bool PrintGameEvent(IGameEvent ev) => false;

protected virtual bool PrintPlayerEvent(IGameEvent ev) {
ReadOnlySpan<char> eventName = ev.GetName();
int userid = ev.GetInt("userid");

if (eventName.StartsWith("player_connect")) {
ReadOnlySpan<char> name = ev.GetString("name");
ReadOnlySpan<char> address = ev.GetString("address");
ReadOnlySpan<char> networkid = ev.GetString("networkid");
Util.LogPrintf($"\"{name}<{userid}><{networkid}><>\" connected, address \"{address}\"\n");
return true;
}
else if (eventName.StartsWith("player_disconnect")) {
ReadOnlySpan<char> reason = ev.GetString("reason");
ReadOnlySpan<char> name = ev.GetString("name");
ReadOnlySpan<char> networkid = ev.GetString("networkid");
Team? team = Util.PlayerByUserId(userid)?.GetTeam();
ReadOnlySpan<char> teamName = team != null ? team.GetName() : "";
Util.LogPrintf($"\"{name}<{userid}><{networkid}><{teamName}>\" disconnected (reason \"{reason}\")\n");
return true;
}

BasePlayer? player = Util.PlayerByUserId(userid);
if (player == null) {
DevMsg($"CEventLog::PrintPlayerEvent: Failed to find player (userid: {userid}, event: {eventName})\n");
return false;
}

if (eventName.StartsWith("player_team") || eventName.StartsWith("player_death")) {
#if GMOD_DLL
return false;
#else
// todo: joined team, killed and committed suicide lines
return false;
#endif
}
else if (eventName.StartsWith("player_activate")) {
Util.LogPrintf($"\"{player.GetPlayerName()}<{userid}><{player.GetNetworkIDString()}><>\" entered the game\n");
return true;
}
else if (eventName.StartsWith("player_changename")) {
ReadOnlySpan<char> newName = ev.GetString("newname");
ReadOnlySpan<char> oldName = ev.GetString("oldname");
Team? team = player.GetTeam();
ReadOnlySpan<char> teamName = team != null ? team.GetName() : "";
Util.LogPrintf($"\"{oldName}<{userid}><{player.GetNetworkIDString()}><{teamName}>\" changed name to \"{newName}\"\n");
return true;
}

return false;
}

protected virtual bool PrintTeamEvent(IGameEvent ev) => false;

protected virtual bool PrintOtherEvent(IGameEvent ev) => false;

public override bool Init() {
gameeventmanager.AddListener(this, "player_changename", true);
gameeventmanager.AddListener(this, "player_activate", true);
gameeventmanager.AddListener(this, "player_death", true);
gameeventmanager.AddListener(this, "player_team", true);
gameeventmanager.AddListener(this, "player_disconnect", true);
gameeventmanager.AddListener(this, "player_connect", true);

return true;
}

public override void Shutdown() => gameeventmanager.RemoveListener(this);
}
1 change: 1 addition & 0 deletions Game.Server/GameInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ public bool DLLInit(IServiceProvider services) {
#endif

IGameSystem.Add(PhysicsGameSystem());
IGameSystem.Add(GameLogSystem());
IGameSystem.Add(g_SoundEmitterSystem);

if (!IGameSystem.InitAllSystems())
Expand Down
30 changes: 30 additions & 0 deletions Game.Server/HL2/HL2EventLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
global using static Game.Server.HL2.HL2EventLogGlobals;

using Game.Shared;

using Source.Common;

namespace Game.Server.HL2;

public static class HL2EventLogGlobals
{
static readonly HL2EventLog s_HL2EventLog = new();
public static IGameSystem GameLogSystem() => s_HL2EventLog;
}

public class HL2EventLog : EventLog
{
public override ReadOnlySpan<char> Name() => "CHL2EventLog";

protected override bool PrintEvent(IGameEvent ev) {
if (base.PrintEvent(ev))
return true;

if (ev.GetName().SequenceEqual("hl2_"))
return PrintHL2Event(ev);

return false;
}

protected virtual bool PrintHL2Event(IGameEvent ev) => false;
}
2 changes: 2 additions & 0 deletions Source.Common/SteamAppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public static ref readonly SteamAppInfo GetSteamInfIDVersionInfo() {
return ref info;
}

#if !GMOD_DLL
public static int build_number() => GetSteamInfIDVersionInfo().ServerVersion;
#endif
}

public struct SteamAppInfo
Expand Down
33 changes: 33 additions & 0 deletions Source.Engine/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public void InitFilesystem(ReadOnlySpan<char> fullModPath) {
FileSystem.LoadSearchPaths(ref initInfo);
Common.Gamedir = Path.Combine(AppContext.BaseDirectory, initInfo.ModPath ?? throw new Exception("Mod path null"));

SetupLogDir(null);

}

public bool Initialized { get; private set; }
Expand Down Expand Up @@ -200,6 +202,37 @@ static ReadOnlySpan<byte> Parse(ReadOnlySpan<byte> data) {
return data;
}

const int MAX_LOG_DIRECTORIES = 10000;
static bool logPathSetup;

public static void SetupLogDir(ReadOnlySpan<char> mapname) {
g_pFileSystem.RemoveSearchPath(ReadOnlySpan<char>.Empty, "LOGDIR");

if (!mapname.IsEmpty && Singleton<Source.Common.Commands.ICommandLine>().CheckParm("-uselogdir")) {
int i;
string relativeLogDir = "";
for (i = 0; i < MAX_LOG_DIRECTORIES; i++) {
relativeLogDir = $"logs/{mapname}/{i:D4}";
if (!g_pFileSystem.IsDirectory(relativeLogDir, "GAME"))
break;
}

if (i == MAX_LOG_DIRECTORIES)
relativeLogDir = $"logs/{mapname}/{0:D4}";

g_pFileSystem.CreateDirHierarchy(relativeLogDir, "GAME");

if (!logPathSetup) {
logPathSetup = true;
g_pFileSystem.AddSearchPath($"{Gamedir}/{relativeLogDir}", "LOGDIR");
}
}
else
g_pFileSystem.AddSearchPath(Gamedir, "LOGDIR");

g_pFileSystem.MarkPathIDByRequestOnly("LOGDIR", true);
}

public static void CreatePath(ReadOnlySpan<char> path) {
Span<char> tempPath = stackalloc char[1024];
strcpy(tempPath, path);
Expand Down
2 changes: 1 addition & 1 deletion Source.Engine/EngineServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public bool LockNetworkStringTables(bool shouldLock) {
}

public void LogPrint(ReadOnlySpan<char> msg) {
// todo
g_Log.Print(msg);
}

int Message_CheckMessageLength() {
Expand Down
9 changes: 8 additions & 1 deletion Source.Engine/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ void _RunFrame(TimeUnit_t time) {
else {
throw new NotImplementedException("Threaded Host.RunFrame path is not implemented yet");
}
#endif

g_Log.RunFrame();

#if !SWDS
if (shouldRender) {
_RunFrame_Render();
_RunFrame_Sound();
Expand Down Expand Up @@ -815,6 +819,8 @@ public void Init(bool dedicated) {

AllowQueuedMaterialSystem(false);

g_Log.Init();

Initialized = true;
hostState.Init();

Expand Down Expand Up @@ -861,7 +867,7 @@ public void Shutdown() {


// HLTV.Shutdown();
// Log.Shutdown();
g_Log.Shutdown();
// GameEventManager.Shutdown();

sv.Shutdown();
Expand Down Expand Up @@ -1605,6 +1611,7 @@ public void ShutdownServer() {
g_GameEventManager.FireEvent(ev);
}

g_Log.Close();

GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive);
Expand Down
8 changes: 6 additions & 2 deletions Source.Engine/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ public NetScratchBuffer ObtainScratchBuffer() {
return res;
}

public static void OutOfBandPrint(NetSocketType sock, NetAddress adr, ReadOnlySpan<char> text){
// todo
public static void OutOfBandPrint(NetSocketType sock, NetAddress? adr, ReadOnlySpan<char> text) {
byte[] buffer = new byte[MAX_ROUTABLE_PAYLOAD];
bf_write msg = new(buffer, buffer.Length);
msg.WriteLong(CONNECTIONLESS_HEADER);
msg.WriteString(text, limit: MAX_ROUTABLE_PAYLOAD - 5);
Singleton<Net>().SendPacket(null!, sock, adr, msg.GetData(), msg.BytesWritten);
}

public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPoint? ep) {
Expand Down
2 changes: 1 addition & 1 deletion Source.Engine/SV_Redirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void RedirectFlush() {
bInFlush = true;
switch(Redirected){
case RedirectType.Packet:
Net.OutOfBandPrint(sv.Socket, RedirectTo, $"{A2A.Print}{RedirectBuffer.SliceNullTerminatedString()}");
Net.OutOfBandPrint(sv.Socket, RedirectTo, $"{(char)A2A.Print}{RedirectBuffer.SliceNullTerminatedString()}");
break;
case RedirectType.Client:
Host.Client!.ClientPrintf(RedirectBuffer.SliceNullTerminatedString());
Expand Down
6 changes: 6 additions & 0 deletions Source.Engine/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,12 @@ internal bool SpawnServer(ReadOnlySpan<char> mapName, ReadOnlySpan<char> mapFile
#if !SWDS
EngineVGui().UpdateProgressBar(LevelLoadingProgress.SpawnServer);
#endif
Common.SetupLogDir(mapName);

g_Log.Open();
g_Log.Print($"Loading map \"{mapName}\"\n");
g_Log.PrintServerVars();

#if !SWDS
Scr.CenterStringOff();
#endif
Expand Down
2 changes: 2 additions & 0 deletions Source.Engine/Server/SteamAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ void OnValidateAuthTicketResponse(ValidateAuthTicketResponse_t validateAuthTicke
sprintf(msg, "\"%s<%i><%s><>\" STEAM USERID validated\n").S(client.GetClientName()).I(client.GetUserID()).S(client.GetNetworkIDString());

DevMsg(msg);
g_Log.Print(msg.SliceNullTerminatedString());

SV.ServerGameClients!.NetworkIDValidated(client.GetClientName(), client.GetNetworkIDString());
}

Expand Down
4 changes: 4 additions & 0 deletions Source.Engine/SourceDLLMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public static class SourceDllMain
[Dependency] public static Cbuf cbuf { get; private set; } = null!;
[Dependency] public static ICvar cvar { get; private set; } = null!;
[Dependency] public static Cmd cmd { get; private set; } = null!;
[Dependency] public static Log g_Log { get; private set; } = null!;
#if GMOD_DLL
public static int build_number() => (int)Sys.BuildNumber();
#endif
[Dependency] public static IMaterialSystemHardwareConfig HardwareConfig { get; private set; } = null!;
[Dependency] public static ICommandLine commandLine { get; private set; } = null!;
[KeyedDependency(Key = Realm.Server)] public static NetworkStringTableContainer networkStringTableContainerServer { get; private set; } = null!;
Expand Down
Loading
Loading