2017-10-07 19:44:23 +02:00
|
|
|
#include <platform.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
#include "orderfile.h"
|
|
|
|
|
2018-02-14 20:00:48 +01:00
|
|
|
#include "kernel/calendar.h"
|
|
|
|
#include "kernel/faction.h"
|
|
|
|
#include "kernel/messages.h"
|
|
|
|
#include "kernel/order.h"
|
|
|
|
#include "kernel/unit.h"
|
2017-10-07 18:03:22 +02:00
|
|
|
|
2018-09-29 19:32:39 +02:00
|
|
|
#include "util/base36.h"
|
|
|
|
#include "util/message.h"
|
|
|
|
#include "util/language.h"
|
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/filereader.h"
|
|
|
|
#include "util/param.h"
|
|
|
|
#include "util/parser.h"
|
2018-12-03 19:19:06 +01:00
|
|
|
#include "util/password.h"
|
2018-09-29 19:32:39 +02:00
|
|
|
#include "util/order_parser.h"
|
2017-10-07 18:03:22 +02:00
|
|
|
|
2017-10-07 19:44:23 +02:00
|
|
|
#include <assert.h>
|
2018-09-30 13:45:19 +02:00
|
|
|
#include <errno.h>
|
2017-10-07 18:03:22 +02:00
|
|
|
#include <stdio.h>
|
2017-10-07 19:44:23 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-09-29 21:19:24 +02:00
|
|
|
static void begin_orders(unit *u) {
|
2018-12-02 15:53:21 +01:00
|
|
|
if ((u->flags & UFL_ORDERS) == 0) {
|
2018-09-29 21:19:24 +02:00
|
|
|
order **ordp;
|
|
|
|
/* alle wiederholbaren, langen befehle werden gesichert: */
|
|
|
|
u->flags |= UFL_ORDERS;
|
|
|
|
u->old_orders = u->orders;
|
|
|
|
ordp = &u->old_orders;
|
|
|
|
while (*ordp) {
|
|
|
|
order *ord = *ordp;
|
|
|
|
keyword_t kwd = getkeyword(ord);
|
|
|
|
if (!is_repeated(kwd)) {
|
|
|
|
*ordp = ord->next;
|
|
|
|
ord->next = NULL;
|
|
|
|
free_order(ord);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ordp = &ord->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
free_orders(&u->orders);
|
|
|
|
}
|
|
|
|
u->orders = NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-02 15:51:35 +01:00
|
|
|
static void unitorders(input *in, faction *f)
|
2017-10-07 19:44:23 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unit *u;
|
|
|
|
|
2018-12-02 15:51:35 +01:00
|
|
|
assert(f);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
i = getid();
|
2018-02-25 18:35:40 +01:00
|
|
|
u = findunit(i);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
if (u && u->faction == f) {
|
|
|
|
order **ordp;
|
|
|
|
|
2018-09-29 21:19:24 +02:00
|
|
|
begin_orders(u);
|
2017-10-07 19:44:23 +02:00
|
|
|
ordp = &u->orders;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char *s;
|
|
|
|
/* Erst wenn wir sicher sind, dass kein Befehl
|
|
|
|
* eingegeben wurde, checken wir, ob nun eine neue
|
|
|
|
* Einheit oder ein neuer Spieler drankommt */
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
s = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
if (s == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (s[0]) {
|
|
|
|
if (s[0] != '@') {
|
|
|
|
char token[64];
|
|
|
|
const char *stok = s;
|
|
|
|
stok = parse_token(&stok, token, sizeof(token));
|
|
|
|
|
|
|
|
if (stok) {
|
|
|
|
bool quit = false;
|
|
|
|
param_t param = findparam(stok, u->faction->locale);
|
|
|
|
switch (param) {
|
|
|
|
case P_UNIT:
|
|
|
|
case P_REGION:
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
case P_FACTION:
|
|
|
|
case P_NEXT:
|
|
|
|
case P_GAMENAME:
|
|
|
|
/* these terminate the orders, so we apply extra checking */
|
|
|
|
if (strlen(stok) >= 3) {
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
quit = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (quit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 11:37:32 +01:00
|
|
|
/* Nun wird der Befehl erzeut und eingehaengt */
|
2017-10-07 19:44:23 +02:00
|
|
|
*ordp = parse_order(s, u->faction->locale);
|
|
|
|
if (*ordp) {
|
|
|
|
ordp = &(*ordp)->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static faction *factionorders(void)
|
2017-10-07 18:03:22 +02:00
|
|
|
{
|
2017-10-07 19:44:23 +02:00
|
|
|
int fid = getid();
|
2018-02-25 20:06:47 +01:00
|
|
|
faction *f = findfaction(fid);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
2018-09-29 19:32:39 +02:00
|
|
|
if (f != NULL && (f->flags & FFL_NPC) == 0) {
|
2018-12-03 19:19:06 +01:00
|
|
|
char token[PASSWORD_MAXSIZE];
|
2017-10-07 19:44:23 +02:00
|
|
|
const char *pass = gettoken(token, sizeof(token));
|
|
|
|
|
|
|
|
if (!checkpasswd(f, (const char *)pass)) {
|
|
|
|
log_debug("Invalid password for faction %s", itoa36(fid));
|
|
|
|
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Die Partei hat sich zumindest gemeldet, so dass sie noch
|
2019-02-08 11:37:32 +01:00
|
|
|
* nicht als untaetig gilt */
|
2017-10-07 19:44:23 +02:00
|
|
|
f->lastorders = turn;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_debug("orders for invalid faction %s", itoa36(fid));
|
|
|
|
}
|
|
|
|
return f;
|
2017-10-07 18:03:22 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
int read_orders(input *in)
|
2017-10-07 18:03:22 +02:00
|
|
|
{
|
2017-10-07 19:44:23 +02:00
|
|
|
const char *b;
|
|
|
|
int nfactions = 0;
|
|
|
|
struct faction *f = NULL;
|
2017-11-20 08:51:06 +01:00
|
|
|
const struct locale *lang = default_locale;
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
/* TODO: recognize UTF8 BOM */
|
2017-10-07 20:17:04 +02:00
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
|
|
|
|
* Partei */
|
|
|
|
|
|
|
|
while (b) {
|
|
|
|
char token[128];
|
|
|
|
param_t p;
|
|
|
|
const char *s;
|
|
|
|
init_tokens_str(b);
|
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
p = findparam_block(s, lang, true);
|
|
|
|
switch (p) {
|
|
|
|
case P_GAMENAME:
|
|
|
|
case P_FACTION:
|
|
|
|
f = factionorders();
|
|
|
|
if (f) {
|
2017-11-20 08:51:06 +01:00
|
|
|
lang = f->locale;
|
2017-10-07 19:44:23 +02:00
|
|
|
++nfactions;
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:17:04 +02:00
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* in factionorders wird nur eine zeile gelesen:
|
|
|
|
* diejenige mit dem passwort. Die befehle der units
|
|
|
|
* werden geloescht, und die Partei wird als aktiv
|
|
|
|
* vermerkt. */
|
|
|
|
|
|
|
|
case P_UNIT:
|
2018-12-02 15:51:35 +01:00
|
|
|
if (f) {
|
|
|
|
unitorders(in, f);
|
2017-10-07 19:44:23 +02:00
|
|
|
do {
|
2017-10-07 20:17:04 +02:00
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
if (!b) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
init_tokens_str(b);
|
|
|
|
s = gettoken(token, sizeof(token));
|
|
|
|
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
|
|
|
|
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
|
|
|
|
&& p != P_GAMENAME);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
|
2019-02-08 11:37:32 +01:00
|
|
|
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
|
|
|
|
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
|
|
|
|
* auf alle Faelle nicht ueberschreiben! Bei allen anderen Eintraegen hier
|
|
|
|
* muss buf erneut gefaellt werden, da die betreffende Information in nur
|
|
|
|
* einer Zeile steht, und nun die naechste gelesen werden muss.
|
|
|
|
*/
|
2017-10-07 19:44:23 +02:00
|
|
|
|
|
|
|
case P_NEXT:
|
|
|
|
f = NULL;
|
2017-11-20 08:51:06 +01:00
|
|
|
lang = default_locale;
|
2017-10-07 20:17:04 +02:00
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-10-07 20:17:04 +02:00
|
|
|
b = in->getbuf(in->data);
|
2017-10-07 19:44:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("done reading orders for %d factions", nfactions);
|
|
|
|
return 0;
|
2017-10-07 18:03:22 +02:00
|
|
|
}
|
2017-10-07 20:17:04 +02:00
|
|
|
|
2018-09-29 21:19:24 +02:00
|
|
|
typedef struct parser_state {
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
|
|
|
order **next_order;
|
|
|
|
} parser_state;
|
|
|
|
|
|
|
|
static void handle_faction(void *userData, int no, const char *password) {
|
|
|
|
parser_state *state = (parser_state *)userData;
|
|
|
|
faction * f = state->f = findfaction(no);
|
|
|
|
if (!f) {
|
|
|
|
log_debug("orders for unknown faction %s", itoa36(no));
|
|
|
|
}
|
|
|
|
else {
|
2018-10-04 04:47:20 +02:00
|
|
|
if (checkpasswd(f, password)) {
|
|
|
|
f->lastorders = turn;
|
|
|
|
}
|
|
|
|
else {
|
2018-09-29 21:19:24 +02:00
|
|
|
log_debug("invalid password for faction %s", itoa36(no));
|
|
|
|
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", password));
|
|
|
|
}
|
2018-10-01 21:32:59 +02:00
|
|
|
state->u = NULL;
|
|
|
|
state->next_order = NULL;
|
2018-09-29 21:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_unit(void *userData, int no) {
|
|
|
|
parser_state *state = (parser_state *)userData;
|
|
|
|
unit * u = findunit(no);
|
|
|
|
|
|
|
|
state->u = NULL;
|
|
|
|
if (!u) {
|
|
|
|
/* TODO: error message */
|
|
|
|
}
|
|
|
|
else if (u->faction != state->f) {
|
|
|
|
/* TODO: error message */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->u = u;
|
|
|
|
begin_orders(u);
|
|
|
|
state->next_order = &u->orders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_order(void *userData, const char *str) {
|
|
|
|
parser_state *state = (parser_state *)userData;
|
2018-10-01 21:32:59 +02:00
|
|
|
const char * tok, *input = str;
|
2018-12-03 10:09:03 +01:00
|
|
|
char buffer[64];
|
2018-10-01 21:32:59 +02:00
|
|
|
const struct locale *lang;
|
|
|
|
param_t p;
|
|
|
|
faction * f = state->f;
|
|
|
|
|
|
|
|
lang = f ? f->locale : default_locale;
|
|
|
|
tok = parse_token(&input, buffer, sizeof(buffer));
|
|
|
|
p = findparam(tok, lang);
|
|
|
|
if (p == P_FACTION || p == P_GAMENAME) {
|
|
|
|
tok = parse_token(&input, buffer, sizeof(buffer));
|
|
|
|
if (tok) {
|
|
|
|
int no = atoi36(tok);
|
|
|
|
tok = parse_token(&input, buffer, sizeof(buffer));
|
|
|
|
handle_faction(userData, no, tok);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* TODO: log_error() */
|
|
|
|
}
|
2018-09-29 21:19:24 +02:00
|
|
|
}
|
2018-10-01 21:32:59 +02:00
|
|
|
else if (p == P_UNIT) {
|
|
|
|
tok = parse_token(&input, buffer, sizeof(buffer));
|
|
|
|
if (tok) {
|
|
|
|
int no = atoi36(tok);
|
|
|
|
handle_unit(userData, no);
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 18:57:21 +02:00
|
|
|
else if (p == P_NEXT) {
|
|
|
|
state->f = NULL;
|
|
|
|
state->u = NULL;
|
|
|
|
state->next_order = NULL;
|
|
|
|
}
|
|
|
|
else if (p == P_REGION) {
|
|
|
|
state->u = NULL;
|
|
|
|
state->next_order = NULL;
|
|
|
|
}
|
2018-10-01 21:32:59 +02:00
|
|
|
else if (state->u) {
|
|
|
|
unit * u = state->u;
|
|
|
|
order * ord = parse_order(str, lang);
|
|
|
|
if (ord) {
|
|
|
|
*state->next_order = ord;
|
|
|
|
state->next_order = &ord->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADDMSG(&u->faction->msgs, msg_message("parse_error", "unit command", u, str));
|
|
|
|
}
|
2018-09-29 21:19:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int parseorders(FILE *F)
|
|
|
|
{
|
2018-10-03 19:47:32 +02:00
|
|
|
char buf[4096];
|
2018-09-29 21:19:24 +02:00
|
|
|
int done = 0, err = 0;
|
|
|
|
OP_Parser parser;
|
|
|
|
parser_state state = { NULL, NULL };
|
|
|
|
|
|
|
|
parser = OP_ParserCreate();
|
|
|
|
if (!parser) {
|
|
|
|
/* TODO: error message */
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
OP_SetOrderHandler(parser, handle_order);
|
|
|
|
OP_SetUserData(parser, &state);
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
size_t len = (int)fread(buf, 1, sizeof(buf), F);
|
|
|
|
if (ferror(F)) {
|
|
|
|
/* TODO: error message */
|
|
|
|
err = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done = feof(F);
|
|
|
|
if (OP_Parse(parser, buf, len, done) == OP_STATUS_ERROR) {
|
|
|
|
/* TODO: error message */
|
2018-10-02 21:40:14 +02:00
|
|
|
err = (int)OP_GetErrorCode(parser);
|
2018-09-29 21:19:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OP_ParserFree(parser);
|
|
|
|
return err;
|
|
|
|
}
|