Source

gldatabase / _StableSaveTable.lua

Full commit


local EnumWriter = {}

function EnumWriter.range(hFile, enumTbl)
	if(enumTbl.maxQuery) then
		hFile:write("range('", enumTbl.start, "', '", enumTbl.maxQuery, "')")
	else
		hFile:write("range '", enumTbl.start, "'")
	end
end

function EnumWriter.include(hFile, enumTbl)
	hFile:write("include '",
		enumTbl.fileName, ".",
		enumTbl.funcName, ".",
		enumTbl.paramName, "'")
end

function EnumWriter.dynamic(hFile, enumTbl)
	hFile:write("dynamic ''")
end

local function WriteTableToFile(outputFilename, tbl)
	local hFile = io.open(outputFilename, "w")
	
	--We want a stable write. So sort the table by function names.
	local funcNames = {}
	for name, funcData in pairs(tbl) do
		funcNames[#funcNames + 1] = name
	end
	
	table.sort(funcNames)
	
	hFile:write("return {\n")
	
	--Iterate over each function, in sorted order.
	for _, funcName in ipairs(funcNames) do
		hFile:write("['", funcName, "'] = {\n")
		
		local funcData = tbl[funcName]
		
		--Again for stability, write in order of parameter names.
		local paramNames = {}
		for paramName, paramList in pairs(funcData) do
			paramNames[#paramNames + 1] = paramName
		end
		
		table.sort(paramNames)
		
		for _, paramName in ipairs(paramNames) do
			hFile:write("\t['", paramName, "'] = {")
			if(#funcData[paramName] ~= 0) then
				hFile:write("\n")
				for _, enumName in ipairs(funcData[paramName]) do
					hFile:write("\t\t")
					if(type(enumName) == "string") then
						hFile:write("'", enumName, "'")
					else
						assert(enumName._type and EnumWriter[enumName._type])
						EnumWriter[enumName._type](hFile, enumName)
					end
					hFile:write(",\n")
				end
				hFile:write("\t")
			end
			hFile:write("},\n")
		end
		
		hFile:write("},\n\n")
	end
	
	hFile:write("}\n")
	hFile:close()
end

return { WriteTableToFile = WriteTableToFile }