Crossfire Server, Trunk
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 extern "C" {
18 #include "string.h"
19 #include "global.h"
20 #include "compat.h"
21 #include "image.h"
22 }
23 
24 PngLoader::PngLoader(Faces *faces, Facesets *facesets) : m_faces(faces), m_facesets(facesets) {
25 }
26 
27 void PngLoader::load(BufferReader *reader, const std::string &filename) {
28  auto slash = strrchr(filename.c_str(), '/');
29  char *dup = slash ? strdup_local(strrchr(filename.c_str(), '/') + 1) : strdup(filename.c_str());
30  char* split[20];
31 
32  size_t count = split_string(dup, split, 20, '.');
33  if (count < 4) {
34  free(dup);
35  return;
36  }
37 
38  char buf[500];
39  buf[0] = '\0';
40  /* char *set = split[count - 3]; */
41  for (size_t p = 0; p < count - 1; p++) {
42  if (p != count - 3) {
43  if (p > 0) {
44  strcat(buf, ".");
45  }
46  strcat(buf, split[p]);
47  }
48  }
49 
50  face_sets *set = m_facesets->get(split[count - 3]);
51 
52  const Face *face = m_faces->get(buf);
53  if (face->number >= set->allocated) {
54  set->faces = static_cast<face_info *>(realloc(set->faces, (face->number + 1) * sizeof(face_info)));
55  for (int i = set->allocated; i <= face->number; i++) {
56  set->faces[i].data = NULL;
57  set->faces[i].datalen = 0;
58  set->faces[i].checksum = 0;
59  }
60  set->allocated = face->number + 1;
61  }
62 
63  if (set->faces[face->number].data) {
64  LOG(llevDebug, "replacing facedata %s by %s\n", face->name, filename.c_str());
65  free(set->faces[face->number].data);
66  }
67 
68  set->faces[face->number].datalen = bufferreader_data_length(reader);
69  set->faces[face->number].data = static_cast<uint8_t *>(malloc(set->faces[face->number].datalen));
70  if (!set->faces[face->number].data) {
72  }
73  memcpy(set->faces[face->number].data, bufferreader_data(reader), set->faces[face->number].datalen);
74  set->faces[face->number].checksum = 0;
75  for (size_t i = 0; i < set->faces[face->number].datalen; i++) {
76  ROTATE_RIGHT(set->faces[face->number].checksum);
77  set->faces[face->number].checksum += set->faces[face->number].data[i];
78  set->faces[face->number].checksum &= 0xffffffff;
79  }
80 
81  free(dup);
82 }
Face::name
sstring name
Definition: face.h:19
PngLoader::m_facesets
Facesets * m_facesets
Definition: PngLoader.h:33
Face
Definition: face.h:14
global.h
strdup_local
#define strdup_local
Definition: compat.h:29
PngLoader::m_faces
Faces * m_faces
Definition: PngLoader.h:32
Facesets
Definition: Facesets.h:25
Facesets.h
inja::string_view::split
std::pair< nonstd::string_view, nonstd::string_view > split(nonstd::string_view view, char Separator)
Definition: inja.hpp:1886
face_sets::allocated
size_t allocated
Definition: image.h:25
npc_dialog.filename
filename
Definition: npc_dialog.py:99
ROTATE_RIGHT
#define ROTATE_RIGHT(c)
Definition: global.h:158
Face::number
uint16_t number
Definition: face.h:15
fatal
void fatal(enum fatal_error err)
Definition: utils.c:597
compat.h
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Definition: bufferreader.c:144
face_info::data
uint8_t * data
Definition: image.h:11
face_info
Definition: image.h:10
disinfect.count
int count
Definition: disinfect.py:7
AssetsCollection::get
T * get(const Key &name)
Definition: AssetsCollection.h:66
image.h
split_string
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Definition: utils.c:500
Faces
Definition: Faces.h:21
Faces.h
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
PngLoader.h
PngLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: PngLoader.cpp:27
buf
StringBuffer * buf
Definition: readable.c:1606
face_info::datalen
uint16_t datalen
Definition: image.h:12
face_info::checksum
uint32_t checksum
Definition: image.h:13
PngLoader::PngLoader
PngLoader(Faces *faces, Facesets *facesets)
Definition: PngLoader.cpp:24
dragon_attune.faces
dictionary faces
Definition: dragon_attune.py:31
bufferreader_data
char * bufferreader_data(BufferReader *br)
Definition: bufferreader.c:148
face_sets
Definition: image.h:17
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
BufferReader
Definition: bufferreader.c:21
face_sets::faces
face_info * faces
Definition: image.h:26
llevDebug
@ llevDebug
Definition: logger.h:13