aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/_custom_codecallbacks.gnut
blob: 4a7f818961ff6622c047b3d3765855ebe0206113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
untyped

global function AddCallback_OnReceivedSayTextMessage
global function NSSetupChathooksServer

global struct ClServer_MessageStruct {
	string message
	entity player
	bool isTeam
	bool shouldBlock
}

struct {
	array< ClServer_MessageStruct functionref( ClServer_MessageStruct ) > OnReceivedSayTextMessageCallbacks
} NsCustomCallbacks

void function CServerGameDLL_ProcessMessageStartThread(int playerIndex, string message, bool isTeam)
{
	thread CServerGameDLL_OnReceivedSayTextMessageCallback(playerIndex, message, isTeam)
}

void function CServerGameDLL_OnReceivedSayTextMessageCallback(int playerIndex, string message, bool isTeam)
{
	entity player = GetPlayerByIndex(playerIndex)
	if (player == null || !player.hasConnected) {
		print("Ignored chat message from invalid player index " + playerIndex + ": " + message)
		return
	}
	print("Received message from " + player + "(" + player.GetUID() + "): " + message)

	ClServer_MessageStruct localMessage
	localMessage.message = message
	localMessage.player = player
	localMessage.isTeam = isTeam
	localMessage.shouldBlock = false
	foreach ( callbackFunc in NsCustomCallbacks.OnReceivedSayTextMessageCallbacks )
	{
		ClServer_MessageStruct returnStruct = callbackFunc(localMessage)
		localMessage.message = returnStruct.message
		localMessage.isTeam = returnStruct.isTeam
		localMessage.shouldBlock = localMessage.shouldBlock || returnStruct.shouldBlock
	}

	if (localMessage.shouldBlock)
	{
		return
	}

	NSSendMessage(player.GetPlayerIndex(), localMessage.message, localMessage.isTeam)
}

void function AddCallback_OnReceivedSayTextMessage( ClServer_MessageStruct functionref (ClServer_MessageStruct) callbackFunc )
{
	NsCustomCallbacks.OnReceivedSayTextMessageCallbacks.append(callbackFunc)
}

void function NSSetupChathooksServer() {
	getroottable().rawset("CServerGameDLL_ProcessMessageStartThread", CServerGameDLL_ProcessMessageStartThread)
}