Crossfire Server, Trunk
ModifiedAssetsManager.h
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2021-2022 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 #ifndef MODIFIEDASSETSMANAGER_H
14 #define MODIFIEDASSETSMANAGER_H
15 
16 #include <string>
17 #include <set>
18 #include <vector>
19 #include <algorithm>
20 #include "AssetWriter.h"
21 #include <QFile>
22 
23 template<class T>
25 public:
27  virtual ~ModifiedAssetsManager() { delete myWriter; }
28 
29  const std::map<std::string, std::set<const T*> >& origins() const { return myOrigins; }
30 
31  std::vector<std::string> files() const {
32  std::vector<std::string> files;
33  for (const auto &origin : myOrigins) {
34  files.push_back(origin.first);
35  }
36  return files;
37  }
38 
39  std::string originOf(const T *asset) const {
40  for (auto file : myOrigins) {
41  if (file.second.count(asset) > 0) {
42  return file.first;
43  }
44  }
45  return std::string();
46  }
47 
48  void assetDefined(const T *asset, const std::string &filename) {
49  for (auto it : myOrigins) {
50  it.second.erase(asset);
51  }
52  myOrigins[filename.c_str()].insert(asset);
53  }
54 
55  void assetModified(T *asset) { myDirty.insert(asset); }
56 
58  for (auto a : myDirty) {
59  for (auto file : myOrigins) {
60  if (file.second.count(a) > 0) {
61  write(file.first, file.second);
62  }
63  }
64  }
65 
66  myDirty.clear();
67  }
68 
69  bool hasPendingChanges() const { return !myDirty.empty(); }
70 
75  std::vector<T *> dirtyAssetsWithNoOrigin() const {
76  std::vector<T *> assets;
77  std::copy_if(myDirty.cbegin(), myDirty.cend(), std::back_inserter(assets), [this] (const auto &dirty) {
78  return this->originOf(dirty).empty();
79  });
80  return assets;
81  }
82 
83 protected:
84  void write(const std::string &filename, std::set<const T*> assets) {
85  auto buf = stringbuffer_new();
86  for (auto asset = assets.begin(); asset != assets.end(); asset++) {
87  myWriter->write(*asset, buf);
88  }
89 
90  size_t length = stringbuffer_length(buf);
91  char *data = stringbuffer_finish(buf);
92 
93  QFile out(QString(filename.data()));
94  out.open(QIODevice::WriteOnly);
95  out.write(data, length);
96  free(data);
97  }
98 
99 private:
101  std::map<std::string, std::set<const T*> > myOrigins;
102  std::set<T *> myDirty;
103 };
104 
105 #endif /* MODIFIEDASSETSMANAGER_H */
ModifiedAssetsManager::origins
const std::map< std::string, std::set< const T * > > & origins() const
Definition: ModifiedAssetsManager.h:29
ModifiedAssetsManager::myOrigins
std::map< std::string, std::set< const T * > > myOrigins
Definition: ModifiedAssetsManager.h:101
ModifiedAssetsManager::~ModifiedAssetsManager
virtual ~ModifiedAssetsManager()
Definition: ModifiedAssetsManager.h:27
stringbuffer_length
size_t stringbuffer_length(StringBuffer *sb)
Definition: stringbuffer.cpp:218
AssetWriter.h
disinfect.a
a
Definition: disinfect.py:13
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
ModifiedAssetsManager::hasPendingChanges
bool hasPendingChanges() const
Definition: ModifiedAssetsManager.h:69
ModifiedAssetsManager::files
std::vector< std::string > files() const
Definition: ModifiedAssetsManager.h:31
mad_mage_user.file
file
Definition: mad_mage_user.py:15
ModifiedAssetsManager::originOf
std::string originOf(const T *asset) const
Definition: ModifiedAssetsManager.h:39
npc_dialog.filename
filename
Definition: npc_dialog.py:99
buf
StringBuffer * buf
Definition: readable.cpp:1565
ModifiedAssetsManager::dirtyAssetsWithNoOrigin
std::vector< T * > dirtyAssetsWithNoOrigin() const
Definition: ModifiedAssetsManager.h:75
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:57
ModifiedAssetsManager::write
void write(const std::string &filename, std::set< const T * > assets)
Definition: ModifiedAssetsManager.h:84
navar-midane_time.data
data
Definition: navar-midane_time.py:11
make_face_from_files.out
out
Definition: make_face_from_files.py:81
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:55
ModifiedAssetsManager
Definition: ModifiedAssetsManager.h:24
ModifiedAssetsManager::assetDefined
void assetDefined(const T *asset, const std::string &filename)
Definition: ModifiedAssetsManager.h:48
ModifiedAssetsManager::ModifiedAssetsManager
ModifiedAssetsManager(AssetWriter< T > *writer)
Definition: ModifiedAssetsManager.h:26
ModifiedAssetsManager::myWriter
AssetWriter< T > * myWriter
Definition: ModifiedAssetsManager.h:100
ModifiedAssetsManager::myDirty
std::set< T * > myDirty
Definition: ModifiedAssetsManager.h:102
AssetWriter
Definition: AssetWriter.h:22