aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/plugins.cpp
blob: e6089f3bf6ef1c6284de6f4b24916795d9e0b058 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "pch.h"
#include "squirrel.h"
#include "plugins.h"
#include "masterserver.h"
#include "convar.h"
#include "serverpresence.h"

#include <chrono>
#include <windows.h>

/// <summary>
/// The data is split into two different representations: one for internal, and one for plugins, for thread safety reasons
/// The struct exposed to plugins contains getter functions for the various data types.
/// We can safely use C++ types like std::string here since these are only ever handled by Northstar internally
/// </summary>
struct InternalGameState
{
	int ourScore;
	int secondHighestScore;
	int highestScore;

	bool connected;
	bool loading;
	std::string map;
	std::string mapDisplayName;
	std::string playlist;
	std::string playlistDisplayName;
	int players;
};
struct InternalServerInfo
{
	std::string id;
	std::string name;
	std::string description;
	std::string password;
	int maxPlayers;
	bool roundBased;
	int scoreLimit;
	int endTime;
};
// TODO: need to extend this to include current player data like loadouts
struct InternalPlayerInfo
{
	int uid;
};

InternalGameState gameState;
InternalServerInfo serverInfo;
InternalPlayerInfo playerInfo;

GameState gameStateExport;
ServerInfo serverInfoExport;
PlayerInfo playerInfoExport;

/// <summary>
/// We use SRW Locks because plugins will often be running their own thread
/// To ensure thread safety, and to make it difficult to fuck up, we force them to use *our* functions to get data
/// </summary>
static SRWLOCK gameStateLock;
static SRWLOCK serverInfoLock;
static SRWLOCK playerInfoLock;

void* getPluginObject(PluginObject var)
{
	switch (var)
	{
	case PluginObject::GAMESTATE:
		return &gameStateExport;
	case PluginObject::SERVERINFO:
		return &serverInfoExport;
	case PluginObject::PLAYERINFO:
		return &playerInfoExport;
	default:
		return (void*)-1;
	}
}

void initGameState()
{
	// Initalize the Slim Reader / Writer locks
	InitializeSRWLock(&gameStateLock);
	InitializeSRWLock(&serverInfoLock);
	InitializeSRWLock(&playerInfoLock);

	gameStateExport.getGameStateChar = &getGameStateChar;
	gameStateExport.getGameStateInt = &getGameStateInt;
	gameStateExport.getGameStateBool = &getGameStateBool;

	serverInfoExport.getServerInfoChar = &getServerInfoChar;
	serverInfoExport.getServerInfoInt = &getServerInfoInt;
	serverInfoExport.getServerInfoBool = &getServerInfoBool;

	playerInfoExport.getPlayerInfoChar = &getPlayerInfoChar;
	playerInfoExport.getPlayerInfoInt = &getPlayerInfoInt;
	playerInfoExport.getPlayerInfoBool = &getPlayerInfoBool;

	serverInfo.id = "";
	serverInfo.name = "";
	serverInfo.description = "";
	serverInfo.password = "";
	serverInfo.maxPlayers = 0;
	gameState.connected = false;
	gameState.loading = false;
	gameState.map = "";
	gameState.mapDisplayName = "";
	gameState.playlist = "";
	gameState.playlistDisplayName = "";
	gameState.players = 0;

	playerInfo.uid = 123;
}

// string gamemode, string gamemodeName, string map, string mapName, bool connected, bool loading
SQRESULT SQ_UpdateGameStateUI(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&gameStateLock);
	gameState.map = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 1);
	gameState.mapDisplayName = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 2);
	gameState.playlist = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 3);
	gameState.playlistDisplayName = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 4);
	gameState.connected = g_pSquirrel<ScriptContext::UI>->getbool(sqvm, 5);
	gameState.loading = g_pSquirrel<ScriptContext::UI>->getbool(sqvm, 6);
	ReleaseSRWLockExclusive(&gameStateLock);
	return SQRESULT_NOTNULL;
}

// int playerCount, int outScore, int secondHighestScore, int highestScore, bool roundBased, int scoreLimit
SQRESULT SQ_UpdateGameStateClient(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&gameStateLock);
	AcquireSRWLockExclusive(&serverInfoLock);
	gameState.players = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 1);
	serverInfo.maxPlayers = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 2);
	gameState.ourScore = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 3);
	gameState.secondHighestScore = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 4);
	gameState.highestScore = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 5);
	serverInfo.roundBased = g_pSquirrel<ScriptContext::CLIENT>->getbool(sqvm, 6);
	serverInfo.scoreLimit = g_pSquirrel<ScriptContext::CLIENT>->getbool(sqvm, 7);
	ReleaseSRWLockExclusive(&gameStateLock);
	ReleaseSRWLockExclusive(&serverInfoLock);
	return SQRESULT_NOTNULL;
}

// string id, string name, string password, int players, int maxPlayers, string map, string mapDisplayName, string playlist, string
// playlistDisplayName
SQRESULT SQ_UpdateServerInfo(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&gameStateLock);
	AcquireSRWLockExclusive(&serverInfoLock);
	serverInfo.id = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 1);
	serverInfo.name = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 2);
	serverInfo.password = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 3);
	gameState.players = g_pSquirrel<ScriptContext::UI>->getinteger(sqvm, 4);
	serverInfo.maxPlayers = g_pSquirrel<ScriptContext::UI>->getinteger(sqvm, 5);
	gameState.map = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 6);
	gameState.mapDisplayName = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 7);
	gameState.playlist = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 8);
	gameState.playlistDisplayName = g_pSquirrel<ScriptContext::UI>->getstring(sqvm, 9);
	ReleaseSRWLockExclusive(&gameStateLock);
	ReleaseSRWLockExclusive(&serverInfoLock);
	return SQRESULT_NOTNULL;
}

// int maxPlayers
SQRESULT SQ_UpdateServerInfoBetweenRounds(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&serverInfoLock);
	serverInfo.id = g_pSquirrel<ScriptContext::CLIENT>->getstring(sqvm, 1);
	serverInfo.name = g_pSquirrel<ScriptContext::CLIENT>->getstring(sqvm, 2);
	serverInfo.password = g_pSquirrel<ScriptContext::CLIENT>->getstring(sqvm, 3);
	serverInfo.maxPlayers = g_pSquirrel<ScriptContext::CLIENT>->getinteger(sqvm, 4);
	ReleaseSRWLockExclusive(&serverInfoLock);
	return SQRESULT_NOTNULL;
}

// float timeInFuture
SQRESULT SQ_UpdateTimeInfo(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&serverInfoLock);
	serverInfo.endTime = ceil(g_pSquirrel<ScriptContext::CLIENT>->getfloat(sqvm, 1));
	ReleaseSRWLockExclusive(&serverInfoLock);
	return SQRESULT_NOTNULL;
}

// bool loading
SQRESULT SQ_SetConnected(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&gameStateLock);
	gameState.loading = g_pSquirrel<ScriptContext::UI>->getbool(sqvm, 1);
	ReleaseSRWLockExclusive(&gameStateLock);
	return SQRESULT_NOTNULL;
}

SQRESULT SQ_UpdateListenServer(HSquirrelVM* sqvm)
{
	AcquireSRWLockExclusive(&serverInfoLock);
	serverInfo.id = g_pMasterServerManager->m_sOwnServerId;
	serverInfo.password = "";// g_pServerPresence->Cvar_ns_server_password->GetString(); todo this fr
	ReleaseSRWLockExclusive(&serverInfoLock);
	return SQRESULT_NOTNULL;
}

int getServerInfoChar(char* out_buf, size_t out_buf_len, ServerInfoType var)
{
	AcquireSRWLockShared(&serverInfoLock);
	int n = 0;
	switch (var)
	{
	case ServerInfoType::id:
		strncpy(out_buf, serverInfo.id.c_str(), out_buf_len);
		break;
	case ServerInfoType::name:
		strncpy(out_buf, serverInfo.name.c_str(), out_buf_len);
		break;
	case ServerInfoType::description:
		strncpy(out_buf, serverInfo.id.c_str(), out_buf_len);
		break;
	case ServerInfoType::password:
		strncpy(out_buf, serverInfo.password.c_str(), out_buf_len);
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&serverInfoLock);

	return n;
}
int getServerInfoInt(int* out_ptr, ServerInfoType var)
{
	AcquireSRWLockShared(&serverInfoLock);
	int n = 0;
	switch (var)
	{
	case ServerInfoType::maxPlayers:
		*out_ptr = serverInfo.maxPlayers;
		break;
	case ServerInfoType::scoreLimit:
		*out_ptr = serverInfo.scoreLimit;
		break;
	case ServerInfoType::endTime:
		*out_ptr = serverInfo.endTime;
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&serverInfoLock);

	return n;
}
int getServerInfoBool(bool* out_ptr, ServerInfoType var)
{
	AcquireSRWLockShared(&serverInfoLock);
	int n = 0;
	switch (var)
	{
	case ServerInfoType::roundBased:
		*out_ptr = serverInfo.roundBased;
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&serverInfoLock);

	return n;
}

int getGameStateChar(char* out_buf, size_t out_buf_len, GameStateInfoType var)
{
	AcquireSRWLockShared(&gameStateLock);
	int n = 0;
	switch (var)
	{
	case GameStateInfoType::map:
		strncpy(out_buf, gameState.map.c_str(), out_buf_len);
		break;
	case GameStateInfoType::mapDisplayName:
		strncpy(out_buf, gameState.mapDisplayName.c_str(), out_buf_len);
		break;
	case GameStateInfoType::playlist:
		strncpy(out_buf, gameState.playlist.c_str(), out_buf_len);
		break;
	case GameStateInfoType::playlistDisplayName:
		strncpy(out_buf, gameState.playlistDisplayName.c_str(), out_buf_len);
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&gameStateLock);

	return n;
}
int getGameStateInt(int* out_ptr, GameStateInfoType var)
{
	AcquireSRWLockShared(&gameStateLock);
	int n = 0;
	switch (var)
	{
	case GameStateInfoType::ourScore:
		*out_ptr = gameState.ourScore;
		break;
	case GameStateInfoType::secondHighestScore:
		*out_ptr = gameState.secondHighestScore;
		break;
	case GameStateInfoType::highestScore:
		*out_ptr = gameState.highestScore;
		break;
	case GameStateInfoType::players:
		*out_ptr = gameState.players;
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&gameStateLock);

	return n;
}
int getGameStateBool(bool* out_ptr, GameStateInfoType var)
{
	AcquireSRWLockShared(&gameStateLock);
	int n = 0;
	switch (var)
	{
	case GameStateInfoType::connected:
		*out_ptr = gameState.connected;
		break;
	case GameStateInfoType::loading:
		*out_ptr = gameState.loading;
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&gameStateLock);

	return n;
}

int getPlayerInfoChar(char* out_buf, size_t out_buf_len, PlayerInfoType var)
{
	AcquireSRWLockShared(&playerInfoLock);
	int n = 0;
	switch (var)
	{
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&playerInfoLock);

	return n;
}
int getPlayerInfoInt(int* out_ptr, PlayerInfoType var)
{
	AcquireSRWLockShared(&playerInfoLock);
	int n = 0;
	switch (var)
	{
	case PlayerInfoType::uid:
		*out_ptr = playerInfo.uid;
		break;
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&playerInfoLock);

	return n;
}
int getPlayerInfoBool(bool* out_ptr, PlayerInfoType var)
{
	AcquireSRWLockShared(&playerInfoLock);
	int n = 0;
	switch (var)
	{
	default:
		n = -1;
	}

	ReleaseSRWLockShared(&playerInfoLock);

	return n;
}

ON_DLL_LOAD_CLIENT_RELIESON("client.dll", PluginCommands, ClientSquirrel, (CModule module))
{
	// i swear there's a way to make this not have be run in 2 contexts but i can't figure it out
	// some funcs i need are just not available in UI or CLIENT

	if (g_pSquirrel<ScriptContext::UI> && g_pSquirrel<ScriptContext::CLIENT>)
	{
		g_pSquirrel<ScriptContext::UI>->AddFuncRegistration(
			"void",
			"NSUpdateGameStateUI",
			"string gamemode, string gamemodeName, string map, string mapName, bool connected, bool loading",
			"",
			SQ_UpdateGameStateUI);
		g_pSquirrel<ScriptContext::CLIENT>->AddFuncRegistration(
			"void",
			"NSUpdateGameStateClient",
			"int playerCount, int maxPlayers, int outScore, int secondHighestScore, int highestScore, bool roundBased, int scoreLimit",
			"",
			SQ_UpdateGameStateClient);
		g_pSquirrel<ScriptContext::UI>->AddFuncRegistration(
			"void",
			"NSUpdateServerInfo",
			"string id, string name, string password, int players, int maxPlayers, string map, string mapDisplayName, string playlist, "
			"string "
			"playlistDisplayName",
			"",
			SQ_UpdateServerInfo);
		g_pSquirrel<ScriptContext::CLIENT>->AddFuncRegistration(
			"void", "NSUpdateServerInfoReload", "int maxPlayers", "", SQ_UpdateServerInfoBetweenRounds);
		g_pSquirrel<ScriptContext::CLIENT>->AddFuncRegistration("void", "NSUpdateTimeInfo", "float timeInFuture", "", SQ_UpdateTimeInfo);
		g_pSquirrel<ScriptContext::UI>->AddFuncRegistration("void", "NSSetLoading", "bool loading", "", SQ_SetConnected);
		g_pSquirrel<ScriptContext::UI>->AddFuncRegistration("void", "NSUpdateListenServer", "", "", SQ_UpdateListenServer);
	}
}