NOISSUE eliminate timestamp strings

This commit is contained in:
Petr Mrázek 2016-03-02 03:03:44 +01:00
parent 80b81c2c1e
commit b54839b897
13 changed files with 97 additions and 81 deletions

View File

@ -60,9 +60,7 @@ void MinecraftProfile::reload()
void MinecraftProfile::clear() void MinecraftProfile::clear()
{ {
id.clear(); id.clear();
m_updateTimeString.clear();
m_updateTime = QDateTime(); m_updateTime = QDateTime();
m_releaseTimeString.clear();
m_releaseTime = QDateTime(); m_releaseTime = QDateTime();
type.clear(); type.clear();
assets.clear(); assets.clear();

View File

@ -121,12 +121,10 @@ public: /* data */
/// the ID - determines which jar to use! ACTUALLY IMPORTANT! /// the ID - determines which jar to use! ACTUALLY IMPORTANT!
QString id; QString id;
/// the time this version was actually released by Mojang, as string and as QDateTime /// the time this version was actually released by Mojang
QString m_releaseTimeString;
QDateTime m_releaseTime; QDateTime m_releaseTime;
/// the time this version was last updated by Mojang, as string and as QDateTime /// the time this version was last updated by Mojang
QString m_updateTimeString;
QDateTime m_updateTime; QDateTime m_updateTime;
/// Release type - "release" or "snapshot" /// Release type - "release" or "snapshot"

View File

@ -192,14 +192,12 @@ void MinecraftVersion::applyTo(MinecraftProfile *version)
{ {
version->type = m_type; version->type = m_type;
} }
if (!m_releaseTimeString.isNull()) if (!m_releaseTime.isNull())
{ {
version->m_releaseTimeString = m_releaseTimeString;
version->m_releaseTime = m_releaseTime; version->m_releaseTime = m_releaseTime;
} }
if (!m_updateTimeString.isNull()) if (!m_updateTime.isNull())
{ {
version->m_updateTimeString = m_updateTimeString;
version->m_updateTime = m_updateTime; version->m_updateTime = m_updateTime;
} }
version->traits.unite(m_traits); version->traits.unite(m_traits);

View File

@ -111,12 +111,10 @@ public: /* data */
/// The type of this release /// The type of this release
QString m_type; QString m_type;
/// the time this version was actually released by Mojang, as string and as QDateTime /// the time this version was actually released by Mojang
QString m_releaseTimeString;
QDateTime m_releaseTime; QDateTime m_releaseTime;
/// the time this version was last updated by Mojang, as string and as QDateTime /// the time this version was last updated by Mojang
QString m_updateTimeString;
QDateTime m_updateTime; QDateTime m_updateTime;
/// MD5 hash of the minecraft jar /// MD5 hash of the minecraft jar

View File

@ -184,14 +184,7 @@ void MinecraftVersionList::loadBuiltinList()
mcVersion->m_name = mcVersion->m_descriptor = versionID; mcVersion->m_name = mcVersion->m_descriptor = versionID;
// Parse the timestamp. // Parse the timestamp.
if (!parse_timestamp(versionObj.value("releaseTime").toString(""), mcVersion->m_releaseTime = timeFromS3Time(versionObj.value("releaseTime").toString(""));
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime))
{
qCritical() << "Error while parsing version" << versionID
<< ": invalid version timestamp";
continue;
}
mcVersion->m_versionFileURL = QString(); mcVersion->m_versionFileURL = QString();
mcVersion->m_versionSource = Builtin; mcVersion->m_versionSource = Builtin;
mcVersion->m_type = versionTypeStr; mcVersion->m_type = versionTypeStr;
@ -270,20 +263,8 @@ void MinecraftVersionList::loadMojangList(QJsonDocument jsonDoc, VersionSource s
std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion()); std::shared_ptr<MinecraftVersion> mcVersion(new MinecraftVersion());
mcVersion->m_name = mcVersion->m_descriptor = versionID; mcVersion->m_name = mcVersion->m_descriptor = versionID;
if (!parse_timestamp(versionObj.value("releaseTime").toString(""), mcVersion->m_releaseTime = timeFromS3Time(versionObj.value("releaseTime").toString(""));
mcVersion->m_releaseTimeString, mcVersion->m_releaseTime)) mcVersion->m_updateTime = timeFromS3Time(versionObj.value("time").toString(""));
{
qCritical() << "Error while parsing version" << versionID
<< ": invalid release timestamp";
continue;
}
if (!parse_timestamp(versionObj.value("time").toString(""),
mcVersion->m_updateTimeString, mcVersion->m_updateTime))
{
qCritical() << "Error while parsing version" << versionID
<< ": invalid update timestamp";
continue;
}
if (mcVersion->m_releaseTime < g_VersionFilterData.legacyCutoffDate) if (mcVersion->m_releaseTime < g_VersionFilterData.legacyCutoffDate)
{ {
@ -593,8 +574,8 @@ void MinecraftVersionList::saveCachedList()
entryObj.insert("id", mcversion->descriptor()); entryObj.insert("id", mcversion->descriptor());
entryObj.insert("version", mcversion->descriptor()); entryObj.insert("version", mcversion->descriptor());
entryObj.insert("time", mcversion->m_updateTimeString); entryObj.insert("time", timeToS3Time(mcversion->m_updateTime));
entryObj.insert("releaseTime", mcversion->m_releaseTimeString); entryObj.insert("releaseTime", timeToS3Time(mcversion->m_releaseTime));
entryObj.insert("url", mcversion->m_versionFileURL); entryObj.insert("url", mcversion->m_versionFileURL);
entryObj.insert("type", mcversion->m_type); entryObj.insert("type", mcversion->m_type);
entriesArr.append(entryObj); entriesArr.append(entryObj);

View File

@ -45,14 +45,8 @@ VersionFilePtr MojangVersionFormat::versionFileFromJson(const QJsonDocument &doc
readString(root, "assets", out->assets); readString(root, "assets", out->assets);
if (!parse_timestamp(root.value("releaseTime").toString(""), out->m_releaseTimeString, out->m_releaseTime)) out->m_releaseTime = timeFromS3Time(root.value("releaseTime").toString(""));
{ out->m_updateTime = timeFromS3Time(root.value("time").toString(""));
out->addProblem(PROBLEM_WARNING, QObject::tr("Invalid 'releaseTime' timestamp"));
}
if (!parse_timestamp(root.value("time").toString(""), out->m_updateTimeString, out->m_updateTime))
{
out->addProblem(PROBLEM_WARNING, QObject::tr("Invalid 'time' timestamp"));
}
if (root.contains("minimumLauncherVersion")) if (root.contains("minimumLauncherVersion"))
{ {
@ -90,8 +84,8 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch)
writeString(root, "minecraftArguments", patch->overwriteMinecraftArguments); writeString(root, "minecraftArguments", patch->overwriteMinecraftArguments);
writeString(root, "type", patch->type); writeString(root, "type", patch->type);
writeString(root, "assets", patch->assets); writeString(root, "assets", patch->assets);
writeString(root, "releaseTime", patch->m_releaseTimeString); writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime));
writeString(root, "time", patch->m_updateTimeString); writeString(root, "time", timeToS3Time(patch->m_updateTime));
if (!patch->addLibs.isEmpty()) if (!patch->addLibs.isEmpty())
{ {

View File

@ -1,23 +1,33 @@
#include <QDateTime> #include <QDateTime>
#include <QString> #include <QString>
#include "ParseUtils.h" #include "ParseUtils.h"
#include <QDebug>
QDateTime timeFromS3Time(QString str) QDateTime timeFromS3Time(QString str)
{ {
return QDateTime::fromString(str, Qt::ISODate); return QDateTime::fromString(str, Qt::ISODate);
} }
bool parse_timestamp (const QString & raw, QString & save_here, QDateTime & parse_here) QString timeToS3Time(QDateTime time)
{ {
save_here = raw; // this all because Qt can't format timestamps right.
if (save_here.isEmpty()) int offsetRaw = time.offsetFromUtc();
{ bool negative = offsetRaw < 0;
return false; int offsetAbs = std::abs(offsetRaw);
}
parse_here = timeFromS3Time(save_here); int offsetSeconds = offsetAbs % 60;
if (!parse_here.isValid()) offsetAbs -= offsetSeconds;
{
return false; int offsetMinutes = offsetAbs % 3600;
} offsetAbs -= offsetMinutes;
return true; offsetMinutes /= 60;
int offsetHours = offsetAbs / 3600;
QString raw = time.toString("yyyy-MM-ddTHH:mm:ss");
raw += (negative ? QChar('-') : QChar('+'));
raw += QString("%1").arg(offsetHours, 2, 10, QChar('0'));
raw += ":";
raw += QString("%1").arg(offsetMinutes, 2, 10, QChar('0'));
return raw;
} }

View File

@ -2,13 +2,10 @@
#include <QString> #include <QString>
#include <QDateTime> #include <QDateTime>
/** #include "multimc_logic_export.h"
* parse the S3 timestamp in 'raw' and fill the forwarded variables.
* return true/false for success/failure
*/
bool parse_timestamp (const QString &raw, QString &save_here, QDateTime &parse_here);
/** /// take the timestamp used by S3 and turn it into QDateTime
* take the timestamp used by S3 and turn it into QDateTime MULTIMC_LOGIC_EXPORT QDateTime timeFromS3Time(QString str);
*/
QDateTime timeFromS3Time(QString str); /// take a timestamp and convert it into an S3 timestamp
MULTIMC_LOGIC_EXPORT QString timeToS3Time(QDateTime);

View File

@ -75,14 +75,12 @@ void VersionFile::applyTo(MinecraftProfile *version)
{ {
version->type = type; version->type = type;
} }
if (!m_releaseTimeString.isNull()) if (!m_releaseTime.isNull())
{ {
version->m_releaseTimeString = m_releaseTimeString;
version->m_releaseTime = m_releaseTime; version->m_releaseTime = m_releaseTime;
} }
if (!m_updateTimeString.isNull()) if (!m_updateTime.isNull())
{ {
version->m_updateTimeString = m_updateTimeString;
version->m_updateTime = m_updateTime; version->m_updateTime = m_updateTime;
} }
} }

View File

@ -151,12 +151,10 @@ public: /* data */
/// Mojang: type of the Minecraft version /// Mojang: type of the Minecraft version
QString type; QString type;
/// Mojang: the time this version was actually released by Mojang, as string and as QDateTime /// Mojang: the time this version was actually released by Mojang
QString m_releaseTimeString;
QDateTime m_releaseTime; QDateTime m_releaseTime;
/// Mojang: the time this version was last updated by Mojang, as string and as QDateTime /// Mojang: the time this version was last updated by Mojang
QString m_updateTimeString;
QDateTime m_updateTime; QDateTime m_updateTime;
/// Mojang: DEPRECATED asset group to be used with Minecraft /// Mojang: DEPRECATED asset group to be used with Minecraft

View File

@ -162,8 +162,8 @@ VersionFilePtr OneSixVersionFormat::versionFileFromJson(const QJsonDocument &doc
readString(root, "+minecraftArguments", out->addMinecraftArguments); readString(root, "+minecraftArguments", out->addMinecraftArguments);
readString(root, "type", out->type); readString(root, "type", out->type);
parse_timestamp(readStringRet(root, "releaseTime"), out->m_releaseTimeString, out->m_releaseTime); out->m_releaseTime = timeFromS3Time(readStringRet(root, "releaseTime"));
parse_timestamp(readStringRet(root, "time"), out->m_updateTimeString, out->m_updateTime); out->m_updateTime = timeFromS3Time(readStringRet(root, "time"));
readString(root, "assets", out->assets); readString(root, "assets", out->assets);
@ -280,8 +280,8 @@ static QJsonDocument versionFileToJson(VersionFilePtr patch, bool saveOrder)
writeString(root, "assets", patch->assets); writeString(root, "assets", patch->assets);
if (patch->isMinecraftVersion()) if (patch->isMinecraftVersion())
{ {
writeString(root, "releaseTime", patch->m_releaseTimeString); writeString(root, "releaseTime", timeToS3Time(patch->m_releaseTime));
writeString(root, "time", patch->m_updateTimeString); writeString(root, "time", timeToS3Time(patch->m_updateTime));
} }
writeStringList(root, "tweakers", patch->overwriteTweakers); writeStringList(root, "tweakers", patch->overwriteTweakers);
writeStringList(root, "+tweakers", patch->addTweakers); writeStringList(root, "+tweakers", patch->addTweakers);

View File

@ -31,6 +31,7 @@ add_unit_test(ModList tst_ModList.cpp)
add_unit_test(Resource tst_Resource.cpp) add_unit_test(Resource tst_Resource.cpp)
add_unit_test(GZip tst_GZip.cpp) add_unit_test(GZip tst_GZip.cpp)
add_unit_test(JavaVersion tst_JavaVersion.cpp) add_unit_test(JavaVersion tst_JavaVersion.cpp)
add_unit_test(ParseUtils tst_ParseUtils.cpp)
# Tests END # # Tests END #

45
tests/tst_ParseUtils.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <QTest>
#include "TestUtil.h"
#include "minecraft/ParseUtils.h"
class ParseUtilsTest : public QObject
{
Q_OBJECT
private
slots:
void test_Through_data()
{
QTest::addColumn<QString>("timestamp");
const char * timestamps[] =
{
"2016-02-29T13:49:54+01:00",
"2016-02-26T15:21:11+00:01",
"2016-02-24T15:52:36+01:13",
"2016-02-18T17:41:00+00:00",
"2016-02-17T15:23:19+00:00",
"2016-02-16T15:22:39+09:22",
"2016-02-10T15:06:41+00:00",
"2016-02-04T15:28:02-05:33"
};
for(int i = 0; i < (sizeof(timestamps) / sizeof(const char *)); i++)
{
QTest::newRow(timestamps[i]) << QString(timestamps[i]);
}
}
void test_Through()
{
QFETCH(QString, timestamp);
auto time_parsed = timeFromS3Time(timestamp);
auto time_serialized = timeToS3Time(time_parsed);
QCOMPARE(time_serialized, timestamp);
}
};
QTEST_GUILESS_MAIN(ParseUtilsTest)
#include "tst_ParseUtils.moc"