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