From 2f3bf61d94ae4d7a0ff8b07aee878b4cc7048c53 Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 20 May 2025 12:54:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8=20PowerShell=20=E6=97=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20NoProfile=20=E5=8F=82=E6=95=B0=20(#3923)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hmcl/util/platform/CommandBuilder.java | 35 +++++++------------ .../windows/WindowsHardwareDetector.java | 1 + 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java index fd7034dbf..4fd7662b3 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java @@ -17,10 +17,7 @@ */ package org.jackhuang.hmcl.util.platform; -import java.io.BufferedReader; -import java.io.InputStreamReader; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -248,37 +245,31 @@ public final class CommandBuilder { } public static boolean hasExecutionPolicy() { - if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) { + if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) return true; - } + if (!OperatingSystem.isWindows7OrLater()) + return false; + try { - final Process process = Runtime.getRuntime().exec(new String[]{"powershell", "-Command", "Get-ExecutionPolicy"}); - if (!process.waitFor(5, TimeUnit.SECONDS)) { - process.destroy(); - return false; - } - try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), OperatingSystem.NATIVE_CHARSET))) { - String policy = reader.readLine(); - return "Unrestricted".equalsIgnoreCase(policy) || "RemoteSigned".equalsIgnoreCase(policy); - } + String policy = SystemUtils.run("powershell.exe", "-NoProfile", "-Command", "Get-ExecutionPolicy").trim(); + return "Unrestricted".equalsIgnoreCase(policy) || "RemoteSigned".equalsIgnoreCase(policy); } catch (Throwable ignored) { } return false; } public static boolean setExecutionPolicy() { - if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) { + if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) return true; - } + if (!OperatingSystem.isWindows7OrLater()) + return false; + try { - final Process process = Runtime.getRuntime().exec(new String[]{"powershell", "-Command", "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"}); - if (!process.waitFor(5, TimeUnit.SECONDS)) { - process.destroy(); - return false; - } + SystemUtils.run("powershell.exe", "-NoProfile", "-Command", "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"); + return true; } catch (Throwable ignored) { + return false; } - return true; } private static boolean containsEscape(String str, String escapeChars) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsHardwareDetector.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsHardwareDetector.java index 332eb89c4..c052aa2d6 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsHardwareDetector.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsHardwareDetector.java @@ -58,6 +58,7 @@ public final class WindowsHardwareDetector extends HardwareDetector { List> videoControllers = SystemUtils.run(Arrays.asList( "powershell.exe", + "-NoProfile", "-Command", String.join(" | ", getCimInstance + " -Class Win32_VideoController",