1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-07 03:22:33 +01:00

Add working lazy loading steam api

This commit is contained in:
kelteseth 2018-04-01 00:04:09 +02:00
parent 9c24a947ef
commit b92c76f081
7 changed files with 140 additions and 74 deletions

View File

@ -1,5 +1,4 @@

#include <QDebug>
#include <QDebug>
#include <QDir>
#include <QFontDatabase>
#include <QGuiApplication>
@ -53,8 +52,6 @@ int main(int argc, char* argv[])
trsl.load(":/translations/ScreenPlay_de.qm");
app.installTranslator(&trsl);
QObject::connect(&app, &QGuiApplication::screenRemoved, [&]() { qDebug() << "removed"; });
QFontDatabase::addApplicationFont(":/assets/fonts/LibreBaskerville-Italic.ttf");
QFontDatabase::addApplicationFont(":/assets/fonts/Roboto-Light.ttf");
QFontDatabase::addApplicationFont(":/assets/fonts/Roboto-Regular.ttf");
@ -70,19 +67,6 @@ int main(int argc, char* argv[])
app.setWindowIcon(QIcon(":/assets/icons/favicon.ico"));
bool steamErrorRestart = false;
bool steamErrorAPIInit = false;
if (SteamAPI_RestartAppIfNecessary(steamID)) {
qWarning() << "SteamAPI_RestartAppIfNecessary";
steamErrorRestart = true;
}
if (!SteamAPI_Init()) {
qWarning() << "Could not init steam sdk!";
steamErrorAPIInit = true;
}
QMLUtilities qmlUtil;
InstalledListModel installedListModel;
MonitorListModel monitorListModel(&app);
@ -115,7 +99,7 @@ int main(int argc, char* argv[])
SPBaseDir.cd("ScreenPlay");
settings.setScreenPlayBasePath(QUrl(SPBaseDir.path()));
#elif QT_NO_DEBUG
#elif
settings.setScreenPlayBasePath(QUrl(SPWorkingDir.path()));
// If we build in the release version we must be cautious!
@ -149,8 +133,7 @@ int main(int argc, char* argv[])
profileListModel.loadProfiles();
settings.loadActiveProfiles();
QQmlApplicationEngine errorWindowEngine,mainWindowEngine;
StartupError suError(&mainWindowEngine, &errorWindowEngine);
QQmlApplicationEngine mainWindowEngine;
mainWindowEngine.rootContext()->setContextProperty("screenPlay", &screenPlay);
mainWindowEngine.rootContext()->setContextProperty("screenPlayCreate", &create);
mainWindowEngine.rootContext()->setContextProperty("utility", &qmlUtil);
@ -161,15 +144,7 @@ int main(int argc, char* argv[])
mainWindowEngine.rootContext()->setContextProperty("profileListModel", &profileListModel);
mainWindowEngine.rootContext()->setContextProperty("screenPlaySettings", &settings);
mainWindowEngine.rootContext()->setContextProperty("steamWorkshop", &steamWorkshop);
if (steamErrorRestart || steamErrorAPIInit) {
errorWindowEngine.load(QUrl(QStringLiteral("qrc:/qml/StartupErrorWindow.qml")));
errorWindowEngine.rootContext()->setContextProperty("suError", &suError);
settings.setOfflineMode(true);
} else {
mainWindowEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
}
mainWindowEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// Set visible if the -silent parameter was not set
QStringList argumentList = app.arguments();
@ -177,12 +152,6 @@ int main(int argc, char* argv[])
settings.setMainWindowVisible(true);
}
// Timer for steam polls. WTF?
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&]() { SteamAPI_RunCallbacks(); });
timer.setInterval(100);
timer.start();
QObject::connect(&steamWorkshop, &SteamWorkshop::workshopSearchResult,
&steamWorkshopListModel, &SteamWorkshopListModel::append, Qt::QueuedConnection);

View File

@ -22,6 +22,7 @@ ApplicationWindow {
if (!screenPlaySettings.autostart) {
show()
}
steamWorkshop.initSteam()
}
function switchPage(name) {

View File

@ -36,6 +36,7 @@ Rectangle {
//Avoid crashing screenplay when steam is not available
if (name === "Workshop" && screenPlaySettings.offlineMode) {
steamWorkshop.initSteam()
return
}

View File

@ -220,6 +220,11 @@ void Settings::createDefaultConfig()
defaultSettings.close();
}
bool Settings::getOfflineMode() const
{
return m_offlineMode;
}
QUrl Settings::getScreenPlayWidgetPath() const
{
return m_screenPlayWidgetPath;

View File

@ -30,7 +30,7 @@
#include "sdkconnector.h"
#include "steam/steam_api.h"
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <qt_windows.h>
#endif
/*!
\class Settings
@ -98,7 +98,6 @@ public:
return m_hasWorkshopBannerSeen;
}
int activeWallpaperCounter() const
{
return m_activeWallpaperCounter;
@ -121,7 +120,9 @@ public:
void setScreenPlayBasePath(QUrl screenPlayBasePath);
QUrl getScreenPlayWidgetPath() const;
void setScreenPlayWidgetPath(const QUrl &screenPlayWidgetPath);
void setScreenPlayWidgetPath(const QUrl& screenPlayWidgetPath);
bool getOfflineMode() const;
signals:
void autostartChanged(bool autostart);
@ -171,9 +172,15 @@ public slots:
if (autostart) {
#ifdef Q_OS_WIN
#ifdef QT_DEBUG
QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
settings.setValue("ScreenPlay", QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + " -silent");
settings.sync();
#else
QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
settings.setValue("ScreenPlay", QDir::toNativeSeparators("C:/Program Files (x86)/Steam/steamapps/common/ScreenPlay/ScreenPlay/ScreenPlay.exe") + " -silent");
settings.sync();
#endif
#endif
} else {
#ifdef Q_OS_WIN
@ -315,6 +322,5 @@ private:
int m_activeWallpaperCounter = 0;
QGuiApplication* m_qGuiApplication;
bool m_offlineMode = false;
bool m_offlineMode = true;
};

View File

@ -3,11 +3,38 @@
SteamWorkshop::SteamWorkshop(QObject* parent)
: QObject(parent)
{
initSteam();
}
void SteamWorkshop::initSteam()
{
if(m_settings->offlineMode()){
if (SteamAPI_RestartAppIfNecessary(m_appID)) {
qWarning() << "SteamAPI_RestartAppIfNecessary";
m_steamErrorRestart = true;
}
if (!SteamAPI_Init()) {
qWarning() << "Could not init steam sdk!";
m_steamErrorAPIInit = true;
}
if(!(m_steamErrorAPIInit || m_steamErrorRestart)){
m_settings->setOfflineMode(false);
m_pollTimer = new QTimer(this);
QObject::connect(m_pollTimer, &QTimer::timeout, [&]() { SteamAPI_RunCallbacks(); });
m_pollTimer->setInterval(250);
m_pollTimer->start();
}
}
}
SteamWorkshop::SteamWorkshop(AppId_t nConsumerAppId, SteamWorkshopListModel* wlm, Settings* s)
{
m_AppId = nConsumerAppId;
m_appID = nConsumerAppId;
m_workshopListModel = wlm;
m_settings = s;
@ -20,7 +47,7 @@ SteamWorkshop::SteamWorkshop(AppId_t nConsumerAppId, SteamWorkshopListModel* wlm
void SteamWorkshop::createWorkshopItem()
{
SteamAPICall_t hSteamAPICall = SteamUGC()->CreateItem(m_AppId, EWorkshopFileType::k_EWorkshopFileTypeCommunity);
SteamAPICall_t hSteamAPICall = SteamUGC()->CreateItem(m_appID, EWorkshopFileType::k_EWorkshopFileTypeCommunity);
m_createWorkshopItemCallResult.Set(hSteamAPICall, this, &SteamWorkshop::workshopItemCreated);
}
@ -29,7 +56,7 @@ void SteamWorkshop::workshopItemCreated(CreateItemResult_t* pCallback, bool bIOF
if (bIOFailure)
return;
m_UGCUpdateHandle = SteamUGC()->StartItemUpdate(m_AppId, pCallback->m_nPublishedFileId);
m_UGCUpdateHandle = SteamUGC()->StartItemUpdate(m_appID, pCallback->m_nPublishedFileId);
emit workshopItemCreated(pCallback->m_bUserNeedsToAcceptWorkshopLegalAgreement, pCallback->m_eResult, pCallback->m_nPublishedFileId);
}
@ -83,6 +110,16 @@ void SteamWorkshop::submitWorkshopItem(QString title, QString description, QStri
SteamUGC()->SetItemUpdateLanguage(m_UGCUpdateHandle, QByteArray(language.toLatin1()).data());
SteamUGC()->SetItemContent(m_UGCUpdateHandle, QByteArray(absoluteContentPath.toLatin1()).data());
SteamUGC()->SetItemPreview(m_UGCUpdateHandle, QByteArray(preview.toLatin1()).data());
auto tagList = new QScopedPointer<SteamParamStringArray_t>(new SteamParamStringArray_t);
//const char** cchKey[5][2000];
//tagList->data()->m_ppStrings = cchKey;
//tagList->data()->m_nNumStrings = 1;
//tagList->data()->m_ppStrings[1][0] = QByteArray("asas").data();
SteamUGC()->SetItemTags(m_UGCUpdateHandle,tagList->data());
auto visibility = static_cast<ERemoteStoragePublishedFileVisibility>(remoteStoragePublishedFileVisibility);
SteamUGC()->SetItemVisibility(m_UGCUpdateHandle, visibility);
@ -194,8 +231,8 @@ void SteamWorkshop::searchWorkshop()
{
m_UGCSearchHandle = SteamUGC()->CreateQueryAllUGCRequest(EUGCQuery::k_EUGCQuery_RankedByVote,
EUGCMatchingUGCType::k_EUGCMatchingUGCType_Items,
m_AppId,
m_AppId,
m_appID,
m_appID,
1);
SteamUGC()->SetAllowCachedResponse(m_UGCSearchHandle, 3000);
//Important: First send the request to get the Steam API Call
@ -210,38 +247,56 @@ void SteamWorkshop::onWorkshopSearched(SteamUGCQueryCompleted_t* pCallback, bool
if (bIOFailure)
return;
QtConcurrent::run([this, pCallback]() {});
//QtConcurrent::run([this, pCallback]() { });
SteamUGCDetails_t details;
const int urlLength = 200;
char url[urlLength];
uint32 previews = 0;
uint32 subscriber = 0;
uint32 results = pCallback->m_unTotalMatchingResults;
qDebug() << results;
SteamUGCDetails_t details;
const int urlLength = 200;
char url[urlLength];
uint32 previews = 0;
uint32 subscriber = 0;
for (uint32 i = 0; i < results; i++) {
if (SteamUGC()->GetQueryUGCResult(pCallback->m_handle, i, &details)) {
//qDebug() << "ok " << pCallback;
if (SteamUGC()->GetQueryUGCPreviewURL(pCallback->m_handle, i, url, static_cast<uint32>(urlLength))) {
QByteArray urlData(url);
// Tags
uint32 keyValueTags = 0;
const int cchKeySize = 2000;
char* cchKey[cchKeySize];
const int cchValueSize = 2000;
char* pchValue[cchValueSize];
//Todo use multiple preview for gif hover effect
previews = SteamUGC()->GetQueryUGCNumAdditionalPreviews(pCallback->m_handle, i);
if (i == 0) {
m_workshopListModel->setBannerWorkshopItem(details.m_nPublishedFileId, QString(details.m_rgchTitle), QUrl(urlData), 0);
emit workshopSearched();
} else {
emit workshopSearchResult(details.m_nPublishedFileId, QString(details.m_rgchTitle), QUrl(urlData), 0);
uint32 results = pCallback->m_unTotalMatchingResults;
for (uint32 i = 0; i < results; i++) {
if (SteamUGC()->GetQueryUGCResult(pCallback->m_handle, i, &details)) {
//qDebug() << "ok " << pCallback;
if (SteamUGC()->GetQueryUGCPreviewURL(pCallback->m_handle, i, url, static_cast<uint32>(urlLength))) {
QByteArray urlData(url);
//Todo use multiple preview for gif hover effect
previews = SteamUGC()->GetQueryUGCNumAdditionalPreviews(pCallback->m_handle, i);
if (i == 0) {
m_workshopListModel->setBannerWorkshopItem(details.m_nPublishedFileId, QString(details.m_rgchTitle), QUrl(urlData), 0);
emit workshopSearched();
} else {
emit workshopSearchResult(details.m_nPublishedFileId, QString(details.m_rgchTitle), QUrl(urlData), 0);
}
// keyValueTags = SteamUGC()->GetQueryUGCNumKeyValueTags(pCallback->m_handle, i);
// if (keyValueTags >= 0) {
// if (SteamUGC()->GetQueryUGCKeyValueTag(pCallback->m_handle, i, 0, cchKey, cchKeySize, cchValueSize, pchValue)) {
// qDebug() << QByteArray(pchKey) << QByteArray(pchValue);
// }
// }
// if(SteamUGC()->GetQueryUGCStatistic(pCallback->m_handle,i,EItemStatistic::k_EItemStatistic_NumSubscriptions,&subscriber)){
// }
}
// if(SteamUGC()->GetQueryUGCStatistic(pCallback->m_handle,i,EItemStatistic::k_EItemStatistic_NumSubscriptions,&subscriber)){
// }
} else {
qDebug() << "Loading error!";
}
} else {
qDebug() << "Loading error!";
}
}
SteamUGC()->ReleaseQueryUGCRequest(pCallback->m_handle);
SteamUGC()->ReleaseQueryUGCRequest(pCallback->m_handle);
}
void SteamWorkshop::onWorkshopItemInstalled(ItemInstalled_t* itemInstalled)
{
emit workshopItemInstalled(itemInstalled->m_unAppID, itemInstalled->m_nPublishedFileId);
}

View File

@ -3,6 +3,7 @@
#include "settings.h"
#include "steam/steam_api.h"
#include "steamworkshoplistmodel.h"
#include <QScopedPointer>
#include <QByteArray>
#include <QQmlEngine>
#include <QDateTime>
@ -14,6 +15,7 @@
#include <QFutureWatcher>
#include <QObject>
#include <QUrl>
#include <QTimer>
/*!
\class Steam Workshop
@ -64,15 +66,22 @@ public:
Q_PROPERTY(unsigned int itemProcessed READ itemProcessed WRITE setItemProcessed NOTIFY itemProcessedChanged)
Q_PROPERTY(unsigned int bytesTotal READ bytesTotal WRITE setBytesTotal NOTIFY bytesTotalChanged)
Q_PROPERTY(unsigned int appID READ appID WRITE setAppID NOTIFY appIDChanged)
// Properties
unsigned int itemProcessed() const { return m_itemProcessed; }
unsigned int bytesTotal() const { return m_bytesTotal; }
unsigned int appID() const
{
return m_appID;
}
public slots:
int getItemUpdateProcess();
bool contentFolderExist(QString folder);
void initSteam();
void searchWorkshop();
void createWorkshopItem();
void submitWorkshopItem(QString title, QString description, QString language, int remoteStoragePublishedFileVisibility, const QUrl projectFile, const QUrl videoFile, int publishedFileId);
@ -99,30 +108,50 @@ public slots:
emit bytesTotalChanged(m_bytesTotal);
}
void setAppID(unsigned int appID)
{
if (m_appID == appID)
return;
m_appID = appID;
emit appIDChanged(m_appID);
}
signals:
void workshopItemCreated(bool userNeedsToAcceptWorkshopLegalAgreement, int eResult, int publishedFileId);
void workshopSearched();
void localWorkshopCreationStatusChanged(LocalWorkshopCreationStatus::Value status);
void remoteWorkshopCreationStatusChanged(RemoteWorkshopCreationStatus::Value status);
void workshopSearchResult(unsigned int id, QString title, QUrl imgUrl,int subscriber);
void workshopItemInstalled(int appID,int publishedFile);
// Properties
void itemProcessedChanged(unsigned int itemProcessed);
void bytesTotalChanged(unsigned int bytesTotal);
void appIDChanged(unsigned int appID);
private:
void workshopItemCreated(CreateItemResult_t* pCallback, bool bIOFailure);
CCallResult<SteamWorkshop, CreateItemResult_t> m_createWorkshopItemCallResult;
void onWorkshopSearched(SteamUGCQueryCompleted_t* pCallback, bool bIOFailure);
CCallResult<SteamWorkshop, SteamUGCQueryCompleted_t> m_steamUGCQueryResult;
AppId_t m_AppId;
STEAM_CALLBACK( SteamWorkshop, onWorkshopItemInstalled, ItemInstalled_t );
UGCUpdateHandle_t m_UGCUpdateHandle = 0;
UGCQueryHandle_t m_UGCSearchHandle = 0;
SteamAPICall_t m_searchCall;
SteamWorkshopListModel* m_workshopListModel;
Settings* m_settings;
QTimer* m_pollTimer;
bool m_steamErrorRestart = false;
bool m_steamErrorAPIInit = false;
unsigned int m_itemProcessed = 0;
unsigned int m_bytesTotal = 0;
unsigned int m_appID;
};