How to access main.lua's directory?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

How to access main.lua's directory?

Post by natev »

I was thinking it would be smart to set up an automated version control system, but I'm having trouble accessing the directory of my lua files. I've tried appdir, workingdir, savedir, and userdir.

What I would like to make is something like

Code: Select all

love.quit = function()
     if _G.debugBuild then
          local baseDir = love.filesystem.getBaseDirectory()   --???
          local dirname = os.date("%d.%m.%Y")
          makeDir(dirname)
          util.deepFileCopy(baseDir, dirname)
     end
     return false
end
Is something like this just impossible? Is there some easy way? Is there some hard way?
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: How to access main.lua's directory?

Post by Azhukar »

natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Re: How to access main.lua's directory?

Post by natev »

Thank you, that's very handy! It's not quite what I need-- but I discovered that "" or "/" is the read path containing main.lua. Not what I would have guessed since "" or "/" is the write path for appdata.

Still running into weird cases-- are folders created in the base directory and then immediately copied to appdata upon filesystem.makeDirectory? In any case, it can be worked around.

If it's useful to anybody, or if anybody wants to tell me a better way to do it, here is the function(s) I've settled on:

Code: Select all

M.copyFile = function(src, dest)
	local data = love.filesystem.read(src)
	if type(data)~="string" then M.log("data in "..src.." is of type "..type(data)) return false end
	love.filesystem.write(dest, data)
end

M.versionControl = function()
	local dirname = os.date("%d.%m.%Y")
	love.filesystem.createDirectory(dirname)
	local source = "/"
	local recursiveFileCopy
	
	recursiveFileCopy = function(src, dest)
	--src is string like "/" or "class/", dest is string like "112200" or "112200/class"
		local files = love.filesystem.getDirectoryItems(src)
		for key, file in ipairs(files) do
			if love.filesystem.isDirectory(src..file) and file~=dirname then
			--don't ask me why I need this, maybe it's writing the folders to basedir first, otherwise makes dirname/dirname/dirname to infinity
				love.filesystem.createDirectory(dest.."/"..file)
				recursiveFileCopy(src..file.."/", dest.."/"..file)
			elseif file and file~=dirname then
				M.copyFile(src..file, dest.."/"..file)
			end
		end
	end
	recursiveFileCopy(source, dirname)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 47 guests