Skip to content

Commit

Permalink
feat: implement override for GLFW/OpenAL with split natives
Browse files Browse the repository at this point in the history
Fixes #513

Signed-off-by: Sefa Eyeoglu <contact@scrumplex.net>
  • Loading branch information
Scrumplex committed Jul 17, 2023
1 parent aaf1726 commit bb28672
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
47 changes: 45 additions & 2 deletions launcher/MangoHud.cpp
Expand Up @@ -16,15 +16,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <QStringList>
#include <QDebug>
#include <QDir>
#include <QString>
#include <QStringList>
#include <QSysInfo>
#include <QtGlobal>

#include "MangoHud.h"
#include "FileSystem.h"
#include "Json.h"
#include "MangoHud.h"

#ifdef __GLIBC__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#define UNDEF_GNU_SOURCE
#endif
#include <dlfcn.h>
#include <linux/limits.h>
#endif

namespace MangoHud {

Expand Down Expand Up @@ -106,4 +116,37 @@ QString getLibraryString()

return QString();
}

QString findLibrary(QString libName)
{
#ifdef __GLIBC__
const char* library = libName.toLocal8Bit().constData();

void* handle = dlopen(library, RTLD_NOW);
if (!handle) {
qCritical() << "dlopen() failed:" << dlerror();
return {};
}

char path[PATH_MAX];
if (dlinfo(handle, RTLD_DI_ORIGIN, path) == -1) {
qCritical() << "dlinfo() failed:" << dlerror();
dlclose(handle);
return {};
}

auto fullPath = FS::PathCombine(QString(path), libName);

dlclose(handle);
return fullPath;
#else
qWarning() << "MangoHud::findLibrary is not implemented on this platform";
return {};
#endif
}
} // namespace MangoHud

#ifdef UNDEF_GNU_SOURCE
#undef _GNU_SOURCE
#undef UNDEF_GNU_SOURCE
#endif
4 changes: 3 additions & 1 deletion launcher/MangoHud.h
Expand Up @@ -24,4 +24,6 @@
namespace MangoHud {

QString getLibraryString();
}

QString findLibrary(QString libName);
} // namespace MangoHud
16 changes: 16 additions & 0 deletions launcher/minecraft/MinecraftInstance.cpp
Expand Up @@ -399,6 +399,22 @@ QStringList MinecraftInstance::extraArguments()
if (version->getModLoaders().value() & ResourceAPI::Quilt && settings()->get("DisableQuiltBeacon").toBool()) {
list.append("-Dloader.disable_beacon=true");
}

{
QString openALPath;
QString glfwPath;

if (settings()->get("UseNativeOpenAL").toBool())
openALPath = MangoHud::findLibrary("libopenal.so");
if (settings()->get("UseNativeGLFW").toBool())
glfwPath = MangoHud::findLibrary("libglfw.so");

if (!openALPath.isEmpty())
list.append("-Dorg.lwjgl.openal.libname=" + openALPath);
if (!glfwPath.isEmpty())
list.append("-Dorg.lwjgl.glfw.libname=" + glfwPath);
}

return list;
}

Expand Down
10 changes: 4 additions & 6 deletions launcher/minecraft/launch/ExtractNatives.cpp
Expand Up @@ -40,7 +40,7 @@ static QString replaceSuffix (QString target, const QString &suffix, const QStri
return target + replacement;
}

static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibHack, bool nativeOpenAL, bool nativeGLFW)
static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibHack, bool nativeOpenAL)
{
QuaZip zip(source);
if(!zip.open(QuaZip::mdUnzip))
Expand All @@ -56,9 +56,6 @@ static bool unzipNatives(QString source, QString targetFolder, bool applyJnilibH
{
QString name = zip.getCurrentFileName();
auto lowercase = name.toLower();
if (nativeGLFW && name.contains("glfw")) {
continue;
}
if (nativeOpenAL && name.contains("openal")) {
continue;
}
Expand Down Expand Up @@ -91,15 +88,16 @@ void ExtractNatives::executeTask()
return;
}
auto settings = minecraftInstance->settings();

// We only need OpenAL here, as modern versions of LWJGL (3+) are handled by JVM args, while older versions (2) didn't have GLFW
bool nativeOpenAL = settings->get("UseNativeOpenAL").toBool();
bool nativeGLFW = settings->get("UseNativeGLFW").toBool();

auto outputPath = minecraftInstance->getNativePath();
auto javaVersion = minecraftInstance->getJavaVersion();
bool jniHackEnabled = javaVersion.major() >= 8;
for(const auto &source: toExtract)
{
if(!unzipNatives(source, outputPath, jniHackEnabled, nativeOpenAL, nativeGLFW))
if(!unzipNatives(source, outputPath, jniHackEnabled, nativeOpenAL))
{
const char *reason = QT_TR_NOOP("Couldn't extract native jar '%1' to destination '%2'");
emit logLine(QString(reason).arg(source, outputPath), MessageLevel::Fatal);
Expand Down

0 comments on commit bb28672

Please sign in to comment.