Pass the embedded option to the Computer

This commit is contained in:
Norbi Peti 2021-03-10 02:04:18 +01:00
parent e23e062be9
commit 2d08ef55fa
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 22 additions and 20 deletions

View file

@ -33,13 +33,15 @@ public final class Computer {
private final VirtualBoxManager manager; private final VirtualBoxManager manager;
private MCFrameBuffer framebuffer; private MCFrameBuffer framebuffer;
private final boolean direct; private final boolean direct;
private final boolean embedded;
public Computer(PluginMain plugin, VirtualBoxManager manager, IVirtualBox vbox, boolean direct) { public Computer(PluginMain plugin, VirtualBoxManager manager, IVirtualBox vbox, boolean direct, boolean embedded) {
this.plugin = plugin; this.plugin = plugin;
this.manager = manager; this.manager = manager;
session = manager.getSessionObject(); session = manager.getSessionObject();
this.vbox = vbox; this.vbox = vbox;
this.direct = direct; this.direct = direct;
this.embedded = embedded;
if (instance != null) throw new IllegalStateException("A computer already exists!"); if (instance != null) throw new IllegalStateException("A computer already exists!");
instance = this; instance = this;
} }
@ -63,8 +65,8 @@ public final class Computer {
} }
session.setName("minecraft"); session.setName("minecraft");
VBoxEventHandler.getInstance().setup(machine.getId(), sender); //TODO: Sometimes null VBoxEventHandler.getInstance().setup(machine.getId(), sender); //TODO: Sometimes null
synchronized (session) { synchronized (this) {
if (plugin.runEmbedded.get()) if (embedded)
machine.lockMachine(session, LockType.VM); //Run in our process <-- Need the VM type to have console access machine.lockMachine(session, LockType.VM); //Run in our process <-- Need the VM type to have console access
else { else {
val progress = machine.launchVMProcess(session, "headless", Collections.emptyList()); //Run in a separate process val progress = machine.launchVMProcess(session, "headless", Collections.emptyList()); //Run in a separate process
@ -105,7 +107,7 @@ public final class Computer {
System.out.println("A"); System.out.println("A");
machine = session.getMachine(); // This is the Machine object we can work with machine = session.getMachine(); // This is the Machine object we can work with
final IConsole console = session.getConsole(); final IConsole console = session.getConsole();
if (plugin.runEmbedded.get()) { //Otherwise it's set while starting the VM if (embedded) { //Otherwise it's set while starting the VM
IProgress progress = console.powerUp(); // https://marc.info/?l=vbox-dev&m=142780789819967&w=2 IProgress progress = console.powerUp(); // https://marc.info/?l=vbox-dev&m=142780789819967&w=2
onStartSetProgress(progress, sender); onStartSetProgress(progress, sender);
} }
@ -113,9 +115,9 @@ public final class Computer {
System.out.println("State: " + console.getState()); System.out.println("State: " + console.getState());
listener = handler.registerTo(console.getEventSource()); listener = handler.registerTo(console.getEventSource());
System.out.println("State: " + console.getState()); System.out.println("State: " + console.getState());
val fb = new MCFrameBuffer(console.getDisplay(), plugin, direct); val fb = new MCFrameBuffer(console.getDisplay(), plugin, embedded, direct);
System.out.println("C"); System.out.println("C");
if (plugin.runEmbedded.get()) if (embedded)
fb.startEmbedded(); fb.startEmbedded();
String fbid = console.getDisplay().attachFramebuffer(0L, String fbid = console.getDisplay().attachFramebuffer(0L,
COMUtils.gimmeAFramebuffer(fb)); COMUtils.gimmeAFramebuffer(fb));
@ -148,7 +150,7 @@ public final class Computer {
return; return;
} }
sendMessage(sender, "§eStopping computer..."); sendMessage(sender, "§eStopping computer...");
synchronized (session) { synchronized (this) {
session.getConsole().powerDown(); session.getConsole().powerDown();
} }
} }
@ -159,7 +161,7 @@ public final class Computer {
if (session.getState() != SessionState.Locked || session.getMachine() == null) { if (session.getState() != SessionState.Locked || session.getMachine() == null) {
Start(sender, index); Start(sender, index);
} else { } else {
synchronized (session) { synchronized (this) {
session.getConsole().powerButton(); session.getConsole().powerButton();
} }
sendMessage(sender, "§ePowerbutton pressed."); sendMessage(sender, "§ePowerbutton pressed.");
@ -171,7 +173,7 @@ public final class Computer {
if (checkMachineNotRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
sendMessage(sender, "§eResetting computer..."); sendMessage(sender, "§eResetting computer...");
synchronized (session) { synchronized (this) {
session.getConsole().reset(); session.getConsole().reset();
} }
sendMessage(sender, "§eComputer reset."); sendMessage(sender, "§eComputer reset.");
@ -181,7 +183,7 @@ public final class Computer {
if (checkMachineNotRunning(sender)) if (checkMachineNotRunning(sender))
return; return;
sendMessage(sender, "§eSaving computer state..."); sendMessage(sender, "§eSaving computer state...");
synchronized (session) { synchronized (this) {
session.getMachine().saveState(); session.getMachine().saveState();
} }
} }
@ -195,11 +197,11 @@ public final class Computer {
} }
sendMessage(sender, "§eFixing screen..."); sendMessage(sender, "§eFixing screen...");
try { try {
synchronized (session) { synchronized (this) {
session.getConsole().getDisplay().setVideoModeHint(0L, true, false, 0, 0, 640L, 480L, 32L, false); session.getConsole().getDisplay().setVideoModeHint(0L, true, false, 0, 0, 640L, 480L, 32L, false);
} //Last param: notify - send PnP notification - stops updates but not changes for some reason } //Last param: notify - send PnP notification - stops updates but not changes for some reason
Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> { Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> {
synchronized (session) { synchronized (this) {
session.getConsole().getMouse().putMouseEventAbsolute(-1, -1, 0, 0, 0); session.getConsole().getMouse().putMouseEventAbsolute(-1, -1, 0, 0, 0);
session.getConsole().getMouse().putMouseEvent(0, 0, 0, 0, 0); //Switch to relative mode session.getConsole().getMouse().putMouseEvent(0, 0, 0, 0, 0); //Switch to relative mode
sendMessage(sender, "§eScreen fixed."); sendMessage(sender, "§eScreen fixed.");
@ -210,7 +212,7 @@ public final class Computer {
} }
} }
public boolean checkMachineNotRunning(@Nullable CommandSender sender) { private 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) if (sender != null)
sender.sendMessage("§cMachine isn't running."); sender.sendMessage("§cMachine isn't running.");
@ -238,7 +240,7 @@ public final class Computer {
} }
// Release key scan code concept taken from VirtualBox source code (KeyboardImpl.cpp:putCAD()) // Release key scan code concept taken from VirtualBox source code (KeyboardImpl.cpp:putCAD())
// +128 // +128
synchronized (session) { synchronized (this) {
if (durationorstate != -2) if (durationorstate != -2)
session.getConsole().getKeyboard().putScancode(code); session.getConsole().getKeyboard().putScancode(code);
Runnable sendrelease = () -> session.getConsole().getKeyboard().putScancodes(Lists.newArrayList(code + 128, Runnable sendrelease = () -> session.getConsole().getKeyboard().putScancodes(Lists.newArrayList(code + 128,
@ -258,7 +260,7 @@ public final class Computer {
if (mbs.length() > 0 && down) if (mbs.length() > 0 && down)
state = Arrays.stream(MouseButtonState.values()).filter(mousebs -> mousebs.name().equalsIgnoreCase(mbs)) state = Arrays.stream(MouseButtonState.values()).filter(mousebs -> mousebs.name().equalsIgnoreCase(mbs))
.findAny().orElseThrow(() -> new Exception("Unknown mouse button")).value(); .findAny().orElseThrow(() -> new Exception("Unknown mouse button")).value();
synchronized (session) { synchronized (this) {
session.getConsole().getMouse().putMouseEvent(x, y, z, w, state); session.getConsole().getMouse().putMouseEvent(x, y, z, w, state);
} }
} }
@ -347,7 +349,7 @@ public final class Computer {
public void onMachineStop(CommandSender sender) { public void onMachineStop(CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
if (session.getState() == SessionState.Locked) { if (session.getState() == SessionState.Locked) {
synchronized (session) { synchronized (this) {
session.unlockMachine(); //Needs to be outside of the event handler session.unlockMachine(); //Needs to be outside of the event handler
} }
handler = null; handler = null;
@ -363,7 +365,7 @@ public final class Computer {
framebuffer.stop(); framebuffer.stop();
} }
public void stopEvents() { private void stopEvents() {
/*if(session!=null && listener!=null) /*if(session!=null && listener!=null)
session.getConsole().getEventSource().unregisterListener(listener);*/ session.getConsole().getEventSource().unregisterListener(listener);*/
if (listener != null) if (listener != null)

View file

@ -131,7 +131,7 @@ public class PluginMain extends ButtonPlugin {
setupBukkitRendering(ccs); setupBukkitRendering(ccs);
direct = false; direct = false;
} }
new Computer(this, manager, vbox, direct); //Saves itself new Computer(this, manager, vbox, direct, runEmbedded.get()); //Saves itself
ccs.sendMessage("§bLoaded!"); ccs.sendMessage("§bLoaded!");
val mlpl = new MouseLockerPlayerListener(); val mlpl = new MouseLockerPlayerListener();
mousetask = getServer().getScheduler().runTaskTimer(this, mlpl, 0, 0); mousetask = getServer().getScheduler().runTaskTimer(this, mlpl, 0, 0);

View file

@ -62,11 +62,11 @@ public class MCFrameBuffer implements IMCFrameBuffer {
* @param plugin The plugin * @param plugin The plugin
* @param direct Whether the GPU rendering is used * @param direct Whether the GPU rendering is used
*/ */
public MCFrameBuffer(IDisplay display, PluginMain plugin, boolean direct) { public MCFrameBuffer(IDisplay display, PluginMain plugin, boolean embedded, boolean direct) {
this.display = display; this.display = display;
this.plugin = plugin; this.plugin = plugin;
this.logger = plugin.getLogger(); this.logger = plugin.getLogger();
this.embedded = plugin.runEmbedded.get(); //Don't change even if the config got updated while running this.embedded = embedded; //Don't change even if the config got updated while running
this.direct = direct; this.direct = direct;
} }