Directly using VirtualBox from Java #5

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

View file

@ -39,13 +39,7 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<pluginExecution>
<action>
<execute />
</action>
</pluginExecution>
</configuration>
<configuration />
</execution>
</executions>
</plugin>

View file

@ -41,11 +41,6 @@
<goal>shade</goal>
</goals>
<configuration>
<pluginExecution>
<action>
<execute />
</action>
</pluginExecution>
</configuration>
</execution>
</executions>

View file

@ -1,16 +1,16 @@
package sznp.virtualcomputer;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
public class BukkitRenderer extends MapRenderer implements IRenderer {
private ByteBuffer allpixels;
private BufferedImage image;
@ -54,7 +54,7 @@ public class BukkitRenderer extends MapRenderer implements IRenderer {
for (int i = startindex, j = 0; i < startindex + 128 * 128; i = i + 4, j++) {
int b, g, r;
b = allpixels.get(i) & 0xFF;
b = allpixels.get(i) & 0xFF; //TODO: <-- IndexOutOfBoundsException sometimes
g = allpixels.get(i + 1) & 0xFF;
r = allpixels.get(i + 2) & 0xFF;

View file

@ -1,18 +1,19 @@
package sznp.virtualcomputer;
import java.awt.Color;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import net.minecraft.server.v1_12_R1.WorldMap;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.map.RenderData;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import net.minecraft.server.v1_12_R1.WorldMap;
import java.awt.*;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Map;
public class DirectRenderer implements IRenderer {
private int startindex;
@ -26,8 +27,6 @@ public class DirectRenderer implements IRenderer {
* The ID of the current map
* @param world
* The world to create new maps in
* @param allpixels
* The raw pixel data from the machine in BGRA format
* @param startindex
* The index to start from in allpixels
* @throws Exception
@ -47,16 +46,41 @@ public class DirectRenderer implements IRenderer {
this.startindex = startindex;
this.buffer = render.buffer;
map.addRenderer(new DummyRenderer());
}
private final class DummyRenderer extends MapRenderer {
@Override
public void render(MapView map, MapCanvas canvas, Player player) {
if(allpixels != null)
DirectRenderer.this.render(allpixels, x, y, width, height); //Render after zeroing whole map
}
}
private Exception ex;
private ByteBuffer allpixels;
private long x, y, width, height;
private long lastrender;
@SuppressWarnings("deprecation")
public void render(ByteBuffer allpixels, long x, long y, long width, long height) { // TODO
this.allpixels=allpixels;
this.x=x;
this.y=y;
this.width=width;
this.height=height;
if(System.nanoTime()-lastrender<100*1000*1000)
return;
lastrender=System.nanoTime();
try {
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)));
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(allpixels.get(i+2)>10)
hascolor=true;
}
if(hascolor)
System.out.println("Some color!");
final Field field = map.getClass().getDeclaredField("worldMap");
field.setAccessible(true);
WorldMap wmap = (WorldMap) field.get(map);

View file

@ -6,7 +6,10 @@ import org.mozilla.interfaces.IFramebuffer;
import org.mozilla.interfaces.IFramebufferOverlay;
import org.mozilla.interfaces.nsISupports;
import org.mozilla.xpcom.Mozilla;
import org.virtualbox_5_2.*;
import org.virtualbox_5_2.BitmapFormat;
import org.virtualbox_5_2.Holder;
import org.virtualbox_5_2.IDisplay;
import org.virtualbox_5_2.IDisplaySourceBitmap;
public class MCFrameBuffer implements IFramebuffer {
private IDisplay display;
@ -76,6 +79,7 @@ public class MCFrameBuffer implements IFramebuffer {
}
private BukkitTask tt;
private BukkitTask tttt;
@Override
public void notifyChange(long screenId, long xOrigin, long yOrigin, long width, long height) {
@ -86,12 +90,12 @@ public class MCFrameBuffer implements IFramebuffer {
* if (width > 640 || height > 480) { tt = Bukkit.getScheduler().runTaskTimerAsynchronously(PluginMain.Instance, () -> display.setVideoModeHint(0L, true, false, 0, 0, 640L, 480L, 32L), 5, 5);
* return; // Don't even try to render too large resolutions }
*/
Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> {
tt = Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> {
display.querySourceBitmap(0L, holder);
byte[] arr = PluginMain.allpixels.array();
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("Arr10:" + arr[10]);
System.out.println("Arr0:" + arr[0]);
System.out.println("whbppbplpf: " + w[0] + " " + h[0] + " " + bpp[0] + " " + bpl[0] + " " + pf[0]);
if (width * height > 640 * 480)
PluginMain.allpixels.limit(640 * 480 * 4);
@ -108,9 +112,13 @@ public class MCFrameBuffer implements IFramebuffer {
@Override
public void notifyUpdate(long x, long y, long width, long height) {
for (IRenderer r : PluginMain.renderers)
if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height);
if(tttt != null)
tttt.cancel(); //We are getting updates, but the pixel array isn't updated - VB reacts slowly
tttt = Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> {
for (IRenderer r : PluginMain.renderers)
if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height);
}, 5);
}
@Override

View file

@ -1,11 +1,7 @@
package sznp.virtualcomputer;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.common.collect.Lists;
import jnr.ffi.LibraryLoader;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
@ -13,9 +9,11 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import org.virtualbox_5_2.*;
import com.google.common.collect.Lists;
import jnr.ffi.LibraryLoader;
import java.io.File;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
public class PluginMain extends JavaPlugin {
private IVirtualBox vbox;
@ -57,10 +55,10 @@ public class PluginMain extends JavaPlugin {
session = manager.getSessionObject(); // TODO: Events
ccs.sendMessage("§bLoading Screen...");
try {
throw new NoClassDefFoundError("Test error pls ignore");
/*for (short i = 0; i < 20; i++)
//throw new NoClassDefFoundError("Test error pls ignore");
for (short i = 0; i < 20; i++)
renderers.add(new DirectRenderer(i, Bukkit.getWorlds().get(0), i * 128 * 128 * 4)); // TODO: The pixels are selected in a horribly wrong way probably
ccs.sendMessage("§bUsing Direct Renderer, all good");*/
ccs.sendMessage("§bUsing Direct Renderer, all good");
} catch (NoClassDefFoundError e) {
for (short i = 0; i < 20; i++)
renderers.add(new BukkitRenderer(i, Bukkit.getWorlds().get(0), i * 128 * 128 * 4));