Directly using VirtualBox from Java #5

Merged
NorbiPeti merged 60 commits from directvb into master 2019-04-18 23:29:21 +00:00
3 changed files with 33 additions and 23 deletions
Showing only changes of commit dea4205a0e - Show all commits

View file

@ -62,9 +62,13 @@ public class DirectRenderer implements IRenderer {
private long x, y, width, height; private long x, y, width, height;
private long lastrender; private long lastrender;
public void setAllpixels(ByteBuffer bb) {
allpixels = bb;
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void render(ByteBuffer allpixels, long x, long y, long width, long height) { // TODO public void render(ByteBuffer allpixels, long x, long y, long width, long height) { // TODO
this.allpixels=allpixels; this.allpixels = allpixels; //TODO: |CRASH| Prevent trying to read memory after computer is stopped
this.x=x; this.x=x;
this.y=y; this.y=y;
this.width=width; this.width=width;
@ -75,12 +79,15 @@ public class DirectRenderer implements IRenderer {
try { try {
boolean hascolor=false; boolean hascolor=false;
for (int i = startindex, j = 0; i < allpixels.limit() - 4 && j < buffer.length; i += 4, j++) { for (int i = startindex, j = 0; i < allpixels.limit() - 4 && j < buffer.length; i += 4, j++) {
buffer[j] = MapPalette.matchColor(new Color(allpixels.get(i), allpixels.get(i + 1), allpixels.get(i + 2))); if (PluginMain.Instance.checkMachineNotRunning(null))
return;
buffer[j] = MapPalette.matchColor(new Color(Byte.toUnsignedInt(allpixels.get(i)), Byte.toUnsignedInt(allpixels.get(i + 1)), Byte.toUnsignedInt(allpixels.get(i + 2))));
if(allpixels.get(i+2)>10) if(allpixels.get(i+2)>10)
hascolor=true; hascolor=true;
} }
if(hascolor) if(hascolor)
System.out.println("Some color!"); System.out.println("Some color!");
else return;
final Field field = map.getClass().getDeclaredField("worldMap"); final Field field = map.getClass().getDeclaredField("worldMap");
field.setAccessible(true); field.setAccessible(true);
WorldMap wmap = (WorldMap) field.get(map); WorldMap wmap = (WorldMap) field.get(map);
@ -92,7 +99,7 @@ public class DirectRenderer implements IRenderer {
* System.out.println("==: " + (buffer == render.buffer)); System.out.println("equals:" + Arrays.equals(buffer, render.buffer)); * System.out.println("==: " + (buffer == render.buffer)); System.out.println("equals:" + Arrays.equals(buffer, render.buffer));
*/ */
} catch (Exception e) { } catch (Exception e) {
if (ex != null && (e.getMessage() == ex.getMessage() if (ex != null && (e.getMessage() == ex.getMessage() //Checking for null with the ==
|| (e.getMessage() != null && e.getMessage().equals(ex.getMessage())))) || (e.getMessage() != null && e.getMessage().equals(ex.getMessage()))))
return; return;
(ex = e).printStackTrace(); (ex = e).printStackTrace();

View file

@ -1,5 +1,6 @@
package sznp.virtualcomputer; package sznp.virtualcomputer;
import com.sun.jna.Pointer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.mozilla.interfaces.IFramebuffer; import org.mozilla.interfaces.IFramebuffer;
@ -100,11 +101,11 @@ public class MCFrameBuffer implements IFramebuffer {
try { try {
display.querySourceBitmap(0L, holder); display.querySourceBitmap(0L, holder);
//byte[] arr = PluginMain.allpixels.array(); //byte[] arr = PluginMain.allpixels.array();
long[] arr = new long[1]; long[] ptr = new long[1], w = new long[1], h = new long[1], bpp = new long[1], bpl = new long[1], pf = new long[1];
long[] w = new long[1], h = new long[1], bpp = new long[1], bpl = new long[1], pf = new long[1]; holder.value.getTypedWrapped().queryBitmapInfo(ptr, w, h, bpp, bpl, pf);
holder.value.getTypedWrapped().queryBitmapInfo(arr, w, h, bpp, bpl, pf); System.out.println("Arr0:" + ptr[0]);
System.out.println("Arr0:" + arr[0]);
System.out.println("whbppbplpf: " + w[0] + " " + h[0] + " " + bpp[0] + " " + bpl[0] + " " + pf[0]); System.out.println("whbppbplpf: " + w[0] + " " + h[0] + " " + bpp[0] + " " + bpl[0] + " " + pf[0]);
PluginMain.allpixels = new Pointer(ptr[0]).getByteBuffer(0L, width * height * 4);
if (width * height > 640 * 480) if (width * height > 640 * 480)
PluginMain.allpixels.limit(640 * 480 * 4); PluginMain.allpixels.limit(640 * 480 * 4);
else else
@ -113,7 +114,8 @@ public class MCFrameBuffer implements IFramebuffer {
if (r instanceof BukkitRenderer) if (r instanceof BukkitRenderer)
((BukkitRenderer) r).setAllPixels(PluginMain.allpixels); ((BukkitRenderer) r).setAllPixels(PluginMain.allpixels);
else if (r instanceof DirectRenderer) else if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, xOrigin, yOrigin, width, height); //((DirectRenderer) r).render(PluginMain.allpixels, xOrigin, yOrigin, width, height);
((DirectRenderer) r).setAllpixels(PluginMain.allpixels);
System.out.println("Change!"); System.out.println("Change!");
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
@ -129,7 +131,7 @@ public class MCFrameBuffer implements IFramebuffer {
for (IRenderer r : PluginMain.renderers) for (IRenderer r : PluginMain.renderers)
if (r instanceof DirectRenderer) if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height); ((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height);
System.out.println("Update!"); System.out.println("Update!"); - The render is done each tick
}, 5);*/ }, 5);*/
} }

View file

@ -1,7 +1,6 @@
package sznp.virtualcomputer; package sznp.virtualcomputer;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.sun.jna.Pointer;
import jnr.ffi.LibraryLoader; import jnr.ffi.LibraryLoader;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -10,6 +9,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.virtualbox_5_2.*; import org.virtualbox_5_2.*;
import javax.annotation.Nullable;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -24,7 +24,8 @@ public class PluginMain extends JavaPlugin {
private BukkitTask mousetask; private BukkitTask mousetask;
public static PluginMain Instance; public static PluginMain Instance;
public static ByteBuffer allpixels = ByteBuffer.allocate(640 * 480 * 4); // It's set on each change //public static ByteBuffer allpixels = ByteBuffer.allocate(640 * 480 * 4); // It's set on each change
public static ByteBuffer allpixels; // It's set on each change
public static ArrayList<IRenderer> renderers = new ArrayList<>(); public static ArrayList<IRenderer> renderers = new ArrayList<>();
// Fired when plugin is first enabled // Fired when plugin is first enabled
@ -39,7 +40,7 @@ public class PluginMain extends JavaPlugin {
? "/Applications/VirtualBox.app/Contents/MacOS" ? "/Applications/VirtualBox.app/Contents/MacOS"
: "/opt/virtualbox"; : "/opt/virtualbox";
File f = new File(vbpath); File f = new File(vbpath);
if (!f.isDirectory() || !Arrays.stream(f.list()).anyMatch(s -> s.contains("xpcom"))) if (!f.isDirectory() || Arrays.stream(f.list()).noneMatch(s -> s.contains("xpcom")))
vbpath = "/usr/lib/virtualbox"; vbpath = "/usr/lib/virtualbox";
if (System.getProperty("vbox.home") == null || System.getProperty("vbox.home").isEmpty()) if (System.getProperty("vbox.home") == null || System.getProperty("vbox.home").isEmpty())
System.setProperty("vbox.home", vbpath); System.setProperty("vbox.home", vbpath);
@ -52,7 +53,6 @@ public class PluginMain extends JavaPlugin {
final VirtualBoxManager manager = VirtualBoxManager.createInstance(getDataFolder().getAbsolutePath()); final VirtualBoxManager manager = VirtualBoxManager.createInstance(getDataFolder().getAbsolutePath());
VBoxLib vbl = LibraryLoader.create(VBoxLib.class).load("vboxjxpcom"); VBoxLib vbl = LibraryLoader.create(VBoxLib.class).load("vboxjxpcom");
vbl.RTR3InitExe(0, "", 0); vbl.RTR3InitExe(0, "", 0);
Pointer addr = new Pointer(10);
vbox = manager.getVBox(); vbox = manager.getVBox();
session = manager.getSessionObject(); // TODO: Events session = manager.getSessionObject(); // TODO: Events
ccs.sendMessage("§bLoading Screen..."); ccs.sendMessage("§bLoading Screen...");
@ -135,7 +135,7 @@ public class PluginMain extends JavaPlugin {
public static int MouseSpeed = 1; public static int MouseSpeed = 1;
public void Stop(CommandSender sender) { public void Stop(CommandSender sender) {
if (!checkMachineRunning(sender)) { if (checkMachineNotRunning(sender)) {
if (session.getState().equals(SessionState.Locked)) { if (session.getState().equals(SessionState.Locked)) {
session.unlockMachine(); session.unlockMachine();
sender.sendMessage("§eComputer powered off, released it."); sender.sendMessage("§eComputer powered off, released it.");
@ -165,7 +165,7 @@ public class PluginMain extends JavaPlugin {
} }
public void Reset(CommandSender sender) { public void Reset(CommandSender sender) {
if (!checkMachineRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
sender.sendMessage("§eResetting computer..."); sender.sendMessage("§eResetting computer...");
session.getConsole().reset(); session.getConsole().reset();
@ -173,7 +173,7 @@ public class PluginMain extends JavaPlugin {
} }
public void FixScreen(CommandSender sender) { public void FixScreen(CommandSender sender) {
if (!checkMachineRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
sender.sendMessage("§eFixing screen..."); sender.sendMessage("§eFixing screen...");
session.getConsole().getDisplay().setSeamlessMode(false); session.getConsole().getDisplay().setSeamlessMode(false);
@ -181,16 +181,17 @@ public class PluginMain extends JavaPlugin {
sender.sendMessage("§eScreen fixed."); sender.sendMessage("§eScreen fixed.");
} }
private boolean checkMachineRunning(CommandSender sender) { public boolean checkMachineNotRunning(@Nullable CommandSender sender) {
if (session.getState() != SessionState.Locked || machine.getState() != MachineState.Running) { if (session.getState() != SessionState.Locked || machine.getState() != MachineState.Running) {
if (sender != null)
sender.sendMessage("§cMachine isn't running."); sender.sendMessage("§cMachine isn't running.");
return false;
}
return true; return true;
} }
return false;
}
public void PressKey(CommandSender sender, String key, String stateorduration) { public void PressKey(CommandSender sender, String key, String stateorduration) {
if (!checkMachineRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
int durationorstate; int durationorstate;
if (stateorduration.length() == 0) if (stateorduration.length() == 0)
@ -216,7 +217,7 @@ public class PluginMain extends JavaPlugin {
} }
public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs, boolean down) { public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs, boolean down) {
if (!checkMachineRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
int state = 0; int state = 0;
if (mbs.length() > 0 && down) if (mbs.length() > 0 && down)
@ -226,7 +227,7 @@ public class PluginMain extends JavaPlugin {
} }
public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs) { public void UpdateMouse(CommandSender sender, int x, int y, int z, int w, String mbs) {
if (!checkMachineRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
UpdateMouse(sender, x, y, z, w, mbs, true); UpdateMouse(sender, x, y, z, w, mbs, true);
UpdateMouse(sender, x, y, z, w, mbs, false); UpdateMouse(sender, x, y, z, w, mbs, false);