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