Some way to check if running from source code vs. .love file? (solved)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Some way to check if running from source code vs. .love file? (solved)

Post by Ross »

Is there any way to check if the game is being run directly from the source files vs. run from a .love file?

I would like to get the file path to the folder where main.lua is if run from source files, or the folder where the .love (or .exe, etc.) is if it's zipped up or packaged. For some reason I thought that love.filesystem.isFused() would return true if the game was in a .love file, but that's not the case.
Last edited by Ross on Wed Dec 07, 2022 5:02 pm, edited 1 time in total.
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Some way to check if running from source code vs. .love file?

Post by Ross »

How are you thinking I would use those for my purpose?

I already know I can use love.filesystem.getSource() and love.filesystem.getSourceBaseDirectory() to pretty much get the path that I want, the problem is, I can't tell if getSource() gives me the folder that I want, or the .love file (which I don't want, I want the folder that it's in). I could theoretically check if the file extension is ".love", but that's pretty questionable since the file extension isn't actually required to be that.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Some way to check if running from source code vs. .love file?

Post by MrFariator »

Personally, I wound up going with a build system that includes a file that's only present in .love or fused builds, when those get made. If this file is not present, then the game assumes it's running from source.
User avatar
knorke
Party member
Posts: 237
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Some way to check if running from source code vs. .love file?

Post by knorke »

Maybe use some command-line argument when running from source/editor?
Those can be read in https://love2d.org/wiki/love.load
If the arguement is missing then it was not launched from sources.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: Some way to check if running from source code vs. .love file?

Post by Andlac028 »

Use:

Code: Select all

if love.filesystem.getRealDirectory('main.lua') then
    print('running from .love file located at ' .. love.filesystem.getRealDirectory('main.lua'))
else
    print('running from directly from directory, not love file')
end
(Idea stolen from boot.lua from LÖVE's source code)
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Some way to check if running from source code vs. .love file?

Post by Ross »

Thanks a lot for all the replies!

@MrFariator - Ah, that is a good idea, foolproof and automatic once set up. Thanks!

@knorke - Also a nice idea. I currently don't have an action set up to run things from my editor, I use a separate terminal window, so I'd just have to remember to give the argument. It could just be a single letter and I could always set up an alias, so no biggie. Thanks!

@Andlac028 - I think the situation must be a bit different when this is used in boot.lua, because when I use it in main.lua it returns a directory regardless of whether it's in a .love file or not. However, this did lead me to a third solution that also seems to work pretty well—since the lua io functions won't have read/write access inside an archive, you can get the real path to main.lua and attempt to read it. If it fails then you know it's inside a .love/.zip. Maybe not foolproof for all scenarios, but it works for my case (it might be better to try writing to an extra file just for this purpose, which also works but adds a little clutter). Thanks!

Code: Select all

local realDir = love.filesystem.getRealDirectory("main.lua")
local file = io.open(realDir.."/main.lua", "r")
if file then
	print("Could open file, running from source.")
	file:close()
else
	print("Could not open file, running from archive (.love/.zip).")
end
Thanks again for all the replies. I'll probably use each solution at different times depending on where I am with a project, whether I'm sharing it with other people, etc. Problem solved.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Some way to check if running from source code vs. .love file? (solved)

Post by MrFariator »

Yeah, my solution might require some extra steps - but I found it to be a necessary thing when sharing builds for testers and the like. Basically, in my build script (just a batch file for Windows fused releases), I have it do this as part of the bundling process:

Code: Select all

echo return { BUILD_DATE='%YYYY%-%MM%-%DD%-%HH%%Min%%Sec%'} > .\FolderToBeFused\BUILD_INFORMATION.lua
which results in a lua file like

Code: Select all

return { BUILD_DATE='2022-12-07-031303'}
And when the game launches, it tries to require this file, so that it can display the build date information on title screen or such. Avoids ambiguity in case if a tester is accidentally using an older build than they are supposed to.

Of course, if an user were to unzip the .love or fused executable, this build information file is present, so running from that "source" might still make things a bit funky - but I think that's an edge case you needn't worry about most of the time.
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Some way to check if running from source code vs. .love file? (solved)

Post by Ross »

Thanks for the info and snippet. That's probably a good idea to use date & time instead of, or at least in addition to, a more obscure version system. I wasn't too worried about the setup. For now I am just testing different things with a new project to make sure this was even possible, but I'll definitely make a build script before long though.
Post Reply

Who is online

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