-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
97 lines (86 loc) · 2.13 KB
/
Copy pathtest.lua
File metadata and controls
97 lines (86 loc) · 2.13 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
local function single(names)
return require(names)
end
local function batch(names)
local sources = {}
for index = 1, #names do
sources = require(names[index])
end
return sources
end
local IMPORT_MAPPING = {
["string"] = single,
["table"] = batch,
}
path = {}
function path.import(names, path)
if not names then
return
end
local type = type(names)
if type == "table" and not next(names) then
return
end
local import = assert(IMPORT_MAPPING[type], type)
-- 备份并改变默认搜索路径
if path then
path, package.path = package.path, path .. "?.lua"
end
local state, sources = xpcall(import, debug.traceback, names)
if not state then
print(sources)
end
-- 恢复默认搜索路径
if path then
package.path = path
end
return sources
end
function path.current()
--[[
功能:
获取指定函数信息,筛选调用此函数的函数所属文件的路径
参数:
2:堆栈第2层函数信息,即调用函数path.current的函数信息
'S':填充source、short_src、linedefined、lastlinedefined,以及what域
拓展:
对于第一参数,0层表示getinfo自身信息,1层表示调用getinfo的函数信息,即path.current的函数信息
对于第二参数,另外还有'n'、'l'、't'、'u'、'f'、'L'等筛选条件或者操作选项
--]]
local info = debug.getinfo(2, "S")
-- 忽略第一字符'@'
local path = string.sub(info.source, 2)
-- 捕获最后一'/'及其之前的内容,即当前文件所在目录
return string.match(path, "^.*[/\\]")
end
local sources = path.import({"stringify", "copy"}, path.current())
local stringify, copy = table.unpack(sources)
local player = {
base = {
id = "RO-0000000001",
sex = 1,
name = "solifree",
nature = "freedom",
dress = 101,
},
organization = "eterfree",
founder = true,
action = function() end,
observer = {},
}
local insert = table.insert
local prototype = copy(player)
---[[
for i = 1, 100000 do
insert(player.observer, copy(prototype))
end
--]]
--[[
for i = 1, 10 do
insert(player.observer, prototype)
end
--]]
local time = os.clock()
stringify(player)
--print(stringify(player))
print(os.clock() - time)