Skip to content

Commit

Permalink
Shade WindowsSandbox into the main project
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaninjas committed Nov 7, 2023
1 parent 7a02f85 commit 157c6dc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ sourceSets.main {

tasks.withType(Jar::class) {
from(shade.map { if(it.isDirectory) it else zipTree(it) })
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes(
"ForceLoadAsMod" to true,
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/gq/malwarefight/nosession/win/WindowsSandbox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gq.malwarefight.nosession.win;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

public class WindowsSandbox {
/**
* Run a process sandboxed
* @return success or failure
*/
public static native boolean runProcess(String[] rwMounts, String[] roMounts, String[] args);

/**
* Determines if we are running in a sandbox
* @return whether we are running in a sandbox
*/
public static native boolean isSandboxed();

/**
* This method was pulled from the Utils class so people can use this module as a standalone library
*/
private static void copy(InputStream i, OutputStream o) throws IOException {
byte[] buffer = new byte[16384];
int read;
while ((read = i.read(buffer)) > 0) {
o.write(buffer, 0, read);
}
i.close();
o.close();
}

static {
try {
File tempFile = Files.createTempFile("minecraft_sandbox", ".dll").toFile();
try (InputStream is = WindowsSandbox.class.getResourceAsStream("/native/" + System.getProperty("os.arch") + "/windows/" + System.mapLibraryName("windows_sandbox"))) {
assert is != null: "Native library not compiled";
copy(is, Files.newOutputStream(tempFile.toPath()));
}
System.load(tempFile.getAbsolutePath());
tempFile.deleteOnExit();
} catch (Exception e) {
throw new RuntimeException("Cannot instantiate WindowsSandbox");
}
}
}

0 comments on commit 157c6dc

Please sign in to comment.