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 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 "AssetWriter.h"
19 #include <QFile>
20 
21 template<class T>
23 public:
25  virtual ~ModifiedAssetsManager() { delete myWriter; }
26 
27  const std::map<std::string, std::set<const T*> >& origins() const { return myOrigins; }
28 
29  std::string originOf(const T *asset) const {
30  for (auto file = myOrigins.begin(); file != myOrigins.end(); file++) {
31  if (file->second.count(asset) > 0) {
32  return file->first;
33  }
34  }
35  return std::string();
36  }
37 
38  void assetDefined(const T *asset, const std::string &filename) {
39  for (auto it = myOrigins.begin(); it != myOrigins.end(); it++) {
40  it->second.erase(asset);
41  }
42  myOrigins[filename.c_str()].insert(asset);
43  }
44 
45  void assetModified(T *asset) { myDirty.insert(asset); }
46 
48  for (auto a = myDirty.begin(); a != myDirty.end(); a++) {
49  for (auto file = myOrigins.begin(); file != myOrigins.end(); file++) {
50  if (file->second.count(*a) > 0) {
51  write(file->first, file->second);
52  }
53  }
54  }
55 
56  myDirty.clear();
57  }
58 
59  bool hasPendingChanges() const { return !myDirty.empty(); }
60 
61 protected:
62  void write(const std::string &filename, std::set<const T*> assets) {
63  auto buf = stringbuffer_new();
64  for (auto asset = assets.begin(); asset != assets.end(); asset++) {
65  myWriter->write(*asset, buf);
66  }
67 
68  size_t length = stringbuffer_length(buf);
69  char *data = stringbuffer_finish(buf);
70 
71  QFile out(QString(filename.data()));
72  out.open(QIODevice::WriteOnly);
73  out.write(data, length);
74  free(data);
75  }
76 
77 private:
79  std::map<std::string, std::set<const T*> > myOrigins;
80  std::set<T *> myDirty;
81 };
82 
83 #endif /* MODIFIEDASSETSMANAGER_H */
ModifiedAssetsManager::origins
const std::map< std::string, std::set< const T * > > & origins() const
Definition: ModifiedAssetsManager.h:27
ModifiedAssetsManager::myOrigins
std::map< std::string, std::set< const T * > > myOrigins
Definition: ModifiedAssetsManager.h:79
ModifiedAssetsManager::~ModifiedAssetsManager
virtual ~ModifiedAssetsManager()
Definition: ModifiedAssetsManager.h:25
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
AssetWriter.h
disinfect.a
a
Definition: disinfect.py:13
ModifiedAssetsManager::hasPendingChanges
bool hasPendingChanges() const
Definition: ModifiedAssetsManager.h:59
mad_mage_user.file
file
Definition: mad_mage_user.py:15
ModifiedAssetsManager::originOf
std::string originOf(const T *asset) const
Definition: ModifiedAssetsManager.h:29
npc_dialog.filename
filename
Definition: npc_dialog.py:99
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:47
ModifiedAssetsManager::write
void write(const std::string &filename, std::set< const T * > assets)
Definition: ModifiedAssetsManager.h:62
navar-midane_time.data
data
Definition: navar-midane_time.py:11
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
make_face_from_files.out
out
Definition: make_face_from_files.py:75
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:45
ModifiedAssetsManager
Definition: ModifiedAssetsManager.h:22
ModifiedAssetsManager::assetDefined
void assetDefined(const T *asset, const std::string &filename)
Definition: ModifiedAssetsManager.h:38
stringbuffer_length
size_t stringbuffer_length(StringBuffer *sb)
Definition: stringbuffer.c:218
buf
StringBuffer * buf
Definition: readable.c:1610
ModifiedAssetsManager::ModifiedAssetsManager
ModifiedAssetsManager(AssetWriter< T > *writer)
Definition: ModifiedAssetsManager.h:24
ModifiedAssetsManager::myWriter
AssetWriter< T > * myWriter
Definition: ModifiedAssetsManager.h:78
ModifiedAssetsManager::myDirty
std::set< T * > myDirty
Definition: ModifiedAssetsManager.h:80
AssetWriter
Definition: AssetWriter.h:24