Skip to content

Commit ff67fd1

Browse files
committed
feat: implement override for GLFW/OpenAL with split natives
Fixes PrismLauncher#513 Signed-off-by: Sefa Eyeoglu <[email protected]>
1 parent 7ba1e7d commit ff67fd1

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

launcher/MangoHud.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <QDebug>
1920
#include <QDir>
2021
#include <QString>
2122
#include <QStringList>
@@ -26,6 +27,15 @@
2627
#include "Json.h"
2728
#include "MangoHud.h"
2829

30+
#ifdef __GLIBC__
31+
#ifndef _GNU_SOURCE
32+
#define _GNU_SOURCE
33+
#define UNDEF_GNU_SOURCE
34+
#endif
35+
#include <dlfcn.h>
36+
#include <linux/limits.h>
37+
#endif
38+
2939
namespace MangoHud {
3040

3141
QString getLibraryString()
@@ -106,4 +116,37 @@ QString getLibraryString()
106116

107117
return QString();
108118
}
119+
120+
QString findLibrary(QString libName)
121+
{
122+
#ifdef __GLIBC__
123+
const char* library = libName.toLocal8Bit().constData();
124+
125+
void* handle = dlopen(library, RTLD_NOW);
126+
if (!handle) {
127+
qCritical() << "dlopen() failed:" << dlerror();
128+
return {};
129+
}
130+
131+
char path[PATH_MAX];
132+
if (dlinfo(handle, RTLD_DI_ORIGIN, path) == -1) {
133+
qCritical() << "dlinfo() failed:" << dlerror();
134+
dlclose(handle);
135+
return {};
136+
}
137+
138+
auto fullPath = FS::PathCombine(QString(path), libName);
139+
140+
dlclose(handle);
141+
return fullPath;
142+
#else
143+
qWarning() << "MangoHud::findLibrary is not implemented on this platform";
144+
return {};
145+
#endif
146+
}
109147
} // namespace MangoHud
148+
149+
#ifdef UNDEF_GNU_SOURCE
150+
#undef _GNU_SOURCE
151+
#undef UNDEF_GNU_SOURCE
152+
#endif

launcher/MangoHud.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
namespace MangoHud {
2525

2626
QString getLibraryString();
27-
}
27+
28+
QString findLibrary(QString libName);
29+
} // namespace MangoHud

launcher/minecraft/MinecraftInstance.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,24 @@ QStringList MinecraftInstance::extraArguments()
389389
if (loaders.has_value() && loaders.value() & ResourceAPI::Quilt && settings()->get("DisableQuiltBeacon").toBool())
390390
list.append("-Dloader.disable_beacon=true");
391391
}
392+
393+
#ifdef Q_OS_LINUX
394+
{
395+
QString openALPath;
396+
QString glfwPath;
397+
398+
if (settings()->get("UseNativeOpenAL").toBool())
399+
openALPath = MangoHud::findLibrary("libopenal.so");
400+
if (settings()->get("UseNativeGLFW").toBool())
401+
glfwPath = MangoHud::findLibrary("libglfw.so");
402+
403+
if (!openALPath.isEmpty())
404+
list.append("-Dorg.lwjgl.openal.libname=" + openALPath);
405+
if (!glfwPath.isEmpty())
406+
list.append("-Dorg.lwjgl.glfw.libname=" + glfwPath);
407+
}
408+
#endif
409+
392410
return list;
393411
}
394412

launcher/minecraft/launch/ExtractNatives.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static QString replaceSuffix(QString target, const QString& suffix, const QStrin
3939
return target + replacement;
4040
}
4141

42-
static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibHack, bool nativeOpenAL, bool nativeGLFW)
42+
static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibHack, bool nativeOpenAL)
4343
{
4444
QuaZip zip(source);
4545
if (!zip.open(QuaZip::mdUnzip)) {
@@ -52,9 +52,6 @@ static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibH
5252
do {
5353
QString name = zip.getCurrentFileName();
5454
auto lowercase = name.toLower();
55-
if (nativeGLFW && name.contains("glfw")) {
56-
continue;
57-
}
5855
if (nativeOpenAL && name.contains("openal")) {
5956
continue;
6057
}
@@ -83,14 +80,15 @@ void ExtractNatives::executeTask()
8380
return;
8481
}
8582
auto settings = minecraftInstance->settings();
83+
84+
// We only need OpenAL here, as modern versions of LWJGL (3+) are handled by JVM args, while older versions (2) didn't have GLFW
8685
bool nativeOpenAL = settings->get("UseNativeOpenAL").toBool();
87-
bool nativeGLFW = settings->get("UseNativeGLFW").toBool();
8886

8987
auto outputPath = minecraftInstance->getNativePath();
9088
auto javaVersion = minecraftInstance->getJavaVersion();
9189
bool jniHackEnabled = javaVersion.major() >= 8;
9290
for (const auto& source : toExtract) {
93-
if (!unzipNatives(source, outputPath, jniHackEnabled, nativeOpenAL, nativeGLFW)) {
91+
if (!unzipNatives(source, outputPath, jniHackEnabled, nativeOpenAL)) {
9492
const char* reason = QT_TR_NOOP("Couldn't extract native jar '%1' to destination '%2'");
9593
emit logLine(QString(reason).arg(source, outputPath), MessageLevel::Fatal);
9694
emitFailed(tr(reason).arg(source, outputPath));

0 commit comments

Comments
 (0)