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
-----------------------------------------------------------------------
-- 设置记录所有欲加载脚本文件的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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值