Rendering und Lua working, need to work on request method via custom URL implementation
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
package org.openautonomousconnection.webclient.settings;
|
||||
|
||||
import javafx.concurrent.Worker;
|
||||
import javafx.scene.web.WebEngine;
|
||||
import javafx.scene.web.WebView;
|
||||
import org.luaj.vm2.Globals;
|
||||
import org.openautonomousconnection.luascript.fx.FxDomHost;
|
||||
import org.openautonomousconnection.luascript.fx.FxEventHost;
|
||||
import org.openautonomousconnection.luascript.fx.FxWebViewResourceHost;
|
||||
import org.openautonomousconnection.luascript.hosts.HostServices;
|
||||
import org.openautonomousconnection.luascript.runtime.LuaRuntime;
|
||||
import org.openautonomousconnection.luascript.security.LuaExecutionPolicy;
|
||||
import org.openautonomousconnection.luascript.utils.LuaGlobalsFactory;
|
||||
import org.openautonomousconnection.webclient.lua.WebLogger;
|
||||
import org.openautonomousconnection.webclient.lua.hosts.ConsoleHostImpl;
|
||||
import org.openautonomousconnection.webclient.lua.hosts.UiHostImpl;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* JavaFX WebView integration entry point for LuaScript (no JavaScript).
|
||||
*
|
||||
* <p>Hard rule: every HTML script tag is treated as Lua.</p>
|
||||
*/
|
||||
public final class FxEngine implements AutoCloseable {
|
||||
|
||||
private final WebEngine engine;
|
||||
private final WebView webView;
|
||||
private final LuaExecutionPolicy policy;
|
||||
private final WebLogger logger;
|
||||
|
||||
private final AtomicBoolean bootstrapped = new AtomicBoolean(false);
|
||||
|
||||
private LuaRuntime runtime;
|
||||
|
||||
/**
|
||||
* Creates an integration engine with default UI execution policy.
|
||||
*
|
||||
* @param engine web engine
|
||||
* @param webView web view
|
||||
*/
|
||||
public FxEngine(WebEngine engine, WebView webView, WebLogger logger) {
|
||||
this(engine, webView, LuaExecutionPolicy.uiDefault(), logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an integration engine with a custom execution policy.
|
||||
*
|
||||
* @param engine web engine
|
||||
* @param webView web view
|
||||
* @param policy execution policy
|
||||
*/
|
||||
public FxEngine(WebEngine engine, WebView webView, LuaExecutionPolicy policy, WebLogger logger) {
|
||||
this.engine = Objects.requireNonNull(engine, "engine");
|
||||
this.webView = webView;
|
||||
this.policy = Objects.requireNonNull(policy, "policy");
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a load hook that bootstraps Lua when a page finished loading.
|
||||
*/
|
||||
public void install() {
|
||||
engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
|
||||
if (newState == Worker.State.SUCCEEDED) {
|
||||
bootstrapped.set(false);
|
||||
bootstrap();
|
||||
} else if (newState == Worker.State.CANCELLED || newState == Worker.State.FAILED) {
|
||||
bootstrapped.set(false);
|
||||
closeRuntimeQuietly();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstraps Lua for the currently loaded document.
|
||||
*/
|
||||
public void bootstrap() {
|
||||
if (!bootstrapped.compareAndSet(false, true)) return;
|
||||
|
||||
closeRuntimeQuietly();
|
||||
|
||||
// DOM host must exist before event/UI tables, and must ensure stable ids.
|
||||
FxDomHost dom = new FxDomHost(engine);
|
||||
dom.ensureAllElementsHaveId();
|
||||
|
||||
// Create per-page globals; harden sandbox in production.
|
||||
Globals globals = LuaGlobalsFactory.create(
|
||||
new LuaGlobalsFactory.Options()
|
||||
.enableDebug(false)
|
||||
.sandbox(true)
|
||||
);
|
||||
|
||||
// Create runtime first (router lives inside it).
|
||||
ConsoleHostImpl console = new ConsoleHostImpl(logger);
|
||||
UiHostImpl uiHost = new UiHostImpl(engine, webView, dom);
|
||||
FxWebViewResourceHost resourceHost = new FxWebViewResourceHost(engine);
|
||||
|
||||
// runtime depends on services; events depends on runtime router.
|
||||
// We'll create eventHost after runtime, then build HostServices with it.
|
||||
LuaRuntime rt = new LuaRuntime(globals, new HostServices.Default(uiHost, dom, null, resourceHost, console), policy);
|
||||
|
||||
FxEventHost eventHost = new FxEventHost(dom, rt.eventRouter());
|
||||
|
||||
// Rebuild services including eventHost and reinstall tables.
|
||||
HostServices services = new HostServices.Default(uiHost, dom, eventHost, resourceHost, console);
|
||||
|
||||
// Replace runtime with correct services (clean and deterministic).
|
||||
rt.close();
|
||||
rt = new LuaRuntime(globals, services, policy);
|
||||
|
||||
rt.installStdTables(true);
|
||||
rt.bootstrapFromDom();
|
||||
|
||||
this.runtime = rt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns active runtime or null if not bootstrapped.
|
||||
*
|
||||
* @return runtime or null
|
||||
*/
|
||||
public LuaRuntime runtimeOrNull() {
|
||||
return runtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closeRuntimeQuietly();
|
||||
}
|
||||
|
||||
private void closeRuntimeQuietly() {
|
||||
LuaRuntime rt = this.runtime;
|
||||
this.runtime = null;
|
||||
if (rt != null) {
|
||||
try {
|
||||
rt.close();
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort shutdown.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.openautonomousconnection.webclient.settings;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class INSList {
|
||||
|
||||
public static String DEFAULT_INS = "open-autonomous-connection.org";
|
||||
public static int DEFAULT_PORT = 1026;
|
||||
|
||||
private static HashMap<String, Integer> insList = new HashMap<>();
|
||||
|
||||
public static void registerINS(String host, int tcpPort) {
|
||||
insList.put(host, tcpPort);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user