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 lastrender;
public void setAllpixels(ByteBuffer bb) {
allpixels = bb;
}
@SuppressWarnings("deprecation")
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.y=y;
this.width=width;
@ -75,12 +79,15 @@ public class DirectRenderer implements IRenderer {
try {
boolean hascolor=false;
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)
hascolor=true;
}
if(hascolor)
System.out.println("Some color!");
else return;
final Field field = map.getClass().getDeclaredField("worldMap");
field.setAccessible(true);
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));
*/
} 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()))))
return;
(ex = e).printStackTrace();

View file

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

View file

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