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.
Some way to check if running from source code vs. .love file? (solved)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Some way to check if running from source code vs. .love file? (solved)
Last edited by Ross on Wed Dec 07, 2022 5:02 pm, edited 1 time in total.
Re: Some way to check if running from source code vs. .love file?
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.
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.
-
- Party member
- Posts: 563
- Joined: Wed Oct 05, 2016 11:53 am
Re: Some way to check if running from source code vs. .love file?
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.
Re: Some way to check if running from source code vs. .love file?
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.
Those can be read in https://love2d.org/wiki/love.load
If the arguement is missing then it was not launched from sources.
Re: Some way to check if running from source code vs. .love file?
Use:
(Idea stolen from boot.lua from LÖVE's source code)
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
Re: Some way to check if running from source code vs. .love file?
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!
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 - 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
-
- Party member
- Posts: 563
- Joined: Wed Oct 05, 2016 11:53 am
Re: Some way to check if running from source code vs. .love file? (solved)
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:
which results in a lua file like
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.
Code: Select all
echo return { BUILD_DATE='%YYYY%-%MM%-%DD%-%HH%%Min%%Sec%'} > .\FolderToBeFused\BUILD_INFORMATION.lua
Code: Select all
return { BUILD_DATE='2022-12-07-031303'}
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.
Re: Some way to check if running from source code vs. .love file? (solved)
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.
Who is online
Users browsing this forum: No registered users and 9 guests