forked from whine/buffer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer_lazy.fos
441 lines (364 loc) · 10.5 KB
/
buffer_lazy.fos
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* WHINE TEAM
* We Had Ini, Now Engine
*
* Buffer addon, allows to send large amount of data without alerting engine about flood
*
* server and client module
*/
#ifndef __BUFFER_LAZY__
#define __BUFFER_LAZY__
#include "_defines.fos"
#include "_macros.fos"
#include "buffer_h.fos"
#include "buffer_lazy_h.fos"
//#define BUFFERLAZY_DEBUG
#ifdef BUFFERLAZY_DEBUG
//#include "buffer_lazy_test.fos" // not ready yet
#endif
#ifdef __CLIENT
#define NormalLog#(text) {Log("BufferLazy: "+text);Message("BufferLazy: "+text);}
#endif
#ifdef __SERVER
#define NormalLog#(text) {Log("BufferLazy: "+text);}
#endif
#ifdef BUFFERLAZY_DEBUG
#define DebugLog#(text) NormalLog(text);
#endif
#ifndef BUFFERLAZY_DEBUG
#define DebugLog#(text) {}
#endif
#ifdef __CLIENT
#pragma bindfunc "bool IsLocalScript(string&) -> client_buffer_lazy.dll Global_IsLocalScript"
#pragma bindfunc "void RunLocalScript(string&,int,int,int,string@+,int[]@+) -> client_buffer_lazy.dll Global_RunLocalScript"
#define RunTargetScript#(_cr,_name,_p0,_p1,_p2,_p3,_p4) RunServerScriptUnsafe(BUFFERLAZY_MODULE+"@unsafe_"+_name,_p0,_p1,_p2,_p3,_p4)
#endif
#ifdef __SERVER
#pragma bindfunc "bool IsLocalScript(string&) -> server_buffer_lazy.dll Global_IsLocalScript"
#pragma bindfunc "void Critter::RunLocalScript(string&,int,int,int,string@+,int[]@+) -> server_buffer_lazy.dll Critter_RunLocalScript"
#define RunTargetScript#(_cr,_name,_p0,_p1,_p2,_p3,_p4) _cr.RunClientScript(BUFFERLAZY_MODULE+"@"+_name,_p0,_p1,_p2,_p3,_p4)
#endif
// handshake
#define BL_HANDSHAKE_QUESTION (0)
#define BL_HANDSHAKE_OK (1)
#define BL_HANDSHAKE_UNKNOWN (253)
#define BL_HANDSHAKE_INVALID (254)
#define BL_HANDSHAKE_DENIED (255)
#ifdef __MAPPER
#error This is NOT a mapper module
#endif
class CBufferLazy
{
uint id_source;
uint id_target;
string funcName;
int p0;
int p1;
int p2;
string@ p3;
array<int> p4;
// used on source side
CBufferLazy( string& funcName, array<int> p4 )
{
this.id_source = CreateBufferLazyId();
this.funcName = funcName;
this.p4 = p4;
}
// used on target side
CBufferLazy( string& funcName, int p0, int p1, int p2, string@ p3 )
{
this.id_source = CreateBufferLazyId();
this.funcName = funcName;
this.p0 = p0;
this.p1 = p1;
this.p2 = p2;
@this.p3 = p3;
this.p4.resize(0);
}
}; array<CBufferLazy@> LazyBuffers;
// Create internal id, used during sending/receiving data
uint CreateBufferLazyId()
{
uint result = GetTick();
while( valid(GetBufferLazy( result )) )
{
result = GetTick()+Random(-100000,100000);
}
return( result );
}
// Retrieve BufferLazy with given id
CBufferLazy@ GetBufferLazy( uint id )
{
for( uint lazy=0, lazyLen=LazyBuffers.length(); lazy<lazyLen; lazy++ )
{
if( LazyBuffers[lazy].id_source == id )
return( @LazyBuffers[lazy] );
}
return( null );
}
// Remove BufferLazy with given id
bool RemoveBufferLazy( uint id )
{
for( uint lazy=0, lazyLen=LazyBuffers.length(); lazy<lazyLen; lazy++ )
{
if( LazyBuffers[lazy].id_source == id )
{
LazyBuffers.removeAt( lazy );
return( true );
}
}
return( false );
}
//----------------------------------------------------------------------------//
#ifdef __CLIENT
void RunLazyScript( string& funcName, int p0, int p1, int p2, string@ p3, array<int>@ p4 )
{
RunLazyScriptGeneric( funcName, p0, p1, p2, p3, p4, false );
}
void RunLazyScriptUnsafe( string& funcName, int p0, int p1, int p2, string@ p3, array<int>@ p4 )
{
RunLazyScriptGeneric( funcName, p0, p1, p2, p3, p4, true );
}
void RunLazyScriptGeneric( string& funcName, int p0, int p1, int p2, string@ p3, array<int>@ p4, bool unsafe )
#endif
#ifdef __SERVER
void RunLazyScript( Critter& player, string& funcName, int p0, int p1, int p2, string@ p3, array<int>@ p4 )
#endif
{
if( !valid(p4) || p4.length() == 0)
{
// we don't need special sending here
DebugLog( "sending using core functions" );
#ifdef __CLIENT
if( !unsafe )
RunServerScript( funcName, p0, p1, p2, p3, p4 );
else
RunServerScriptUnsafe( funcName, p0, p1, p2, p3, p4 );
#endif
#ifdef __SERVER
player.RunClientScript( funcName, p0, p1, p2, p3, p4 );
#endif
return;
}
array<int32> data;
Buffer@ buff = NewBuffer();
// create source-side function buffer
// p0-p3 not needed, as these are sent during handshake
LazyBuffers.insertLast( CBufferLazy( funcName, p4 ));
buff << uint8( BL_HANDSHAKE_QUESTION );
buff << LazyBuffers.last().id_source;;
buff << funcName;
#ifdef __CLIENT
buff << unsafe;
#endif
buff >>>= data;
RunTargetScript( player, "handshake", p0, p1, p2, p3, data );
}
// pseudo-protocol //
#ifdef __CLIENT
void handshake_result( uint8 result, int id_source, int id_target = -1 )
#endif
#ifdef __SERVER
void handshake_result( Critter& player, uint8 result, int id_source, int id_target = -1 )
#endif
{
array<int32> data;
Buffer@ buff = NewBuffer();
buff << result;
buff << id_source;
if( id_target >= 0 )
buff << id_target;
buff >>>= data;
RunTargetScript( player, "handshake", int(0), int(0), int(0), @" ", data );
#ifdef __SERVER
if( result == BL_HANDSHAKE_DENIED )
{
player.Say( SAY_NETMSG, "Access denied. Disconnect." );
player.Disconnect();
}
#endif
}
#ifdef __CLIENT
void handshake( int p0, int p1, int p2, string@ p3, array<int>@ p4 )
#endif
#ifdef __SERVER
void unsafe_handshake( Critter& player, int p0, int p1, int p2, string@ p3, array<int>@ p4 )
#endif
{
if( !valid(p4) || p4.length() == 0 )
{
NormalLog( "empty data, ignoring" );
return;
}
Buffer@ buff = NewBuffer( p4 );
uint8 handshake = 0;
uint32 id_source = 0;
buff >> handshake;
buff >> id_source;
if( handshake == BL_HANDSHAKE_QUESTION )
{
string funcName = "";
buff >> funcName;
array<string@>@ nameSplit = split( funcName, "@" );
if( !valid(nameSplit) || nameSplit.length() != 2 )
{
NormalLog(
#ifdef __SERVER
player.Name+"<"+player.Id+"> "+
#endif
"trying to use invalid function name<"+funcName+">" );
handshake_result(
#ifdef __SERVER
player,
#endif
BL_HANDSHAKE_INVALID, id_source );
return;
}
#ifdef __SERVER
// simulate engine behaviour
bool unsafe = true;
buff >> unsafe;
// unsafe functions must be prefixed with "unsafe_"
if( unsafe && (nameSplit[1].length() <= 7 || substring(nameSplit[1], 0, 7) != "unsafe_") )
{
NormalLog( player.Name+"<"+player.Id+"> trying to use unsafe function<"+funcName+"> without required prefix" );
handshake_result( player, BL_HANDSHAKE_DENIED, id_source );
return;
}
// safe functions require minimum access level specified
// we will spam log if it's set too low, but proceed with execution
if( BUFFERLAZY_SAFE_ACCESS <= ACCESS_CLIENT )
NormalLog( "WARNING ! BUFFERLAZY_SAFE_ACCESS <= ACCESS_CLIENT" );
if( !unsafe && player.GetAccess() < BUFFERLAZY_SAFE_ACCESS )
{
NormalLog( player.Name+"<"+player.Id+"> trying to use safe function<"+funcName+"> without required access" );
handshake_result( player, BL_HANDSHAKE_DENIED, id_source );
return;
}
#endif // __SERVER
~buff;
if( !IsLocalScript( funcName ))
{
NormalLog(
#ifdef __SERVER
player.Name+"<"+player.Id+"> "+
#endif
"trying to use unknown function<"+funcName+">" );
handshake_result(
#ifdef __SERVER
player,
#endif
BL_HANDSHAKE_UNKNOWN, id_source );
return;
}
// create target-side function buffer
LazyBuffers.insertLast( CBufferLazy( funcName, p0, p1, p2, p3 ));
uint32 id_target = LazyBuffers.last().id_source;
DebugLog( "Function<"+funcName+"> accepted, finishing handshake" );
// send result back to source
handshake_result(
#ifdef __SERVER
player,
#endif
BL_HANDSHAKE_OK, id_source, id_target );
return;
}
else
{
switch( handshake )
{
case BL_HANDSHAKE_OK:
{
// target allows to send data, start the Run*Script chain
uint32 id_target = 0;
buff >> id_target;
#ifdef __CLIENT
request(
#endif
#ifdef __SERVER
unsafe_request( player,
#endif
id_source, id_target, int(0), @" ", null );
}; break;
case BL_HANDSHAKE_UNKNOWN:
case BL_HANDSHAKE_INVALID:
case BL_HANDSHAKE_DENIED:
RemoveBufferLazy( id_source );
break;
default:
NormalLog( "unknown handshake result <"+handshake+">, ignoring" );
return;
}
}
}
#ifdef __CLIENT
void request( int id_source, int id_target, int position, string@, array<int>@ )
#endif
#ifdef __SERVER
void unsafe_request( Critter& player, int id_source, int id_target, int position, string@, array<int>@ )
#endif
{
array<int> data;
bool dataEnd = false;
CBufferLazy@ bufflazy = GetBufferLazy( id_source );
if( !valid(bufflazy) )
{
DebugLog( "Requesting unknown BufferLazy<"+id_source+">" );
return;
}
uint size = 16 + bufflazy.funcName.length();
int positionMax = bufflazy.p4.length();
while( size+4 < __FloodSize-32 )
{
data.insertLast( bufflazy.p4[position++] );
if( position == positionMax )
{
dataEnd = true;
break;
}
size += 4;
}
DebugLog( "size "+size+" position "+position+" positionMax "+positionMax );
RunTargetScript( player, "receive", id_source, id_target, position,
(dataEnd == true ? "-" : "+"), data );
}
#ifdef __CLIENT
void receive( int id_source, int id_target, int position, string@ dataEnd, array<int>@ data )
#endif
#ifdef __SERVER
void unsafe_receive( Critter& player, int id_source, int id_target, int position, string@ dataEnd, array<int>@ data )
#endif
{
CBufferLazy@ lazy = GetBufferLazy( id_target );
if( !valid(lazy) )
{
DebugLog( "Received unknown BufferLazy<"+id_target+">" );
return;
}
for( uint d=0, dLen=data.length(); d<dLen; d++ )
{
lazy.p4.insertLast( data[d] );
}
if( dataEnd == "-" )
{
DebugLog( "RunLocalScript("+lazy.funcName+","+lazy.p0+","+lazy.p1+","+lazy.p2+","+lazy.p3+",len:"+lazy.p4.length() );
#ifdef __CLIENT
RunLocalScript(
#endif
#ifdef __SERVER
player.RunLocalScript(
#endif
lazy.funcName, lazy.p0, lazy.p1, lazy.p2, lazy.p3, lazy.p4 );
for( uint l=0 ,lLen=LazyBuffers.length(); l<lLen; l++ )
{
if( LazyBuffers[l].id_target == lazy.id_target )
{
LazyBuffers.removeAt( l );
break;
}
}
}
else
RunTargetScript( player, "request", id_source, id_target, position, "+", null );
}
#endif // __BUFFER_LAZY__ //