function exportLevel(levelname, filename)
	love.filesystem.createDirectory("export")
	filename = filename or "output.lua"
	if string.sub(filename, 1, 1) ~= "/" then
		filename = "export/"..filename
	end
	exportFile = io.open(filename, "w+")

	if exportFile then
		logPrint("Exporting level \"".. levelname .. "\"...")
		exportFile:write("return {")

			logPrint("- level name")
			exportFile:write("\n	name = \"" .. levelname .. "\",")

			logPrint("- tileset")
			for k, v in pairs(tileset) do
				if v == LevelData.tileset then
					exportFile:write("\n	tileset = tileset." .. k .. ",")
				end
			end

			logPrint("- properties")
			exportFile:write("\n	properties = {")
			logPrint(" - darkness: ".. tostring(LevelData.properties.darkness))
			exportFile:write("\n		darkness = " .. tostring(LevelData.properties.darkness))
			exportFile:write("\n	},")

			logPrint("- tiles")
			exportFile:write("\n	tiles = {")
			local rows = #LevelTiles
			for i = 1, #LevelTiles do
				if i > 1 then
						exportFile:write(", ")
				end
				exportFile:write("\n		{ ")
				for j = 1, #LevelTiles[i] do
					if j ~= 1 then
						exportFile:write(", ")
					end
					exportFile:write(tostring(LevelTiles[i][j].id))
				end
				exportFile:write("}")
				logPrint(" - row "..i.."/"..rows.." "..math.floor(100*((i-1)*100/rows))/100 .."%")
			end
			exportFile:write("\n	},")
			logPrint("- objects")
			exportFile:write("\n	objects = {")
				logPrint(" - spawns")
				exportFile:write("\n		spawns = {")
				for i, v in ipairs(LoadedObjects.Spawns) do
					if i > 1 then
						exportFile:write(",")
					end
					exportFile:write("\n			{")
					exportFile:write(v.archetype.type)
					exportFile:write(",{")
					for i=1, #v.args do
						if i > 1 then
							exportFile:write(",")
						end
						exportFile:write(v.args[i])
					end
					exportFile:write("}}")
				end
				exportFile:write("\n		},")
				logPrint(" - rooms")
				exportFile:write("\n		rooms = {")
				for i, room in ipairs(LoadedObjects.Rooms) do
					if i > 1 then
						exportFile:write(",")
					end
					exportFile:write("\n			{{")
					exportFile:write(room.from.x)
					exportFile:write(",")
					exportFile:write(room.from.y)
					exportFile:write("},{")
					exportFile:write(room.to.x)
					exportFile:write(",")
					exportFile:write(room.to.y)
					exportFile:write("}}")
				end
				exportFile:write("\n		},")
			exportFile:write("\n	},")
			logPrint("Exporting complete.")
		exportFile:write("\n}")
		exportFile:close()
	end
end

-- Source https://stackoverflow.com/a/11130774
function scandir(directory)
		local i, t, popen = 0, {}, io.popen
		local pfile = popen('ls "'..directory..'"')
		for filename in pfile:lines() do
				i = i + 1
				t[i] = filename
		end
		pfile:close()
		return t
end

if logging then
	-- Make log stuff
	os.execute( "mkdir \"./logs\"" )
	logFile = io.open("logs/mothback_"..os.date("%Y-%m-%d_%H-%M-%S")..".log", "a+")
end
