NML = {}
--- helpers
function NML.GetLuaFiles( dir, recurse, root )
if not ULib.fileIsDir( dir ) then
return nil
end
local files = {}
local relDir
if root then
relDir = dir:gsub( root .. "[\\/]", "" )
end
root = root or dir
local result, dirs = file.Find( dir .. "/*", "GAME" )
for i=1, #result do
if (string.Right(result[ i ],4) == ".lua") then
if not relDir then
table.insert( files, result[ i ] )
else
table.insert( files, relDir .. "/" .. result[ i ] )
end
end
end
for i=1, #dirs do
files = table.Add( files, NML.GetLuaFiles( dir .. "/" .. dirs[ i ], recurse, root ) )
end
return files
end
function NML.split(str, delim)
local res = {}
local pattern = string.format("([^%s]+)%s", delim, delim)
for line in str:gmatch(pattern) do
table.insert(res, line)
end
return res
end
function NML.trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- end helpers
--- pretty print to console
function NML.ConsoleNotice(src, checkname, filename, linenum, tofind)
MsgC( Color(0,255,255), checkname, "\t\t", Color(255,255,255), filename..":", Color(0,255,255), linenum, "\t\t")
local startpos,endpos = string.find(src, tofind)
local lefts = string.Left(src, startpos - 1)
local rights = string.Right(src, string.len(src) - endpos)
MsgC(Color(255,255,255), lefts)
MsgC(Color(255,255,0), tofind)
MsgC(Color(255,255,255), rights)
MsgC("\n")
end
--- standardized check for presence of token in string
function NML.SimpleCheck(src, tofind, checkname, filename, linenum)
src = NML.trim1(src)
if string.Left(src,2) == "--" or string.Left(src,2) == "//" then -- rem statement
return
end
if (string.find(src, tofind) != nil) then
NML.ConsoleNotice(src, checkname, filename, linenum, tofind)
end
end
--- main loop
function NML.CheckFiles()
local luaFiles = {}
luaFiles = table.Add(luaFiles, NML.GetLuaFiles("lua", true))
luaFiles = table.Add(luaFiles, NML.GetLuaFiles("addons", true))
luaFiles = table.Add(luaFiles, NML.GetLuaFiles("gamemodes", true))
for k,filename in pairs(luaFiles) do
local content = file.Read( filename, "LUA" )
if (content != nil) then
content = string.gsub(content, "\r", "") -- better way to handle win eol?
local content_lns = NML.split(content,"\n") -- split source into lines
for linenum,linesrc in pairs(content_lns) do
-- simple STEAM reference (make this a proper regex. Doing it this way for now to avoid catching dummy steam IDs)
NML.SimpleCheck(linesrc, "STEAM_0:0", "AUTHENT", filename, linenum)
NML.SimpleCheck(linesrc, "STEAM_0:1", "AUTHENT", filename, linenum)
NML.SimpleCheck(linesrc, "http.Post", "NETWORK", filename, linenum)
NML.SimpleCheck(linesrc, "http.Fetch", "NETWORK", filename, linenum)
NML.SimpleCheck(linesrc, "CompileString", "DYNCODE", filename, linenum)
NML.SimpleCheck(linesrc, "RunString", "DYNCODE", filename, linenum)
NML.SimpleCheck(linesrc, "removeip", "BANMGMT", filename, linenum)
NML.SimpleCheck(linesrc, "removeid", "BANMGMT", filename, linenum)
NML.SimpleCheck(linesrc, "banip", "BANMGMT", filename, linenum)
NML.SimpleCheck(linesrc, "writeid", "BANMGMT", filename, linenum)
NML.SimpleCheck(linesrc, "file.Delete", "FILESYS", filename, linenum)
end
end
end
end