aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/_custom_codecallbacks.gnut
blob: ee21116d98402f32d982b25557856acca2586f72 (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
untyped

global function AddCallback_OnReceivedSayTextMessage

// this is global due to squirrel bridge v3 making native not be able to find non-global funcs properly
// temp fix (surely it will get replaced), do not use this function please
global function CServerGameDLL_ProcessMessageStartThread

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)
}