-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
250 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
VS2015 x64 本机工具命令提示符 | ||
cl /LD /I"%JAVA_HOME%\include" /I"%JAVA_HOME%\include\win32" ghost-common-jni.cpp /link /out:ghost-common-jni_vc2015_amd64.dll | ||
VS2015 x64 ARM 兼容工具命令提示符 | ||
cl /LD /I"%JAVA_HOME%\include" /I"%JAVA_HOME%\include\win32" ghost-common-jni.cpp /link /out:ghost-common-jni_vc2015_aarch64.dll |
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,80 @@ | ||
#include <jni.h> | ||
#include <windows.h> | ||
#include <processthreadsapi.h> | ||
#include <string> | ||
|
||
extern "C" | ||
{ | ||
JNIEXPORT jstring JNICALL Java_com_ghostchu_lib_jni_EcoMode_setEcoMode(JNIEnv *env, jobject obj, jobject enable) | ||
{ | ||
PROCESS_POWER_THROTTLING_STATE PowerThrottling; | ||
RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling)); | ||
PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION; | ||
|
||
// | ||
// EcoQoS | ||
// Turn EXECUTION_SPEED throttling on. | ||
// ControlMask selects the mechanism and StateMask declares which mechanism should be on or off. | ||
// | ||
|
||
PowerThrottling.ControlMask = enable ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED : NULL; | ||
PowerThrottling.StateMask = enable ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED : NULL; | ||
|
||
std::string message; | ||
|
||
if (!SetProcessInformation(GetCurrentProcess(), | ||
ProcessPowerThrottling, | ||
&PowerThrottling, | ||
sizeof(PowerThrottling))) | ||
{ | ||
DWORD error = GetLastError(); | ||
message = "SetProcessInformation failed with error: " + std::to_string(error); | ||
return env->NewStringUTF(message.c_str()); | ||
} | ||
|
||
if (!SetPriorityClass(GetCurrentProcess(), enable ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS)) | ||
{ | ||
DWORD error = GetLastError(); | ||
message = "SetPriorityClass failed with error: " + std::to_string(error); | ||
} | ||
else | ||
{ | ||
message = "SUCCESS"; | ||
} | ||
// 将 C++ 字符串转换为 Java 字符串并返回 | ||
return env->NewStringUTF(message.c_str()); | ||
} | ||
|
||
JNIEXPORT jint JNICALL Java_com_ghostchu_lib_jni_ProcessPriority_setPriority(JNIEnv *env, jclass cls, jint priority) | ||
{ | ||
HANDLE hProcess = GetCurrentProcess(); | ||
DWORD dwPriorityClass; | ||
|
||
switch (priority) | ||
{ | ||
case -1: | ||
dwPriorityClass = IDLE_PRIORITY_CLASS; | ||
break; | ||
case 0: | ||
dwPriorityClass = NORMAL_PRIORITY_CLASS; | ||
break; | ||
case 1: | ||
dwPriorityClass = HIGH_PRIORITY_CLASS; | ||
break; | ||
case 2: | ||
dwPriorityClass = REALTIME_PRIORITY_CLASS; | ||
break; | ||
default: | ||
return -1; // Invalid priority | ||
} | ||
|
||
if (SetPriorityClass(hProcess, dwPriorityClass)) | ||
{ | ||
return 0; // Success | ||
} | ||
else | ||
{ | ||
return -1; // Failure | ||
} | ||
} | ||
} |
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
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,41 @@ | ||
package com.ghostchu.lib.jni; | ||
|
||
import com.ghostchu.peerbanhelper.Main; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Locale; | ||
|
||
@Slf4j | ||
public class EcoMode { | ||
public static boolean ecoMode(boolean enable) { | ||
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT); | ||
if (!os.startsWith("win")) { | ||
throw new IllegalStateException("Only Windows OS support EcoMode API"); | ||
} | ||
String arch = System.getProperty("os.arch").toLowerCase(Locale.ROOT); | ||
try { | ||
File tmpFile = Files.createTempFile("pbh-jni-lib", ".dll").toFile(); | ||
tmpFile.deleteOnExit(); | ||
if (arch.contains("aarch64")) { | ||
Files.copy(Main.class.getResourceAsStream("/native/windows/ghost-common-jni_vc2015_aarch64.dll"), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} else { | ||
Files.copy(Main.class.getResourceAsStream("/native/windows/ghost-common-jni_vc2015_amd64.dll"), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
System.load(tmpFile.getAbsolutePath()); | ||
} catch (IOException e) { | ||
log.error("Unable load JNI native libraries", e); | ||
} | ||
try { | ||
String data = setEcoMode(enable); | ||
return data.equals("SUCCESS"); | ||
} catch (Throwable e) { | ||
return false; | ||
} | ||
} | ||
|
||
private native static String setEcoMode(boolean enable); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/ghostchu/peerbanhelper/exchange/ExchangeMap.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,27 @@ | ||
package com.ghostchu.peerbanhelper.exchange; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
public class ExchangeMap { | ||
public static final Set<DisplayFlag> GUI_DISPLAY_FLAGS = Collections.synchronizedSet(new TreeSet<>()); | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class DisplayFlag implements Comparable<DisplayFlag> { | ||
private int priority; | ||
private String content; | ||
|
||
@Override | ||
public int compareTo(@NotNull ExchangeMap.DisplayFlag o) { | ||
return Integer.compare(priority, o.priority); | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ghostchu/peerbanhelper/platform/WindowsEcoQosAPI.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,36 @@ | ||
package com.ghostchu.peerbanhelper.platform; | ||
|
||
import com.ghostchu.lib.jni.EcoMode; | ||
import com.ghostchu.peerbanhelper.Main; | ||
import com.ghostchu.peerbanhelper.exchange.ExchangeMap; | ||
import com.ghostchu.peerbanhelper.text.Lang; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Locale; | ||
|
||
import static com.ghostchu.peerbanhelper.text.TextManager.tlUI; | ||
|
||
@Component | ||
@Slf4j | ||
public class WindowsEcoQosAPI { | ||
private final YamlConfiguration config; | ||
|
||
public WindowsEcoQosAPI() { | ||
this.config = Main.getMainConfig(); | ||
if (this.config.getBoolean("performance.windows-ecoqos-api")) { | ||
installEcoQosApi(); | ||
} | ||
} | ||
|
||
private void installEcoQosApi() { | ||
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT); | ||
if (os.startsWith("win")) { | ||
if (EcoMode.ecoMode(true)) { | ||
log.info(tlUI(Lang.IN_ECOMODE_DESCRIPTION)); | ||
ExchangeMap.GUI_DISPLAY_FLAGS.add(new ExchangeMap.DisplayFlag(10, tlUI(Lang.IN_ECOMODE_SHORT))); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
Binary file added
BIN
+80.5 KB
src/main/resources/native/windows/ghost-common-jni_vc2015_aarch64.dll
Binary file not shown.
Binary file not shown.