Skip to content

Instantly share code, notes, and snippets.

@ixjf
Last active December 26, 2015 01:59
Show Gist options
  • Save ixjf/7074664 to your computer and use it in GitHub Desktop.
Save ixjf/7074664 to your computer and use it in GitHub Desktop.
local classFunctions = {"Element", "Vehicle", "Ped", "Player", "Object", "Marker", "Blip", "Pickup", "ColShape", "Projectile", "RadarArea", "Team", "Water", "Sound", "Weapon", "GuiElement", "GuiWindow", "GuiButton", "GuiEdit", "GuiLabel", "GuiMemo", "GuiImage", "GuiComboBox", "GuiCheckBox", "GuiRadioButton", "GuiScrollPane", "GuiScrollBar", "GuiProgressBar", "GuiGridList", "GuiTabPanel", "GuiTab", "Resource", "Timer", "File", "XmlNode", "Camera", "Vector3D", "Vector2D", "Matrix"}
local File = {}
local function OutputToFile(data)
local format = "*[[%s|%s]]"
local filename_f = "data/%s.txt"
for func, data in pairs(data) do
local filename = filename_f:format(func)
local f = assert(io.open(filename, "w"), ("Unable to create file '%s'"):format(filename))
-- Write the data to the files
--- Metamethods
if #data.meta > 0 then
f:write("===Metamethods===", "\n")
for _, value in ipairs(data.meta) do
f:write("\n", format:format(func .. "." .. value.name, value.name))
end
end
f:write("\n\n")
--- Methods
if #data.methods > 0 then
f:write("===Methods===", "\n")
for _, value in ipairs(data.methods) do
f:write("\n", format:format(value.func, value.name))
end
end
f:write("\n\n")
--- Variables
if #data.variables > 0 then
f:write("===Variables===", "\n")
for _, value in ipairs(data.variables) do
f:write("\n", format:format(func .. "." .. value.name, value.name))
end
end
f:close()
end
end
local function ProcessLine(currentFunc, line)
-- Is this a variable?
if line:find("lua_classvariable") then
-- Find the variable name
local start = line:find('"')
if start then
local _end = line:find('"', start + 1)
if _end then
table.insert(File[currentFunc].variables, {name = line:sub(start + 1, _end - 1)})
end
end
-- Is this a method?
elseif line:find("lua_classfunction") then
-- Find the function name & actual function
local start = line:find('"')
if start then
local _end = line:find('"', start + 1)
if _end then
local methodName = line:sub(start + 1, _end - 1)
local actualFunction = ("%s.%s"):format(currentFunc, methodName)
-- Find the actual function name
start = line:find('"', _end + 1)
if start then
_end = line:find('"', start + 1)
if _end then
actualFunction = line:sub(start + 1, _end - 1)
end
end
table.insert(File[currentFunc].methods, {func = actualFunction, name = methodName})
end
end
-- Is this a metamethod?
elseif line:find("lua_classmetamethod") then
-- Find the metamethod name
local start = line:find('"')
if start then
local _end = line:find('"', start + 1)
if _end then
table.insert(File[currentFunc].meta, {name = line:sub(start + 1, _end - 1)})
end
end
-- Is this a comment?
elseif line:find("%/%/") or line:find("%/%*") then
return 0 -- Don't parse this
-- Is this the end of the function body?
elseif line == "}" or line:find("}") then -- This might not work if the line contains scope brackets
return 1 -- Tell the caller we found the end of the function
end
end
(function()
local currentFunc = false
for line in io.lines("CLuaMain.cpp") do
-- Are we parsing a function?
if currentFunc then
local err = ProcessLine(currentFunc, line)
-- If the function returns error 1, we reached the end of the function body
if (err == 1) then
currentFunc = false
end
else
-- Find a function to parse
for _, value in ipairs(classFunctions) do
local func = "CLuaMain::Add%sClass"
if line:find(func:format(value)) then
currentFunc = value
File[value] = {meta = {}, methods = {}, variables = {}}
end
end
end
end
-- Write the data to the files
OutputToFile(File)
end)()
@qaisjp
Copy link

qaisjp commented Oct 20, 2013

why wrap line 62 onwards in a function. why not just execute it in the main scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment