Lua 检测全局表重复赋值

来源:互联网 发布:大襟绸缎面花棉袄淘宝 编辑:程序博客网 时间:2024/05/01 06:42

若要检测全局表的赋值情况,可以通过接管全局表的赋值操作来进行判断。示例代码如下:

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
-----------------------------------------------------------------------
-- 设置记录所有欲加载脚本文件的ini路径,从参数一读入
-- 此ini文件包含如下格式:
-- [AllScript]
-- testa
-- testb
local _TxtFile = arg[1or ""
if "" == _TxtFile then  
    return "未指定记录所有欲加载脚本文件的ini路径"
end
local _FilePath = _TxtFile:sub(1, _TxtFile:len() - _TxtFile:reverse():find("\\") + 1)

-----------------------------------------------------------------------
-- 读取脚本文件列表
local io, string = io, string
local file, msg = io.open(_TxtFile)
if not file then    
    return msg
end
local tFile = file:read("*all")
file:close()
local _, nPos = tFile:find("%[(.+)%]")
if not nPos then
    return "没有找到脚本文件列表字段"
end
local FileExists = function(file)
    local f = io.open(file)
    if not f then
        return false
    end
    f:close()
    return true
end
local _FileTable = {}       -- 脚本文件列表
local tList = tFile:sub(nPos + 1)
for v in tList:gmatch("(%w+)"do
    --local s = string.format("%s%s.dat", _FilePath, v)
    --if FileExists(s) then
    --  _FileTable[#_FileTable + 1] = s
    --else
        s = string.format("%s%s.lua", _FilePath, v)
        if FileExists(s) then
            _FileTable[#_FileTable + 1] = s
        end
    --end
end

-----------------------------------------------------------------------
-- 核心检测_G重复赋值
local rawget, getinfo = rawget, debug.getinfo
local _ProxyG = {}      -- 代理全局表
local _VarTable = {}    -- 变量表,变量对应所在文件,所在行
setmetatable(_G, {
    __newindex = function (t, n, v)     
        if rawget(_ProxyG, n) == nil then
            _VarTable[n] = {}
            _ProxyG[n] = v
        end
        local info = getinfo(2"Sl")
        _VarTable[n][#_VarTable[n] + 1] = {info.short_src, info.currentline}
    end,
    __index = function (t, n)       
        return _ProxyG[n]
    end
})

-----------------------------------------------------------------------
-- 加载脚本文件
for i, v in ipairs(_FileTable) do
    local f, msg = loadfile(v)
    if not f then
        return msg
    end
    f()
end

-----------------------------------------------------------------------
-- 构造结果信息
local nPos = _FilePath:len() + 1
local msgTable = {}
for n, t in pairs(_VarTable) do
    if #t > 1 then
        msgTable[#msgTable + 1] = string.format("%s 变量在以下文件出现:", n)
        for i, v in ipairs(t) do
            local str = v[1]:sub(nPos)  -- 防止出现\
            if str:byte() == 92 then
                str = str:sub(2)
            end
            msgTable[#msgTable + 1] = string.format("     %s 第%d行", str, v[2])
        end
    end
end
local msg = table.concat(msgTable, "\r\n")
if "" == msg then
    msg = "检测完成,未发现任何重复赋值的变量"
end
return msg

0 0
原创粉丝点击