Accessing VirtualBox directly (XPCOM)

This commit is contained in:
Norbi Peti 2016-12-29 23:30:02 +01:00
parent 06b785ff4a
commit 4ca3529ee6
14 changed files with 163 additions and 105 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
@ -16,6 +16,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="VirtualComputerSender.j4n.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

89
VirtualComputer/pom.xml Normal file
View file

@ -0,0 +1,89 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer</artifactId>
<version>1.2-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>*.dll</include>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.virtualbox:VirtualBox</include>
</includes>
</artifactSet>
<pluginExecution>
<action>
<execute />
</action>
</pluginExecution>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>repo</id>
<url>file://${basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.countercraft</groupId>
<artifactId>Movecraft</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.virtualbox</groupId>
<artifactId>VirtualBox</artifactId>
<version>5.1</version>
</dependency>
</dependencies>
</project>

View file

@ -2,8 +2,8 @@
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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>SenderBridge</artifactId>
<version>1.0</version>
<groupId>org.virtualbox</groupId>
<artifactId>VirtualBox</artifactId>
<version>5.1</version>
<description>POM was created from install:install-file</description>
</project>

View file

@ -1,53 +1,45 @@
package com.mcplugindev.slipswhitley.sketchmap.map;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ImageRenderer extends MapRenderer
{
private BufferedImage image;
public ImageRenderer(BufferedImage image)
{
this.image = image;
}
private int progress = 0;
public static int updatepixels = 15;
@Override
public void render(MapView view, MapCanvas canvas, Player player)
{
long time = System.nanoTime();
try
{
canvas.drawImage(0, progress * updatepixels, image.getSubimage(0,
progress * updatepixels, 128, (progress * updatepixels
+ updatepixels >= 128 ? 128 - progress
* updatepixels : updatepixels)));
if (progress < 128 / updatepixels)
progress++;
else
progress = 0;
long diff = System.nanoTime() - time;
if (TimeUnit.NANOSECONDS.toMillis(diff) > 40)
{
System.out.println("Map rendering took "
+ TimeUnit.NANOSECONDS.toMillis(diff) + " ms");
}
} catch (Exception e)
{
e.printStackTrace();
System.out.println("Progess: " + progress);
System.out.println("UpdatePixels: " + updatepixels);
}
}
}
package com.mcplugindev.slipswhitley.sketchmap.map;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class ImageRenderer extends MapRenderer // Modified
{
private BufferedImage image;
public ImageRenderer(BufferedImage image) {
this.image = image;
}
private int progress = 0;
public static int updatepixels = 15;
@Override
public void render(MapView view, MapCanvas canvas, Player player) {
long time = System.nanoTime();
try {
canvas.drawImage(0, progress * updatepixels, image.getSubimage(0, progress * updatepixels, 128,
(progress * updatepixels + updatepixels >= 128 ? 128 - progress * updatepixels : updatepixels)));
if (progress < 128 / updatepixels)
progress++;
else
progress = 0;
long diff = System.nanoTime() - time;
if (TimeUnit.NANOSECONDS.toMillis(diff) > 40) {
System.out.println("Map rendering took " + TimeUnit.NANOSECONDS.toMillis(diff) + " ms");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Progess: " + progress);
System.out.println("UpdatePixels: " + updatepixels);
}
}
}

View file

@ -7,7 +7,6 @@ import java.util.HashMap;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.sf.jni4net.Bridge;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -15,14 +14,16 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import virtualcomputersender.Computer;
import org.virtualbox_5_1.ISession;
import org.virtualbox_5_1.IVirtualBox;
import org.virtualbox_5_1.VirtualBoxManager;
import com.mcplugindev.slipswhitley.sketchmap.map.RelativeLocation;
import com.mcplugindev.slipswhitley.sketchmap.map.SketchMap;
public class PluginMain extends JavaPlugin {
private Computer computer;
private IVirtualBox vbox;
private ISession session;
private SketchMap smap;
public static PluginMain Instance;
@ -34,26 +35,10 @@ public class PluginMain extends JavaPlugin {
try {
ConsoleCommandSender ccs = getServer().getConsoleSender();
this.getCommand("computer").setExecutor(new Commands());
ccs.sendMessage("§bExtracting necessary libraries...");
final File[] libs = new File[] {
// new File(getDataFolder(), "jni4net.j-0.8.8.0.jar"),
new File(getDataFolder(), "jni4net.n-0.8.8.0.dll"),
new File(getDataFolder(), "jni4net.n.w64.v40-0.8.8.0.dll"),
// new File(getDataFolder(), "VirtualComputerSender.j4n.jar"),
new File(getDataFolder(), "VirtualComputerSender.j4n.dll"),
new File(getDataFolder(), "VirtualComputerSender.dll"),
new File(getDataFolder(), "Interop.VirtualBox.dll") };
for (final File lib : libs) {
if (!lib.exists()) {
JarUtils.extractFromJar(lib.getName(), lib.getAbsolutePath());
}
}
ccs.sendMessage("§bInitializing bridge...");
Bridge.init(new File(getDataFolder(), "jni4net.n.w64.v40-0.8.8.0.dll").getAbsoluteFile());
Bridge.LoadAndRegisterAssemblyFrom(new File(getDataFolder(), "VirtualComputerSender.j4n.dll"));
ccs.sendMessage("§bInititalizing VirtualBox interface...");
computer = new Computer();
ccs.sendMessage("§bInitializing VirtualBox...");
final VirtualBoxManager manager = VirtualBoxManager.createInstance(null);
vbox = manager.getVBox();
session = manager.getSessionObject();
ccs.sendMessage("§bLoading SketchMap...");
img = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB);
HashMap<Short, RelativeLocation> map = new HashMap<>();
@ -82,7 +67,7 @@ public class PluginMain extends JavaPlugin {
public void Start(CommandSender sender) {
sender.sendMessage("§eStarting computer...");
computer.Start();
// computer.Start();
sender.sendMessage("§eComputer started.");
DoStart();
}
@ -94,8 +79,8 @@ public class PluginMain extends JavaPlugin {
task = this.getServer().getScheduler().runTaskTimerAsynchronously(this, new Runnable() {
public void run() {
final int[] a = ((DataBufferInt) smap.image.getRaster().getDataBuffer()).getData();
final int[] data = computer.GetScreenPixelColors();
System.arraycopy(data, 0, a, 0, data.length);
// final int[] data = computer.GetScreenPixelColors();
// System.arraycopy(data, 0, a, 0, data.length);
}
}, 1, 10);
if (getServer().getPluginManager().isPluginEnabled("Movecraft")) {
@ -123,7 +108,7 @@ public class PluginMain extends JavaPlugin {
public void Stop(CommandSender sender) {
sender.sendMessage("§eStopping computer...");
computer.PowerOff();
// computer.PowerOff();
sender.sendMessage("§eComputer stopped.");
}
@ -133,43 +118,36 @@ public class PluginMain extends JavaPlugin {
getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
@Override
public void run() {
if (computer.PowerButton()) {
DoStart();
s.sendMessage("§eComputer started.");
} else
s.sendMessage("§ePowerbutton pressed.");
/*
* if (computer.PowerButton()) { DoStart(); s.sendMessage("§eComputer started."); } else s.sendMessage("§ePowerbutton pressed.");
*/
}
});
}
public void Reset(CommandSender sender) {
sender.sendMessage("§eResetting computer...");
computer.Reset();
// computer.Reset();
sender.sendMessage("§eComputer reset.");
}
public void FixScreen(CommandSender sender) {
sender.sendMessage("§eFixing screen...");
computer.FixScreen();
// computer.FixScreen();
sender.sendMessage("§eScreen fixed.");
}
public void PressKey(CommandSender sender, String key, String stateorduration) {
if (stateorduration.length() == 0)
computer.PressKey(key, (short) 0);
else if (stateorduration.equalsIgnoreCase("down"))
computer.PressKey(key, (short) -1);
else if (stateorduration.equalsIgnoreCase("up"))
computer.PressKey(key, (short) -2);
else
computer.PressKey(key, Short.parseShort(stateorduration));
/*
* if (stateorduration.length() == 0) computer.PressKey(key, (short) 0); else if (stateorduration.equalsIgnoreCase("down")) computer.PressKey(key, (short) -1); else if
* (stateorduration.equalsIgnoreCase("up")) computer.PressKey(key, (short) -2); else computer.PressKey(key, Short.parseShort(stateorduration));
*/
}
public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs, boolean down) {
if (down)
computer.UpdateMouse(x, y, z, w, mbs);
else
computer.UpdateMouse(x, y, z, w, "");
/*
* if (down) computer.UpdateMouse(x, y, z, w, mbs); else computer.UpdateMouse(x, y, z, w, "");
*/
}
public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs) {