--- deliantra/server/socket/image.C 2007/02/16 22:33:46 1.21 +++ deliantra/server/socket/image.C 2007/03/12 23:45:37 1.27 @@ -88,7 +88,7 @@ { 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); @@ -174,150 +174,6 @@ #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 (cps[1]); - facesets[len].fullname = strdup (cps[2]); - facesets[len].fallback = atoi (cps[3]); - facesets[len].size = strdup (cps[4]); - facesets[len].extension = strdup (cps[5]); - facesets[len].comment = strdup (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 (); - } - - crc32 chksum; - - for (i = 0; i < len; i++) - chksum (facesets[fileno].faces[num].data[i]); - - facesets[fileno].faces[num].checksum = chksum; - } - - close_and_delete (infile, compressed); - } /* For fileno < MAX_FACE_SETS */ -} - -/** * Client tells us what type of faces it wants. Also sets * the caching attribute. * @@ -350,6 +206,32 @@ esrv_send_face (ns, facenum, 1); } +// 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 wrotes a broken checksum to its cache file, so we +// have to provide gcfclient with a useless checksum just to +// have to 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; +} + /** * Sends a face to a client if they are in pixmap mode * nothing gets sent in bitmap mode. @@ -361,33 +243,27 @@ void esrv_send_face (client *ns, short face_num, int nocache) { - char fallback; - - if (face_num <= 0 || face_num >= nrofpixmaps) + if (face_num <= 0 || face_num >= faces.size ()) { LOG (llevError, "esrv_send_face (%d) out of bounds??\n", face_num); return; } - packet sl; - fallback = get_face_fallback (ns->faceset, face_num); + const facedata *d = face_data (face_num, ns->faceset); - if (facesets[fallback].faces[face_num].data == NULL) - { - LOG (llevError, "esrv_send_face: faces[%d].data == NULL\n", face_num); - return; - } + packet sl; if (ns->facecache && !nocache) { - sl << (ns->image2 ? "face2 " : "face1 ") - << uint16 (face_num); - - if (ns->image2) - sl << uint8 (fallback); + if (ns->force_face0) + sl << "face " << uint16 (face_num); + else if (ns->image2) + sl << "face2 " << uint16 (face_num) << uint8 (0) << uint32 (ns->force_bad_checksum ? gcfclient_checksum (d) : 0); + else + sl << "face1 " << uint16 (face_num) << uint32 (ns->force_bad_checksum ? gcfclient_checksum (d) : 0); - sl << uint32 (facesets[fallback].faces[face_num].checksum) - << new_faces[face_num].name; + // how lame + print_facename (sl, *d); ns->send_packet (sl); } @@ -397,10 +273,10 @@ << uint32 (face_num); if (ns->image2) - sl << uint8 (fallback); + sl << uint8 (0); - sl << uint32 (facesets[fallback].faces[face_num].datalen) - << data (facesets[fallback].faces[face_num].data, facesets[fallback].faces[face_num].datalen); + sl << uint32 (d->data.size ()) + << data (d->data.data (), d->data.size ()); ns->send_packet (sl); } @@ -419,13 +295,10 @@ { packet sl; - sl.printf ("replyinfo image_info\n%d\n%u\n", nrofpixmaps - 1, (unsigned int)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); } @@ -443,7 +316,7 @@ send_image_sums (client *ns, char *params) { int start, stop; - char *cp, buf[MAX_BUF]; + char *cp; packet sl; @@ -453,7 +326,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); @@ -463,16 +336,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; - 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. @@ -480,6 +357,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);