Skip to content

Commit

Permalink
Add LWJGL check and (disabled) TFD implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jun 20, 2024
1 parent 0b06ed4 commit ebed80b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class SodiumPreLaunch implements PreLaunchEntrypoint {
@Override
public void onPreLaunch() {
PreLaunchChecks.beforeLWJGLInit();
GraphicsAdapterProbe.findAdapters();
PreLaunchChecks.onGameInit();
Workarounds.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BugChecks {
public static final boolean ISSUE_899 = configureCheck("issue899", true);
public static final boolean ISSUE_1486 = configureCheck("issue1486", true);
public static final boolean ISSUE_2048 = configureCheck("issue2048", true);
public static final boolean ISSUE_2561 = configureCheck("issue2561", true);

private static boolean configureCheck(String name, boolean defaultValue) {
var propertyValue = System.getProperty(getPropertyKey(name), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,59 @@
import net.caffeinemc.mods.sodium.client.util.OsUtils;
import net.caffeinemc.mods.sodium.client.util.OsUtils.OperatingSystem;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
* Performs OpenGL driver validation before the game creates an OpenGL context. This runs during the earliest possible
* opportunity at game startup, and uses a custom hardware prober to search for problematic drivers.
*/
public class PreLaunchChecks {
private static final Logger LOGGER = LoggerFactory.getLogger("Sodium-EarlyDriverScanner");

// This string should be determined at compile time, so it can be checked against the runtime version.
private static final String REQUIRED_LWJGL_VERSION = Version.VERSION_MAJOR + "." + Version.VERSION_MINOR + "." + Version.VERSION_REVISION;

private static final String normalMessage = "You must change the LWJGL version in your launcher to continue. This is usually controlled by the settings for a profile or instance in your launcher.";

private static final String prismMessage = "It appears you are using Prism Launcher to start the game. You can likely fix this problem by opening your instance settings and navigating to the Version section in the sidebar.";

public static void beforeLWJGLInit() {
if (BugChecks.ISSUE_2561) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String message = normalMessage;

if (Arrays.stream(stackTrace).map(StackTraceElement::toString).anyMatch(s -> s.contains("org.prismlauncher."))) {
message = prismMessage;
}

if (!Version.getVersion().startsWith(REQUIRED_LWJGL_VERSION)) {
showCriticalErrorAndClose("Sodium Renderer - Unsupported LWJGL",
("""
The game failed to start because the currently active LWJGL version is not \
compatible.
Installed version: ###CURRENT_VERSION###
Required version: ###REQUIRED_VERSION###
""" + message)
.replace("###CURRENT_VERSION###", org.lwjgl.Version.getVersion())
.replace("###REQUIRED_VERSION###", REQUIRED_LWJGL_VERSION),
"https://github.com/CaffeineMC/sodium-fabric/wiki/LWJGL-Compatibility");

}
}
}

public static void onGameInit() {
if (BugChecks.ISSUE_899) {
var installedVersion = findIntelDriverMatchingBug899();

if (installedVersion != null) {
showUnsupportedDriverMessageBox(
showCriticalErrorAndClose("Sodium Renderer - Unsupported Driver",
"""
The game failed to start because the currently installed Intel Graphics Driver is not \
compatible.
Expand All @@ -42,7 +79,7 @@ public static void onGameInit() {
var installedVersion = findNvidiaDriverMatchingBug1486();

if (installedVersion != null) {
showUnsupportedDriverMessageBox(
showCriticalErrorAndClose("Sodium Renderer - Unsupported Driver",
"""
The game failed to start because the currently installed NVIDIA Graphics Driver is not \
compatible.
Expand All @@ -58,17 +95,17 @@ public static void onGameInit() {
}
}

private static void showUnsupportedDriverMessageBox(String message, String url) {
private static void showCriticalErrorAndClose(String title, String message, String url) {
// Always print the information to the log file first, just in case we can't show the message box.
LOGGER.error("""
###ERROR_DESCRIPTION###
For more information, please see: ###HELP_URL###"""
.replace("###ERROR_DESCRIPTION###", message)
.replace("###HELP_URL###", url));
.replace("###HELP_URL###", url == null ? "" : url));

// Try to show a graphical message box (if the platform supports it) and shut down the game.
MessageBox.showMessageBox(null, MessageBox.IconType.ERROR, "Sodium Renderer - Unsupported Driver", message, url);
MessageBox.showMessageBox(null, MessageBox.IconType.ERROR, title, message, url);
System.exit(1 /* failure code */);
}

Expand Down Expand Up @@ -130,5 +167,4 @@ private static void showUnsupportedDriverMessageBox(String message, String url)

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import com.mojang.blaze3d.platform.Window;
import org.lwjgl.util.tinyfd.TinyFileDialogs;

import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.Objects;

public class MessageBox {
Expand All @@ -30,10 +33,11 @@ private interface MessageBoxImpl {
static @Nullable MessageBoxImpl chooseImpl() {
if (OsUtils.getOs() == OsUtils.OperatingSystem.WIN) {
return new WindowsMessageBoxImpl();
} else {
// TODO: Tiny File Dialogs is really bad. We need something better.
//return new TFDMessageBoxImpl();
return null;
}

// TODO: Provide an implementation on other platforms
return null;
}

void showMessageBox(@Nullable Window window,
Expand All @@ -42,6 +46,21 @@ void showMessageBox(@Nullable Window window,
@Nullable String helpUrl);
}

private static class TFDMessageBoxImpl implements MessageBoxImpl {
// This adds information about how to open the help box, since we cannot change the buttons.
private static final String NOTICE = "\n\nFor more information, click OK; otherwise, click Cancel.";

@Override
public void showMessageBox(@Nullable Window window, IconType icon, String title, String description, @Nullable String helpUrl) {
boolean clicked = TinyFileDialogs.tinyfd_messageBox(title, helpUrl == null ? description : description + NOTICE, helpUrl == null ? "ok" : "okcancel", icon.name().toLowerCase(Locale.ROOT), false);

if (clicked && helpUrl != null) {
Util.getPlatform()
.openUri(helpUrl);
}
}
}

private static class WindowsMessageBoxImpl implements MessageBoxImpl {
@Override
public void showMessageBox(@Nullable Window window,
Expand Down

0 comments on commit ebed80b

Please sign in to comment.