Crossfire Server  1.75.0
PngLoader.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "PngLoader.h"
14 #include "Faces.h"
15 #include "Facesets.h"
16 
17 #include "string.h"
18 #include "global.h"
19 #include "compat.h"
20 #include "image.h"
21 
22 PngLoader::PngLoader(Faces *faces, Facesets *facesets) : m_faces(faces), m_facesets(facesets) {
23 }
24 
25 void PngLoader::load(BufferReader *reader, const std::string &filename) {
26  auto slash = strrchr(filename.c_str(), '/');
27  char *dup = slash ? strdup_local(strrchr(filename.c_str(), '/') + 1) : strdup(filename.c_str());
28  char* split[20];
29 
30  size_t count = split_string(dup, split, 20, '.');
31  if (count < 4) {
32  free(dup);
33  return;
34  }
35 
36  char buf[500];
37  buf[0] = '\0';
38  /* char *set = split[count - 3]; */
39  for (size_t p = 0; p < count - 1; p++) {
40  if (p != count - 3) {
41  if (p > 0) {
42  strcat(buf, ".");
43  }
44  strcat(buf, split[p]);
45  }
46  }
47 
48  face_sets *set = m_facesets->get(split[count - 3]);
49 
50  const Face *face = m_faces->get(buf);
51  if (face->number >= set->allocated) {
52  set->faces = static_cast<face_info *>(realloc(set->faces, (face->number + 1) * sizeof(face_info)));
53  for (int i = set->allocated; i <= face->number; i++) {
54  set->faces[i].data = NULL;
55  set->faces[i].datalen = 0;
56  set->faces[i].checksum = 0;
57  }
58  set->allocated = face->number + 1;
59  }
60 
61  if (set->faces[face->number].data) {
62  LOG(llevDebug, "replacing facedata %s by %s\n", face->name, filename.c_str());
63  free(set->faces[face->number].data);
64  }
65 
66  set->faces[face->number].datalen = bufferreader_data_length(reader);
67  set->faces[face->number].data = static_cast<uint8_t *>(malloc(set->faces[face->number].datalen));
68  if (!set->faces[face->number].data) {
70  }
71  memcpy(set->faces[face->number].data, bufferreader_data(reader), set->faces[face->number].datalen);
72  set->faces[face->number].checksum = 0;
73  for (size_t i = 0; i < set->faces[face->number].datalen; i++) {
74  ROTATE_RIGHT(set->faces[face->number].checksum);
75  set->faces[face->number].checksum += set->faces[face->number].data[i];
76  set->faces[face->number].checksum &= 0xffffffff;
77  }
78 
79  free(dup);
80 }
Actual image data the client will display.
Definition: image.h:10
Faces * m_faces
Definition: PngLoader.h:32
T * get(const Key &name)
Get a named asset.
#define ROTATE_RIGHT(c)
Definition: global.h:162
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
#define strdup_local
Definition: compat.h:29
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2734
New face structure - this enforces the notion that data is face by face only - you can not change the...
Definition: face.h:14
Facesets * m_facesets
Definition: PngLoader.h:33
char * bufferreader_data(BufferReader *br)
Get the whole buffer, independently of the calls to bufferreader_next_line().
Global type definitions and header inclusions.
size_t bufferreader_data_length(BufferReader *br)
Return the length of the buffer data.
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Splits a string delimited by passed in sep value into characters into an array of strings...
Definition: utils.cpp:473
Image-related structures.
uint8_t * data
Image data.
Definition: image.h:11
uint16_t number
This is the image unique identifier.
Definition: face.h:15
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
Compatibility implementations of useful nonstandard types and functions.
Available facesets for clients.
Definition: Facesets.h:23
Only for debugging purposes.
Definition: logger.h:13
PngLoader(Faces *faces, Facesets *facesets)
Definition: PngLoader.cpp:22
virtual void load(BufferReader *reader, const std::string &filename) override
Load assets from the specified reader.
Definition: PngLoader.cpp:25
StringBuffer * buf
Definition: readable.cpp:1563
Definition: Faces.h:19
Information about one face set.
Definition: image.h:17
sstring name
Face name, as used by archetypes and such.
Definition: face.h:19