Crossfire Server, Trunk  R22047
image.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
25 #include "global.h"
26 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "image.h"
31 #include "newserver.h"
32 #include "shared/newclient.h"
33 #include "sproto.h"
34 #include "assets.h"
35 
44 void send_face_cmd(char *buff, int len, socket_struct *ns) {
45  long tmpnum;
46  uint16_t faceid;
47 
48  if (len <= 0 || !buff) {
49  LOG(llevDebug, "IP '%s' sent bogus send_face_cmd information\n", ns->host);
50  return;
51  }
52 
53  tmpnum = atoi(buff);
54  faceid = tmpnum&0xffff;
55 
56  if (faceid != 0)
57  esrv_send_face(ns, get_face_by_id(faceid), 1);
58 }
59 
71 void esrv_send_face(socket_struct *ns, const Face *face, int nocache) {
72  SockList sl;
73 
74  if (face == NULL) {
75  LOG(llevError, "esrv_send_face NULL??\n");
76  return;
77  }
78 
79  SockList_Init(&sl);
81 
82  if (!fs || fs->faces[face->number].data == NULL) {
83  LOG(llevError, "esrv_send_face: faces[%d].data == NULL\n", face->number);
84  return;
85  }
86 
87  if (ns->facecache && !nocache) {
88  SockList_AddString(&sl, "face2 ");
89  SockList_AddShort(&sl, face->number);
90  SockList_AddChar(&sl, fs->id);
91  SockList_AddInt(&sl, fs->faces[face->number].checksum);
92  SockList_AddString(&sl, face->name);
93  Send_With_Handling(ns, &sl);
94  } else {
95  SockList_AddString(&sl, "image2 ");
96  SockList_AddInt(&sl, face->number);
97  SockList_AddChar(&sl, fs->id);
98  SockList_AddInt(&sl, fs->faces[face->number].datalen);
99  SockList_AddData(&sl, fs->faces[face->number].data, fs->faces[face->number].datalen);
100  Send_With_Handling(ns, &sl);
101  }
102  ns->faces_sent[face->number] |= NS_FACESENT_FACE;
103  SockList_Term(&sl);
104 }
105 
107 static SockList *ugly;
108 static void do_faceset(const face_sets *fs) {
109  SockList_AddPrintf(ugly, "%d:%s:%s:%d:%s:%s:%s\n",
110  fs->id, fs->prefix, fs->fullname,
111  fs->fallback ? fs->fallback->id : 0, fs->size,
112  fs->extension, fs->comment);
113 }
114 
122  SockList sl;
123  int i;
124 
125  SockList_Init(&sl);
126 
127  ugly = &sl;
128  SockList_AddPrintf(&sl, "replyinfo image_info\n%d\n%d\n", get_faces_count()-1, get_bitmap_checksum());
130  Send_With_Handling(ns, &sl);
131  SockList_Term(&sl);
132 }
133 
143 void send_image_sums(socket_struct *ns, char *params) {
144  unsigned int start, stop;
145  unsigned short i;
146  char *cp;
147  SockList sl;
148 
149  SockList_Init(&sl);
150 
151  start = atoi(params);
152  for (cp = params; *cp != '\0'; cp++)
153  if (*cp == ' ')
154  break;
155 
156  stop = atoi(cp);
157  if (stop < start
158  || *cp == '\0'
159  || (stop-start) > 1000
160  || stop >= get_faces_count()) {
161  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
162  Send_With_Handling(ns, &sl);
163  SockList_Term(&sl);
164  return;
165  }
166  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d ", start, stop);
167 
168  for (i = start; i <= stop; i++) {
169  int faceset;
170  const Face *face = get_face_by_id(i);
171 
172  if (SockList_Avail(&sl) < 2+4+1+1+strlen(face->name)+1) {
173  LOG(llevError, "send_image_sums: buffer overflow, rejecting range %d..%d\n", start, stop);
174  SockList_Reset(&sl);
175  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
176  Send_With_Handling(ns, &sl);
177  SockList_Term(&sl);
178  return;
179  }
180 
181  SockList_AddShort(&sl, i);
182  ns->faces_sent[face->number] |= NS_FACESENT_FACE;
183 
185  SockList_AddInt(&sl, fs->faces[i].checksum);
186  SockList_AddChar(&sl, fs->id);
187  SockList_AddLen8Data(&sl, face->name, strlen(face->name)+1);
188  }
189  Send_With_Handling(ns, &sl);
190  SockList_Term(&sl);
191 }
void SockList_AddPrintf(SockList *sl, const char *format,...)
Definition: lowlevel.c:194
uint8_t faceset
Definition: newserver.h:117
void SockList_Reset(SockList *sl)
Definition: lowlevel.c:66
#define NS_FACESENT_FACE
Definition: newserver.h:137
face_info * faces
Definition: image.h:26
void SockList_Init(SockList *sl)
Definition: lowlevel.c:48
Definition: face.h:14
int get_bitmap_checksum()
Definition: assets.cpp:331
void SockList_AddShort(SockList *sl, uint16_t data)
Definition: lowlevel.c:108
int id
Definition: image.h:18
const char * name
Definition: face.h:19
face_sets * find_faceset(int id)
Definition: assets.cpp:335
void SockList_AddInt(SockList *sl, uint32_t data)
Definition: lowlevel.c:119
char * host
Definition: newserver.h:100
uint32_t facecache
Definition: newserver.h:102
static void do_faceset(const face_sets *fs)
Definition: image.c:108
void SockList_AddData(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:159
uint8_t * data
Definition: image.h:11
void SockList_Term(SockList *sl)
Definition: lowlevel.c:58
char * prefix
Definition: image.h:19
char * extension
Definition: image.h:23
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:149
size_t get_faces_count()
Definition: assets.cpp:288
char * size
Definition: image.h:22
size_t SockList_Avail(const SockList *sl)
Definition: lowlevel.c:238
uint16_t number
Definition: face.h:15
uint32_t checksum
Definition: image.h:13
char * comment
Definition: image.h:24
unsigned short uint16_t
Definition: win32.h:163
void send_image_info(socket_struct *ns)
Definition: image.c:121
void facesets_for_each(faceset_op op)
Definition: assets.cpp:296
void SockList_AddChar(SockList *sl, unsigned char c)
Definition: lowlevel.c:98
void send_image_sums(socket_struct *ns, char *params)
Definition: image.c:143
uint16_t datalen
Definition: image.h:12
struct face_sets * fallback
Definition: image.h:21
void esrv_send_face(socket_struct *ns, const Face *face, int nocache)
Definition: image.c:71
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:171
char * fullname
Definition: image.h:20
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
int get_face_fallback(int faceset, int imageno)
Definition: image.c:132
static SockList * ugly
Definition: image.c:107
uint8_t * faces_sent
Definition: newserver.h:96
void send_face_cmd(char *buff, int len, socket_struct *ns)
Definition: image.c:44
const Face * get_face_by_id(uint16_t id)
Definition: assets.cpp:314
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:435