Crossfire Server, Trunk  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 }
PngLoader::m_facesets
Facesets * m_facesets
Definition: PngLoader.h:33
Face
New face structure - this enforces the notion that data is face by face only - you can not change the...
Definition: face.h:14
global.h
bufferreader_data
char * bufferreader_data(BufferReader *br)
Get the whole buffer, independently of the calls to bufferreader_next_line().
Definition: bufferreader.cpp:148
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
strdup_local
#define strdup_local
Definition: compat.h:29
PngLoader::m_faces
Faces * m_faces
Definition: PngLoader.h:32
Facesets
Available facesets for clients.
Definition: Facesets.h:23
Facesets.h
bufferreader_data_length
size_t bufferreader_data_length(BufferReader *br)
Return the length of the buffer data.
Definition: bufferreader.cpp:144
buf
StringBuffer * buf
Definition: readable.cpp:1565
ROTATE_RIGHT
#define ROTATE_RIGHT(c)
Definition: global.h:162
split_string
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
treasurelist::name
sstring name
Usually monster-name/combination.
Definition: treasure.h:86
compat.h
face_info
Actual image data the client will display.
Definition: image.h:10
AssetsCollection::get
T * get(const Key &name)
Get a named asset.
Definition: AssetsCollection.h:89
image.h
fatal
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
Faces
Definition: Faces.h:19
Faces.h
PngLoader.h
PngLoader::load
virtual void load(BufferReader *reader, const std::string &filename) override
Load assets from the specified reader.
Definition: PngLoader.cpp:25
PngLoader::PngLoader
PngLoader(Faces *faces, Facesets *facesets)
Definition: PngLoader.cpp:22
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2734
face_sets
Information about one face set.
Definition: image.h:17
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
BufferReader
Definition: bufferreader.cpp:21
set
*envar *is the environment if one that can also be used as an override If both the flag and the envar are set
Definition: server-directories.txt:12
llevDebug
@ llevDebug
Only for debugging purposes.
Definition: logger.h:13
face
in that case they will be relative to whatever the PWD of the crossfire server process is You probably shouldn though Notes on Specific and settings file datadir Usually usr share crossfire Contains data that the server does not need to modify while such as the etc A default install will pack the and treasurelist definitions into a single or trs file and the graphics into a face(metadata) and .tar(bitmaps) file