Progress with using jni4net

Using code from v1
This commit is contained in:
Norbi Peti 2020-11-21 21:34:56 +01:00
parent 6eee57f18e
commit e1352b7f87
13 changed files with 186 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package sznp.virtualcomputer;
import jnr.ffi.LibraryLoader;
import lombok.val;
import net.sf.jni4net.Bridge;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin;
@ -75,7 +76,31 @@ public class PluginMain extends JavaPlugin {
System.setProperty("sun.boot.library.path", vbpath);
if (System.getProperty("java.library.path") == null || System.getProperty("java.library.path").isEmpty())
System.setProperty("java.library.path", vbpath);
Utils.addLibraryPath(vbpath); //TODO: Jacob DLL must be in the server folder
Utils.addLibraryPath(vbpath);
if (windows) {
ccs.sendMessage("§bExtracting Windows-specific libraries...");
final File[] libs = new File[]{
new File(getDataFolder(), "jni4net.n-0.8.9.0.dll"),
new File(getDataFolder(), "jni4net.n.w64.v40-0.8.9.0.dll"),
new File(getDataFolder(), "VirtualComputerWindows.j4n.dll"),
new File(getDataFolder(), "VirtualComputerWindows.dll"),
new File(getDataFolder(), "Interop.VirtualBox.dll"),
new File(getDataFolder(), "Interop.VirtualBox.j4n.dll")
};
for (final File lib : libs) {
if (!lib.exists()) {
JarUtils.extractFromJar(lib.getName(), lib.getAbsolutePath());
}
}
ccs.sendMessage("§bInitializing bridge...");
Bridge.setVerbose(true);
//Bridge.init(new File(getDataFolder(), "jni4net.n.w64.v40-0.8.9.0.dll").getAbsoluteFile());
Bridge.init(getDataFolder().getAbsoluteFile());
Bridge.getSetup().setVeryVerbose(true);
Bridge.LoadAndRegisterAssemblyFrom(new File(getDataFolder(), "VirtualComputerWindows.j4n.dll"));
Bridge.LoadAndRegisterAssemblyFrom(new File(getDataFolder(), "Interop.VirtualBox.j4n.dll"));
}
final VirtualBoxManager manager = VirtualBoxManager.createInstance(getDataFolder().getAbsolutePath());
if (!windows) {
VBoxLib vbl = LibraryLoader.create(VBoxLib.class).load("vboxjxpcom");

View file

@ -25,16 +25,38 @@
<groupId>net.sf.jni4net</groupId>
<artifactId>clr</artifactId>
<version>0.8.9.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jni4net.j-0.8.9.0.jar</systemPath>
</dependency>
<dependency>
<groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer-Windows</artifactId>
<version>0.8.9.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/VirtualComputerWindows.j4n.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactSet>
<includes>
<include>net.sf.jni4net:clr</include>
<include>io.github.NorbiPeti:VirtualComputer-Windows</include>
</includes>
</artifactSet>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -171,10 +171,10 @@ public interface IVirtualBox {
@net.sf.jni4net.attributes.ClrMethod("()Lorg/virtualbox_6_1/IUnattended;")
IUnattended createUnattendedInstaller();
@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;LSystem/String;Lorg/virtualbox_6_1/AccessMode;Lorg/virtualbox_6_1/DeviceType;)Lorg/virtualbox_6_1/IMedium;")
@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;LSystem/String;Lorg/virtualbox_6_1_FixIt/AccessMode;Lorg/virtualbox_6_1/DeviceType;)Lorg/virtualbox_6_1/IMedium;")
IMedium createMedium_FixIt(String aFormat, String aLocation, org.virtualbox_6_1_FixIt.AccessMode aAccessMode, org.virtualbox_6_1_FixIt.DeviceType aADeviceTypeType);
@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;Lorg/virtualbox_6_1/DeviceType;Lorg/virtualbox_6_1/AccessMode;I)Lorg/virtualbox_6_1/IMedium;")
@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;Lorg/virtualbox_6_1/DeviceType;Lorg/virtualbox_6_1_FixIt/AccessMode;I)Lorg/virtualbox_6_1/IMedium;")
IMedium openMedium_FixIt(String aLocation, org.virtualbox_6_1_FixIt.DeviceType aDeviceType, org.virtualbox_6_1_FixIt.AccessMode aAccessMode, int aForceNewUuid);
@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;)Lorg/virtualbox_6_1/IGuestOSType;")

View file

@ -0,0 +1,90 @@
package sznp.virtualcomputer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarUtils {
public static boolean extractFromJar(final String fileName,
final String dest) throws IOException {
if (getRunningJar() == null) {
return false;
}
final File file = new File(dest);
if (file.isDirectory()) {
file.mkdir();
return false;
}
if (!file.exists()) {
file.getParentFile().mkdirs();
}
final JarFile jar = getRunningJar();
final Enumeration<JarEntry> e = jar.entries();
while (e.hasMoreElements()) {
final JarEntry je = e.nextElement();
if (!je.getName().contains(fileName)) {
continue;
}
final InputStream in = new BufferedInputStream(
jar.getInputStream(je));
final OutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
copyInputStream(in, out);
jar.close();
return true;
}
jar.close();
return false;
}
private static void copyInputStream(final InputStream in,
final OutputStream out) throws IOException {
try {
final byte[] buff = new byte[4096];
int n;
while ((n = in.read(buff)) > 0) {
out.write(buff, 0, n);
}
} finally {
out.flush();
out.close();
in.close();
}
}
public static URL getJarUrl(final File file) throws IOException {
return new URL("jar:" + file.toURI().toURL().toExternalForm() + "!/");
}
public static JarFile getRunningJar() throws IOException {
if (!RUNNING_FROM_JAR) {
return null; // null if not running from jar
}
String path = new File(JarUtils.class.getProtectionDomain()
.getCodeSource().getLocation().getPath()).getAbsolutePath();
path = URLDecoder.decode(path, "UTF-8");
return new JarFile(path);
}
private static boolean RUNNING_FROM_JAR = false;
static {
final URL resource = JarUtils.class.getClassLoader().getResource(
"plugin.yml");
if (resource != null) {
RUNNING_FROM_JAR = true;
}
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer-Windows</artifactId>
<version>0.8.9.0</version>
<description>POM was created from install:install-file</description>
</project>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer-Windows</artifactId>
<versioning>
<release>0.8.9.0</release>
<versions>
<version>0.8.9.0</version>
</versions>
<lastUpdated>20201121164706</lastUpdated>
</versioning>
</metadata>

Binary file not shown.

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>net.sf.jni4net</groupId>
<artifactId>clr</artifactId>
<version>0.8.9.0</version>
<description>POM was created from install:install-file</description>
</project>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>net.sf.jni4net</groupId>
<artifactId>clr</artifactId>
<versioning>
<release>0.8.9.0</release>
<versions>
<version>0.8.9.0</version>
</versions>
<lastUpdated>20201121164514</lastUpdated>
</versioning>
</metadata>