Crossfire Server, Trunk
MessageManager.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 "MessageManager.h"
14 #include "MessageFile.h"
15 #include "QuestConditionScript.h"
16 #include "quests/QuestWrapper.h"
17 
18 extern "C" {
19  #include "global.h"
20 }
21 
22 #include <QDir>
23 #include <QDebug>
24 
26  setProperty(tipProperty, tr("Display all NPC dialogs."));
27 }
28 
30 {
31  qDeleteAll(myMessages);
32  qDeleteAll(myPreConditions);
33  qDeleteAll(myPostConditions);
34 }
35 
37 {
38  loadDirectory("");
39 
40  /* get pre and post conditions */
43 }
44 
46 {
47  foreach(MessageFile* file, myMessages)
48  {
49  file->save();
50  }
51 }
52 
53 QList<MessageFile*>& MessageManager::messages()
54 {
55  return myMessages;
56 }
57 
58 const QList<MessageFile*>& MessageManager::messages() const
59 {
60  return myMessages;
61 }
62 
64 {
65  foreach(MessageFile* file, myMessages)
66  {
67  if (file->path() == path)
68  return file;
69  }
70 
71  return NULL;
72 }
73 
74 void MessageManager::loadDirectory(const QString& directory)
75 {
76  //qDebug() << "load" << directory;
77  QDir dir(QString("%1/%2/%3").arg(settings.datadir, settings.mapdir, directory));
78 
79  // first messages
80  QStringList messages = dir.entryList(QStringList("*.msg"), QDir::Files);
81  //qDebug() << "found" << messages;
82  foreach(QString message, messages)
83  {
84  QString path = directory + QDir::separator() + message;
85  MessageFile* file = new MessageFile(this, path);
86  if (file->parseFile())
87  {
88  myMessages.append(file);
89  }
90  else
91  {
92  qDebug() << "dialog parse error" << path;
93  delete file;
94  }
95  }
96 
97  // recurse
98  QStringList subdirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
99  foreach(QString sub, subdirs)
100  loadDirectory(directory + QDir::separator() + sub);
101 }
102 
103 QList<QuestConditionScript*> MessageManager::preConditions() const
104 {
105  return myPreConditions;
106 }
107 
108 QList<QuestConditionScript*> MessageManager::postConditions() const
109 {
110  return myPostConditions;
111 }
112 
113 QString MessageManager::loadScriptComment(const QString& path) const
114 {
115  QFile file(path);
116  if (!file.open(QIODevice::ReadOnly))
117  return "";
118 
119  QTextStream stream(&file);
120  QStringList lines = stream.readAll().split("\n");
121 
122  QString comment, line;
123 
124  /* by convention, the first 2 lines are encoding and script name */
125  for(int i = 2; i < lines.size(); i++)
126  {
127  line = lines[i];
128  if (!line.startsWith("# "))
129  break;
130  comment += line.mid(2) + "\n";
131  }
132 
133  return comment.trimmed();
134 }
135 
136 void MessageManager::findPrePost(const QString directory, QList<QuestConditionScript*>& list)
137 {
138  QDir dir(QString("%1/%2/python/dialog/%3").arg(settings.datadir, settings.mapdir, directory));
139  QFileInfoList files = dir.entryInfoList(QStringList("*.py"));
140  foreach(QFileInfo file, files)
141  {
142  list.append(new QuestConditionScript(file.baseName(), loadScriptComment(file.absoluteFilePath())));
143  }
144 }
145 
146 AssetWrapper::PossibleUse MessageManager::uses(const AssetWrapper *asset, std::string &) const {
147  auto quest = dynamic_cast<const QuestWrapper *>(asset);
148  if (quest) {
149  return ChildrenMayUse;
150  }
151  return DoesntUse;
152 }
MessageManager::loadScriptComment
QString loadScriptComment(const QString &path) const
Definition: MessageManager.cpp:113
Settings::mapdir
const char * mapdir
Definition: global.h:246
global.h
MessageManager::myPreConditions
QList< QuestConditionScript * > myPreConditions
Definition: MessageManager.h:73
QuestConditionScript
Definition: QuestConditionScript.h:21
MessageManager.h
MessageManager::findMessage
MessageFile * findMessage(const QString &path)
Definition: MessageManager.cpp:63
Settings::datadir
const char * datadir
Definition: global.h:243
MessageManager::loadMessages
void loadMessages()
Definition: MessageManager.cpp:36
QuestWrapper
Definition: QuestWrapper.h:68
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:28
guildoracle.list
list
Definition: guildoracle.py:87
MessageManager::uses
virtual PossibleUse uses(const AssetWrapper *asset, std::string &) const override
Definition: MessageManager.cpp:146
mad_mage_user.file
file
Definition: mad_mage_user.py:15
MessageManager::messages
QList< MessageFile * > & messages()
Definition: MessageManager.cpp:53
MessageManager::MessageManager
MessageManager(AssetWrapper *parent)
Definition: MessageManager.cpp:25
MessageFile
Definition: MessageFile.h:68
MessageManager::postConditions
QList< QuestConditionScript * > postConditions() const
Definition: MessageManager.cpp:108
AssetWrapper::tipProperty
static const char * tipProperty
Definition: AssetWrapper.h:30
MessageManager::myPostConditions
QList< QuestConditionScript * > myPostConditions
Definition: MessageManager.h:74
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:28
MessageManager::~MessageManager
virtual ~MessageManager()
Definition: MessageManager.cpp:29
settings
struct Settings settings
Definition: init.c:39
QuestWrapper.h
quest
Definition: quest.py:1
is_valid_types_gen.line
line
Definition: is_valid_types_gen.py:34
MessageManager::preConditions
QList< QuestConditionScript * > preConditions() const
Definition: MessageManager.cpp:103
AssetWrapper
Definition: AssetWrapper.h:22
python_init.path
path
Definition: python_init.py:8
MessageManager::myMessages
QList< MessageFile * > myMessages
Definition: MessageManager.h:72
diamondslots.message
string message
Definition: diamondslots.py:57
MessageFile.h
MessageManager::loadDirectory
void loadDirectory(const QString &directory)
Definition: MessageManager.cpp:74
QuestConditionScript.h
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:28
MessageManager::findPrePost
void findPrePost(const QString directory, QList< QuestConditionScript * > &list)
Definition: MessageManager.cpp:136
MessageManager::saveMessages
void saveMessages()
Definition: MessageManager.cpp:45