[SOLVED] Is it possible to have a config file next to executable?

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.
Master Thief
Prole
Posts: 8
Joined: Wed Jun 13, 2018 3:39 pm

[SOLVED] Is it possible to have a config file next to executable?

Post by Master Thief »

I'm making a Conways Game Of Life type of thing and I'd like to have a .ini file with a bunch of settings right next to the executable. I managed to get this to work but only for while I've been coding the game. I noticed a few days when reading the docs that I would have to do something more to make it work after I pack it into the .exe file, but now that I've been trying to figure it out, I'm stuck on it... I wonder if it's possible.

I didn't quite understand love's filesystem at first, so this is how I got it to work (I have an Ini "class" for this):

Code: Select all

function Ini:load_file(file) -- receives a "life.ini" string from main.lua
	self._file = file
	if love.filesystem.getInfo(self._file) then
		local f = love.filesystem.read(self._file)
		
		-- this parses the file and stores everything in a table of key/value pairs
		self:store_data( f:split('\n') ) 
	else
		print("\n\n  (ini) ERROR: '".. self._file .. "' does not exist or something.\n\n")
	end
end
Just a snippet of the ini file itself. Probably not relevant.

Code: Select all

	[DISPLAY]
	use_textures = 0
	cellsize_scalar = 2	// for textures use 1 = 16x16 | 2 = 8x8 | 4 = 4x4
	backgroundColor = {0, 0, 0, 1}
	
	[MISC]
	randomize_at_start = 0
	start_paused = 1
	screen_wrap = 1	// if true, cells at one end count as neighbors for the ones on the opposite end

That code above requires that life.ini is amidst the lua files, but that's the equivalent of it being inside the .love file or packed into the exe. It works packed into the exe, but then, the file is inaccessible...

I tried adapting the code that's in the docs but I got stuck trying to figure out how to load a file that isn't an image/sound/etc.

Is there a way to do this?
Last edited by Master Thief on Thu Jun 21, 2018 6:29 am, edited 1 time in total.
"You can't find LÖVE in the dictionary." -- Daniel Dennett
Master Thief
Prole
Posts: 8
Joined: Wed Jun 13, 2018 3:39 pm

Re: Is it possible to have a config file next to executable?

Post by Master Thief »

I gave it another try yesterday evening with a fresh mind and I managed to figure it out! It's actually so much simpler than it seemed...

Code: Select all

function Ini:load_file(file) -- receives a "life.ini" string
    self._file = file

    local dir = love.filesystem.getSourceBaseDirectory( )
    local file_path = dir .."/".. file
    local f = nil

    if love.filesystem.isFused() and love.filesystem.mount(dir, "root") then
        f = love.filesystem.read("root/".. file)
    else
        f = io.open( file_path ):read('*a')
    end
    
    assert(f, "\n\n  (ini) Assertion failed: could not load '".. file_path .."'.\n\n")
    self:store_data( f:split('\n') )
end
Last edited by Master Thief on Thu Jun 21, 2018 3:05 pm, edited 1 time in total.
"You can't find LÖVE in the dictionary." -- Daniel Dennett
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by KayleMaster »

Thanks, this is really useful!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by ivan »

For parsing ini files you could look at barbes' inifile
https://github.com/bartbes/inifile
Having said that, please consider what you are trying to do.
Especially if your ini file is not read-only.
First off, you don't want to be writing in the game directory since that could be a zipped in a .love file.
Depending on the OS you may not have permissions to write in the install folder.
So in general you want to save stuff in the AppData folder.
Next, INIs are charming but that format has been gone for a long time.
Plus you will lose all of your type information.
Lua is much more powerful and can be loaded directly without parsing:

Code: Select all

return {
display={
  use_textures = 0,
  cellsize_scalar = 2,
  backgroundColor = {0, 0, 0, 1},
},
misc={
  randomize_at_start = 0,
  start_paused = 1,
  screen_wrap = 1,
},
}
Just save your options as a Lua file and everything will be much simpler.
It makes much more sense than mixing Lua code in your INIs:

Code: Select all

	[DISPLAY]
	backgroundColor = {0, 0, 0, 1}
Master Thief
Prole
Posts: 8
Joined: Wed Jun 13, 2018 3:39 pm

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by Master Thief »

I don't think any of that could affect this particular project, but I think it's worth keeping all that in mind. Especially that about permissions. As far as I'm aware permissions are only usually denied in mobile and web platforms, which I don't make anything for (so far, at least). Either way, it's something I intend to look properly into whenever I develop something more serious. Personally, as a user I always disliked having stuff in the common clutter folders... I mean data folders, and as a "developer" I dislike it even more, as I have to be looking into two distinct folders for the game's content and source...

I guess it might be something to consider having a check for permissions, and if there's none, then write to the appdata.

But in this case though, the ini file is more for just letting the user change a few options before running the game. It's not of that much importance either, as I only use the Game Of Life as my own way of learning new languages and frameworks.

By the way, that wasn't Lua code. :) I initially had colors in hex, but then I wondered if I could implement a parser for a group of decimals, and so I did. The curly braces were an arbitrary choice, I could've used parentheses. A line that wasn't included in the snippet had remnants of the hex colors.

Code: Select all

aliveCellColor        = {1, .5, 0, 1}       //FF8000
I have fun with these things. :)

All that said, thanks for the ideas.
"You can't find LÖVE in the dictionary." -- Daniel Dennett
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by KayleMaster »

I don't agree with ivan's suggestions that INI files have been gone for a long time. They're super useful for changing game settings when you can't do that ingame, due to wrong game settings! And also a bunch of other stuff. They're still used for many commercial games and nearly every game has an ini file in it's directory or subdir.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by grump »

KayleMaster wrote: Thu Jun 21, 2018 4:02 pm I don't agree with ivan's suggestions that INI files have been gone for a long time. They're super useful for changing game settings when you can't do that ingame, due to wrong game settings! And also a bunch of other stuff. They're still used for many commercial games and nearly every game has an ini file in it's directory or subdir.
I don't get it. Why would anyone use a limited and aged format like ini files, when there's a much better alternative right there in your game framework? Lua was literally designed as a configuration language.

Using ini files just for the sake of it smells like cargo cult programming. It has zero advantages and introduces unnecessary bloat and sources for bugs in the parser. LÖVE is a Lua framework. Use it.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by ivan »

Master Thief wrote: Thu Jun 21, 2018 3:41 pm Especially that about permissions. As far as I'm aware permissions are only usually denied in mobile and web platforms, which I don't make anything for (so far, at least).
Depends on the platform. Windows does have the option for "restricted" accounts too.
Apps are NOT supposed to save data in the installation folder
so that the size of the install folder remains constant which is important in cases like mobile.
Or what if I'm oldschool and I'm running your game off read-only memory like a CD or DVD? :)
Or what if you're using a distribution service/client like Steam that syncs the install folder automatically?
By the way, that wasn't Lua code. :) I initially had colors in hex, but then I wondered if I could implement a parser for a group of decimals, and so I did. The curly braces were an arbitrary choice, I could've used parentheses.
This is another idiosyncrasy introduced to your code.
I'm aware that the INI format is based on strings,
but (in this case) I consider that as a shortcoming compared to plain Lua.
I don't agree with ivan's suggestions that INI files have been gone for a long time. They're super useful for changing game settings when you can't do that ingame, due to wrong game settings! And also a bunch of other stuff. They're still used for many commercial games and nearly every game has an ini file in it's directory or subdir.
I'm sure INI files are still floating around, but it's a legacy thing.
Haven't heard of any new developments around the INI format. :)

Don't mean to sound grump-y, but yea sure go ahead and use INIs,
just remember that Lua provides a much simpler way. :)
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by grump »

ivan wrote: Thu Jun 21, 2018 5:34 pm Haven't heard of any new developments around the INI format. :)
I've heard of some extensions to the format though that allow subsections. Still no reason to favor it over Lua, especially for strict read-only purposes like reading from the app folder.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: [SOLVED] Is it possible to have a config file next to executable?

Post by KayleMaster »

Jeez ok, I'll use lua.
Do I need to sanitize the lua file somehow?
Post Reply

Who is online

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