Crossfire Server, Trunk
ResourcesManager.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 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 #include <QString>
14 #include <qlist.h>
15 #include <qhash.h>
16 #include <QStringList>
17 #include <QMessageBox>
18 #include "ResourcesManager.h"
19 #include <locale.h>
20 
21 #include "global.h"
22 #include "libproto.h"
23 #include "recipe.h"
24 #include "image.h"
25 #include "sproto.h"
26 #include "assets.h"
27 #include "logger.h"
28 #include "AssetsManager.h"
30 #include "random_maps/RandomMap.h"
31 #include "LicenseManager.h"
32 #include "ArchetypeWriter.h"
33 #include "QuestWriter.h"
34 #include "MessageWriter.h"
35 #include "TreasureWriter.h"
36 #include "ArtifactWriter.h"
39 #include "faces/FaceWrapper.h"
42 
43 ResourcesManager::ResourcesManager() : myMapInformationManager(nullptr), myArchetypes(new ArchetypeWriter()), myQuests(new QuestWriter()), myTreasures(new TreasureWriter()),
44  myGeneralMessages(new MessageWriter()), myArtifacts(new ArtifactWriter())
45 {
46 }
47 
49 {
50 }
51 
52 static void onFatalInit(enum fatal_error) {
53  QMessageBox::critical(nullptr, "Fatal error", "Error while initializing Crossfire data, make sure you have maps and archetypes correctly installed.");
54 }
55 
57 {
58  setlocale(LC_NUMERIC, "C");
59 
62  static std::function<void(BufferReader *, const char *)> dummy = [this] (BufferReader *reader, const char *filename) { myLicenseManager.readLicense(reader, filename); };
63  settings.hooks[settings.hooks_count] = [] (BufferReader *reader, const char *filename) { dummy(reader, filename); };
66  settings.hooks[settings.hooks_count] = [] (BufferReader *, const char *) { QCoreApplication::processEvents(); };
71 
72  QStringList log;
73  bool hasWarningOrError = false;
74  static std::function<void(LogLevel, const char *, va_list)> lc = [&] (LogLevel logLevel, const char *format, va_list va) {
75  if (logLevel > llevInfo) {
76  return;
77  }
78  char buf[8192];
79  vsnprintf(buf, sizeof(buf), format, va);
80  QString l(tr("%1%2").arg(loglevel_names[logLevel], buf));
81  log.append(l);
82  if (logLevel == llevError) {
83  hasWarningOrError = true;
84  }
85  };
86  settings.log_callback = [] (LogLevel logLevel, const char *format, va_list va) { lc(logLevel, format, va); };
87 
88  init_globals();
89  init_library();
90  settings.fatal_hook = nullptr;
91  init_gods();
92  init_readable();
93  settings.log_callback = nullptr;
94 
95  if (hasWarningOrError) {
96  QString msg(tr("The following errors occurred during asset collection:\n") + log.join(""));
97  QMessageBox::warning(nullptr, tr("Errors during asset collection!"), msg);
98  }
99 
100  QString key;
101 
102  for (int ing = 1; ; ing++)
103  {
105  if (!list)
106  break;
107 
108  QHash<QString, recipestruct*> recipes;
109  for (recipestruct* rec = list->items; rec; rec = rec->next)
110  {
111  key = QString("%1_%2").arg(rec->arch_name[0], rec->title);
112  recipes[key] = rec;
113  }
114  myRecipes.append(recipes);
115  }
116 }
117 
119 {
120  return myRecipes.size();
121 }
122 
123 QStringList ResourcesManager::recipes(int count) const
124 {
125  if (count < 1 || count > myRecipes.size())
126  return QStringList();
127 
128  QStringList keys = myRecipes[count - 1].keys();
129  qSort(keys);
130  return keys;
131 }
132 
133 const recipe* ResourcesManager::getRecipe(int ingredients, const QString& name) const
134 {
135  if (ingredients < 1 || ingredients > myRecipes.size())
136  return NULL;
137 
138  return myRecipes[ingredients - 1][name];
139 }
140 
142 {
143  bool goOn = true;
144  getManager()->archetypes()->each([&item, &callback, &goOn] (archt *arch) {
145  if (!goOn)
146  return;
147 
148  if (arch->clone.other_arch == item)
149  {
150  goOn = callback(OTHER_ARCH, arch, nullptr, nullptr, nullptr);
151  }
152 
153  sstring death_anim = NULL;
154  if (goOn && (death_anim = object_get_value(&arch->clone, "death_animation")) && strcmp(death_anim, item->name) == 0)
155  {
156  goOn = callback(DEATH_ANIM, arch, nullptr, nullptr, nullptr);
157  }
158  });
159 
160  getManager()->treasures()->each([&item, callback, &goOn] (treasurelist *list) {
161  if (!goOn)
162  return;
163  for (auto t = list->items; t; t = t->next)
164  {
165  if (t->item == item)
166  {
167  goOn = callback(TREASURE_USE, nullptr, list, nullptr, nullptr);
168  }
169  }
170  });
171 
172  QList<CREMapInformation*> mapuse = store->getArchetypeUse(item);
173  foreach(CREMapInformation* information, mapuse)
174  {
175  if (!goOn)
176  continue;
177  goOn = callback(MAP_USE, nullptr, nullptr, information, nullptr);
178  }
179  auto allMaps = store->allMaps();
180  foreach(CREMapInformation *information, allMaps)
181  {
182  if (!goOn)
183  return;
184  foreach(RandomMap* rm, information->randomMaps())
185  {
186  if (strcmp(item->name, rm->parameters()->final_exit_archetype) == 0)
187  {
188  goOn = callback(RANDOM_MAP_FINAL_EXIT, nullptr, nullptr, information, nullptr);
189  }
190  if (!goOn)
191  return;
192  }
193  }
194 
195  int count = 1;
196  recipelist* list;
197  while ((list = get_formulalist(count++)))
198  {
199  if (!goOn)
200  break;
201  recipestruct* rec = list->items;
202  while (goOn && rec)
203  {
204  for (size_t ing = 0; ing < rec->arch_names; ing++)
205  {
206  if (strcmp(rec->arch_name[ing], item->name) == 0)
207  {
208  goOn = callback(ALCHEMY_PRODUCT, nullptr, nullptr, nullptr, rec);
209  break;
210  }
211  }
212  rec = rec->next;
213  }
214  }
215 }
216 
218  myArchetypes.assetModified(arch);
219 }
220 
222  myArchetypes.saveModifiedAssets();
223 }
224 
227 }
228 
231 }
232 
234  myTreasures.assetModified(treasure);
235 }
236 
238  auto no = myTreasures.dirtyAssetsWithNoOrigin();
239  while (!no.empty()) {
240  auto list = no.back();
241  auto origins = myTreasures.files();
242  while (true) {
244  origins, std::vector<std::string>());
245  if (dlg.exec() == QDialog::Accepted) {
246  myTreasures.assetDefined(list, dlg.file().toStdString());
247  break;
248  } else {
249  if (QMessageBox::question(nullptr, tr("Lose changes to treasure list %1?").arg(list->name),
250  tr("Really discard changes to treasure list %1?").arg(list->name)) == QMessageBox::Yes) {
251  break;
252  }
253  }
254  }
255  no.pop_back();
256  }
257  myTreasures.saveModifiedAssets();
258 }
259 
262 }
263 
266 }
267 
269  myArtifacts.saveModifiedAssets();
270 }
ResourcesManager::treasureModified
void treasureModified(treasurelist *treasure)
Definition: ResourcesManager.cpp:233
AssetUseCallback
std::function< bool(ArchetypeUse use, const archt *, const treasurelist *, const CREMapInformation *, recipe *)> AssetUseCallback
Definition: ResourcesManager.h:75
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
init_globals
void init_globals(void)
Definition: init.cpp:397
ResourcesManager::myQuests
ModifiedAssetsManager< quest_definition > myQuests
Definition: ResourcesManager.h:164
RandomMap
Definition: RandomMap.h:24
global.h
RandomMap::parameters
const RMParms * parameters() const
Definition: RandomMap.cpp:39
Settings::hooks_count
uint8_t hooks_count
Definition: global.h:332
settings
struct Settings settings
Definition: init.cpp:139
banquet.l
l
Definition: banquet.py:164
GeneralMessage
Definition: book.h:44
get_formulalist
recipelist * get_formulalist(int i)
Definition: recipe.cpp:97
ArchetypeWriter.h
ResourcesManager::ResourcesManager
ResourcesManager()
Definition: ResourcesManager.cpp:43
Settings::archetypes_tracker
void * archetypes_tracker
Definition: global.h:336
llevError
@ llevError
Definition: logger.h:11
ArchetypeWriter
Definition: ArchetypeWriter.h:19
log_born.log
log
Definition: log_born.py:28
ResourcesManager::archetypeUse
static void archetypeUse(const archt *item, CREMapInformationManager *store, AssetUseCallback callback)
Definition: ResourcesManager.cpp:141
ResourcesManager::getRecipe
const recipestruct * getRecipe(int ingredients, const QString &name) const
Definition: ResourcesManager.cpp:133
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
ResourcesManager::saveArchetypes
void saveArchetypes()
Definition: ResourcesManager.cpp:221
AssetsManager.h
ResourcesManager::questModified
void questModified(quest_definition *quest)
Definition: ResourcesManager.cpp:225
logger.h
QuestWriter.h
ResourcesManager::myArtifacts
ModifiedAssetsManager< artifact > myArtifacts
Definition: ResourcesManager.h:167
MessageWriter.h
guildoracle.list
list
Definition: guildoracle.py:87
recipe.h
ArtifactWriter.h
ObjectWrapper.h
hall_of_fame.keys
keys
Definition: hall_of_fame.py:43
ResourcesManager::myLicenseManager
LicenseManager myLicenseManager
Definition: ResourcesManager.h:183
ResourcesManager::recipeMaxIngredients
int recipeMaxIngredients() const
Definition: ResourcesManager.cpp:118
RandomMap.h
ResourcesManager::saveQuests
void saveQuests()
Definition: ResourcesManager.cpp:229
npc_dialog.filename
filename
Definition: npc_dialog.py:99
AssetOriginAndCreationDialog::DefineOrigin
@ DefineOrigin
Definition: AssetOriginAndCreationDialog.h:24
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.cpp:4339
Settings::ignore_assets_errors
int ignore_assets_errors
Definition: global.h:335
add_server_collect_hooks
void add_server_collect_hooks()
Definition: init.cpp:1092
Settings::fatal_hook
fatalHook fatal_hook
Definition: global.h:337
buf
StringBuffer * buf
Definition: readable.cpp:1611
getManager
AssetsManager * getManager()
Definition: assets.cpp:309
treasurestruct
Definition: treasure.h:63
LicenseManager.h
ResourcesManager::myGeneralMessages
ModifiedAssetsManager< GeneralMessage > myGeneralMessages
Definition: ResourcesManager.h:166
archt
Definition: object.h:472
Settings::log_callback
logHook log_callback
Definition: global.h:244
init_gods
void init_gods(void)
Definition: holy.cpp:61
ResourcesManager::saveArtifacts
void saveArtifacts()
Definition: ResourcesManager.cpp:268
quest
Definition: quest.py:1
CREMapInformation
Definition: CREMapInformation.h:27
recipestruct
Definition: recipe.h:10
AssetsTracker
Definition: AssetsTracker.h:26
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:57
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
ResourcesManager::saveGeneralMessages
void saveGeneralMessages()
Definition: ResourcesManager.cpp:264
ResourcesManager::load
void load()
Definition: ResourcesManager.cpp:56
AssetOriginAndCreationDialog.h
recipestruct::arch_name
char ** arch_name
Definition: recipe.h:13
AnimationWrapper.h
init_readable
void init_readable(void)
Definition: readable.cpp:904
AssetOriginAndCreationDialog
Definition: AssetOriginAndCreationDialog.h:21
navar-midane_pickup.msg
list msg
Definition: navar-midane_pickup.py:13
nlohmann::detail::cbor_tag_handler_t::store
@ store
store tags as binary type
ArchetypeWrapper.h
ArtifactWriter
Definition: ArtifactWriter.h:19
ResourcesManager::recipes
QStringList recipes(int count) const
Definition: ResourcesManager.cpp:123
LicenseManager::readLicense
void readLicense(BufferReader *reader, const char *filename)
Definition: LicenseManager.cpp:25
QuestWriter
Definition: QuestWriter.h:19
sstring
const typedef char * sstring
Definition: global.h:43
disinfect.count
int count
Definition: disinfect.py:7
FaceWrapper.h
Settings::hooks_filename
const char * hooks_filename[20]
Definition: global.h:333
sproto.h
recipeliststruct
Definition: recipe.h:37
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
image.h
MAP_USE
@ MAP_USE
Definition: ResourcesManager.h:70
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
CREMapInformationManager.h
ResourcesManager.h
AssetOriginAndCreationDialog::Treasure
@ Treasure
Definition: AssetOriginAndCreationDialog.h:23
ResourcesManager::archetypeModified
void archetypeModified(archetype *arch)
Definition: ResourcesManager.cpp:217
ResourcesManager::myRecipes
QList< QHash< QString, recipestruct * > > myRecipes
Definition: ResourcesManager.h:162
CREMapInformation::randomMaps
QList< RandomMap * > randomMaps() const
Definition: CREMapInformation.cpp:268
RMParms::final_exit_archetype
char final_exit_archetype[RM_SIZE]
Definition: random_map.h:59
llevInfo
@ llevInfo
Definition: logger.h:12
Floor.t
t
Definition: Floor.py:62
init_library
void init_library(void)
Definition: init.cpp:324
MessageWriter
Definition: MessageWriter.h:19
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:55
loglevel_names
const char *const loglevel_names[]
Definition: logger.cpp:31
item
Definition: item.py:1
quest_definition
Definition: quest.h:38
onFatalInit
static void onFatalInit(enum fatal_error)
Definition: ResourcesManager.cpp:52
assets.h
recipestruct::next
struct recipestruct * next
Definition: recipe.h:24
Settings::hooks
collectorHook hooks[20]
Definition: global.h:334
TreasureWriter
Definition: TreasureWriter.h:19
ALCHEMY_PRODUCT
@ ALCHEMY_PRODUCT
Definition: ResourcesManager.h:72
ResourcesManager::generalMessageModified
void generalMessageModified(GeneralMessage *message)
Definition: ResourcesManager.cpp:260
castle_read.key
key
Definition: castle_read.py:64
ResourcesManager::origins
const std::map< std::string, std::set< const archt * > > & origins() const
Definition: ResourcesManager.h:107
RANDOM_MAP_FINAL_EXIT
@ RANDOM_MAP_FINAL_EXIT
Definition: ResourcesManager.h:71
fatal_error
fatal_error
Definition: define.h:47
ResourcesManager::myArchetypes
ModifiedAssetsManager< archetype > myArchetypes
Definition: ResourcesManager.h:163
ResourcesManager::saveTreasures
void saveTreasures()
Definition: ResourcesManager.cpp:237
TreasureWriter.h
LogLevel
LogLevel
Definition: logger.h:10
ResourcesManager::myTreasures
ModifiedAssetsManager< treasurelist > myTreasures
Definition: ResourcesManager.h:165
CREMapInformationManager
Definition: CREMapInformationManager.h:27
BufferReader
Definition: bufferreader.cpp:21
if
if(!(yy_init))
Definition: loader.c:2626
ResourcesManager::~ResourcesManager
virtual ~ResourcesManager()
Definition: ResourcesManager.cpp:48
libproto.h
treasureliststruct
Definition: treasure.h:85
give.name
name
Definition: give.py:27
AssetOriginAndCreationDialog::file
QString file() const
Definition: AssetOriginAndCreationDialog.h:37