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