Page 1 of 1

How to access main.lua's directory?

Posted: Thu Dec 11, 2014 8:18 pm
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 ="%d.%m.%Y")
          util.deepFileCopy(baseDir, dirname)
     return false
Is something like this just impossible? Is there some easy way? Is there some hard way?

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

Posted: Thu Dec 11, 2014 9:19 pm
by Azhukar

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

Posted: Thu Dec 11, 2014 9:54 pm
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 =
	if type(data)~="string" then M.log("data in "..src.." is of type "..type(data)) return false end
	love.filesystem.write(dest, data)

M.versionControl = function()
	local dirname ="%d.%m.%Y")
	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
				recursiveFileCopy(src..file.."/", dest.."/"..file)
			elseif file and file~=dirname then
				M.copyFile(src..file, dest.."/"..file)
	recursiveFileCopy(source, dirname)