-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.lua
More file actions
238 lines (209 loc) · 6.23 KB
/
sandbox.lua
File metadata and controls
238 lines (209 loc) · 6.23 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
sandbox = {};
sandbox.files = {};
sandbox.meta = {__index=function(t, k) return _G[k]; end};
-- log level
sandbox.LogVerbose = function(Log)
sandbox.Log("Verbose", Log);
end
sandbox.LogNormal = function(InLog)
sandbox.Log("Log", InLog);
end
sandbox.LogWarning = function(Log)
sandbox.Log("Warning", Log);
end
sandbox.LogError = function(Log)
sandbox.Log("Error", Log);
end
local try_load = function(node)
local filename = node.filename;
local filetext = sandbox.ReadFile(filename);
if not filetext then
sandbox.LogError(string.format("failed to read file: %s", filename));
return;
end
local trunk, msg = load(filetext, filename, "bt", node.env);
if not trunk then
sandbox.LogError(string.format("load file: %s ... ... [failed]", node.filename));
sandbox.LogError(msg);
return;
end
local ok, err = pcall(trunk);
if not ok then
sandbox.LogError(string.format("exec file: %s ... ... [failed]", node.filename));
sandbox.LogError(err);
return;
end
sandbox.LogNormal(string.format("load file: %s ... ... [ok]", node.filename));
end
local get_filenode = function(filename)
local node = sandbox.files[filename];
if node then
return node;
end
local env = {};
setmetatable(env, sandbox.meta);
node = {env=env, filename=filename};
sandbox.files[filename] = node;
return node;
end
sandbox.import = function(filename)
sandbox.Log("Log", "import " .. filename)
local node = get_filenode(filename);
if node.time then
return node.env;
end
node.time = sandbox.GetFileTime(filename);
local filetext = sandbox.ReadFile(filename);
if not filetext then
sandbox.LogError(string.format("failed to read file: %s", filename));
return node.env;
end
local trunk, code_err = load(filetext, filename, "bt", node.env);
if not trunk then
sandbox.Log("Error", code_err);
error(code_err);
end
local ok, exec_err = pcall(trunk);
if not ok then
sandbox.Log("Error", code_err);
error(exec_err);
end
return node.env;
end
sandbox.reload = function()
local now = os.time();
for path, node in pairs(sandbox.files) do
local filetime = sandbox.GetFileTime(node.filename);
if filetime ~= node.time and filetime ~= 0 and math.abs(now - filetime) > 1 then
node.time = filetime;
try_load(node);
end
end
end
function import(filename)
local node = get_filenode(filename);
if not node.time then
node.time = sandbox.GetFileTime(filename);
try_load(node);
end
return node.env;
end
--------------------------
print = sandbox.LogNormal;
--[[
函数: log_tree(desc, var)
功能: 打印变量,特别是对table类型做树形输出
desc: 变量描述
var: 要输出的变量,可以是任何类型table, bool, number, nil
注意: 不保证性能,仅供调试用;对table中的多个key不保证输出顺序.
示例:
local player = {name="bitch", level=10, id=123};
log_tree("player_data", player);
--]]
local function var2string(var)
local text = tostring(var);
text = string.gsub(text, "\r", "\\r");
text = string.gsub(text, "\n", "\\n");
text = string.gsub(text, "%c", "\\^");
return text;
end
local function tab2string(visited, path, base, tab)
local pre = visited[tab];
if pre then
return pre;
end
visited[tab] = path;
local size = 0;
for k, v in pairs(tab) do
size = size + 1;
end
if size == 0 then
return "{ }";
end
local lines = {};
local idx = 1;
for k, v in pairs(tab) do
local vtype = type(v);
local header = base..(idx < size and "├─ " or "└─ ")..var2string(k);
if vtype == "table" then
local vpath = visited[v];
if vpath then
lines[#lines + 1] = header..": "..vpath;
else
local out = tab2string(visited, path.."/"..var2string(k), base..(idx < size and "│ " or " "), v);
if type(out) == "string" then
lines[#lines + 1] = header..": "..out;
else
lines[#lines + 1] = header;
for _, one in ipairs(out) do
table.insert(lines, one);
end
end
end
else
local txt = var2string(v);
if vtype == "string" then
txt = '\"'..txt..'\"';
end
lines[#lines + 1] = header..": "..txt;
end
idx = idx + 1;
end
return lines;
end
function _G.log_tree(desc, var)
if var == nil then
var = desc;
end
if type(var) ~= "table" then
print(var2string(desc)..": "..var2string(var));
return;
end
local visited = {};
local out = tab2string(visited, "", "", var);
if type(out) == "string" then
print(var2string(desc)..": "..out);
return;
end
print(var2string(desc));
for i, line in ipairs(out) do
print(line);
end
end
-------------------------------------------------------------------------------
--[[
函数: DeepCopy(obj)
功能: 对table类型进行深拷贝
obj: 传入table
示例:
local player = {name="bitch", level=10, id=123};
local t = player; ---浅拷贝,t是player的引用
local t1 = DeepCopy(player); ---深拷贝
player.level = 11;
print(t.level) --也是11,
print(t1.level)--仍是10,
--]]
function _G.DeepCopy( obj )
local InTable = {};
local function Func(obj)
if type(obj) ~= "table" then --判断表中是否有表
return obj;
end
local NewTable = {}; --定义一个新表
InTable[obj] = NewTable; --若表中有表,则先把表给InTable,再用NewTable去接收内嵌的表
for k,v in pairs(obj) do --把旧表的key和Value赋给新表
NewTable[Func(k)] = Func(v);
end
return setmetatable(NewTable, getmetatable(obj))--赋值元表
end
return Func(obj) --若表中有表,则把内嵌的表也复制了
end
function Function_Lua()
local x=0
for i=0,1000 do
for j=0,100 do
x = x + (i*i+j*2+i/1.5)
end
end
sandbox.Log("Verbose", "Function_Lua");
end