create config files in the user's home dir from a zip template

This new feature reads a list of config files from a zip file named
config/configtemplates.zip and checks each file, if it is already
existing in the game's search path. If it can not be found, it will
be created in the user's home dir (where saved.cfg gets written).

Before this, the config files were installed with the rest of the game,
and if the user customised any of the files, he most likely did not move
the file to his personal directory first. A re-install in the same place
would then overwrite the customised file.

This feature creates the files where they will usually not be overwritten.

To use the feature, before packaging the game, the files
 config/autoexec.cfg
 config/favourites.cfg
 config/pcksources.cfg
have to be archived into a zip file
 config/configtemplates.zip

The zip file should not contain directories. It should not be checked
into git. The original single config files should not be distributed
with the package.
This commit is contained in:
ac-stef
2014-10-05 23:30:29 +02:00
parent d021909224
commit 41fd31deed
2 changed files with 36 additions and 0 deletions
+1
View File
@@ -8,6 +8,7 @@ config/history
config/init.cfg
config/saved.cfg
config/servers.cfg
config/configtemplates.zip
source/enet/.deps/
source/enet/.libs/
source/enet/Makefile
+35
View File
@@ -905,6 +905,39 @@ const char *rndmapname()
else return "";
}
void createconfigtemplates(const char *templatezip) // create customisable config files in homedir - only if missing
{
if(addzip(templatezip))
{
vector<char *> files;
listzipfiles("", "cfg", files); // only look for config files in the root dir of the zip file
loopv(files)
{
defformatstring(fname)("config%c%s.cfg", PATHDIV, behindpath(files[i])); // (the "behindpath()" should not be necessary, but we want to be careful: the zip may be weird)
stream *f = openrawfile(fname, "rb");
if(f) delete f; // config already exists
else
{
int flen;
char *buf = loadfile(behindpath(fname), &flen); // fetch file content from zip
if(buf)
{
f = openfile(fname, "wb");
if(f)
{
f->write(buf, flen);
delete f;
conoutf("created %s from template %s", fname, templatezip);
}
delete[] buf;
}
}
delete[] files[i];
}
removezip(templatezip);
}
}
extern void connectserv(char *, int *, char *);
void connectprotocol(char *protocolstring, string &servername, int &serverport, string &password, bool &direct_connect)
@@ -1085,6 +1118,8 @@ int main(int argc, char **argv)
}
if(quitdirectly) return EXIT_SUCCESS;
createconfigtemplates("config/configtemplates.zip");
i18nmanager i18n("AC", path("packages/locale", true));
initing = NOT_INITING;