From 41fd31deed43c68e02153def6eeb9cd014f9bc5e Mon Sep 17 00:00:00 2001 From: ac-stef Date: Sun, 5 Oct 2014 23:30:29 +0200 Subject: [PATCH] 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. --- .gitignore | 1 + source/src/main.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/.gitignore b/.gitignore index a6cbd554..b2407f2a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/source/src/main.cpp b/source/src/main.cpp index 640e9463..4892021c 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -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 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;