Crossfire Server, Trunk
AssetCollector.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 "AssetCollector.h"
14 
15 #ifndef WIN32 /* ---win32 exclude headers */
16 #include <dirent.h>
17 #include <sys/stat.h>
18 #include "autoconf.h"
19 #endif
20 
21 extern "C" {
22 #include "global.h"
23 #include "compat.h"
24 #include "Utils.h"
25 #include <string.h>
26 }
27 #include "microtar.h"
28 
29 #include "PngLoader.h"
30 #include <algorithm>
31 
34 }
35 
37  for (auto loader : m_loaders) {
38  delete loader;
39  }
41 }
42 
43 void AssetCollector::processFiles(const std::vector<std::string>& files) {
44  for (const auto &file : files) {
45  for (auto loader : m_loaders) {
46  if (loader->willLoad(file)) {
47  if (bufferreader_init_from_file(m_reader, file.c_str(), "Unable to open asset file %s: %s\n", llevError) == NULL) {
48  continue;
49  }
50  loader->load(m_reader, file);
51  }
52  }
53  }
54 }
55 
56 
57 void AssetCollector::collect(const std::string& directory) {
58  DIR *dp;
59  struct dirent *d;
60  struct stat sb;
61 
62  char full_path[PATH_MAX];
63  std::vector<std::string> names[2];
64  int type;
65 
66  dp = opendir(directory.c_str());
67  if (dp == NULL) {
68  return;
69  }
70 
71  while ((d = readdir(dp)) != NULL) {
72 
73  if (d->d_name[0] == 0 || d->d_name[0] == '.' || strcmp(d->d_name, "dev") == 0 || strcmp(d->d_name, "trashbin") == 0)
74  continue;
75 
76  snprintf(full_path, sizeof(full_path), "%s/%s", directory.c_str(), d->d_name);
77  stat(full_path, &sb);
78  type = S_ISDIR(sb.st_mode) ? 0 : 1;
79  names[type].push_back(full_path);
80  }
81  (void)closedir(dp);
82 
83  for (type = 0; type < 2; type++) {
84  std::sort(names[type].begin(), names[type].end());
85  }
86 
87  processFiles(names[1]);
88 
89  for (const auto &dir : names[0]) {
90  collect(dir);
91  }
92 }
93 
94 void AssetCollector::load(BufferReader *reader, const std::string &filename) {
95  for (AssetLoader *loader : m_loaders) {
96  if (loader->willLoad(filename)) {
97  loader->load(reader, filename);
98  }
99  }
100 }
global.h
llevError
@ llevError
Definition: logger.h:11
bufferreader_init_from_file
BufferReader * bufferreader_init_from_file(BufferReader *br, const char *filepath, const char *failureMessage, LogLevel failureLevel)
Definition: bufferreader.c:65
AssetCollector::load
virtual void load(BufferReader *reader, const std::string &filename) override
Definition: AssetCollector.cpp:94
mad_mage_user.file
file
Definition: mad_mage_user.py:15
AssetLoader
Definition: AssetLoader.h:24
npc_dialog.filename
filename
Definition: npc_dialog.py:99
AssetCollector::m_loaders
std::vector< AssetLoader * > m_loaders
Definition: AssetCollector.h:55
AssetCollector::collect
void collect(const std::string &directory)
Definition: AssetCollector.cpp:57
AssetCollector::m_reader
BufferReader * m_reader
Definition: AssetCollector.h:56
AssetCollector::AssetCollector
AssetCollector()
Definition: AssetCollector.cpp:32
opendir
DIR * opendir(const char *)
Utils.h
microtar.h
bufferreader_create
BufferReader * bufferreader_create()
Definition: bufferreader.c:31
AssetCollector::processFiles
void processFiles(const std::vector< std::string > &files)
Definition: AssetCollector.cpp:43
readdir
struct dirent * readdir(DIR *)
compat.h
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
dirent
#define dirent
Definition: xdir.h:12
AssetCollector::~AssetCollector
virtual ~AssetCollector()
Definition: AssetCollector.cpp:36
guildbuy.names
list names
Definition: guildbuy.py:18
bufferreader_destroy
void bufferreader_destroy(BufferReader *br)
Definition: bufferreader.c:40
PngLoader.h
closedir
int closedir(DIR *)
BufferReader
Definition: bufferreader.c:21
AssetCollector.h
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
autoconf.h