Embed our data files inside the binary with QRC

This commit is contained in:
Cameron Gutman 2019-03-23 10:45:44 -07:00
parent b7116657d9
commit c313f1a20b
7 changed files with 35 additions and 55 deletions

View File

@ -302,18 +302,6 @@ DEPENDPATH += $$PWD/../h264bitstream/h264bitstream
DEPENDPATH += $$PWD/../AntiHooking DEPENDPATH += $$PWD/../AntiHooking
} }
defineTest(copyToDestDir) {
files = $$1
for(FILE, files) {
!equals(PWD, $$OUT_PWD) {
QMAKE_POST_LINK += $$QMAKE_COPY $$shell_quote($$shell_path($$PWD/$$FILE)) $$shell_quote($$shell_path($$OUT_PWD)) $$escape_expand(\\n\\t)
}
}
export(QMAKE_POST_LINK)
}
unix:!macx: { unix:!macx: {
isEmpty(PREFIX) { isEmpty(PREFIX) {
PREFIX = /usr/local PREFIX = /usr/local
@ -339,12 +327,6 @@ unix:!macx: {
appdata.files = SDL_GameControllerDB/gamecontrollerdb.txt ModeSeven.ttf appdata.files = SDL_GameControllerDB/gamecontrollerdb.txt ModeSeven.ttf
appdata.path = "$$PREFIX/$$DATADIR/Moonlight Game Streaming Project/Moonlight/" appdata.path = "$$PREFIX/$$DATADIR/Moonlight Game Streaming Project/Moonlight/"
CONFIG(debug, debug|release) {
# Copy the required data files into the application directory only for debug builds.
# Release builds will use the INSTALLS variable to place these in the correct folders.
copyToDestDir(SDL_GameControllerDB/gamecontrollerdb.txt ModeSeven.ttf)
}
INSTALLS += target appdata desktop icons appstream INSTALLS += target appdata desktop icons appstream
} }
win32 { win32 {
@ -353,9 +335,6 @@ win32 {
QMAKE_TARGET_DESCRIPTION = Moonlight Game Streaming Client QMAKE_TARGET_DESCRIPTION = Moonlight Game Streaming Client
QMAKE_TARGET_PRODUCT = Moonlight QMAKE_TARGET_PRODUCT = Moonlight
# Copy the required data files into the application directory
copyToDestDir(SDL_GameControllerDB/gamecontrollerdb.txt ModeSeven.ttf)
CONFIG -= embed_manifest_exe CONFIG -= embed_manifest_exe
QMAKE_LFLAGS += /MANIFEST:embed /MANIFESTINPUT:$${PWD}/Moonlight.exe.manifest QMAKE_LFLAGS += /MANIFEST:embed /MANIFESTINPUT:$${PWD}/Moonlight.exe.manifest
} }
@ -366,7 +345,7 @@ macx {
QMAKE_INFO_PLIST = $$OUT_PWD/Info.plist QMAKE_INFO_PLIST = $$OUT_PWD/Info.plist
APP_BUNDLE_RESOURCES.files = moonlight.icns SDL_GameControllerDB/gamecontrollerdb.txt ModeSeven.ttf APP_BUNDLE_RESOURCES.files = moonlight.icns
APP_BUNDLE_RESOURCES.path = Contents/Resources APP_BUNDLE_RESOURCES.path = Contents/Resources
APP_BUNDLE_FRAMEWORKS.files = $$files(../libs/mac/Frameworks/*.framework, true) APP_BUNDLE_FRAMEWORKS.files = $$files(../libs/mac/Frameworks/*.framework, true)

View File

@ -21,6 +21,13 @@ QString Path::getBoxArtCacheDir()
return s_BoxArtCacheDir; return s_BoxArtCacheDir;
} }
QByteArray Path::readDataFile(QString fileName)
{
QFile dataFile(getDataFilePath(fileName));
dataFile.open(QIODevice::ReadOnly);
return dataFile.readAll();
}
QString Path::getDataFilePath(QString fileName) QString Path::getDataFilePath(QString fileName)
{ {
QString candidatePath; QString candidatePath;
@ -28,33 +35,28 @@ QString Path::getDataFilePath(QString fileName)
// Check the current directory first // Check the current directory first
candidatePath = QDir(QDir::currentPath()).absoluteFilePath(fileName); candidatePath = QDir(QDir::currentPath()).absoluteFilePath(fileName);
if (QFile::exists(candidatePath)) { if (QFile::exists(candidatePath)) {
qInfo() << "Found" << fileName << "at" << candidatePath;
return candidatePath; return candidatePath;
} }
// Now check the data directories (for Linux, in particular) // Now check the data directories (for Linux, in particular)
candidatePath = QStandardPaths::locate(QStandardPaths::DataLocation, fileName); candidatePath = QStandardPaths::locate(QStandardPaths::DataLocation, fileName);
if (!candidatePath.isEmpty() && QFile::exists(candidatePath)) { if (!candidatePath.isEmpty() && QFile::exists(candidatePath)) {
qInfo() << "Found" << fileName << "at" << candidatePath;
return candidatePath; return candidatePath;
} }
// Now try the directory of our app installation (for Windows, if current dir doesn't find it) // Now try the directory of our app installation (for Windows, if current dir doesn't find it)
candidatePath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(fileName); candidatePath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath(fileName);
if (QFile::exists(candidatePath)) { if (QFile::exists(candidatePath)) {
qInfo() << "Found" << fileName << "at" << candidatePath;
return candidatePath; return candidatePath;
} }
// Now try the Resources folder in our app bundle for macOS // Return the QRC embedded copy
QDir dir = QDir(QCoreApplication::applicationDirPath()); candidatePath = ":/data/" + fileName;
dir.cdUp(); qInfo() << "Found" << fileName << "at" << candidatePath;
dir.cd("Resources"); return QString(candidatePath);
dir.filePath(fileName);
candidatePath = dir.absoluteFilePath(fileName);
if (QFile::exists(candidatePath)) {
return candidatePath;
}
// Couldn't find it
return QString();
} }
void Path::initialize(bool portable) void Path::initialize(bool portable)

View File

@ -9,6 +9,9 @@ public:
static QString getBoxArtCacheDir(); static QString getBoxArtCacheDir();
static QByteArray readDataFile(QString fileName);
// Only safe to use directly for Qt classes
static QString getDataFilePath(QString fileName); static QString getDataFilePath(QString fileName);
static void initialize(bool portable); static void initialize(bool portable);

View File

@ -16,4 +16,8 @@
<file>res/moonlight.svg</file> <file>res/moonlight.svg</file>
<file>res/update.svg</file> <file>res/update.svg</file>
</qresource> </qresource>
<qresource prefix="/data">
<file alias="gamecontrollerdb.txt">SDL_GameControllerDB/gamecontrollerdb.txt</file>
<file alias="ModeSeven.ttf">ModeSeven.ttf</file>
</qresource>
</RCC> </RCC>

View File

@ -54,15 +54,10 @@ void MappingManager::save()
void MappingManager::applyMappings() void MappingManager::applyMappings()
{ {
QString mappingFile = Path::getDataFilePath("gamecontrollerdb.txt"); QByteArray mappingData = Path::readDataFile("gamecontrollerdb.txt");
if (!mappingFile.isEmpty()) { if (!mappingData.isEmpty()) {
std::string mappingFileNative = QDir::toNativeSeparators(mappingFile).toStdString(); int newMappings = SDL_GameControllerAddMappingsFromRW(
SDL_RWFromConstMem(mappingData.constData(), mappingData.size()), 1);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Loading gamepad mappings from: %s",
mappingFileNative.c_str());
int newMappings = SDL_GameControllerAddMappingsFromFile(mappingFileNative.c_str());
if (newMappings < 0) { if (newMappings < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Error loading gamepad mappings: %s", "Error loading gamepad mappings: %s",
@ -75,8 +70,8 @@ void MappingManager::applyMappings()
} }
} }
else { else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"No gamepad mapping file found"); "Unable to load gamepad mapping file");
} }
QList<SdlGamepadMapping> mappings = m_Mappings.values(); QList<SdlGamepadMapping> mappings = m_Mappings.values();

View File

@ -88,15 +88,16 @@ void SdlRenderer::notifyOverlayUpdated(Overlay::OverlayType type)
{ {
// Construct the required font to render the overlay // Construct the required font to render the overlay
if (m_OverlayFonts[type] == nullptr) { if (m_OverlayFonts[type] == nullptr) {
QByteArray fontPath = QDir::toNativeSeparators(Path::getDataFilePath("ModeSeven.ttf")).toUtf8(); QByteArray fontData = Path::readDataFile("ModeSeven.ttf");
if (fontPath.isEmpty()) { if (fontData.isEmpty()) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Unable to locate SDL overlay font"); "Unable to load SDL overlay font");
return; return;
} }
m_OverlayFonts[type] = TTF_OpenFont(fontPath.data(), m_OverlayFonts[type] = TTF_OpenFontRW(SDL_RWFromConstMem(fontData.constData(), fontData.size()),
Session::get()->getOverlayManager().getOverlayFontSize(type)); 1,
Session::get()->getOverlayManager().getOverlayFontSize(type));
if (m_OverlayFonts[type] == nullptr) { if (m_OverlayFonts[type] == nullptr) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"TTF_OpenFont() failed: %s", "TTF_OpenFont() failed: %s",

View File

@ -123,10 +123,6 @@ echo Copying GC mapping list
copy %SOURCE_ROOT%\app\SDL_GameControllerDB\gamecontrollerdb.txt %DEPLOY_FOLDER% copy %SOURCE_ROOT%\app\SDL_GameControllerDB\gamecontrollerdb.txt %DEPLOY_FOLDER%
if !ERRORLEVEL! NEQ 0 goto Error if !ERRORLEVEL! NEQ 0 goto Error
echo Copying SDL overlay font
copy %SOURCE_ROOT%\app\ModeSeven.ttf %DEPLOY_FOLDER%
if !ERRORLEVEL! NEQ 0 goto Error
echo Deploying Qt dependencies echo Deploying Qt dependencies
windeployqt.exe --dir %DEPLOY_FOLDER% --%BUILD_CONFIG% --qmldir %SOURCE_ROOT%\app\gui --no-opengl-sw --no-compiler-runtime %BUILD_FOLDER%\app\%BUILD_CONFIG%\Moonlight.exe windeployqt.exe --dir %DEPLOY_FOLDER% --%BUILD_CONFIG% --qmldir %SOURCE_ROOT%\app\gui --no-opengl-sw --no-compiler-runtime %BUILD_FOLDER%\app\%BUILD_CONFIG%\Moonlight.exe
if !ERRORLEVEL! NEQ 0 goto Error if !ERRORLEVEL! NEQ 0 goto Error