better and more efficient uid generation

This commit is contained in:
Syping 2018-06-14 11:36:01 +02:00
parent c7ec038e26
commit 291236ff2c
2 changed files with 28 additions and 22 deletions

View file

@ -56,6 +56,8 @@
#include <QUrl> #include <QUrl>
#include <QDir> #include <QDir>
#include <random>
#define importTimeFormat "HHmmss" #define importTimeFormat "HHmmss"
#define findRetryLimit 500 #define findRetryLimit 500
@ -496,8 +498,7 @@ fileDialogPreOpen: //Work?
{ {
QString selectedFile = selectedFiles.at(0); QString selectedFile = selectedFiles.at(0);
QDateTime importDateTime = QDateTime::currentDateTime(); QDateTime importDateTime = QDateTime::currentDateTime();
int currentTime = importDateTime.toString(importTimeFormat).toInt(); if (!importFile(selectedFile, importDateTime, true)) goto fileDialogPreOpen; //Work?
if (!importFile(selectedFile, importDateTime, &currentTime, true)) goto fileDialogPreOpen; //Work?
} }
else if (selectedFiles.length() > 1) else if (selectedFiles.length() > 1)
{ {
@ -540,14 +541,13 @@ void ProfileInterface::importFilesProgress(QStringList selectedFiles)
// THREADING HERE PLEASE // THREADING HERE PLEASE
QDateTime importDateTime = QDateTime::currentDateTime(); QDateTime importDateTime = QDateTime::currentDateTime();
int currentTime = importDateTime.time().toString(importTimeFormat).toInt();
for (QString selectedFile : selectedFiles) for (QString selectedFile : selectedFiles)
{ {
overallId++; overallId++;
pbDialog.setValue(overallId); pbDialog.setValue(overallId);
pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId))); pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
importDateTime = QDateTime::currentDateTime(); importDateTime = QDateTime::currentDateTime();
if (!importFile(selectedFile, importDateTime, &currentTime, false)) if (!importFile(selectedFile, importDateTime, false))
{ {
failed << QFileInfo(selectedFile).fileName(); failed << QFileInfo(selectedFile).fileName();
} }
@ -565,12 +565,12 @@ void ProfileInterface::importFilesProgress(QStringList selectedFiles)
} }
} }
bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime, int *currentTime, bool notMultiple) bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime, bool notMultiple)
{ {
QString selectedFileName = QFileInfo(selectedFile).fileName(); QString selectedFileName = QFileInfo(selectedFile).fileName();
if (QFile::exists(selectedFile)) if (QFile::exists(selectedFile))
{ {
if (selectedFileName.left(4) == "PGTA" || selectedFileName.right(4) == ".g5e") if ((selectedFileName.left(4) == "PGTA" && !selectedFileName.contains('.')) || selectedFileName.right(4) == ".g5e")
{ {
SnapmaticPicture *picture = new SnapmaticPicture(selectedFile); SnapmaticPicture *picture = new SnapmaticPicture(selectedFile);
if (picture->readingPicture(true, true, true)) if (picture->readingPicture(true, true, true))
@ -602,7 +602,7 @@ bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime
return false; return false;
} }
} }
else if(isSupportedImageFile(selectedFileName)) else if (isSupportedImageFile(selectedFileName))
{ {
SnapmaticPicture *picture = new SnapmaticPicture(":/template/template.g5e"); SnapmaticPicture *picture = new SnapmaticPicture(":/template/template.g5e");
if (picture->readingPicture(true, false, true, false)) if (picture->readingPicture(true, false, true, false))
@ -673,17 +673,16 @@ bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime
return false; return false;
} }
SnapmaticProperties spJson = picture->getSnapmaticProperties(); SnapmaticProperties spJson = picture->getSnapmaticProperties();
spJson.uid = QString(QString::number(*currentTime) % spJson.uid = getRandomUid();
QString::number(importDateTime.date().dayOfYear())).toInt();
bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid)); bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden"); bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
int cEnough = 0; int cEnough = 0;
while ((fExists || fExistsHidden) && cEnough < findRetryLimit) while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
{ {
*currentTime = *currentTime - 1; spJson.uid = getRandomUid();
spJson.uid = QString(QString::number(*currentTime) %
QString::number(importDateTime.date().dayOfYear())).toInt();
fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid)); fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden"); fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
cEnough++; cEnough++;
} }
@ -728,17 +727,16 @@ bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime
if (picture->setImage(importDialog->image())) if (picture->setImage(importDialog->image()))
{ {
SnapmaticProperties spJson = picture->getSnapmaticProperties(); SnapmaticProperties spJson = picture->getSnapmaticProperties();
spJson.uid = QString(QString::number(*currentTime) % spJson.uid = getRandomUid();
QString::number(importDateTime.date().dayOfYear())).toInt();
bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid)); bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden"); bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
int cEnough = 0; int cEnough = 0;
while ((fExists || fExistsHidden) && cEnough < findRetryLimit) while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
{ {
*currentTime = *currentTime - 1; spJson.uid = getRandomUid();
spJson.uid = QString(QString::number(*currentTime) %
QString::number(importDateTime.date().dayOfYear())).toInt();
fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid)); fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden"); fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
cEnough++; cEnough++;
} }
@ -1337,8 +1335,7 @@ void ProfileInterface::on_saProfileContent_dropped(const QMimeData *mimeData)
{ {
QString selectedFile = pathList.at(0); QString selectedFile = pathList.at(0);
QDateTime importDateTime = QDateTime::currentDateTime(); QDateTime importDateTime = QDateTime::currentDateTime();
int currentTime = importDateTime.toString(importTimeFormat).toInt(); importFile(selectedFile, QDateTime::currentDateTime(), true);
importFile(selectedFile, QDateTime::currentDateTime(), &currentTime, true);
} }
else if (pathList.length() > 1) else if (pathList.length() > 1)
{ {
@ -1945,3 +1942,11 @@ preSelectionTitle:
break; break;
} }
} }
int ProfileInterface::getRandomUid()
{
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> uiddist(10000000, 2147483647);
return uiddist(rng);
}

View file

@ -113,7 +113,7 @@ private:
int contentMode; int contentMode;
bool isSupportedImageFile(QString selectedFileName); bool isSupportedImageFile(QString selectedFileName);
bool importFile(QString selectedFile, QDateTime importDateTime, int *currentTime, bool notMultiple); bool importFile(QString selectedFile, QDateTime importDateTime, bool notMultiple);
void importFilesProgress(QStringList selectedFiles); void importFilesProgress(QStringList selectedFiles);
bool importSnapmaticPicture(SnapmaticPicture *picture, bool warn = true); bool importSnapmaticPicture(SnapmaticPicture *picture, bool warn = true);
bool importSavegameData(SavegameData *savegame, QString sgdPath, bool warn = true); bool importSavegameData(SavegameData *savegame, QString sgdPath, bool warn = true);
@ -124,6 +124,7 @@ private:
void insertSnapmaticIPI(QWidget *widget); void insertSnapmaticIPI(QWidget *widget);
void insertSavegameIPI(QWidget *widget); void insertSavegameIPI(QWidget *widget);
void sortingProfileInterface(); void sortingProfileInterface();
static int getRandomUid();
signals: signals:
void profileLoaded(); void profileLoaded();