Lua 检测全局表重复赋值
来源:互联网 发布:大襟绸缎面花棉袄淘宝 编辑:程序博客网 时间:2024/05/01 06:42
若要检测全局表的赋值情况,可以通过接管全局表的赋值操作来进行判断。示例代码如下:
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[1] or ""
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
-- 设置记录所有欲加载脚本文件的ini路径,从参数一读入
-- 此ini文件包含如下格式:
-- [AllScript]
-- testa
-- testb
local _TxtFile = arg[1] or ""
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
- Lua 检测全局表重复赋值
- ToLua学习笔记,获取全局lua表,局部lua表
- lua 表赋值是引用关系
- 全局临时表控制重复登录
- lua非全局函数
- 遍历Lua全局环境变量
- lua中的全局函数
- Cocos2d-Lua之赋值
- lua 赋值语句
- lua table成员赋值
- 用C/C++为 Lua 脚本设置全局表变量
- 检测全局鼠标动作
- 表中重复数据的检测
- 利用SQLServer的全局临时表防止用户重复登录
- 利用SQL的全局临时表防止用户重复登录
- 利用SQLServer的全局临时表防止用户重复登录
- 利用的全局临时表防止用户重复登录
- 巧用SQL的全局临时表防止用户重复登录
- crontab 中的指定特定的用户身份来运行相关的命令或者脚本
- metasploit更新
- 深入dwr2之三 Dwr2页面请求处理机制分析之engine.js
- CentOS-6.3安装配置Tomcat-7
- 链表逆序
- Lua 检测全局表重复赋值
- Android开发之LisitView的图文并排效果实现(源代码分享)
- hashmap 的一些研究
- 关于VBS调用MSWinsock控件
- C++ primer plus 1-3
- AS3.0中的事件类型分类
- mybaties基础教程之四:SQL语句映射文件(1)
- 黑马程序员---java多线程学习总结
- shader 学习