-
Notifications
You must be signed in to change notification settings - Fork 6
/
eeparser.cpp
285 lines (235 loc) · 8.43 KB
/
eeparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "eeparser.h"
#include <QFile>
#include <QDebug>
#include <QRegularExpression>
#include <QDateTime>
#include <QFileSystemWatcher>
#include <QFileInfo>
#include <QDir>
#include <QTimer>
#include <QThread>
#include <QSettings>
#include "filewatcher.h"
#include "globals.h"
namespace Yate {
EEParser::EEParser(QString logFilename, bool isLive, QObject *parent):
QObject(parent), filename_(logFilename), liveParsing_(isLive), settings_(new QSettings),
currentPosition_(0), evtId_(0),
lineParseRegex_("(\\d+\\.\\d+)\\s+(\\w+)\\s+\\[(\\w+)\\]\\s*\\:\\s*(.*)"),
logDoesNotExist_(true), hostJustUnloaded_(true)
{
}
bool EEParser::liveParsing() const
{
return liveParsing_;
}
void EEParser::setLiveParsing(bool newLiveParsing)
{
liveParsing_ = newLiveParsing;
}
const QString &EEParser::filename() const
{
return filename_;
}
void EEParser::setFilename(const QString &newFilename)
{
filename_ = newFilename;
}
int EEParser::currentPosition() const
{
return currentPosition_;
}
void EEParser::setCurrentPosition(int newCurrentPosition)
{
currentPosition_ = newCurrentPosition;
}
void EEParser::reset()
{
setCurrentPosition(0);
evtId_ = 0;
hostJustUnloaded_ = true;
}
void Yate::EEParser::parseLine(QString &line)
{
line = line.trimmed();
if (!line.length()) {
return;
}
auto it = lineParseRegex_.globalMatch(line);
bool matched = false;
float timestamp;
QString msgText;
if (it.hasNext()) {
matched = true;
auto match = it.next();
timestamp = match.captured(1).toFloat();
msgText = match.captured(4).trimmed();
}
if(matched) {
int val;
QString strVal;
auto evtType = EEParser::msgToEventType(msgText, val, strVal);
if (evtType != LogEventType::Invalid) {
if (hostJustUnloaded_) {
if (evtType != LogEventType::TeralystSpawn && evtType != LogEventType::NightBegin && evtType != LogEventType::HostJoin
&& evtType != LogEventType::DoorOpening && evtType != LogEventType::DoorOpened) {
return;
}
}
LogEvent evt(evtId_++, evtType, timestamp, val, strVal);
if (evtType == LogEventType::HostUnload) {
hostJustUnloaded_ = true;
} else {
hostJustUnloaded_ = false;
}
emit logEvent(evt);
}
}
}
void EEParser::stopParsing()
{
qDebug() << "Stopping live feedback parser.";
emit stopWatcher();
emit parsingFinished();
}
void EEParser::startOffline()
{
qDebug() << "Offline parsing started.";
emit parsingStarted();
QFile logFile(filename());
QString fileContent;
if(logFile.open(QIODevice::ReadOnly)) {
fileContent = QString(logFile.readAll());
auto lines = fileContent.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
for(int i = 0; i < lines.size(); i++) {
parseLine(lines[i]);
}
logFile.close();
} else {
qDebug() << "Offline parsing file error: " << logFile.errorString();
emit parsingError(logFile.errorString());
}
qDebug() << "Offline parsing finshed.";
emit parsingFinished();
}
void EEParser::processFileContent(QFile &logFile)
{
QString fileContent = QString(logFile.readAll());
auto lines = fileContent.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
for(int i = 0; i < lines.size() - 2; i++) {
parseLine(lines[i]);
}
int currPos = currentPosition() + fileContent.length();
if (lines.size() > 0) {
currPos -= (lines[lines.size() - 1].size() + 2);
if (lines.size() > 1) {
currPos -= (lines[lines.size() - 2].size() + 2);
}
}
setCurrentPosition(currPos);
}
void EEParser::startLive()
{
qDebug() << "Live parsing started.";
emit parsingStarted();
QFile logFile(filename());
QThread *watcherThread = new QThread;
watcher_ = new FileWatcher(nullptr, filename());
watcher_->moveToThread(watcherThread);
connect( watcherThread, &QThread::started, watcher_, &FileWatcher::start);
connect( this, &EEParser::parsingFinished, watcher_, &FileWatcher::stop, Qt::DirectConnection);
connect( watcherThread, &QThread::finished, watcher_, &QThread::deleteLater);
connect( watcherThread, &QThread::finished, watcherThread, &QThread::deleteLater);
connect(watcher_, &FileWatcher::fileChanged, this, &EEParser::onFileChanged);
QTimer *tmr = new QTimer(this);
tmr->setInterval(1000);
if (settings_->value(SETTINGS_KEY_FORCE_LOGS, false).toBool()) {
connect(tmr, &QTimer::timeout, [&]() {if(QFileInfo::exists(filename())) { QFileInfo(filename()).lastModified();}});
} else {
connect(tmr, &QTimer::timeout, [&]() {if(QFileInfo::exists(filename())) {onFileChanged(true);}});
}
tmr->start();
QString fileContent;
if(logFile.open(QIODevice::ReadOnly)) {
setCurrentPosition(0);
processFileContent(logFile);
logFile.close();
} else {
qDebug() << "Live parsing file error: " << logFile.errorString();
emit parsingError(logFile.errorString());
}
watcherThread->start();
}
LogEventType EEParser::msgToEventType(QString msg, int &val, QString &strVal)
{
QMap<QString, LogEventType> msgEvtMap{
{"TeralystEncounter.lua: It's nighttime!", LogEventType::NightBegin},
{"TeralystEncounter.lua: It's daytime!", LogEventType::DayBegin},
{"TeralystAvatarScript.lua: Weakpoint Destroyed", LogEventType::LimbBreak},
{"TeralystAvatarScript.lua: Teralyst Captured", LogEventType::EidolonCapture},
{"TeralystAvatarScript.lua: Teralyst Killed", LogEventType::EidolonKill},
{"SnapPickupToGround.lua: Snapping pickup to ground (DefaultArcanePickup)", LogEventType::LootDrop},
{"TeralystEncounter.lua: Shrine enabled", LogEventType::ShrineEnable},
{"TeralystEncounter.lua: Shrine disabled", LogEventType::ShrineDisable},
{"EidolonMP.lua: EIDOLONMP: Finalize Eidolon transition", LogEventType::TeralystSpawn},
{"TeralystEncounter.lua: Eidolon spawning SUCCESS", LogEventType::EidolonSpawn},
{"TeralystAvatarScript.lua: Wailing Song Complete - Teleporting", LogEventType::EidolonTeleport},
{"EidolonMP.lua: EIDOLONMP: Level fully destroyed", LogEventType::HostUnload},
{"TeralystEncounter.lua: Teralyst Escape complete. All Teralysts should be gone now", LogEventType::EidolonDespawn},
{"EidolonMP.lua: EIDOLONMP: TownTriggerFirstTouched", LogEventType::DoorOpening},
{"EidolonMP.lua: EIDOLONMP: EnableDoor(WORLD,true)", LogEventType::DoorOpened}
};
val = -1;
if (msgEvtMap.contains(msg)) {
return msgEvtMap[msg];
} else if (msg.startsWith("Starting load for")) {
const QRegularExpression rx("Starting load for\\s+(.+)");
auto match = rx.match(msg);
if (!match.isValid()) {
return LogEventType::Invalid;
}
strVal = match.captured(1);
if (strVal == "Player") {
return LogEventType::Invalid;
}
return LogEventType::SquadJoin;
} else if (msg.startsWith("Logged in ")) {
const QRegularExpression rx("Logged in\\s+(.+)\\s+\\(");
auto match = rx.match(msg);
if (!match.isValid()) {
return LogEventType::Invalid;
}
strVal = match.captured(1);
if (strVal == "Player") {
return LogEventType::Invalid;
}
return LogEventType::HostJoin;
} else if (msg.startsWith("TeralystEncounter.lua: A shard has been put in the Eidolon Shrine.")) {
const QRegularExpression rx("[^0-9]+");
const auto&& parts = msg.split(rx, Qt::SkipEmptyParts);
val = parts[0].toInt();
return LogEventType::ShardInsert;
} else if (msg.startsWith("TeralystEncounter.lua: A shard has been removed from the Eidolon Shrine.")) {
return LogEventType::ShardRemove;
} else {
return LogEventType::Invalid;
}
}
void EEParser::onFileChanged(bool exists)
{
if (!exists) {
setCurrentPosition(0);
emit parsingReset();
} else {
QFile logFile(filename());
QString fileContent;
if(logFile.open(QIODevice::ReadOnly)) {
logFile.seek(currentPosition());
processFileContent(logFile);
logFile.close();
} else {
emit parsingError(logFile.errorString());
}
}
}
}