Crossfire Server, Trunk  R21670
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 
43 void send_face_cmd(char *buff, int len, socket_struct *ns) {
44  long tmpnum;
45  uint16_t faceid;
46 
47  if (len <= 0 || !buff) {
48  LOG(llevDebug, "IP '%s' sent bogus send_face_cmd information\n", ns->host);
49  return;
50  }
51 
52  tmpnum = atoi(buff);
53  faceid = tmpnum&0xffff;
54 
55  if (faceid != 0)
56  esrv_send_face(ns, get_face_by_id(faceid), 1);
57 }
58 
70 void esrv_send_face(socket_struct *ns, const Face *face, int nocache) {
71  SockList sl;
72  int fallback;
73 
74  if (face == NULL) {
75  LOG(llevError, "esrv_send_face NULL??\n");
76  return;
77  }
78 
79  SockList_Init(&sl);
80  fallback = get_face_fallback(ns->faceset, face->number);
81 
82  if (facesets[fallback].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, fallback);
91  SockList_AddInt(&sl, facesets[fallback].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, fallback);
98  SockList_AddInt(&sl, facesets[fallback].faces[face->number].datalen);
99  SockList_AddData(&sl, facesets[fallback].faces[face->number].data, facesets[fallback].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 
113  SockList sl;
114  int i;
115 
116  SockList_Init(&sl);
117  SockList_AddPrintf(&sl, "replyinfo image_info\n%d\n%d\n", get_faces_count()-1, bmaps_checksum);
118  for (i = 0; i < MAX_FACE_SETS; i++) {
119  if (facesets[i].prefix) {
120  SockList_AddPrintf(&sl, "%d:%s:%s:%d:%s:%s:%s",
121  i, facesets[i].prefix, facesets[i].fullname,
122  facesets[i].fallback, facesets[i].size,
123  facesets[i].extension, facesets[i].comment);
124  }
125  }
126  Send_With_Handling(ns, &sl);
127  SockList_Term(&sl);
128 }
129 
139 void send_image_sums(socket_struct *ns, char *params) {
140  unsigned int start, stop;
141  unsigned short i;
142  char *cp;
143  SockList sl;
144 
145  SockList_Init(&sl);
146 
147  start = atoi(params);
148  for (cp = params; *cp != '\0'; cp++)
149  if (*cp == ' ')
150  break;
151 
152  stop = atoi(cp);
153  if (stop < start
154  || *cp == '\0'
155  || (stop-start) > 1000
156  || stop >= get_faces_count()) {
157  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
158  Send_With_Handling(ns, &sl);
159  SockList_Term(&sl);
160  return;
161  }
162  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d ", start, stop);
163 
164  for (i = start; i <= stop; i++) {
165  int faceset;
166  const Face *face = get_face_by_index(i);
167 
168  if (SockList_Avail(&sl) < 2+4+1+1+strlen(face->name)+1) {
169  LOG(llevError, "send_image_sums: buffer overflow, rejecting range %d..%d\n", start, stop);
170  SockList_Reset(&sl);
171  SockList_AddPrintf(&sl, "replyinfo image_sums %d %d", start, stop);
172  Send_With_Handling(ns, &sl);
173  SockList_Term(&sl);
174  return;
175  }
176 
177  SockList_AddShort(&sl, i);
178  ns->faces_sent[face->number] |= NS_FACESENT_FACE;
179 
180  faceset = get_face_fallback(ns->faceset, i);
181  SockList_AddInt(&sl, facesets[faceset].faces[i].checksum);
182  SockList_AddChar(&sl, faceset);
183  SockList_AddLen8Data(&sl, face->name, strlen(face->name)+1);
184  }
185  Send_With_Handling(ns, &sl);
186  SockList_Term(&sl);
187 }
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
const Face * get_face_by_id(uint16_t id)
Definition: image.c:653
face_info * faces
Definition: image.h:24
void SockList_Init(SockList *sl)
Definition: lowlevel.c:48
Definition: face.h:14
const Face * get_face_by_index(int index)
Definition: image.c:642
void SockList_AddShort(SockList *sl, uint16_t data)
Definition: lowlevel.c:108
const char * name
Definition: face.h:19
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
void SockList_AddData(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:159
void SockList_Term(SockList *sl)
Definition: lowlevel.c:58
void SockList_AddString(SockList *sl, const char *data)
Definition: lowlevel.c:149
size_t SockList_Avail(const SockList *sl)
Definition: lowlevel.c:238
uint16_t number
Definition: face.h:15
unsigned int get_faces_count()
Definition: image.c:633
unsigned short uint16_t
Definition: win32.h:163
void send_image_info(socket_struct *ns)
Definition: image.c:112
void SockList_AddChar(SockList *sl, unsigned char c)
Definition: lowlevel.c:98
void send_image_sums(socket_struct *ns, char *params)
Definition: image.c:139
#define MAX_FACE_SETS
Definition: image.h:26
face_sets facesets[MAX_FACE_SETS]
Definition: image.c:47
uint16_t datalen
Definition: image.h:12
void esrv_send_face(socket_struct *ns, const Face *face, int nocache)
Definition: image.c:70
EXTERN int bmaps_checksum
Definition: global.h:162
void SockList_AddLen8Data(SockList *sl, const void *data, size_t len)
Definition: lowlevel.c:171
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
int get_face_fallback(int faceset, int imageno)
Definition: image.c:610
uint8_t * faces_sent
Definition: newserver.h:96
void send_face_cmd(char *buff, int len, socket_struct *ns)
Definition: image.c:43
void Send_With_Handling(socket_struct *ns, SockList *sl)
Definition: lowlevel.c:435