-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto exit software when user quits minecraft (#107)
* Add util for finding minecraft process * Add trace log * Add Native process id handle method * Add test for onExit method * Add scheduler for auto quitting * Add setting for auto exit * Add setting for auto exit and migration logic * make auto-exit feature compatible for any other oses rather than windows
- Loading branch information
1 parent
7faa8eb
commit 4312103
Showing
5 changed files
with
140 additions
and
24 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
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
52 changes: 52 additions & 0 deletions
52
app/src/main/java/io/github/hizumiaoba/mctimemachine/internal/natives/NativeHandleUtil.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,52 @@ | ||
package io.github.hizumiaoba.mctimemachine.internal.natives; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class NativeHandleUtil { | ||
|
||
private NativeHandleUtil() {} | ||
|
||
public static Optional<ProcessHandle> getMinecraftProcessId() { | ||
ProcessBuilder pwsh = new ProcessBuilder("powershell", "-Command", "\"Get-Process -Name javaw | Where-Object { $_.MainWindowTitle -like 'Minecraft*' } | Select-Object -ExpandProperty Id | Out-File -Encoding ASCII .\\.mctm-mc.id.txt \""); | ||
final String idFile = ".mctm-mc.id.txt"; | ||
List<String> lines; | ||
try { | ||
Process p = pwsh.start(); | ||
p.onExit().join(); | ||
Path path = Paths.get(idFile); | ||
if (Files.notExists(path)) { | ||
log.warn("File {} cannot be found", idFile); | ||
return Optional.empty(); | ||
} | ||
lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
Files.deleteIfExists(path); | ||
} catch (IOException e) { | ||
log.error("Failed to start shell process to find Minecraft process id", e); | ||
return Optional.empty(); | ||
} | ||
if(lines.size() != 1) { | ||
log.warn("It seems that there are multiple Minecraft processes running? Found {} processes: ", lines.size()); | ||
lines.forEach(log::warn); | ||
log.warn("No process id will be returned"); | ||
return Optional.empty(); | ||
} | ||
log.trace("Found Minecraft process id: {}", lines.get(0)); | ||
int pid = Integer.parseInt(lines.get(0).trim()); | ||
return ProcessHandle.of(pid); | ||
} | ||
|
||
@Deprecated | ||
public static Optional<ProcessHandle> getMinecraftProcess() { | ||
Optional<ProcessHandle> handle = ProcessHandle.allProcesses().parallel().filter(h -> h.info().command().isPresent()).filter(h -> h.info().command().get().contains("javaw")).findFirst(); | ||
return handle; | ||
} | ||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
app/src/test/java/io/github/hizumiaoba/mctimemachine/NativeHandleTest.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,35 @@ | ||
package io.github.hizumiaoba.mctimemachine; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import io.github.hizumiaoba.mctimemachine.internal.natives.NativeHandleUtil; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class NativeHandleTest { | ||
|
||
@Test | ||
@Disabled("This test is already completed under development environment") | ||
public void testGetMinecraftProcessId() { | ||
Optional<ProcessHandle> processHandle = NativeHandleUtil.getMinecraftProcessId(); | ||
processHandle.ifPresentOrElse( | ||
handle -> { | ||
assertThat(handle.pid()).isGreaterThan(0); | ||
assertThat(handle.isAlive()).isTrue(); | ||
}, | ||
() -> fail("Failed to get Minecraft process id") | ||
); | ||
} | ||
|
||
@Test | ||
@Disabled("This test is already completed under development environment") | ||
public void onExitEventSettingTest() { | ||
Optional<ProcessHandle> mcProcess = NativeHandleUtil.getMinecraftProcessId(); | ||
mcProcess.ifPresentOrElse( | ||
handle -> handle.onExit().thenRun(() -> assertThat(true).isTrue()).join(), | ||
() -> fail("Failed to get Minecraft process id") | ||
); | ||
} | ||
} |