-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientcvar.as
More file actions
320 lines (267 loc) · 7.87 KB
/
clientcvar.as
File metadata and controls
320 lines (267 loc) · 7.87 KB
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
/*
Copyright (c) 2019, Jannik Kolodziej, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
/*
This file depends on WF scriptutils:
functioncallcenter.as
*/
funcdef void ClientCvarChanged( Client @client, String cvar, String oldcontent, String newcontent );
namespace ClientCvar
{
array<ClientCvar@> list_clientcvars;
bool cb_init = false;
String Get( Client @client, String name )
{
if( @client == null || client.state() < ::CS_SPAWNED )
return "null";
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == name )
{
if( list_clientcvars[i].exists( client ) && list_clientcvars[i].upToDate( client ) )
return list_clientcvars[i].getValue( client );
else if( list_clientcvars[i].exists( client ) && !list_clientcvars[i].upToDate( client ) )
return "null";
list_clientcvars[i].create( client );
return list_clientcvars[i].standartValue;
}
}
return "null";
}
void Set( Client @client, String name, String value )
{
if( @client == null || client.state() < ::CS_SPAWNED )
return;
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == name )
{
list_clientcvars[i].setValue( client, value );
return;
}
}
}
void Register( String name, String standartcontent )
{
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == name )
return; // already exists
}
list_clientcvars.insertLast( ClientCvar( name, standartcontent ) );
list_clientcvars[list_clientcvars.length - 1].update();
FunctionCallCenter::SetIntervalArg( Update, 5000, any(name) );
if( !cb_init )
{
cb_init = true;
FunctionCallCenter::RegisterGametypeCommandListener( CheckCvarResponse );
FunctionCallCenter::RegisterScoreEventListener( ConnectionEvent );
}
}
uint RegisterChangeCallback( String name, ClientCvarChanged @cb )
{
if( name.len() == 0 || cb is null )
return 0;
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == name )
return list_clientcvars[i].registerChangeListener( cb );
}
return 0;
}
void RemoveChangeCallback( String name, uint cbindex )
{
if( name.len() == 0 )
return;
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == name )
list_clientcvars[i].removeChangeListener( cbindex );
}
}
bool Update( any &arg )
{
String cvarname;
arg.retrieve( cvarname );
if( cvarname.len() == 0 )
{
::G_Print( "ClientCvar: ERROR: Update() without argument!\n" );
return false;
}
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == cvarname )
{
list_clientcvars[i].update();
return true;
}
}
return false;
}
bool CheckCvarResponse( Client @client, String &cmdString, String &argsString, int argc )
{
if( cmdString != "cvarinfo" )
return false;
String cvarname = argsString.getToken( 0 );
for( uint i = 0; i < list_clientcvars.length; i++ )
{
if( @list_clientcvars[i] != null && list_clientcvars[i].name == cvarname )
return list_clientcvars[i].handleClientAnswer( client, cmdString, argsString, argc );
}
return false;
}
void ConnectionEvent( Client @client, String &score_event, String &args )
{
if( @client == null )
return;
if( score_event == "enterGame" )
{
for( uint i = 0; i < list_clientcvars.length; i++ )
list_clientcvars[i].update( client );
}
else if( score_event == "disconnect" )
{
for( uint i = 0; i < list_clientcvars.length; i++ )
list_clientcvars[i].reset( client );
}
}
}
class ClientCvar
{
private String m_cvarName;
private array<String> m_cvarContent;
private String m_standartContent;
private array<bool> m_upToDate;
private array<ClientCvarChanged@> m_changeCallbacks;
ClientCvar( String name, String standartcontent )
{
m_cvarContent.length = maxClients;
m_upToDate.length = maxClients;
m_cvarName = name;
for( int i = 0; i < maxClients; i++ )
{
m_cvarContent[i] = "";
m_upToDate[i] = false;
}
m_standartContent = standartcontent;
}
void reset( Client @client )
{
if( @client == null )
return;
m_cvarContent[client.playerNum] = "";
m_upToDate[client.playerNum] = false;
}
bool exists( Client @client )
{
if( @client == null )
return false;
return ( m_cvarContent[client.playerNum].len() != 0 && m_cvarContent[client.playerNum] != "not found" );
}
void create( Client @client )
{
if( @client == null || client.state() < CS_SPAWNED )
return;
if( !exists( client ) && m_upToDate[client.playerNum] )
client.execGameCommand( "cmd seta " + m_cvarName + " \"" + m_standartContent +"\";\n" );
}
void update( Client @client )
{
if( @client == null || client.state() < CS_SPAWNED )
return;
G_CmdExecute( "cvarcheck " + client.playerNum + " \"" + m_cvarName + "\"\n" );
}
void update()
{
G_CmdExecute( "cvarcheck all \"" + m_cvarName + "\"\n" );
}
uint registerChangeListener( ClientCvarChanged@ cb )
{
if( cb is null )
return 0;
//if( m_changeCallbacks.find( @cb ) > -1 )
// return m_changeCallbacks.find( @cb );
for( uint i = 0; i < m_changeCallbacks.length; i++ )
{
if( m_changeCallbacks[i] is cb )
return i;
}
m_changeCallbacks.insertLast( cb );
return uint( m_changeCallbacks.length - 1 );
}
void removeChangeListener( uint cbindex )
{
if( m_changeCallbacks.length <= cbindex )
return;
@m_changeCallbacks[cbindex] = null;
}
void callChangeCallbacks( Client @client, String oldcontent )
{
if( @client == null )
return;
for( uint i = 0; i < m_changeCallbacks.length; i++ )
{
ClientCvarChanged @cb = @m_changeCallbacks[i];
if( cb is null )
continue;
cb( client, m_cvarName, oldcontent, m_cvarContent[client.playerNum] );
}
}
bool handleClientAnswer( Client @client, String cmdString, String argsString, int argc )
{
if( @client == null )
return false;
if( cmdString != "cvarinfo" )
return false;
String cvarName = argsString.getToken( 0 );
String cvarContent = argsString.getToken( 1 );
if( cvarName != m_cvarName )
return false;
String oldContent = m_cvarContent[client.playerNum];
bool wasUpToDate = m_upToDate[client.playerNum];
m_cvarContent[client.playerNum] = cvarContent;
m_upToDate[client.playerNum] = true;
if( oldContent != cvarContent && cvarContent != "not found" && wasUpToDate )
callChangeCallbacks( client, oldContent );
if( !exists( client ) )
create( client );
return true;
}
String get_name()
{
return m_cvarName;
}
String getValue( Client @client )
{
if( @client == null || client.state() < CS_SPAWNED || !m_upToDate[client.playerNum] )
return "null";
return m_cvarContent[client.playerNum];
}
void setValue( Client @client, String value )
{
if( @client == null || client.state() < CS_SPAWNED || !m_upToDate[client.playerNum] )
return;
m_cvarContent[client.playerNum] = value;
client.execGameCommand( "cmd seta " + m_cvarName + " \"" + value +"\";\n" );
}
String get_standartValue()
{
return m_standartContent;
}
bool upToDate( Client @client )
{
if( @client == null || client.state() < CS_SPAWNED )
return false;
return m_upToDate[client.playerNum];
}
}