2016-02-26 22:22:34 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* gta5sync GRAND THEFT AUTO V SYNC
|
2016-03-27 09:52:23 +02:00
|
|
|
* Copyright (C) 2016 Syping
|
2016-02-26 22:22:34 +01:00
|
|
|
*
|
2016-03-21 07:54:14 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-02-26 22:22:34 +01:00
|
|
|
*
|
2016-03-21 07:54:14 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2016-02-26 22:22:34 +01:00
|
|
|
*
|
2016-03-21 07:54:14 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-26 22:22:34 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-03-20 21:57:18 +01:00
|
|
|
#include "SnapmaticPicture.h"
|
2016-03-21 21:33:22 +01:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QVariantMap>
|
|
|
|
#include <QJsonArray>
|
2016-03-21 03:10:28 +01:00
|
|
|
#include <QString>
|
2016-03-27 09:52:23 +02:00
|
|
|
#include <QImage>
|
2016-03-21 03:10:28 +01:00
|
|
|
#include <QFile>
|
2016-02-26 22:22:34 +01:00
|
|
|
|
2016-03-23 02:08:16 +01:00
|
|
|
SnapmaticPicture::SnapmaticPicture(QString fileName, QObject *parent) : QObject(parent), picFileName(fileName)
|
2016-02-26 22:22:34 +01:00
|
|
|
{
|
2016-03-21 21:46:36 +01:00
|
|
|
// PARSE INT INIT - DO NOT CHANGE THIS VALUES
|
|
|
|
snapmaticHeaderLength = 278;
|
2016-03-23 01:09:27 +01:00
|
|
|
snapmaticUsefulLength = 260;
|
2016-03-21 21:46:36 +01:00
|
|
|
jpegHeaderLineDifStr = 2;
|
|
|
|
jpegPreHeaderLength = 14;
|
|
|
|
jpegPicStreamLength = 524288;
|
|
|
|
jsonStreamLength = 3076;
|
|
|
|
|
|
|
|
// INIT PIC
|
2016-03-27 09:52:23 +02:00
|
|
|
cachePicture = QImage(0, 0, QImage::Format_RGB32);
|
2016-03-21 03:10:28 +01:00
|
|
|
pictureStr = "";
|
|
|
|
lastStep = "";
|
2016-03-21 22:29:57 +01:00
|
|
|
picOk = 0;
|
2016-03-21 21:46:36 +01:00
|
|
|
|
|
|
|
// INIT JSON
|
2016-03-21 22:29:57 +01:00
|
|
|
jsonOk = 0;
|
2016-03-21 03:10:28 +01:00
|
|
|
jsonStr = "";
|
2016-03-21 21:46:36 +01:00
|
|
|
jsonLocX = 0;
|
|
|
|
jsonLocY = 0;
|
|
|
|
jsonLocZ = 0;
|
|
|
|
jsonCrewID = 0;
|
|
|
|
jsonPlyrsList = QStringList();
|
2016-03-21 03:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SnapmaticPicture::readingPicture()
|
|
|
|
{
|
|
|
|
// Start opening file
|
|
|
|
// lastStep is like currentStep
|
|
|
|
|
|
|
|
QFile *picFile = new QFile(picFileName);
|
|
|
|
if (!picFile->open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
lastStep = "1;/1,OpenFile," + convertDrawStringForLog(picFileName);
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 03:10:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading Snapmatic Header
|
|
|
|
if (!picFile->isReadable())
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",1,NOHEADER";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 03:10:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray snapmaticHeaderLine = picFile->read(snapmaticHeaderLength);
|
|
|
|
pictureStr = getSnapmaticPictureString(snapmaticHeaderLine);
|
|
|
|
|
|
|
|
// Reading JPEG Header Line
|
|
|
|
if (!picFile->isReadable())
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",2,NOHEADER";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 03:10:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray jpegHeaderLine = picFile->read(jpegPreHeaderLength);
|
|
|
|
|
|
|
|
// Checking for JPEG
|
2016-03-21 21:46:36 +01:00
|
|
|
jpegHeaderLine.remove(0, jpegHeaderLineDifStr);
|
2016-03-21 03:10:28 +01:00
|
|
|
if (jpegHeaderLine.left(4) != "JPEG")
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",2,NOJPEG";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 03:10:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-21 07:54:14 +01:00
|
|
|
// Read JPEG Stream
|
|
|
|
if (!picFile->isReadable())
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",3,NOPIC";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 07:54:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-21 03:10:28 +01:00
|
|
|
QByteArray jpegRawContent = picFile->read(jpegPicStreamLength);
|
2016-03-27 09:52:23 +02:00
|
|
|
picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
|
2016-03-21 07:54:14 +01:00
|
|
|
|
|
|
|
// Read JSON Stream
|
|
|
|
if (!picFile->isReadable())
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",3,NOJSON";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 22:29:57 +01:00
|
|
|
return picOk;
|
2016-03-21 07:54:14 +01:00
|
|
|
}
|
|
|
|
else if (picFile->read(4) != "JSON")
|
|
|
|
{
|
|
|
|
lastStep = "2;/3,ReadingFile," + convertDrawStringForLog(picFileName) + ",3,CTJSON";
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 22:29:57 +01:00
|
|
|
return picOk;
|
2016-03-21 07:54:14 +01:00
|
|
|
}
|
|
|
|
QByteArray jsonRawContent = picFile->read(jsonStreamLength);
|
|
|
|
jsonStr = getSnapmaticJSONString(jsonRawContent);
|
2016-03-21 21:33:22 +01:00
|
|
|
parseJsonContent(); // JSON parsing is own function
|
2016-03-21 07:54:14 +01:00
|
|
|
|
2016-03-28 15:24:09 +02:00
|
|
|
picFile->close();
|
|
|
|
picFile->deleteLater();
|
|
|
|
delete picFile;
|
2016-03-21 22:29:57 +01:00
|
|
|
return picOk;
|
2016-03-21 03:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::getSnapmaticPictureString(QByteArray snapmaticHeader)
|
|
|
|
{
|
|
|
|
QByteArray snapmaticUsefulBytes = snapmaticHeader.left(snapmaticUsefulLength);
|
|
|
|
snapmaticUsefulBytes.replace(QByteArray::fromHex("00"),"");
|
|
|
|
snapmaticUsefulBytes.replace(QByteArray::fromHex("01"),"");
|
2016-03-21 20:44:07 +01:00
|
|
|
return QString::fromLatin1(snapmaticUsefulBytes);
|
2016-03-21 03:10:28 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 07:54:14 +01:00
|
|
|
QString SnapmaticPicture::getSnapmaticJSONString(QByteArray jsonBytes)
|
|
|
|
{
|
|
|
|
QByteArray jsonUsefulBytes = jsonBytes;
|
|
|
|
jsonUsefulBytes.replace(QByteArray::fromHex("00"),"");
|
|
|
|
jsonUsefulBytes.replace(QByteArray::fromHex("0C"),"");
|
2016-03-21 20:44:07 +01:00
|
|
|
return QString::fromLatin1(jsonUsefulBytes);
|
2016-03-21 07:54:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 03:10:28 +01:00
|
|
|
bool SnapmaticPicture::readingPictureFromFile(QString fileName)
|
|
|
|
{
|
|
|
|
if (fileName != "")
|
|
|
|
{
|
|
|
|
picFileName = fileName;
|
|
|
|
return readingPicture();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 09:52:23 +02:00
|
|
|
void SnapmaticPicture::setPicture(QImage picture)
|
2016-03-21 03:10:28 +01:00
|
|
|
{
|
2016-03-27 09:52:23 +02:00
|
|
|
cachePicture = picture;
|
2016-03-21 03:10:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::getPictureStr()
|
|
|
|
{
|
|
|
|
return pictureStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::getLastStep()
|
|
|
|
{
|
|
|
|
return lastStep;
|
|
|
|
}
|
|
|
|
|
2016-03-27 09:52:23 +02:00
|
|
|
QImage SnapmaticPicture::getPicture()
|
2016-03-21 03:10:28 +01:00
|
|
|
{
|
|
|
|
return cachePicture;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::convertDrawStringForLog(QString inputStr)
|
|
|
|
{
|
|
|
|
return inputStr.replace("&","&u;").replace(",","&c;");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::convertLogStringForDraw(QString inputStr)
|
|
|
|
{
|
|
|
|
return inputStr.replace("&c;",",").replace("&u;","&");
|
2016-03-20 21:57:18 +01:00
|
|
|
}
|
2016-03-21 21:33:22 +01:00
|
|
|
|
2016-03-21 22:29:57 +01:00
|
|
|
bool SnapmaticPicture::isPicOk()
|
|
|
|
{
|
|
|
|
return picOk;
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:33:22 +01:00
|
|
|
// JSON part
|
|
|
|
|
|
|
|
void SnapmaticPicture::parseJsonContent()
|
|
|
|
{
|
|
|
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toLatin1());
|
|
|
|
QJsonObject jsonObject = jsonDocument.object();
|
|
|
|
QVariantMap jsonMap = jsonObject.toVariantMap();
|
|
|
|
|
|
|
|
if (jsonMap.contains("loc"))
|
|
|
|
{
|
|
|
|
QJsonObject locObject = jsonObject["loc"].toObject();
|
|
|
|
QVariantMap locMap = locObject.toVariantMap();
|
|
|
|
if (locMap.contains("x")) { jsonLocX = locMap["x"].toDouble(); }
|
|
|
|
if (locMap.contains("y")) { jsonLocY = locMap["y"].toDouble(); }
|
|
|
|
if (locMap.contains("z")) { jsonLocZ = locMap["z"].toDouble(); }
|
|
|
|
}
|
2016-03-21 21:46:36 +01:00
|
|
|
if (jsonMap.contains("crewid"))
|
|
|
|
{
|
|
|
|
jsonCrewID = jsonMap["crewid"].toInt();
|
|
|
|
}
|
2016-03-21 21:33:22 +01:00
|
|
|
if (jsonMap.contains("plyrs"))
|
|
|
|
{
|
|
|
|
jsonPlyrsList = jsonMap["plyrs"].toStringList();
|
|
|
|
}
|
2016-03-21 22:29:57 +01:00
|
|
|
|
|
|
|
jsonOk = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SnapmaticPicture::isJsonOk()
|
|
|
|
{
|
|
|
|
return jsonOk;
|
2016-03-21 21:33:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SnapmaticPicture::getJsonStr()
|
|
|
|
{
|
|
|
|
return jsonStr;
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:46:36 +01:00
|
|
|
int SnapmaticPicture::getCrewNumber()
|
|
|
|
{
|
|
|
|
return jsonCrewID;
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:33:22 +01:00
|
|
|
double SnapmaticPicture::getLocationX()
|
|
|
|
{
|
|
|
|
return jsonLocX;
|
|
|
|
}
|
|
|
|
|
|
|
|
double SnapmaticPicture::getLocationY()
|
|
|
|
{
|
|
|
|
return jsonLocY;
|
|
|
|
}
|
|
|
|
|
|
|
|
double SnapmaticPicture::getLocationZ()
|
|
|
|
{
|
|
|
|
return jsonLocZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SnapmaticPicture::getPlayers()
|
|
|
|
{
|
|
|
|
return jsonPlyrsList;
|
|
|
|
}
|