aboutsummaryrefslogtreecommitdiff
path: root/replaydata.cc
blob: 021a79fec92f22d68f9b8c7f1d018ce37edf379d (plain)
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
#include <QtCore/QDebug>
#include <QtCore/QDataStream>
#include <QtCore/QDirIterator>
#include "smartpen.h"
#include "replaydata.h"

namespace
{
bool readUtfString(QDataStream &stream, QString &s)
{
	quint16 len;
	stream >> len;
	QByteArray buffer(len, Qt::Uninitialized);
	if (stream.readRawData(buffer.data(), len) != len) {
		return false;
	}
	s = QString::fromUtf8(buffer);
	return true;
}
}

ReplayData::ReplayData(QObject *parent) :
    QObject(parent)
{
}

bool ReplayData::open(const QString &path)
{
	_dir.setPath(path);
	if (!_dir.exists()) {
		return false;
	}

	QDirIterator iter(_dir.path(), QStringList("session.info"), QDir::Files, QDirIterator::Subdirectories);
	while (iter.hasNext()) {
		QFileInfo finfo(iter.next());
		QDir sessionDir(finfo.path());
		Session session;
		if (!parseSessionInfo(session, finfo.filePath())) {
			qWarning() << "Could not parse:" << finfo.absoluteFilePath();
		}
		if (!parseSessionPages(session, sessionDir.filePath("session.pages"))) {
			qWarning() << "Could not parse:" << sessionDir.absoluteFilePath("session.pages");
		}
	}

	return true;
}

bool ReplayData::parseSessionInfo(Session &session, const QString &path)
{
	QFile f(path);
	if (f.open(QIODevice::ReadOnly)) {
		return parseSessionInfo(session, &f);
	} else {
		return false;
	}
}

bool ReplayData::parseSessionInfo(Session &session, QIODevice *dev)
{
	unsigned char magic[2];
	if (dev->read(reinterpret_cast<char*>(magic), 2) != 2 ||
	        magic[0] != 0xFA || magic[1] != 0xCE) {
		qWarning() << "Invalid magic";
		return false;
	}

	char version = 0;
	if (!dev->getChar(&version)) {
		qWarning() << "Short read while getting version number";
		return false;
	}

	switch (version) {
	case 3:
		return parseSessionInfoV3(session, dev);
	default:
		qWarning() << "Unknown version:" << version;
		return false;
	}
}

bool ReplayData::parseSessionInfoV3(Session &session, QIODevice *dev)
{
	QDataStream s(dev);
	if (s.skipRawData(5) != 5) return false;

	qint64 startTime, endTime, creationTime;
	QString name;

	s >> startTime >> endTime >> creationTime;
	if (!readUtfString(s, name)) {
		return false;
	}

	qDebug() << "Name:" << name << Smartpen::fromPenTime(startTime) << Smartpen::fromPenTime(endTime) << creationTime;

	quint16 num_clips;
	s >> num_clips;

	for (uint i = 0; i < num_clips; ++i) {
		QString file;
		if (!readUtfString(s, file)) {
			return false;
		}
		s >> startTime >> endTime;
		qDebug() << " Clip:" << file << startTime << endTime;
		qDebug() << "    " << QDateTime::fromTime_t(startTime) << QDateTime::fromTime_t(endTime);
	}

	quint16 num_strokes;
	s >> num_strokes;

	for (uint i = 0; i < num_strokes; ++i) {
		quint64 a, b, c;
		quint32 d;
		QString nbGuid;
		quint8 f, g;
		s >> a >> b >> c >> d;
		if (!readUtfString(s, nbGuid)) {
			return false;
		}
		s >> f >> g;
		qDebug() << " Stroke:" << a << b << c << d << nbGuid << f << g;
	}

	return true;
}

bool ReplayData::parseSessionPages(Session &session, const QString &path)
{
	QFile f(path);
	if (f.open(QIODevice::ReadOnly)) {
		return parseSessionPages(session, &f);
	} else {
		return false;
	}
}

bool ReplayData::parseSessionPages(Session &session, QIODevice *dev)
{
	unsigned char magic[2];
	if (dev->read(reinterpret_cast<char*>(magic), 2) != 2 ||
	        magic[0] != 0xCA || magic[1] != 0xBE) {
		qWarning() << "Invalid magic";
		return false;
	}

	char version = 0;
	if (!dev->getChar(&version)) {
		qWarning() << "Short read while getting version number";
		return false;
	}

	switch (version) {
	case 1:
		return parseSessionPagesV1(session, dev);
	default:
		qWarning() << "Unknown version:" << version;
		return false;
	}
}

bool ReplayData::parseSessionPagesV1(Session &session, QIODevice *dev)
{
	QDataStream s(dev);
	if (s.skipRawData(1) != 1) return false;

	quint16 num_pages = 0;
	s >> num_pages;
	session.pages.reserve(session.pages.size() + num_pages);

	for (uint i = 0; i < num_pages; ++i) {
		quint64 address, unk;
		s >> address >> unk;

		session.pages.append(address);
		qDebug() << " Page:" << address << unk;
	}

	return true;
}