read-ahead for server info files

* poll and read motd and serverinfo along the other server config files
* "en" language setting is now immutable
This commit is contained in:
ac-stef
2017-06-27 15:15:22 +02:00
parent e147000ba2
commit b82a38b2e2
4 changed files with 62 additions and 77 deletions
+2 -3
View File
@@ -2,7 +2,6 @@
// ========================
// WARNING: You can NOT use CubeScript in here!!
//
// This text file is read, whenever a the MOTD is sent to a client.
// Comments are removed, also leading and trailing blanks and empty lines.
//
// Don't use more than 80 characters per line. You can use colors and TABs.
@@ -14,7 +13,7 @@
// Y: light gray, Z: dark gray
//
// Example:
// BobsInstagibServer \f3play nicely!\n
// Coloring a motd is fun: \f0Green\f5 and another \fLgreen\f5 from the set.\n
// BobsInstagibServer \f3play nicely!
// Coloring a motd is fun: \f0Green\f5 and another \fLgreen\f5 from the set.
// contact me at [email protected]
+2 -1
View File
@@ -213,7 +213,8 @@ enum
#define FTXT__MDLATTR (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_TABTOBLANK | FTXT_NOCOLOR | FTXT_SAFECS)
#define FTXT__DEMONAME (FTXT_NOCOLOR | FTXT_MAPNAME)
#define FTXT__PLAYERNAME (FTXT_NOWHITE | FTXT_NOCOLOR)
#define FTXT__SERVERINFO (FTXT_ALLOWTAB)
#define FTXT__SERVERINFOLINE (FTXT_ALLOWTAB | FTXT_CROPWHITE)
#define FTXT__SERVERINFO (FTXT_ALLOWTAB | FTXT_ALLOWNL)
#define FTXT__MOTD (FTXT_TABTOBLANK)
#define FTXT__CHAT (FTXT_NOWHITE | FTXT_ALLOWBLANKS | FTXT_NOCOLOR | FTXT_TABTOBLANK)
#define FTXT__LOG (FTXT_NOCOLOR | FTXT_TABTOBLANK)
+17 -13
View File
@@ -83,7 +83,8 @@ serveripcclist geoiplist;
servernickblacklist nickblacklist;
serverforbiddenlist forbiddenlist;
serverpasswords passwords;
serverinfofile infofiles;
serverinfofile serverinfoinfo;
serverinfofile serverinfomotd;
// server state
bool isdedicated = false;
@@ -2718,8 +2719,8 @@ void welcomepacket(packetbuf &p, int n)
}
putint(p, SV_SERVERMODE);
putint(p, sendservermode(false));
const char *motd = scl.motd[0] ? scl.motd : infofiles.getmotd(c ? c->lang : "");
if(motd)
const char *motd = scl.motd[0] ? scl.motd : serverinfomotd.getmsg();
if(*motd)
{
putint(p, SV_TEXT);
sendstring(motd, p);
@@ -4159,16 +4160,19 @@ void extping_namelist(ucharbuf &p)
void extping_serverinfo(ucharbuf &pi, ucharbuf &po)
{
char lang[3];
lang[0] = tolower(getint(pi)); lang[1] = tolower(getint(pi)); lang[2] = '\0';
const char *reslang = lang, *buf = infofiles.getinfo(lang); // try client language
if(!buf) buf = infofiles.getinfo(reslang = "en"); // try english
sendstring(buf ? reslang : "", po);
if(buf)
getint(pi); getint(pi); // dummy read language code
if(strlen(serverinfoinfo.getmsg()))
{
for(const char *c = buf; *c && po.remaining() > MAXINFOLINELEN + 10; c += strlen(c) + 1) sendstring(c, po);
sendstring("", po);
sendstring("en", po);
int pos = 0;
string buf;
while(po.remaining() > MAXINFOLINELEN + 10)
{
sendstring(serverinfoinfo.getmsgline(buf, &pos), po);
if(!*buf) break;
}
}
else sendstring("", po);
}
void extping_maprot(ucharbuf &po)
@@ -4337,8 +4341,8 @@ void initserver(bool dedicated)
geoiplist.init(scl.geoipfile);
nickblacklist.init(scl.nbfile);
forbiddenlist.init(scl.forbidden);
infofiles.init(scl.infopath, scl.motdpath);
infofiles.getinfo("en"); // cache 'en' serverinfo
serverinfoinfo.init(scl.infopath, MAXTRANS / 2);
serverinfomotd.init(scl.motdpath, MAXSTRLEN - 5);
logline(ACLOG_VERBOSE, "holding up to %d recorded demos in memory", scl.maxdemos);
if(scl.demopath[0]) logline(ACLOG_VERBOSE,"all recorded demos will be written to: \"%s\"", scl.demopath);
if(scl.voteperm[0]) logline(ACLOG_VERBOSE,"vote permission string: \"%s\"", scl.voteperm);
+41 -60
View File
@@ -1298,78 +1298,59 @@ struct serverpasswords : serverconfigfile
#define MAXINFOLINELEN 100 // including color codes
struct serverinfofile
struct serverinfofile : serverconfigfile // plaintext info file, used for serverinfo and motd
{
struct serverinfotext { const char *type; char lang[3]; char *info; int lastcheck; };
vector<serverinfotext> serverinfotexts;
const char *infobase, *motdbase;
vector<char> msg;
int maxlen;
void init(const char *info, const char *motd) { infobase = info; motdbase = motd; }
char *readinfofile(const char *fnbase, const char *lang)
void init(const char *fnbase, int ml)
{
defformatstring(fname)("%s_%s.txt", fnbase, lang);
path(fname);
int len, n;
char *c, *b, *s, *t, *buf = loadfile(fname, &len);
if(!buf) return NULL;
char *nbuf = new char[len + 2];
for(t = nbuf, s = strtok_r(buf, "\n\r", &b); s; s = strtok_r(NULL, "\n\r", &b))
{
c = strstr(s, "//");
if(c) *c = '\0'; // strip comments
for(n = (int)strlen(s) - 1; n >= 0 && s[n] == ' '; n--) s[n] = '\0'; // strip trailing blanks
filterrichtext(t, s + strspn(s, " "), MAXINFOLINELEN); // skip leading blanks
n = (int)strlen(t);
if(n) t += n + 1;
}
*t = '\0';
DELETEA(buf);
if(!*nbuf) DELETEA(nbuf);
logline(ACLOG_DEBUG,"read file \"%s\"", fname);
return nbuf;
maxlen = ml;
defformatstring(fname)("%s_en.txt", fnbase);
serverconfigfile::init(fname);
}
const char *getinfocache(const char *fnbase, const char *lang)
void clear()
{
serverinfotext sn = { fnbase, { 0, 0, 0 }, NULL, 0} , *s = &sn;
filterlang(sn.lang, lang);
if(!sn.lang[0]) return NULL;
loopv(serverinfotexts)
{
serverinfotext &si = serverinfotexts[i];
if(si.type == s->type && !strcmp(si.lang, s->lang))
{
if(servmillis - si.lastcheck > (si.info ? 15 : 45) * 60 * 1000)
{ // re-read existing files after 15 minutes; search missing again after 45 minutes
DELETEA(si.info);
s = &si;
}
else return si.info;
}
}
s->info = readinfofile(fnbase, lang);
s->lastcheck = servmillis;
if(s == &sn) serverinfotexts.add(*s);
if(fnbase == motdbase && s->info)
{
char *c = s->info;
while(*c) { c += strlen(c); if(c[1]) *c++ = '\n'; }
if(strlen(s->info) > MAXSTRLEN) s->info[MAXSTRLEN] = '\0'; // keep MOTD at sane lengths
}
return s->info;
msg.shrink(0);
}
const char *getinfo(const char *lang)
void read()
{
return getinfocache(infobase, lang);
string s;
char *l, *p = buf;
logline(ACLOG_VERBOSE,"reading info text file '%s'", filename);
while(p < buf + filelen)
{
l = p; p += strlen(p) + 1;
ASSERT(MAXINFOLINELEN < MAXSTRLEN);
filtertext(s, l, FTXT__SERVERINFOLINE, MAXINFOLINELEN);
if(*s) cvecprintf(msg, "%s\n", s);
}
if(msg.length() > maxlen)
{
logline(ACLOG_VERBOSE,"info text file '%s' truncated: %d -> %d", filename, msg.length(), maxlen);
msg.shrink(maxlen);
}
}
const char *getmotd(const char *lang)
const char *getmsg()
{
const char *motd;
if(*lang && (motd = getinfocache(motdbase, lang))) return motd;
return getinfocache(motdbase, "en");
if(!msg.length() || msg.last()) msg.add('\0'); // make sure, msg is terminated
return msg.getbuf();
}
char *getmsgline(string s, int *pos)
{
s[0] = '\0';
if(msg.inrange(*pos))
{
const char *p = getmsg() + *pos;
int len = strcspn(p, "\n") + 1;
copystring(s, p, len);
*pos += len;
}
return s;
}
};