--- deliantra/server/socket/image.C 2007/03/15 16:25:45 1.36 +++ deliantra/server/socket/image.C 2008/12/27 01:25:00 1.58 @@ -1,25 +1,24 @@ /* - * 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 + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2001,2007 Mark Wedel + * Copyright (©) 1992,2007 Frank Tore Johansen + * + * Deliantra 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 + * the Free Software Foundation, either version 3 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 + * 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 + * along with this program. If not, see . + * + * The authors can be reached via e-mail to */ /** \file @@ -38,195 +37,55 @@ #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; - - uint8 fallback; /**< faceset to use when an image is not found in this faceset */ - char *size; - char *extension; - char *comment; - - FaceInfo *faces; /**< images in this faceset */ -} FaceSets; - -static FaceSets facesets[MAX_FACE_SETS]; /**< All facesets */ - -/** - * Checks specified faceset is valid - * \param fsn faceset number - */ -int -is_valid_faceset (int fsn) -{ - if (fsn >= 0 && fsn < MAX_FACE_SETS && facesets[fsn].prefix) - return TRUE; - return FALSE; -} +#define MAX_IMAGE_SIZE 10000 /** - * Frees all faceset information + * client requested an image. send it rate-limited + * before flushing. */ void -free_socket_images (void) +AskFaceCmd (char *buf, int len, client *ns) { - int num, q; + int idx = 0, pri = 0; - for (num = 0; num < MAX_FACE_SETS; num++) - { - if (facesets[num].prefix) - { - 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); - free (facesets[num].extension); - free (facesets[num].comment); - free (facesets[num].faces); - } - } -} + sscanf (buf, "%d %d", &idx, &pri); -/** - * This returns the set we will actually use when sending - * a face. This is used because the image files may be sparse. - * This function is recursive. imageno is the face number we are - * trying to send - * - * If face is not found in specified faceset, tries with 'fallback' faceset. - * - * \param faceset faceset to check - * \param imageno image number - * - */ -static int -get_face_fallback (int faceset, int imageno) -{ - /* faceset 0 is supposed to have every image, so just return. Doing - * so also prevents infinite loops in the case if it not having - * the face, but in that case, we are likely to crash when we try - * to access the data, but that is probably preferable to an infinite - * loop. - */ - if (faceset == 0) - return 0; + //TODO: somehow fetch default priority from send_fx here - if (!facesets[faceset].prefix) - { - LOG (llevError, "get_face_fallback called with unused set (%d)?\n", faceset); - return 0; /* use default set */ - } + const facedata *d = face_data (idx, ns->faceset); - if (facesets[faceset].faces[imageno].data) - return faceset; + if (!d) + return; // doh - return get_face_fallback (facesets[faceset].fallback, imageno); -} + client::ixsend ix; -/** - * Checks fallback are correctly defined. - * This is a simple recursive function that makes sure the fallbacks - * are all proper (eg, the fall back to defined sets, and also - * eventually fall back to 0). At the top level, togo is set to MAX_FACE_SETS, - * if togo gets to zero, it means we have a loop. - * This is only run when we first load the facesets. - */ -static void -check_faceset_fallback (int faceset, int togo) -{ - int fallback = facesets[faceset].fallback; + ix.pri = pri; + ix.idx = idx; + ix.ofs = d->data.size (); - /* proper case - falls back to base set */ - if (fallback == 0) - return; + auto (pos, ns->ixface.end ()); - if (!facesets[fallback].prefix) + // the by far most common case will be to insert + // near the end, so little looping. + while (pos != ns->ixface.begin ()) { - LOG (llevError, "Face set %d falls to non set faceset %d\n", faceset, fallback); - abort (); - } + --pos; - togo--; - if (togo == 0) - { - LOG (llevError, "Infinite loop found in facesets. aborting.\n"); - abort (); + // sort within 2k bins, to slightly prefer smaller images + if (pri > pos->pri || (pri == pos->pri && (ix.ofs >> 11) <= (pos->ofs >> 11))) + { + ++pos; + break; + } } - check_faceset_fallback (fallback, togo); -} - -#define MAX_IMAGE_SIZE 10000 - -/** - * Client tells us what type of faces it wants. Also sets - * the caching attribute. - * - */ -void -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) - ns->send_packet_printf ("drawinfo %d %s", NDI_RED, "Warning - send unsupported face mode. Will use Png"); + ns->ixface.insert (pos, ix); - if (mask) - ns->facecache = 1; -} - -/** - * Client has requested pixmap that it somehow missed getting. - * This will be called often if the client is - * caching images. - */ -void -SendFaceCmd (char *buf, int len, client *ns) -{ - 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; - } - - return csum; +#if 0 + for (auto (i, ns->ixface.begin ()); i != ns->ixface.end (); ++i) + fprintf (stderr, "<%d,%d> ", i->pri, i->ofs); + fprintf (stderr, "\n"); +#endif } /** @@ -238,111 +97,81 @@ * we look at the facecache, and if set, send the image name. */ void -client::send_face (faceidx facenum) +client::send_face (faceidx facenum, int pri) { // never send face 0. ever. it does not exist. if (!facenum) return; - const facedata *d = face_data (facenum, faceset); + faceinfo *f = face_info (facenum); - if (!d) + if (!f) { - LOG (llevError, "client::send_face (%d) out of bounds??\n", facenum); + LOG (llevError | logBacktrace, "client::send_face (%d) out of bounds??\n", facenum); return; } - if (faces_sent[facenum]) + // refuse to send non-image faces + if (!fx_want [f->type]) return; - faces_sent[facenum] = true; - - if (!facecache) - { - send_drawinfo ( - "\n" - "\n" - "***\n" - "*** WARNING:\n" - "*** Your client does not support face/image caching,\n" - "*** or it has been disabled. Face caching is mandatory\n" - "*** so please enable it or use a newer client.\n" - "***\n" - "*** Look at your client preferences:\n" - "***\n" - "*** CFPlus: all known versions automatically enable the facecache.\n" - "*** cfclient: use the -cache commandline option.\n" - "*** cfclient: map will not redraw automatically (bug).\n" - "*** gcfclient: use -cache commandline option, or enable\n" - "*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.\n" - "*** jcrossclient: your client is broken, use CFPlus or gcfclient.\n" - "***\n" - "***\n", - NDI_RED - ); - return; - //return send_image (facenum); - } - - 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); - - faceinfo *f = face_info (facenum); - if (f->smooth && EMI_smooth) - { - send_face (f->smooth); - - packet sl ("smooth"); + if (faces_sent [facenum]) + return; - sl << uint16 (facenum) - << uint16 (f->smooth); + faces_sent[facenum] = true; - send_packet (sl); - } + fxface.push_back (facenum); } -void -client::send_image (faceidx facenum) +void client::flush_fx () { - // never send face 0. ever. it does not exist. - if (!facenum) - return; - - const facedata *d = face_data (facenum, faceset); - - if (!d) + while (!fxface.empty ()) { - LOG (llevError, "client::send_image (%d) out of bounds??\n", facenum); - return; - } - - faces_sent[facenum] = true; - - if (force_image_newmap) - force_newmap = true; - - packet sl; + packet fx ("fx"); + packet sx ("sx"); + int type = 0; - sl << (image2 ? "image2 " : "image ") - << uint32 (facenum); - - if (image2) - sl << uint8 (0); + do + { + faceidx facenum = fxface.back (); fxface.pop_back (); - sl << uint32 (d->data.size ()) - << data (d->data.data (), d->data.size ()); + if (const faceinfo *f = face_info (facenum)) + { + if (f->type != type) + { + type = f->type; + + fx << ber32 (0) + << uint8 (1) << uint8 (type); + } + + const facedata *d = f->data (faceset); + + fx << ber32 (facenum) + << data8 (d->chksum, CHKSUM_SIZE); + + if (smoothing) + { + faceinfo *f = face_info (facenum); + + if (f->smooth) + { + send_face (f->smooth, -110); + + sx << ber32 (facenum) + << ber32 (f->smooth) + << ber32 (f->smoothlevel); + } + } + } + } + while (!fxface.empty () + && fx.room () > ber32::size + CHKSUM_SIZE + 1 + 3 /* type switch */ + && sx.room () > ber32::size * 3); - send_packet (sl); + send_packet (fx); + if (sx.length () > 3) send_packet (sx); + } } // send all faces of this object to the client @@ -352,14 +181,14 @@ void client::send_faces (object *ob) { - send_face (ob->face); + send_face (ob->face, 10); if (ob->animation_id) { animation &anim = animations [ob->animation_id]; for (int i = 0; i < anim.num_animations; i++) - send_face (anim.faces [i]); + send_face (anim.faces [i], -10); } } @@ -377,7 +206,7 @@ * 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) + if (anim_num < 0 || anim_num >= animations.size ()) { LOG (llevError, "esrv_send_anim (%d) out of bounds??\n", anim_num); return; @@ -393,7 +222,7 @@ */ for (int i = 0; i < animations[anim_num].num_animations; i++) { - send_face (animations[anim_num].faces[i]); + send_face (animations[anim_num].faces[i], -20); sl << uint16 (animations[anim_num].faces[i]); /* flags - not used right now */ } @@ -454,20 +283,25 @@ sl.printf ("replyinfo image_sums %d %d ", start, stop); for (int i = start; i <= stop && i < faces.size (); i++) - { - ns->faces_sent[i] = true; + if (const faceinfo *f = face_info (i)) + if (ns->fx_want [f->type]) + { + ns->faces_sent[i] = true; - const facedata *d = face_data (i, ns->faceset); + const facedata *d = f->data (ns->faceset); - if (sl.room () < 2 + 4 + 1 + d->data.size () + 1) - break; + if (sl.room () < 2 + 4 + 1 + d->data.size () + 1) + break; - sl << uint16 (i) - << uint32 (0) // checksum - << uint8 (ns->faceset); + sl << uint16 (i) + << uint32 (0) // checksum + << uint8 (ns->faceset); - print_facename (sl, *d); sl << uint8 (0); - } + for (int i = 0; i < CHKSUM_SIZE; ++i) + sl.printf ("%02x", d->chksum [i]); + + sl << uint8 (0); + } /* It would make more sense to catch this pre-emptively in the code above. * however, if this really happens, we probably just want to cut down the @@ -475,7 +309,7 @@ * support. */ //TODO: taken care of above, should simply abort or make sure the above code is correct - if (sl.length () >= MAXSOCKBUF) + if (sl.length () > MAXSOCKBUF) { LOG (llevError, "send_image_send: buffer overrun, %d > %d\n", sl.length (), MAXSOCKBUF); abort ();