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 */
stringbuffer_length
size_t stringbuffer_length(StringBuffer *sb)
Definition: stringbuffer.cpp:218
it
**Media tags please refer to the protocol file in doc Developers protocol Quick for your pleasure an example[/b][i] This is an old full of dirt and partially destroyed[hand] My dear as you two years i had to leave quickly Words have come to me of powerful magic scrolls discovered in an old temple by my uncle I have moved to study them I not forgot your knowledge in ancient languages I need your help for[print][b] Some parts of document are to damaged to be readable[/b][arcane] Arghis[color=Red] k h[color=dark slate blue] ark[color=#004000] fido[/color][hand] please come as fast as possible my friend[print][b] The bottom of letter seems deliberatly shredded What is it
Definition: media-tags.txt:28
ModifiedAssetsManager::ModifiedAssetsManager
ModifiedAssetsManager(AssetWriter< T > *writer)
Definition: ModifiedAssetsManager.h:26
AssetWriter.h
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
AssetWriter
Definition: AssetWriter.h:22
mad_mage_user.file
file
Definition: mad_mage_user.py:15
npc_dialog.filename
filename
Definition: npc_dialog.py:99
ModifiedAssetsManager::~ModifiedAssetsManager
virtual ~ModifiedAssetsManager()
Definition: ModifiedAssetsManager.h:27
buf
StringBuffer * buf
Definition: readable.cpp:1552
ModifiedAssetsManager::origins
const std::map< std::string, std::set< const T * > > & origins() const
Definition: ModifiedAssetsManager.h:29
ModifiedAssetsManager::originOf
std::string originOf(const T *asset) const
Definition: ModifiedAssetsManager.h:39
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
ModifiedAssetsManager::assetDefined
void assetDefined(const T *asset, const std::string &filename)
Definition: ModifiedAssetsManager.h:48
navar-midane_time.data
data
Definition: navar-midane_time.py:11
ModifiedAssetsManager::write
void write(const std::string &filename, std::set< const T * > assets)
Definition: ModifiedAssetsManager.h:84
ModifiedAssetsManager::dirtyAssetsWithNoOrigin
std::vector< T * > dirtyAssetsWithNoOrigin() const
Definition: ModifiedAssetsManager.h:75
ModifiedAssetsManager::files
std::vector< std::string > files() const
Definition: ModifiedAssetsManager.h:31
make_face_from_files.out
out
Definition: make_face_from_files.py:81
ModifiedAssetsManager::myDirty
std::set< T * > myDirty
Definition: ModifiedAssetsManager.h:102
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:55
ModifiedAssetsManager
Definition: ModifiedAssetsManager.h:24
ModifiedAssetsManager::hasPendingChanges
bool hasPendingChanges() const
Definition: ModifiedAssetsManager.h:69
ModifiedAssetsManager::myWriter
AssetWriter< T > * myWriter
Definition: ModifiedAssetsManager.h:100
a
Magical Runes Runes are magical inscriptions on the dungeon which cast a spell or detonate when something steps on them Flying objects don t detonate runes Beware ! Runes are invisible most of the time They are only visible occasionally ! There are several runes which are there are some special runes which may only be called with the invoke and people may apply it to read it Maybe useful for mazes ! This rune will not nor is it ordinarily invisible Partial Visibility of they ll be visible only part of the time They have a(your level/2) chance of being visible in any given round
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:57
ModifiedAssetsManager::myOrigins
std::map< std::string, std::set< const T * > > myOrigins
Definition: ModifiedAssetsManager.h:101