World = {}

function World.createnew(file)
	World.save(file,{entities ={}},true)
end

function World.save(file,tab,noupdate)
	file = file or defaultlevel
	if not file:match(system.projectpath()) then
		file = system.projectpath().."/levels/"..file
	end
	--EditEntity.savetofile (file)
	local tab = tab or {}
	tab.version = 1.0
	if not noupdate then
		tab.entities = EditEntity.savetotable()
	end
	local ser = UtilFunctions.serialize(tab)
	local fp = assert(io.open(file,"w"))
	fp:write(ser)
	fp:close()
end

function World.load (file,dontcreateentities)
	file = file or defaultlevel
	if not file:match(system.projectpath()) then
		file = system.projectpath().."/levels/"..file
	end
	
	local fn,err = loadfile(file)
	if fn then
		local tab = fn()
		if not tab.version then
			if not dontcreateentities then
				EditEntity.loadfromtable(tab)
			end
			return {version = 0,entities = tab}
		else
			if not dontcreateentities then
				EditEntity.loadfromtable(tab.entities)
			end
			return tab
		end
	else 
		print("Error",err)
		EditEntity.loadfromtable({})
	end
end