-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsQuery.nut
52 lines (43 loc) · 1.33 KB
/
csQuery.nut
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
if ("csQuery" in getroottable() && typeof ::csQuery == "table" )
return;
::csQuery <- {};
IncludeScript("csQuery/QueryArray");
csQuery.SELECTOR <- {
CLASS = 46, // .
TARGETNAME = 35, // #
ALL = 42, // *
}
local findBy = function(findFunction, arg) {
local ents = [];
for ( local ent; ent = findFunction.call(Entities, ent, arg); )
{
ents.push(ent.weakref());
}
return ents;
}
local all = function() {
local ents = [];
for ( local ent; ent = Entities.Next(ent); )
{
ents.push(ent.weakref());
}
return ents;
}
function csQuery::Find(query) : (findBy, all) {
if (typeof query == "instance")
return QueryArray( query );
if (typeof query != "string" && typeof query != "integer")
throw "The parameter's data type is invalid (Valid Types: class instance, string, char)";
local selector = query[0];
local body = query.slice(1);
switch (selector) {
case SELECTOR.CLASS:
return QueryArray( findBy(Entities.FindByClassname, body) );
case SELECTOR.TARGETNAME:
return QueryArray( findBy(Entities.FindByName, body) );
case SELECTOR.ALL:
return QueryArray( all() );
default:
throw "The parameter has an invalid selector";
}
}