
function loadConfig()
	print("LOAD CONFIG")

	if  lfs.exists( "config.sav" )  then 
		print("Found config file")
		config 			= Tserial.unpack( lfs.read( "config.sav" ) ) 
		print(inspect(config))
	else
		print("No config to load")
		config={
			tempTips = true
		}
	end

	config = config or {}
	config.catmull = config.catmull or 4
	config.flatUI=false
	config.projects= config.projects or {}
	config.defaultProjectLocation = config.defaultProjectLocation or lfs.getUserDirectory( )
	config.window = config.window or {x=0,y=0,display=1,size={width=launchWidth,height=launchHeight}}
	print("confiw",config.window.size.width)
	loadedWidth,loadedHeight = loadedWidth or config.window.size.width,loadedHeight or config.window.size.height
	print("loadedWidth",config.window.size.width)
 	config.launchFullscreen = config.launchFullscreen==true
 	config.entered = false

	traceEnabled 		= config.traceEnabled
	logEnabled 			= config.logEnabled
	helpEnabled 		= config.helpEnabled

	if bench and bench.folded and config.benchFolded then 
		for f,fold in pairs(config.benchFolded) do 
			bench.folded[f] = true
		end
	end

	game.resolution = config.resolution or 1
	game.newRes 		= game.resolution



end

function saveConfig()

	config=config or {}

	if  game.state == "Edit" then 
		print("\nSAVE CONVIG\n")
		config = {
			flatUI = config.flatUI,
			projects=config.projects,
			entered=config.entered,
			autoload=config.autoload,
			catmull=config.catmull,
			tempTips = config.tempTips
		}

		config.traceEnabled 		= traceEnabled
		config.logEnabled 			= logEnabled
		config.helpEnabled 			= helpEnabled
		config.window 					= config.window or {}	

		config.window.x, config.window.y, config.window.display = lwindow.getPosition()
		config.window.size = {width=screenWidth,height=screenHeight}  
		
		config.sidebar={
			visible 	= (sidebar.visible and sidebar.wasOpen) ,
			width 		= sidebar.width,
			darkMode 	= sidebar.darkMode,
		}

		config.currentProject = CURRENT_PROJECT
		config.fontSize 			= scriptsPanel.fontSize 
		config.editingLevel 	= level.key
		config.autoSaveEnabled= editor.autoSaveEnabled
		config.autoSaveTime   = editor.autoSaveTime
		config.editingSet 		= editor.editingSet
		config.renderLines 		= renderLines
		config.timelineBottom = animationPanel.bottom
		config.galleryOpen		= galleryPanel.open
		config.galleryThumb		= galleryPanel.thumbSize
		config.gridEnabled 		= editor.gridEnabled
		config.debug 					= editor.debug
		config.animDebugView  = animationPanel.debugView
		config.recentColors   = editor.recentColors

		if bench and bench.folded then 
			config.benchFolded 		= {}
			for f,fold in pairs(bench.folded) do 
				if fold then 
					config.benchFolded[f] = true
				end
			end
		end

		config.drawingResolution = editor.drawingResolution
		config.resolution 		= game.resolution

		config.set 					= currentSet.key
		config.room 				= currentRoom.room.key
		config.roomsView 		= roomsView

		if level.isAnim then 
			local settings = {
				 isAnim = true
				,recentColors		= drawingPanel.recentColors
				,currentColor 	= drawingPanel.recentColors[1] or nil
				,recentBrushes 	= drawingPanel.recentBrushes
				,currentBrush 	= {brush=drawingPanel.brush}
				,currentPen 		= drawingPanel.pen
				,fillGap        = drawingPanel.fillGap
				,fillDivs 			= animationPanel.fillDivs  
				,forceRefill 		= animationPanel.forceRefill  
				,frameShift 		= animationPanel.frameShift  
				,antiAliasing		= animationPanel.antiAliasing  
				,antiAlias 			= animationPanel.antiAlias  
				,maxUndos 			= animationPanel.maxUndos
			}
			lfs.write(LINK_DIR..".config",Tserial.pack(settings,nil,true))
		end
	end
	
	lfs.write("config.sav", Tserial.pack(config,nil,true))
end


function currentRoomsForSave(t)
	local current = deepcopy(t or editor.currentRooms)
	for i,v in ipairs(current)do
		current[i]=currentRoomForSave(current[i])
	end
	return current
end


function currentRoomForSave(t)
	local current = deepcopy(t or editor.currentRoom)
	current.room = nil
	current.viewport = nil
	current.camera = nil
	current.viewPolys = nil
	return current
end