This repository was archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole.lua
More file actions
209 lines (170 loc) · 7.29 KB
/
console.lua
File metadata and controls
209 lines (170 loc) · 7.29 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
--[=============================================================================[
The MIT License (MIT)
Copyright (c) 2014 RepeatPan
excluding parts that were written by Radiant Entertainment
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]=============================================================================]
local console = { _commands = {}, _datastore = radiant.create_datastore() }
-- The currently selected entity
local SELECTED
local USAGE -- function that can be called in case the command was used wrongly
local _L = {} -- local environment that new variables are inserted into
-- Offer all radiant default objects already as available.
for k, v in pairs(_radiant.csg) do
_L[k] = v
end
-- Scopes a function so they gain access to SELECTED. This is really evil. Kinda.
-- (it's also not working with lua5.2 I think?)
local set_scope
local last_usage_text -- last string to be used for usage()
-- Insert some helpful functions into _L
do
function _L.get_component(...)
return SELECTED:get_component(...)
end
_L.gc = _L.get_component
function _L.add_component(...)
return SELECTED:add_component(...)
end
_L.ac = _L.add_component
-- Returns all methods of `obj` as table
function _L.methods(obj)
return class_info(obj).methods
end
function _L.load_json(...)
return radiant.resources.load_json(...)
end
_L.entities = radiant.entities
function _L.get_player_id(...)
return radiant.entities.get_player_id(...)
end
end
do
local function create_env(old_env)
local ENV = {}
function ENV:__index(key)
-- Because of its nature, "SELECTED" and "USAGE" are always available
-- and are not meant to be overwritten/changed by console commands
if key == 'SELECTED' then
return SELECTED
elseif key == 'USAGE' then
return USAGE
elseif key == '_L' then
return _L
else
-- Last attempt; prefer local values
local val = rawget(_L, key)
if val ~= nil then
return val
elseif old_env == _G then
val = rawget(old_env, key)
-- Make errors more obvious. TODO: Only do this if strict lua is active?
if val == nil then
error("variable '" .. key .. "' is not declared", 2)
end
end
return old_env[key]
end
end
function ENV:__newindex(key, value)
if key == 'SELECTED' then
SELECTED = value
-- Functions that are already defined globally may be overwritten
elseif rawget(old_env, key) == nil then
_L[key] = value
else
old_env[key] = value
end
end
ENV = setmetatable({}, ENV)
return ENV
end
local G_ENV = create_env(_G)
-- Scopes a function to allow it access to SELECTED and other nasty bits I have yet to add
function set_scope(func)
local old_env = getfenv(func)
if old_env == _G then
return setfenv(func, G_ENV)
else
return setfenv(func, create_env(old_env))
end
end
console.set_function_scope = set_scope
function USAGE(additional_text)
error((additional_text and additional_text .. ' ' or '') .. last_usage_text, 2)
end
end
local function update_command_list()
local t = {}
for cmd, data in pairs(console._commands) do
table.insert(t, { name = cmd, endpoint = radiant.is_server and 'server' or 'client', plain_allowed = data.plain_allowed, usage_text = data.usage_text })
end
console._datastore:set_data({ commands = t })
end
-- Adds one or multiple commands called `names' (or elements of `names') that point towards `callback'.
-- lua functions will most likely override same-named JS functions. It's possible to name your lua functions with
-- special characters, but whitespace is not allowed, neither are '@' at the beginning.
--
-- Note that `callback` is a function receiving the following parameters:
-- * cmdName: Name of the cmd, identical with what it was called (and one of the registered names)
-- * args: List of arguments. This can be either parsed (if called with "name") or the raw, space-splitted arguments (if called with "@name")
-- * arg_string: The raw string after the command as it was entered by the user. Useful for eval, naming and the like.
-- * response: The response object from the call handler in case you wish to manually resolve/reject the answer. Note that as of current, this has no observable effect on the console.
-- The return value of `callback` is returned to the console and displayed as JSON. Errors are caught by jelly_console and displayed accordingly.
-- `callback` will be put into a different environment which allows it access to console-exclusive functions and variables:
-- * SELECTED: last selected entity using the 'select' command
function console.add_command(names, callback, usage_text)
if type(names) == 'string' then
names = { names }
end
for _, name in pairs(names) do
if not name or name:find(' ') then
error('Invalid command name ' .. tostring(name))
end
local plain_allowed
if name:sub(1, 1) == '@' then
name, plain_allowed = name:sub(2), true
end
console._commands[name] = { call = set_scope(callback), usage_text = usage_text, plain_allowed = plain_allowed }
end
update_command_list()
end
-- Returns the environment that this part of the lua environment uses.
function console.get_environment()
return _L
end
function console._dispatch(session, response, name, args, arg_str)
local command = console._commands[name]
if not command then
response:reject('Command not found')
return
end
last_usage_text = command.usage_text or 'Invalid use of ' .. name .. ' (and the author hasn\'t specified how to properly use it)'
local ret = { pcall(command.call, name, args, arg_str, response) }
local status = table.remove(ret, 1)
if not status then
-- Sadly, we cannot just return the string - the data binding seems to
-- transform it into { result: str }
response:reject({ error = 'Executing command failed: ' .. ret[1] })
return
end
return unpack(ret)
end
function console._set_selected(entity)
SELECTED = entity
end
return console