-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
concommand.lua
135 lines (108 loc) · 3.04 KB
/
concommand.lua
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
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Concommand class
--
--==========================================================================--
class( "concommand" )
concommand._concommands = concommand._concommands or {}
local sv_cheats = convar( "sv_cheats", 0, nil, nil, "Allow cheats on server",
nil, { "notify" } )
function concommand.dispatch( player, name, argString, argTable )
local concommand = concommand.getConcommand( name )
if ( concommand == nil ) then
return false
end
local flags = concommand:getFlags()
if ( flags ) then
local cheat = table.hasvalue( flags, "cheat" )
if ( cheat and not sv_cheats:getBoolean() ) then
return true
end
end
if ( _CLIENT ) then
if ( flags ) then
local game = table.hasvalue( flags, "game" )
if ( game and not engine.client.isInGame() ) then
return true
end
end
end
concommand:callback( player, name, argString, argTable )
if ( flags ) then
local networked = table.hasvalue( flags, "network" )
if ( _CLIENT and networked ) then
local payload = payload( "concommand" )
payload:set( "name", name )
payload:set( "argString", argString )
payload:sendToServer()
end
end
return true
end
if ( _CLIENT ) then
function concommand.run( name )
local command = string.match( name, "^([^%s]+)" )
if ( command == nil ) then
return false
end
local _, endPos = string.find( name, command, 1, true )
local argString = string.trim( string.utf8sub( name, endPos + 1 ) )
local argTable = string.parseargs( argString )
if ( concommand.getConcommand( command ) ) then
concommand.dispatch( localplayer, command, argString, argTable )
return true
end
local convar = convar.getConvar( command )
if ( convar ) then
convar:setValue( argString )
return true
end
return false
end
end
function concommand.getConcommand( name )
return concommand._concommands[ name ]
end
function concommand:concommand( name, helpString, callback, flags, autocomplete )
self.name = name
self.helpString = helpString
self.callback = callback
self.flags = flags
self.autocomplete = autocomplete
concommand._concommands[ name ] = self
end
function concommand:callback( player, command, argString, argTable )
end
function concommand:getAutocomplete()
return self.autocomplete
end
function concommand:getCallback()
return self.callback
end
function concommand:getFlags()
return self.flags
end
function concommand:getHelpString()
return self.helpString
end
function concommand:getName()
return self.name
end
function concommand:remove()
concommand._concommands[ self:getName() ] = nil
end
function concommand:setAutocomplete( autocomplete )
self.autocomplete = autocomplete
end
function concommand:setCallback( callback )
self.callback = callback
end
function concommand:setFlags( flags )
self.flags = flags
end
function concommand:setHelpString( helpString )
self.helpString = helpString
end
function concommand:__tostring()
return "concommand: \"" .. self.name .. "\""
end