$OpenBSD: patch-api_logic_launch_steps_PatchLWJGL_cpp,v 1.1 2021/02/28 09:54:39 phessler Exp $

Index: api/logic/launch/steps/PatchLWJGL.cpp
--- api/logic/launch/steps/PatchLWJGL.cpp.orig
+++ api/logic/launch/steps/PatchLWJGL.cpp
@@ -0,0 +1,146 @@
+/* Copyright 2013-2019 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "PatchLWJGL.h"
+#include <launch/LaunchTask.h>
+#include <minecraft/OneSixVersionFormat.h>
+#include <QSaveFile>
+
+void PatchLWJGL::executeTask()
+{
+    qDebug() << "Patching instance's LWJGL to use system libraries.";
+
+    auto component = m_components->getComponent("org.lwjgl3");
+    bool isLwjgl3 = true;
+
+    if (component == nullptr) {
+      component = m_components->getComponent("org.lwjgl");
+
+      if (component == nullptr) {
+        emitFailed(tr("Failed to find LWJGL component for this Minecraft instance."));
+        return;
+      }
+
+      isLwjgl3 = false;
+    }
+
+    qDebug() << "Checking if instance's LWJGL is customized";
+
+    if (component->customize())
+    {
+        qDebug() << "Instance's LWJGL is not customized, will patch.";
+
+        auto vfile = component->getVersionFile();
+        vfile->version = "System";
+        vfile->libraries = QList<LibraryPtr>();
+
+        if (isLwjgl3)
+        {
+            qDebug() << "Instance uses LWJGL 3, applying appropiate patches.";
+
+            QList<QString> libs = {
+              "lwjgl-glfw", "lwjgl-openal", "lwjgl-opengl",
+              "lwjgl-stb", "lwjgl-tinyfd", "lwjgl"
+            };
+
+            QList<QString> nativeLibs = {
+              "lwjgl-opengl", "lwjgl-stb", "lwjgl-tinyfd", "lwjgl"
+            };
+
+            for (auto &libName : libs)
+            {
+                LibraryPtr lib(new Library());
+                lib->setHint("local");
+                lib->setPath(LWJGL3_DIR + libName + ".jar");
+                lib->setRawName(GradleSpecifier("org.lwjgl:" + libName + ":system"));
+                vfile->libraries.append(lib);
+            }
+
+
+            for (auto &libName : nativeLibs)
+            {
+                LibraryPtr lib(new Library());
+                lib->setHint("local");
+                lib->setPath(LWJGL3_DIR + libName + "-natives-openbsd.jar");
+                lib->setRawName(GradleSpecifier("org.lwjgl:" + libName + ":system"));
+                lib->setNative(true);
+                vfile->libraries.append(lib);
+            }
+        } else {
+            qDebug() << "Instance uses LWJGL 2, applying appropiate patches.";
+
+            QString lwjglDir(LWJGL_DIR);
+
+            LibraryPtr jinput(new Library());
+            jinput->setHint("local");
+            jinput->setPath(lwjglDir + "jinput.jar");
+            jinput->setRawName(GradleSpecifier("net.java.jinput:jinput:system"));
+            vfile->libraries.append(jinput);
+
+            LibraryPtr lwjgl(new Library());
+            lwjgl->setHint("local");
+            lwjgl->setPath(lwjglDir + "lwjgl.jar");
+            lwjgl->setRawName(GradleSpecifier("org.lwjgl.lwjgl:lwjgl:system"));
+            vfile->libraries.append(lwjgl);
+
+            LibraryPtr lwjglUtil(new Library());
+            lwjglUtil->setHint("local");
+            lwjglUtil->setPath(lwjglDir + "lwjgl_util.jar");
+            lwjglUtil->setRawName(GradleSpecifier("org.lwjgl.lwjgl:lwjgl_util:system"));
+            vfile->libraries.append(lwjglUtil);
+        }
+
+        qDebug() << "Writing patch file.";
+
+        auto filename = component->getFilename();
+        auto document = OneSixVersionFormat::versionFileToJson(vfile);
+
+        QSaveFile jsonFile(filename);
+        if(!jsonFile.open(QIODevice::WriteOnly))
+        {
+            emitFailed(tr("Failed to open patch file for writing."));
+            return;
+        }
+
+        jsonFile.write(document.toJson());
+        if (!jsonFile.commit())
+        {
+            emitFailed(tr("Failed to commit patch file."));
+            return;
+        }
+
+        qDebug() << "Reloading instance's component list.";
+
+        m_components->reload(m_mode);
+
+        qDebug() << "Instance should now the system-wide LWJGL library!";
+    }
+    else
+    {
+        qDebug() << "Instance's LWJGL is customized/patched, leaving it as-is.";
+    }
+
+    emitSucceeded();
+}
+
+bool PatchLWJGL::canAbort() const
+{
+    return false;
+}
+
+bool PatchLWJGL::abort()
+{
+    return false;
+}
