--- deliantra/server/socket/image.C 2006/12/14 20:39:54 1.11 +++ deliantra/server/socket/image.C 2007/04/10 09:35:24 1.40 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2001 Mark Wedel - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The author can be reached via e-mail to -*/ + * CrossFire, A Multiplayer game + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2001 Mark Wedel + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The author can be reached via e-mail to + */ /** \file * Image related communication @@ -35,27 +36,21 @@ #include #include -#include -#include -#include +#include "crc.h" #define MAX_FACE_SETS 20 /**< Maximum number of image sets the program will handle */ /** Information about one image */ typedef struct FaceInfo { - uint8 *data; /**< image data */ - uint16 datalen; /**< length of the xpm data */ - uint32 checksum; /**< Checksum of face data */ } FaceInfo; /** Information about one face set */ typedef struct { - char *prefix; /**< */ char *fullname; @@ -93,9 +88,10 @@ { if (facesets[num].prefix) { - for (q = 0; q < nrofpixmaps; q++) + for (q = 0; q < faces.size (); q++) if (facesets[num].faces[q].data) free (facesets[num].faces[q].data); + free (facesets[num].prefix); free (facesets[num].fullname); free (facesets[num].size); @@ -135,8 +131,10 @@ LOG (llevError, "get_face_fallback called with unused set (%d)?\n", faceset); return 0; /* use default set */ } + if (facesets[faceset].faces[imageno].data) return faceset; + return get_face_fallback (facesets[faceset].fallback, imageno); } @@ -162,170 +160,33 @@ LOG (llevError, "Face set %d falls to non set faceset %d\n", faceset, fallback); abort (); } + togo--; if (togo == 0) { LOG (llevError, "Infinite loop found in facesets. aborting.\n"); abort (); } + check_faceset_fallback (fallback, togo); } #define MAX_IMAGE_SIZE 10000 /** - * Loads all the image types into memory. - * - * This way, we can easily send them to the client. We should really do something - * better than abort on any errors - on the other hand, these are all fatal - * to the server (can't work around them), but the abort just seems a bit - * messy (exit would probably be better.) - * - * Couple of notes: We assume that the faces are in a continous block. - * This works fine for now, but this could perhaps change in the future - * - * Function largely rewritten May 2000 to be more general purpose. - * The server itself does not care what the image data is - to the server, - * it is just data it needs to allocate. As such, the code is written - * to do such. - */ - -void -read_client_images (void) -{ - char filename[400]; - char buf[HUGE_BUF]; - char *cp, *cps[7]; - FILE *infile; - int num, len, compressed, fileno, i, badline; - - memset (facesets, 0, sizeof (facesets)); - sprintf (filename, "%s/image_info", settings.datadir); - if ((infile = open_and_uncompress (filename, 0, &compressed)) == NULL) - { - LOG (llevError, "Unable to open %s\n", filename); - abort (); - } - while (fgets (buf, HUGE_BUF - 1, infile) != NULL) - { - badline = 0; - - if (buf[0] == '#') - continue; - if (!(cps[0] = strtok (buf, ":"))) - badline = 1; - for (i = 1; i < 7; i++) - { - if (!(cps[i] = strtok (NULL, ":"))) - badline = 1; - } - if (badline) - { - LOG (llevError, "Bad line in image_info file, ignoring line:\n %s", buf); - } - else - { - len = atoi (cps[0]); - if (len >= MAX_FACE_SETS) - { - LOG (llevError, "To high a setnum in image_info file: %d > %d\n", len, MAX_FACE_SETS); - abort (); - } - facesets[len].prefix = strdup_local (cps[1]); - facesets[len].fullname = strdup_local (cps[2]); - facesets[len].fallback = atoi (cps[3]); - facesets[len].size = strdup_local (cps[4]); - facesets[len].extension = strdup_local (cps[5]); - facesets[len].comment = strdup_local (cps[6]); - } - } - close_and_delete (infile, compressed); - for (i = 0; i < MAX_FACE_SETS; i++) - { - if (facesets[i].prefix) - check_faceset_fallback (i, MAX_FACE_SETS); - } - /* Loaded the faceset information - now need to load up the - * actual faces. - */ - - for (fileno = 0; fileno < MAX_FACE_SETS; fileno++) - { - /* if prefix is not set, this is not used */ - if (!facesets[fileno].prefix) - continue; - facesets[fileno].faces = (FaceInfo *) calloc (nrofpixmaps, sizeof (FaceInfo)); - - sprintf (filename, "%s/crossfire.%d", settings.datadir, fileno); - LOG (llevDebug, "Loading image file %s\n", filename); - - if ((infile = open_and_uncompress (filename, 0, &compressed)) == NULL) - { - LOG (llevError, "Unable to open %s\n", filename); - abort (); - } - while (fgets (buf, HUGE_BUF - 1, infile) != NULL) - { - if (strncmp (buf, "IMAGE ", 6) != 0) - { - LOG (llevError, "read_client_images:Bad image line - not IMAGE, instead\n%s", buf); - abort (); - } - num = atoi (buf + 6); - if (num < 0 || num >= nrofpixmaps) - { - LOG (llevError, "read_client_images: Image num %d not in 0..%d\n%s", num, nrofpixmaps, buf); - abort (); - } - /* Skip accross the number data */ - for (cp = buf + 6; *cp != ' '; cp++); - len = atoi (cp); - if (len == 0 || len > MAX_IMAGE_SIZE) - { - LOG (llevError, "read_client_images: length not valid: %d > %d \n%s", len, MAX_IMAGE_SIZE, buf); - abort (); - } - /* We don't actualy care about the name of the image that - * is embedded in the image file, so just ignore it. - */ - facesets[fileno].faces[num].datalen = len; - facesets[fileno].faces[num].data = (uint8 *) malloc (len); - if ((i = fread (facesets[fileno].faces[num].data, len, 1, infile)) != 1) - { - LOG (llevError, "read_client_images: Did not read desired amount of data, wanted %d, got %d\n%s", len, i, buf); - abort (); - } - facesets[fileno].faces[num].checksum = 0; - for (i = 0; i < len; i++) - { - ROTATE_RIGHT (facesets[fileno].faces[num].checksum); - facesets[fileno].faces[num].checksum += facesets[fileno].faces[num].data[i]; - facesets[fileno].faces[num].checksum &= 0xffffffff; - } - } - close_and_delete (infile, compressed); - } /* For fileno < MAX_FACE_SETS */ -} - -/** * Client tells us what type of faces it wants. Also sets * the caching attribute. * */ - void -SetFaceMode (char *buf, int len, client_socket *ns) +SetFaceMode (char *buf, int len, client *ns) { int mask = (atoi (buf) & CF_FACE_CACHE), mode = (atoi (buf) & ~CF_FACE_CACHE); if (mode == CF_FACE_NONE) ns->facecache = 1; else if (mode != CF_FACE_PNG) - { - packet sl; - sl.printf ("drawinfo %d %s", NDI_RED, "Warning - send unsupported face mode. Will use Png"); - ns->send_packet (sl); - } + ns->send_packet_printf ("drawinfo %d %s", NDI_RED, "Warning - send unsupported face mode. Will use Png"); if (mask) ns->facecache = 1; @@ -336,15 +197,36 @@ * This will be called often if the client is * caching images. */ - void -SendFaceCmd (char *buff, int len, client_socket * ns) +SendFaceCmd (char *buf, int len, client *ns) { - long tmpnum = atoi (buff); - short facenum = tmpnum & 0xffff; + ns->send_image (atoi (buf)); +} + +// how lame +static void print_facename (packet &sl, const facedata &d) +{ + for (int i = 0; i < CHKSUM_SIZE; ++i) + sl.printf ("%02x", d.chksum [i]); +} + +// gcfclient uses the server-provided checksum for comparison, but always +// writes a broken checksum to its cache file, so we have to provide +// gcfclient with the same broken (and useless) checksum just to have it +// cache the image despite its bugs. +static uint32 gcfclient_checksum (const facedata *d) +{ + uint32 csum = 0; + + for (std::string::const_iterator i = d->data.begin (); + i != d->data.end (); + ++i) + { + csum = rotate_right (csum); + csum += *(uint8 *)&*i; + } - if (facenum != 0) - esrv_send_face (ns, facenum, 1); + return csum; } /** @@ -355,55 +237,198 @@ * face (and askface is the only place that should be setting it). Otherwise, * we look at the facecache, and if set, send the image name. */ - void -esrv_send_face (client_socket *ns, short face_num, int nocache) +client::send_face (faceidx facenum) { - char fallback; + // never send face 0. ever. it does not exist. + if (!facenum) + return; - if (face_num <= 0 || face_num >= nrofpixmaps) + const facedata *d = face_data (facenum, faceset); + + if (!d) { - LOG (llevError, "esrv_send_face (%d) out of bounds??\n", face_num); + LOG (llevError, "client::send_face (%d) out of bounds??\n", facenum); return; } - packet sl; - fallback = get_face_fallback (ns->faceset, face_num); + if (faces_sent[facenum]) + return; + + faces_sent[facenum] = true; + + // if for some reason we let a client without face caching connect, + // we better support that decision here and implement it. + if (!facecache) + return send_image (facenum); - if (facesets[fallback].faces[face_num].data == NULL) + if (fxix) { - LOG (llevError, "esrv_send_face: faces[%d].data == NULL\n", face_num); + fxface.push_back (facenum); return; } - if (ns->facecache && !nocache) + packet sl; + + if (force_face0) + sl << "face " << uint16 (facenum); + else if (image2) + sl << "face2 " << uint16 (facenum) << uint8 (0) << uint32 (force_bad_checksum ? gcfclient_checksum (d) : 0); + else + sl << "face1 " << uint16 (facenum) << uint32 (force_bad_checksum ? gcfclient_checksum (d) : 0); + + // how lame + print_facename (sl, *d); + send_packet (sl); + + if (EMI_smooth) { - sl << (ns->image2 ? "face2 " : "face1 ") - << uint16 (face_num); + faceinfo *f = face_info (facenum); - if (ns->image2) - sl << uint8 (fallback); + if (f->smooth) + { + send_face (f->smooth); - sl << uint32 (facesets[fallback].faces[face_num].checksum) - << new_faces[face_num].name; + packet sl ("smooth"); - ns->send_packet (sl); + sl << uint16 (facenum) + << uint16 (f->smooth); + + send_packet (sl); + } } - else +} + +void client::flush_fx () +{ + while (!fxface.empty ()) { - sl << (ns->image2 ? "image2 " : "image ") - << uint32 (face_num); + packet fx ("fx"); + packet sx ("sx"); - if (ns->image2) - sl << uint8 (fallback); + do + { + faceidx facenum = fxface.back (); fxface.pop_back (); - sl << uint32 (facesets[fallback].faces[face_num].datalen) - << data (facesets[fallback].faces[face_num].data, facesets[fallback].faces[face_num].datalen); + const facedata *d = face_data (facenum, faceset); - ns->send_packet (sl); + if (d) + { + fx << ber32 (facenum) + << data8 (d->chksum, CHKSUM_SIZE); + + if (smoothing) + { + faceinfo *f = face_info (facenum); + + if (f->smooth) + { + send_face (f->smooth); + sx << ber32 (facenum) + << ber32 (f->smooth) + << ber32 (f->smoothlevel); + } + } + } + } + while (!fxface.empty () + && fx.room () > ber32::size + CHKSUM_SIZE + 1 + && sx.room () > ber32::size * 3); + + send_packet (fx); + if (sx.length () > 3) send_packet (sx); + } +} + +void +client::send_image (faceidx facenum) +{ + // never send face 0. ever. it does not exist. + if (!facenum) + return; + + const facedata *d = face_data (facenum, faceset); + + if (!d) + { + LOG (llevError, "client::send_image (%d) out of bounds??\n", facenum); + return; } - ns->faces_sent[face_num] |= NS_FACESENT_FACE; + faces_sent[facenum] = true; + + if (force_image_newmap) + force_newmap = true; + + packet sl; + + sl << (image2 ? "image2 " : "image ") + << uint32 (facenum); + + if (image2) + sl << uint8 (0); + + sl << uint32 (d->data.size ()) + << data (d->data.data (), d->data.size ()); + + send_packet (sl); +} + +// send all faces of this object to the client +// this uses more bandwidth initially, but makes +// animations look much smoother, and every client +// is supposed to do client-side caching anyways. +void +client::send_faces (object *ob) +{ + send_face (ob->face); + + if (ob->animation_id) + { + animation &anim = animations [ob->animation_id]; + + for (int i = 0; i < anim.num_animations; i++) + send_face (anim.faces [i]); + } +} + +/** + * Need to send an animation sequence to the client. + * We will send appropriate face commands to the client if we haven't + * sent them the face yet (this can become quite costly in terms of + * how much we are sending - on the other hand, this should only happen + * when the player logs in and picks stuff up. + */ +void +client::send_animation (short anim_num) +{ + /* Do some checking on the anim_num we got. Note that the animations + * are added in contigous order, so if the number is in the valid + * range, it must be a valid animation. + */ + if (anim_num < 0 || anim_num > num_animations) + { + LOG (llevError, "esrv_send_anim (%d) out of bounds??\n", anim_num); + return; + } + + packet sl ("anim"); + + sl << uint16 (anim_num) + << uint16 (0); /* flags - not used right now */ + + /* Build up the list of faces. Also, send any information (ie, the + * the face itself) down to the client. + */ + for (int i = 0; i < animations[anim_num].num_animations; i++) + { + send_face (animations[anim_num].faces[i]); + sl << uint16 (animations[anim_num].faces[i]); /* flags - not used right now */ + } + + send_packet (sl); + + anims_sent[anim_num] = 1; } /** @@ -411,19 +436,15 @@ * and the image_info file information. See the doc/Developers/protocol * if you want further detail. */ - void -send_image_info (client_socket * ns, char *params) +send_image_info (client *ns, char *params) { packet sl; - sl.printf ("replyinfo image_info\n%d\n%d\n", nrofpixmaps - 1, bmaps_checksum); + //TODO: second parameter is a checksum, but it makes no sense in this current framework + sl.printf ("replyinfo image_info\n%d\n%u\n", MAX_FACES, 0); - for (int i = 0; i < MAX_FACE_SETS; i++) - if (facesets[i].prefix) - sl.printf ("%d:%s:%s:%d:%s:%s:%s", - i, facesets[i].prefix, facesets[i].fullname, facesets[i].fallback, - facesets[i].size, facesets[i].extension, facesets[i].comment); + sl << "0:base:standard:0:32x32:none:The old 32x32 faceset.\n"; ns->send_packet (sl); } @@ -438,10 +459,10 @@ * - name */ void -send_image_sums (client_socket * ns, char *params) +send_image_sums (client *ns, char *params) { int start, stop; - char *cp, buf[MAX_BUF]; + char *cp; packet sl; @@ -451,7 +472,7 @@ break; stop = atoi (cp); - if (stop < start || *cp == '\0' || (stop - start) > 1000 || stop >= nrofpixmaps) + if (stop < start || *cp == '\0' || (stop - start) > 1000 || stop >= MAX_FACES) { sl.printf ("replyinfo image_sums %d %d", start, stop); ns->send_packet (sl); @@ -461,16 +482,20 @@ sl.printf ("replyinfo image_sums %d %d ", start, stop); - for (int i = start; i <= stop; i++) + for (int i = start; i <= stop && i < faces.size (); i++) { - ns->faces_sent[i] |= NS_FACESENT_FACE; + ns->faces_sent[i] = true; - int qq = get_face_fallback (ns->faceset, i); + const facedata *d = face_data (i, ns->faceset); + + if (sl.room () < 2 + 4 + 1 + d->data.size () + 1) + break; sl << uint16 (i) - << uint32 (facesets[qq].faces[i].checksum) - << uint8 (qq) - << data8 (&new_faces[i].name, new_faces[i].name.length () + 1); + << uint32 (0) // checksum + << uint8 (ns->faceset); + + print_facename (sl, *d); sl << uint8 (0); } /* It would make more sense to catch this pre-emptively in the code above. @@ -478,6 +503,7 @@ * size to less than 1000, since that is what we claim the protocol would * support. */ + //TODO: taken care of above, should simply abort or make sure the above code is correct if (sl.length () >= MAXSOCKBUF) { LOG (llevError, "send_image_send: buffer overrun, %d > %d\n", sl.length (), MAXSOCKBUF);