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 #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 }
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
bufferreader_data
char * bufferreader_data(BufferReader *br)
Definition: bufferreader.cpp:148
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:51
strdup_local
#define strdup_local
Definition: compat.h:29
PngLoader::m_faces
Faces * m_faces
Definition: PngLoader.h:32
Facesets
Definition: Facesets.h:23
Facesets.h
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Definition: bufferreader.cpp:144
face_sets::allocated
size_t allocated
Definition: image.h:25
npc_dialog.filename
filename
Definition: npc_dialog.py:99
buf
StringBuffer * buf
Definition: readable.cpp:1551
ROTATE_RIGHT
#define ROTATE_RIGHT(c)
Definition: global.h:164
Face::number
uint16_t number
Definition: face.h:15
split_string
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Definition: utils.cpp:473
compat.h
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:89
image.h
fatal
void fatal(enum fatal_error err)
Definition: utils.cpp:570
Faces
Definition: Faces.h:19
Faces.h
PngLoader.h
PngLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: PngLoader.cpp:25
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:22
dragon_attune.faces
dictionary faces
Definition: dragon_attune.py:31
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2603
face_sets
Definition: image.h:17
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
BufferReader
Definition: bufferreader.cpp:21
face_sets::faces
face_info * faces
Definition: image.h:26
llevDebug
@ llevDebug
Definition: logger.h:13