-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ShellExecuteW from message box callbacks
This fixes a regression caused by 26f4263. The underlying problem is that accessing Java's AWT *after* LWJGL3 has initialized is not possible. Minecraft has a utility class which uses rundll32 internally, but we cannot access that due to classloader restrictions on NeoForge. That leaves us with having to implement the call ourselves, and simply using Shell32 directly (like we do for other Windows APIs) seems easiest.
- Loading branch information
1 parent
da61519
commit 930c0c5
Showing
2 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
.../src/workarounds/java/net/caffeinemc/mods/sodium/client/platform/windows/api/Shell32.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.caffeinemc.mods.sodium.client.platform.windows.api; | ||
|
||
import net.caffeinemc.mods.sodium.client.platform.NativeWindowHandle; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.lwjgl.system.JNI; | ||
import org.lwjgl.system.MemoryStack; | ||
import org.lwjgl.system.SharedLibrary; | ||
|
||
import java.util.Objects; | ||
|
||
import static org.lwjgl.system.APIUtil.apiCreateLibrary; | ||
import static org.lwjgl.system.APIUtil.apiGetFunctionAddressOptional; | ||
import static org.lwjgl.system.MemoryUtil.NULL; | ||
|
||
public class Shell32 { | ||
private static final SharedLibrary LIBRARY = apiCreateLibrary("shell32"); | ||
|
||
private static final long PFN_ShellExecuteW = apiGetFunctionAddressOptional(LIBRARY, "ShellExecuteW"); | ||
|
||
public static void browseUrl(@Nullable NativeWindowHandle window, String url) { | ||
Objects.requireNonNull(url, "URL parameter must be non-null"); | ||
|
||
try (var stack = MemoryStack.stackPush()) { | ||
stack.nUTF16("open", true); | ||
var lpOperation = stack.getPointerAddress(); | ||
|
||
stack.nUTF16(url, true); | ||
var lpFile = stack.getPointerAddress(); | ||
|
||
nShellExecuteW(window != null ? window.getWin32Handle() : NULL, | ||
lpOperation, | ||
lpFile, | ||
NULL, | ||
NULL, | ||
0x1 /* SW_NORMAL */); | ||
} | ||
} | ||
|
||
public static long nShellExecuteW( | ||
/* HWND */ long hwnd, | ||
/* LPCWSTR */ long lpOperation, | ||
/* LPCWSTR */ long lpFile, | ||
/* LPCWSTR */ long lpParameters, | ||
/* LPCWSTR */ long lpDirectory, | ||
/* INT */ int nShowCmd | ||
) { | ||
return JNI.invokePPPPPP(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd, checkPfn(PFN_ShellExecuteW)); | ||
} | ||
|
||
private static long checkPfn(long pfn) { | ||
if (pfn == NULL) { | ||
throw new NullPointerException("Function pointer not available"); | ||
} | ||
|
||
return pfn; | ||
} | ||
} |