[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.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg »

ivan wrote: Thu Jun 21, 2018 5:34 pm
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?
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. :)
Usually, people did have an installation folder on their CDs/DVDs in the olden times, and they installed the games on their hard drives, thereby that one folder becoming the game's own folder itself, where it was free to write and read data; same location as the game files themselves, less time spent trying to figure out why one's C: drive was constantly running out of space, for one. (or partition, whatever).

This is one of my biggest issues, and steam did solve it kinda; i don't need to locally store any installation files, and i guess the tradeoff is that you can't have selective file sync for games with it... i guess? i'm not exactly sure, never needed to look into it.

Then again, maybe INI files haven't been in development, but a lot of games and game engines still use the format; i do agree that since löve is lua based, and lua is a scripting language, it would make sense to have config files in lua... with the caveat mentioned just above; needing to sanitize anything in said file(s).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
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 »

Using the lua file is an advice I'll probably take myself. As much as I actually enjoy coding parsers, it's a bit time consuming, and as @grump said, some bugs may be introduced. Especially if you're, like me, not much of an expert in parsers at all. :)

I'm sure there's plenty of caveats in this code: (the most obvious one is I'm manually using specific functions for each data type, because I postponed automating that on the parser side because it would take a while (assuming I'd manage to get it done).)

Code: Select all

function Ini:get_value(key)
    assert(self._by_key[key], "\n\n    (Ini) Assertion failed:\n\n\t    Key '".. key .."' doesn't exist.\n\n")
    return self._by_cat[ self._by_key[key] ][key]      end

function Ini:get_bool(key)
    return self:get_value( key ) ~= '0' 
    and self:get_value( key ) ~= 'false' end

function Ini:get_number(key) return tonumber(self:get_value( key )) end
function Ini:get_string(key) return self:get_value( key ) end
function Ini:get_table(key) return self:parse_table(self:get_value( key )) end
function Ini:get_color(key) return self:parse_color(self:get_value( key )) end
function Ini:parse_color(c) 
    if c == nil then return {1, 1, 1, 1} end
    local r, g, b, a

    if c:starts_with("{") then
        c = c:sub(2, -2):gsub(' ', ''):split(',')
        -- num() is just a shorthand for tonumber() that I made
        r, g, b, a = num(c[1]) or 1, num(c[2]) or 1, num(c[3]) or 1, num(c[4]) or 1
        c = { r, g, b, a }
    elseif c:starts_with("#") then
        r = num(c:sub(2, 3), 16) / 255
        g = num(c:sub(4, 5), 16) / 255
        b = num(c:sub(6, 7), 16) / 255
        a = num(c:sub(8, 9), 16) / 255
        c = {r, g, b, a}
    end
    return c
end
ivan wrote: Thu Jun 21, 2018 5:34 pm 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?
On the other hand, when the user uninstalls an app, litter will be left lying around in appdata. That's also one of my major objections to it (and by the way, my objections pertain to desktop/laptop platforms only - I understand that there's also security issues and stuff on mobile/web/etc). But maybe that's a problem with windows? I never used mac and linux, so maybe they sort that out while windows never did (up to win7 at least).
"You can't find LÖVE in the dictionary." -- Daniel Dennett
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 »

setfenv is probably all you need to make configuration data secure.

Note that the installation folder should be treated as read-only in any case. Don't expect the user to be able to edit configuration files that are saved outside his home directory.
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 »

grump wrote: Thu Jun 21, 2018 7:51 pmDon't expect the user to be able to edit configuration files that are saved outside his home directory.
I think that for just about every game I played that had editable files in system specific folders, I saw people asking online where to find them. On one hand many have no clue where files go apart from where the installation wizards tell them, on the other hand it's still not really that obvious where they go when you know where to look. Sometimes it's somewhere in appdata, sometimes it's My Documents, sometimes it's MyGames, sometimes it's the User folder... And then it's also different for every version of the OS (at least windows). And of course, every dev has their own way of organizing things. My MyDocuments folder alone is a scary mess...

I think one shouldn't expect anything from the user, but one can expect many desktop users will want to tinker with whatever your app has that can be tinkered with.

In my own case with this game I'm not even expecting anything at all. It's more of a learning project so it's probably never gonna be an actual game to begin with, but if it does, then it's also more on the technical side of things and a bit niche anyway, so... Although I had some ideas on making a UI for it that could make it more gamey.
"You can't find LÖVE in the dictionary." -- Daniel Dennett
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno »

I may be pretty much alone in thinking that using Lua for configuration files is not a good idea. There are several reasons, but the main ones are DoS and memory. Among the many text serialization libraries, there's one that stands out for the fact that it does not generate Lua, even though it supports the normal Lua types: Smallfolk, created by forum member Robin. For something as simple as tables/arrays of strings/numbers/booleans, JSON is also good.
User avatar
ivan
Party member
Posts: 1911
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 »

KayleMaster wrote: Thu Jun 21, 2018 6:17 pm Do I need to sanitize the lua file somehow?
Basic sandboxing is recommended when running code from the AppData directory.
grump wrote: Thu Jun 21, 2018 7:51 pm setfenv is probably all you need to make configuration data secure.
That's a good way to do it.
Master Thief wrote: Thu Jun 21, 2018 6:45 pm On the other hand, when the user uninstalls an app, litter will be left lying around in appdata.
Generally speaking, preserving the user's save files is considered a good thing.
These could be (optionally) deleted by an uninstaller.
While running unfused .love files everything goes in the "LOVE" folder anyways.
pgimeno wrote: Thu Jun 21, 2018 10:31 pm I may be pretty much alone in thinking that using Lua for configuration files is not a good idea. There are several reasons, but the main ones are DoS and memory.
Do you mean infinite loops or malicious code in the save file?
If you use setfenv the worst thing that could happen is that the process would hang.
This could be handled gracefully with slightly more sophisticated sandboxing but it's not a big concern for games.
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 tried JSON a few times, and to be honest, didn't click. My biggest peeve is that parsers (the ones I tried) don't ever save the files in an easily readable way. It makes it harder to learn, as you have to waste time manually sorting it out in multiple lines to see if the output is as you intended. Xml is also usually convoluted as hell to deal with, so it's been quite a few years that I turned to ini-ish file types. It's pretty simple to parse, even for me, and if you need to lay it out by hand it's peanuts. I'm also a fan of white space separated stuff like this guy did.
"You can't find LÖVE in the dictionary." -- Daniel Dennett
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 »

pgimeno wrote: Thu Jun 21, 2018 10:31 pmJSON is also good.
https://arp242.net/weblog/json_as_confi ... lease_dont
Master Thief wrote: Thu Jun 21, 2018 10:02 pm
grump wrote: Thu Jun 21, 2018 7:51 pmDon't expect the user to be able to edit configuration files that are saved outside his home directory.
I think that for just about every game I played that had editable files in system specific folders, I saw people asking online where to find them.
Just put a link/shortcut in the installation folder. %appdata%/<identity> should point to the correct location for your LÖVE game on Windows.
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 »

This is how factorio does it, it has shortcuts to the mods, saves folders and it's really useful.
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 »

BTW, it's two lines of code to securely read and parse configuration files written in Lua:

Code: Select all

local config = {} -- you can put your defaults here
setfenv(assert(love.filesystem.load('test.conf')), config)()
This is what the config file looks like:

Code: Select all

option1=42
option2="whatever"
subsection={ 
    suboption=23
}
A malicious user could place some kind of DoS code in there though. Like, an endless loop or some kind of memory exhausting code, but other than that, it's pretty secure.

Gracefully catching errors with pcall instead of a hard assert would be a good idea though. A big downside is the rather strict table syntax. If you want configuration to be easy and fault tolerant, Lua is not the best way to do that. JSON has the same problem.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests