Killing Floor Mutator - Chat Logging

·3 min. read·

This mutator is based on CorLogMut by Dr_Killjoy.

It utilizes the FileLog class for file read and write operations.

Log file name example: 8007_2022-01-30_14-54_KF-Bank.

Chat line example: 2022.01.30:12-45 Wave=3 Hello World.

File structure:

- /ChatLoggingMut
-- /Classes
--- /ChatLoggingMut.uc
--- /BroadcastHandlerChatLog.uc

ChatLoggingMut.uc:

class ChatLoggingMut extends Mutator;

var config bool bChatLogEnabled;

function PostBeginPlay()
{
  if (bChatLogEnabled)
    Spawn(class'BroadcastHandlerChatLog');
}

defaultproperties
{
	GroupName="KF-ChatLoggingMut"
	FriendlyName="ChatLoggingMut"
	Description="cuzus.net"

  bChatLogEnabled=True
}

BroadcastHandlerChatLog.uc:

class BroadcastHandlerChatLog extends BroadcastHandler;

var FileLog	_fileLogPointer;

var string _fileLogName;
var string _mapName;
var int _serverPort;

function LogStr(string msg)
{
	Log("[DEBUG]" @ Class.Name @ msg);
}

function InitServerRules()
{
	local array<string> parts;

	Split(Level.GetAddressURL(), ":", parts);
	_serverPort = int(parts[1]);
}

function PreBeginPlay()
{
	local BroadcastHandler BH;

	InitServerRules();

	_mapName = GetShortUrl(Level.GetLocalURL());

	_fileLogName = GetLogFileNamePattern();

	if (_fileLogPointer == none)
	{
		_fileLogPointer = Spawn(class'FileLog');
	}

	if (Level.Game.BroadcastHandler.class != Level.Game.default.BroadcastClass)
	{
		LogStr("WARNING: Unexpected broadcast handler `" $ Level.Game.BroadcastHandler.class.name $ "`. Will try to use the original.");

		foreach AllActors(class'BroadcastHandler', BH)
		{
			if (BH.class == Level.Game.default.BroadcastClass)
			{
				LogStr("Found the original broadcast handler " $ BH.class.name);
				BH.RegisterBroadcastHandler(Self);

				break;
			}
		}

		if (BH == none)
		{
			LogStr("Unable to find the original broadcast handler. Will fallback to the current handler and hope it works out.");
			Level.Game.BroadcastHandler.RegisterBroadcastHandler(Self);
		}
	}
	else
	{
		Level.Game.BroadcastHandler.RegisterBroadcastHandler(Self);
	}
}

event Destroyed()
{
	_fileLogPointer.CloseLog();

	super.Destroyed();
}

function string GetLogFileNamePattern()
{
	local string s;

	s = "Chat_" $ string(_serverPort) $ "_";
	s $= string(Level.Year);
	s $= "-";
	s $= GetNumberIn24hFormat(Level.Month);
	s $= "-";
	s $= GetNumberIn24hFormat(Level.Day);
	s $= "_";
	s $= GetNumberIn24hFormat(Level.Hour);
	s $= "-";
	s $= GetNumberIn24hFormat(Level.Minute);
	s $= "_";
	s $= _mapName;

	return s;
}

function string GetChatLinePrefix()
{
	local string s;

	s = string(Level.Year);
	s $= ".";
	s $= GetNumberIn24hFormat(Level.Month);
	s $= ".";
	s $= GetNumberIn24hFormat(Level.Day);
	s $= " ";
	s $= GetNumberIn24hFormat(Level.Hour);
	s $= ":";
	s $= GetNumberIn24hFormat(Level.Minute);
	s $= ":";
	s $= GetNumberIn24hFormat(Level.Second);
	s $= " Wave=";
	s $= string(KFGameType(Level.Game).WaveNum + 1);

	return s;
}

static function string GetNumberIn24hFormat(int num)
{
	local string result;

	result = string(num);

	if (Len(result) == 1)
		result = "0" $ result;

	return result;
}

function BroadcastText(PlayerReplicationInfo SenderPRI, PlayerController Receiver, coerce string Msg, optional name Type)
{
	local string str;

	if (Len(Msg) == 0)
	{
		return;
	}

	if (SenderPRI == none)
	{
		super.BroadcastText(SenderPRI, Receiver, Msg, Type);
		return;
	}

	if (Receiver.PlayerReplicationInfo == SenderPRI)
	{
		_fileLogPointer.OpenLog(_fileLogName);

		str = GetChatLinePrefix();
		str $= " ";
		str $= SenderPRI.PlayerName;
		str $= " ";
		str $= ConvertString(Msg);

		_fileLogPointer.LogF(str);
		_fileLogPointer.CloseLog();
	}

	super.BroadcastText(SenderPRI, Receiver, Msg, Type);
}

function string ConvertString(string msg)
{
	local int i;
	local string tmp;
	local string result;
	local int code;

	tmp = msg;

	for (i = 0; i < Len(msg); i++)
	{
		code = Asc(tmp);

		if (code > 848)
			code -= 848;

		if (i == 0)
			result = Chr(code);
		else
			result = result $ Chr(code);

		tmp = Mid(tmp, 1);
	}

	return result;
}

You may also like

How to enable HTTPS on localhost for Node.js

Sometimes, you may need to secure your localhost to test certain things, such... Read more
·2 min. read

How to get GIT repository URL from source folder

There are several ways to get the link to a GIT repository from the directory... Read more
·1 min. read

Developer mode in Lineage 2

To switch to developer mode, you need to edit the file system/l2.ini, or better... Read more
·3 min. read

© geekrainian.com.