package org.scape2006.web; import org.teavm.interop.Async; import org.teavm.interop.AsyncCallback; import org.teavm.jso.JSBody; import org.teavm.jso.JSByRef; import org.teavm.jso.JSFunctor; import org.teavm.jso.JSObject; import org.teavm.jso.dom.html.HTMLCanvasElement; import org.teavm.jso.typedarrays.Uint8Array; import org.teavm.jso.typedarrays.Uint8ClampedArray; /** * Web implementation of DesktopHooks for TeaVM. * Bridges audio (Web Audio + SpessaSynth), clipboard, screenshots, and 317HD backend init. */ public final class DesktopHooks { private static volatile float soundVolume = 1.0f; private static volatile float midiVolume = 0.7f; private static int lastCssWidth = -1; private static int lastCssHeight = -1; private DesktopHooks() {} @JSFunctor public interface ImageDecodeCallback extends JSObject { void onDecoded(int width, int height, Uint8ClampedArray rgba); } public static void decodeImage(Sprite sprite, byte[] data, Object component) { if (sprite == null || data == null || data.length == 0) return; Uint8Array u8 = Uint8Array.create(data.length); u8.set(data, 0); decodeImageAsync(u8, sprite); } @Async private static native void decodeImageAsync(Uint8Array data, Sprite sprite); private static void decodeImageAsync(Uint8Array data, Sprite sprite, AsyncCallback callback) { jsDecodeImage(data, (width, height, rgba) -> { if (width > 0 && height > 0 && rgba != null) { sprite.width = width; sprite.height = height; sprite.trimWidth = width; sprite.trimHeight = height; sprite.anInt1442 = 0; sprite.anInt1443 = 0; int count = width * height; sprite.pixels = new int[count]; for (int i = 0, j = 0; i < count; i++, j += 4) { sprite.pixels[i] = ((rgba.get(j) & 255) << 16) | ((rgba.get(j + 1) & 255) << 8) | (rgba.get(j + 2) & 255); } } else { RSApplet.appendLog("[DesktopHooks] decodeImage returned 0x0 (bytes=" + data.getLength() + ")"); } callback.complete(null); }); } @JSBody(params = {"bytes", "cb"}, script = "var blob = new Blob([bytes]);\n" + "var url = URL.createObjectURL(blob);\n" + "var img = new Image();\n" + "img.onload = function() {\n" + " try {\n" + " var c = document.createElement('canvas');\n" + " c.width = img.naturalWidth || img.width;\n" + " c.height = img.naturalHeight || img.height;\n" + " var ctx = c.getContext('2d');\n" + " ctx.drawImage(img, 0, 0);\n" + " var imgData = ctx.getImageData(0, 0, c.width, c.height);\n" + " URL.revokeObjectURL(url);\n" + " cb(c.width, c.height, imgData.data);\n" + " } catch (e) {\n" + " URL.revokeObjectURL(url);\n" + " cb(0, 0, null);\n" + " }\n" + "};\n" + "img.onerror = function() {\n" + " URL.revokeObjectURL(url);\n" + " cb(0, 0, null);\n" + "};\n" + "img.src = url;") private static native void jsDecodeImage(Uint8Array bytes, ImageDecodeCallback cb); public static String getClipBoard() { return ""; } public static void takeScreenshot(Game game, boolean sendMessage, String... subfolders) { ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas != null) { downloadCanvasPng(canvas, "2006scape_" + System.currentTimeMillis() + ".png"); } } public static void showErrorScreen(Game game, boolean loadingError, boolean genericLoadingError, boolean rsAlreadyLoaded) { System.err.println("[2006Scape-Web] Error screen: loading=" + loadingError + " generic=" + genericLoadingError); } public static void checkJavaVersion() {} public static void playSound(byte[] buffer, int offset, int length, int level, int delay) { if (buffer == null || length <= 0 || offset + length > buffer.length) return; float vol = soundVolume * Math.max(0f, Math.min(1f, level / 100.0f)); if (vol <= 0.0f) return; Uint8Array arr = Uint8Array.create(length); for (int i = 0; i < length; i++) { arr.set(i, (short) (buffer[offset + i] & 0xFF)); } jsPlayPcm(arr, vol, Math.max(0, delay)); } public static void setSoundVolume(int config) { // RS volume config: 0 (max) to 4 (off) soundVolume = (config >= 4) ? 0.0f : Math.max(0.0f, 1.0f - (config * 0.25f)); } public static String getParameter(String s) { return getUrlParam(s); } public static void showDocument(Object caller, String s) { if (s != null && !s.isEmpty()) { openUrl(s); } } public static void playSignlinkMidi(String location, int vol) { midiVolume = Math.max(0.0f, Math.min(1.0f, vol / 255.0f)); jsSetMidiVolume(midiVolume); } public static void playMidiBytes(byte[] midiBytes, int vol) { if (midiBytes == null || midiBytes.length == 0) return; midiVolume = Math.max(0.0f, Math.min(1.0f, vol / 255.0f)); Uint8Array arr = Uint8Array.create(midiBytes.length); arr.set(midiBytes, 0); jsPlayMidi(arr, midiVolume); } public static boolean setSignlinkVolume(int vol) { midiVolume = Math.max(0.0f, Math.min(1.0f, vol / 255.0f)); jsSetMidiVolume(midiVolume); return true; } public static void stopSignlinkMidi() { jsStopMidi(); } private static int[] modernTabPixels = null; private static boolean[] modernTabMask = null; private static boolean hudImagesRequested = false; public static void drawModernMinimap(Object graphics, int[] minimapPixels, int windowWidth) { if (minimapPixels == null || minimapPixels.length < 172 * 156) return; ensureHudAssets(); ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas == null) return; Uint8ClampedArray rgba = toRgbaOpaque(minimapPixels, 172 * 156); boolean toGpu = WebHd317Renderer.ensureOverlayReady(); HTMLCanvasElement hudC = jsDrawModernMinimap(canvas, rgba, windowWidth, !toGpu); if (toGpu && hudC != null) { WebHd317Renderer.drawHudCanvas(hudC, windowWidth - 249, 0, 249, 168); } } public static void drawModernChat(Object graphics, int[] chatContentPixels, int[] chatButtonsPixels, int windowHeight) { ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas == null) return; Uint8ClampedArray contentRgba = (chatContentPixels != null && chatContentPixels.length >= 479 * 96) ? toRgbaOpaque(chatContentPixels, 479 * 96) : null; Uint8ClampedArray buttonsRgba = (chatButtonsPixels != null && chatButtonsPixels.length >= 496 * 50) ? toRgbaOpaque(chatButtonsPixels, 496 * 50) : null; boolean toGpu = WebHd317Renderer.ensureOverlayReady(); HTMLCanvasElement hudC = jsDrawModernChat(canvas, contentRgba, buttonsRgba, windowHeight, !toGpu); if (toGpu && hudC != null) { WebHd317Renderer.drawHudCanvas(hudC, 0, windowHeight - 165, 519, 165); } } public static void drawModernTab(Object graphics, int[] topTabPixels, int[] leftPillarTopPixels, int[] leftPillarMidPixels, int[] leftPillarBotPixels, int[] tabBodyPixels, int[] rightPillarPixels, int[] bottomTabPixels, int windowWidth, int windowHeight) { ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas == null) return; if (modernTabPixels == null) { modernTabPixels = new int[249 * 343]; modernTabMask = new boolean[249 * 343]; for (int y = 0; y < 343; y++) { for (int x = 0; x < 249; x++) { modernTabMask[y * 249 + x] = ModernLayout.isInsideTabArea(x, y); } } } if (topTabPixels != null && topTabPixels.length >= 249 * 45) { System.arraycopy(topTabPixels, 0, modernTabPixels, 0, 249 * 45); } if (leftPillarTopPixels != null && leftPillarTopPixels.length >= 37 * 133) { for (int y = 0; y < 133; y++) { System.arraycopy(leftPillarTopPixels, y * 37, modernTabPixels, (45 + y) * 249, 37); } } if (leftPillarMidPixels != null && leftPillarMidPixels.length >= 553 * 19) { for (int y = 0; y < 19; y++) { System.arraycopy(leftPillarMidPixels, y * 553 + 516, modernTabPixels, (178 + y) * 249, 37); } } if (leftPillarBotPixels != null && leftPillarBotPixels.length >= 57 * 109) { for (int y = 0; y < 109; y++) { System.arraycopy(leftPillarBotPixels, y * 57 + 20, modernTabPixels, (197 + y) * 249, 37); } } if (tabBodyPixels != null && tabBodyPixels.length >= 190 * 261) { for (int y = 0; y < 261; y++) { System.arraycopy(tabBodyPixels, y * 190, modernTabPixels, (45 + y) * 249 + 37, 190); } } if (rightPillarPixels != null && rightPillarPixels.length >= 22 * 261) { for (int y = 0; y < 261; y++) { System.arraycopy(rightPillarPixels, y * 22, modernTabPixels, (45 + y) * 249 + 227, 22); } } if (bottomTabPixels != null && bottomTabPixels.length >= 269 * 37) { for (int y = 0; y < 37; y++) { System.arraycopy(bottomTabPixels, y * 269 + 20, modernTabPixels, (306 + y) * 249, 249); } } Uint8ClampedArray rgba = Uint8ClampedArray.create(249 * 343 * 4); int total = 249 * 343; for (int i = 0, j = 0; i < total; i++, j += 4) { if (modernTabMask[i]) { int c = modernTabPixels[i]; rgba.set(j, (short) ((c >> 16) & 255)); rgba.set(j + 1, (short) ((c >> 8) & 255)); rgba.set(j + 2, (short) (c & 255)); rgba.set(j + 3, (short) 255); } else { rgba.set(j + 3, (short) 0); } } boolean toGpu = WebHd317Renderer.ensureOverlayReady(); HTMLCanvasElement hudC = jsDrawModernTab(canvas, rgba, windowWidth - 249, windowHeight - 343, !toGpu); if (toGpu && hudC != null) { WebHd317Renderer.drawHudCanvas(hudC, windowWidth - 249, windowHeight - 343, 249, 343); } } public static Object getRenderGraphics(Object screenGraphics) { return screenGraphics; } public static void flip(Object screenGraphics) {} public static void checkResize(Object comp) { if (ClientSettings.ANDROID) { return; } int desiredMode = jsGetDisplayMode(); if (desiredMode >= 0) { boolean wantResizable = (desiredMode != 0); if (wantResizable != ClientSettings.RESIZABLE) { ClientSettings.RESIZABLE = wantResizable; HTMLCanvasElement canvas = RSImageProducer.getCanvas(); HTMLCanvasElement glCanvas = (HTMLCanvasElement) org.teavm.jso.dom.html.HTMLDocument.current().getElementById("gl-canvas"); if (!wantResizable) { lastCssWidth = -1; lastCssHeight = -1; if (canvas != null) { canvas.setWidth(765); canvas.setHeight(503); jsSetCanvasFillStyle(canvas, 765, 503); } if (glCanvas != null) { glCanvas.setWidth(765); glCanvas.setHeight(503); } jsSyncNavbarWidth(765); if (comp instanceof Game) { ((Game) comp).switchToFixedLayout(); } else { ModernLayout.updateWindowSize(765, 503); ClientSettings.VIEWPORT_WIDTH = 512; ClientSettings.VIEWPORT_HEIGHT = 334; } return; } } } if (!ClientSettings.RESIZABLE) { return; } ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas == null) return; int rawW = Math.max(1, getStageWidth()); int rawH = Math.max(1, getStageHeight()); double minScale = Math.max(1.0, Math.max(765.0 / rawW, 503.0 / rawH)); int w = Math.max(765, (int) Math.round(rawW * minScale)); int h = Math.max(503, (int) Math.round(rawH * minScale)); boolean fullRes = "true".equalsIgnoreCase(getParameter("fullres")) || HD317Pipeline.isGpuActive(); if (!fullRes && w > 1280) { w = 1280; h = Math.max(503, (int) Math.round(rawH * (1280.0 / rawW))); } boolean sizeChanged = (w != ModernLayout.windowWidth || h != ModernLayout.windowHeight || canvas.getWidth() != w || canvas.getHeight() != h); boolean cssChanged = (rawW != lastCssWidth || rawH != lastCssHeight); if (sizeChanged) { canvas.setWidth(w); canvas.setHeight(h); HTMLCanvasElement glCanvas = (HTMLCanvasElement) org.teavm.jso.dom.html.HTMLDocument.current().getElementById("gl-canvas"); if (glCanvas != null) { glCanvas.setWidth(w); glCanvas.setHeight(h); } if (comp instanceof Game) { ((Game) comp).applyWindowSize(w, h); } else { ModernLayout.updateWindowSize(w, h); ClientSettings.VIEWPORT_WIDTH = ClientSettings.isDesktopModern() ? w : 512; ClientSettings.VIEWPORT_HEIGHT = ClientSettings.isDesktopModern() ? h : 334; } } if (sizeChanged || cssChanged) { lastCssWidth = rawW; lastCssHeight = rawH; jsSetCanvasFillStyle(canvas, rawW, rawH); jsSyncNavbarWidth(rawW); } } @JSBody(script = "return typeof window.scapeDisplayMode === 'number' ? window.scapeDisplayMode : -1;") private static native int jsGetDisplayMode(); @JSBody(params = {"canvas", "sw", "sh"}, script = "canvas.style.width = sw + 'px';\n" + "canvas.style.height = sh + 'px';") private static native void jsSetCanvasFillStyle(HTMLCanvasElement canvas, int sw, int sh); @JSBody(params = {"sw"}, script = "var nav = document.getElementById('rs-navbar');\n" + "if (nav) nav.style.width = sw + 'px';") private static native void jsSyncNavbarWidth(int sw); public static void updateLoginTile(int[] tilePixels, int fixedX, int fixedY, int width, int height) { if (tilePixels == null || tilePixels.length < width * height) return; Uint8ClampedArray rgba = toRgbaOpaque(tilePixels, width * height); jsUpdateLoginTile(rgba, fixedX, fixedY, width, height); } public static void drawScaledLoginTile(Object screenGraphics, int[] tilePixels, int fixedX, int fixedY, int width, int height) { updateLoginTile(tilePixels, fixedX, fixedY, width, height); if (fixedX == 0 && fixedY == 0 && width == 128 && height == 265) { return; } redrawScaledLoginScreen(screenGraphics); } public static void redrawScaledLoginScreen(Object screenGraphics) { ensureHudAssets(); HTMLCanvasElement canvas = RSImageProducer.getCanvas(); if (canvas == null) return; int w = ModernLayout.windowWidth; int h = ModernLayout.windowHeight; if (w <= 0 || h <= 0) return; int[] bounds = ModernLayout.getScaledScreenBounds(w, h); jsRedrawScaledLogin(canvas, w, h, bounds[0], bounds[1], bounds[2], bounds[3]); } private static Uint8ClampedArray toRgbaOpaque(int[] pixels, int count) { Uint8ClampedArray rgba = Uint8ClampedArray.create(count * 4); jsCopyRgbToRgba(pixels, rgba, count); return rgba; } @JSBody(params = {"src", "dstBytes", "count"}, script = "var dst = new Uint32Array(dstBytes.buffer);\n" + "for (var i = 0; i < count; i++) {\n" + " var c = src[i];\n" + " dst[i] = -16777216 | ((c & 255) << 16) | (c & 65280) | ((c >> 16) & 255);\n" + "}") private static native void jsCopyRgbToRgba(@JSByRef int[] src, Uint8ClampedArray dstBytes, int count); private static void ensureHudAssets() { if (!hudImagesRequested) { hudImagesRequested = true; jsInitHudAssets(); } } @JSBody(script = "if (typeof window.scapeStageW === 'number' && window.scapeStageW > 0) return window.scapeStageW;\n" + "var s = document.getElementById('stage');\n" + "var w = s ? s.clientWidth : window.innerWidth;\n" + "if (window.visualViewport) w = Math.min(w, Math.floor(window.visualViewport.width));\n" + "return w;") private static native int getStageWidth(); @JSBody(script = "if (typeof window.scapeStageH === 'number' && window.scapeStageH > 0) return window.scapeStageH;\n" + "var s = document.getElementById('stage');\n" + "var h = s ? s.clientHeight : window.innerHeight;\n" + "if (window.visualViewport) h = Math.min(h, Math.floor(window.visualViewport.height));\n" + "var nav = document.getElementById('rs-navbar');\n" + "if (nav && nav.offsetHeight > 0) h -= 25;\n" + "return h;") private static native int getStageHeight(); @JSBody(script = "if (window.scapeHud) return;\n" + "function loadImg(src) { var img = new Image(); img.src = src; return img; }\n" + "var lc = document.createElement('canvas'); lc.width = 765; lc.height = 503;\n" + "var mc = document.createElement('canvas'); mc.width = 249; mc.height = 168;\n" + "var mContent = document.createElement('canvas'); mContent.width = 172; mContent.height = 156;\n" + "var cc = document.createElement('canvas'); cc.width = 519; cc.height = 165;\n" + "var cContent = document.createElement('canvas'); cContent.width = 479; cContent.height = 96;\n" + "var cButtons = document.createElement('canvas'); cButtons.width = 496; cButtons.height = 50;\n" + "var tc = document.createElement('canvas'); tc.width = 249; tc.height = 343;\n" + "window.scapeHud = {\n" + " minimapBezel: loadImg('hud_minimap_bezel.png'),\n" + " minimapHole: loadImg('hud_minimap_hole.png'),\n" + " chatFrame: loadImg('hud_chat_frame.png'),\n" + " loginCanvas: lc, loginCtx: lc.getContext('2d'),\n" + " mc: mc, mcCtx: mc.getContext('2d'), mContent: mContent, mContentCtx: mContent.getContext('2d'),\n" + " cc: cc, ccCtx: cc.getContext('2d'), cContent: cContent, cContentCtx: cContent.getContext('2d'), cButtons: cButtons, cButtonsCtx: cButtons.getContext('2d'),\n" + " tc: tc, tcCtx: tc.getContext('2d')\n" + "};") private static native void jsInitHudAssets(); @JSBody(params = {"canvas", "rgba", "winW", "draw2d"}, script = "var h = window.scapeHud; if (!h || !h.minimapBezel.complete || !h.minimapHole.complete) return null;\n" + "var id = h.mContentCtx.createImageData(172, 156); id.data.set(rgba); h.mContentCtx.putImageData(id, 0, 0);\n" + "var g = h.mcCtx; g.clearRect(0, 0, 249, 168);\n" + "g.globalCompositeOperation = 'source-over'; g.drawImage(h.mContent, 34, 4);\n" + "g.globalCompositeOperation = 'destination-in'; g.drawImage(h.minimapHole, 0, 0);\n" + "g.globalCompositeOperation = 'source-over'; g.drawImage(h.minimapBezel, 0, 0);\n" + "if (draw2d) canvas.getContext('2d').drawImage(h.mc, winW - 249, 0);\n" + "return h.mc;") private static native HTMLCanvasElement jsDrawModernMinimap(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int winW, boolean draw2d); @JSBody(params = {"canvas", "contentRgba", "buttonsRgba", "winH", "draw2d"}, script = "var h = window.scapeHud; if (!h || !h.chatFrame.complete) return null;\n" + "var g = h.ccCtx; g.clearRect(0, 0, 519, 165);\n" + "g.globalCompositeOperation = 'source-over'; g.drawImage(h.chatFrame, 0, 0);\n" + "if (contentRgba) { var id1 = h.cContentCtx.createImageData(479, 96); id1.data.set(contentRgba); h.cContentCtx.putImageData(id1, 0, 0); g.drawImage(h.cContent, 17, 19); }\n" + "if (buttonsRgba) { var id2 = h.cButtonsCtx.createImageData(496, 50); id2.data.set(buttonsRgba); h.cButtonsCtx.putImageData(id2, 0, 0); g.drawImage(h.cButtons, 0, 115); }\n" + "if (draw2d) canvas.getContext('2d').drawImage(h.cc, 0, winH - 165);\n" + "return h.cc;") private static native HTMLCanvasElement jsDrawModernChat(HTMLCanvasElement canvas, Uint8ClampedArray contentRgba, Uint8ClampedArray buttonsRgba, int winH, boolean draw2d); @JSBody(params = {"canvas", "rgba", "x", "y", "draw2d"}, script = "var hh = window.scapeHud; if (!hh) return null;\n" + "var id = hh.tcCtx.createImageData(249, 343); id.data.set(rgba); hh.tcCtx.putImageData(id, 0, 0);\n" + "if (draw2d) canvas.getContext('2d').drawImage(hh.tc, x, y);\n" + "return hh.tc;") private static native HTMLCanvasElement jsDrawModernTab(HTMLCanvasElement canvas, Uint8ClampedArray rgba, int x, int y, boolean draw2d); @JSBody(params = {"rgba", "fx", "fy", "w", "h"}, script = "if (!window.scapeLoginBuf) {\n" + " var lc = document.createElement('canvas'); lc.width = 765; lc.height = 503;\n" + " window.scapeLoginBuf = { c: lc, ctx: lc.getContext('2d') };\n" + "}\n" + "var b = window.scapeLoginBuf;\n" + "var id = b.ctx.createImageData(w, h); id.data.set(rgba); b.ctx.putImageData(id, fx, fy);") private static native void jsUpdateLoginTile(Uint8ClampedArray rgba, int fx, int fy, int w, int h); @JSBody(params = {"canvas", "winW", "winH", "dx", "dy", "dw", "dh"}, script = "var b = window.scapeLoginBuf; if (!b) return;\n" + "var ctx = canvas.getContext('2d');\n" + "ctx.fillStyle = '#000'; ctx.fillRect(0, 0, winW, winH);\n" + "ctx.imageSmoothingEnabled = true;\n" + "ctx.drawImage(b.c, dx, dy, dw, dh);") private static native void jsRedrawScaledLogin(HTMLCanvasElement canvas, int winW, int winH, int dx, int dy, int dw, int dh); public static void initHdBackend() { if (HD317Pipeline.backend == null) { HD317Pipeline.backend = new WebHd317Renderer(); } } @JSBody(params = {"arr", "vol", "delayMs"}, script = "if (window.scapeWebAudio && window.scapeWebAudio.playPcm) window.scapeWebAudio.playPcm(arr, vol, delayMs);") private static native void jsPlayPcm(Uint8Array arr, float vol, int delayMs); @JSBody(params = {"arr", "vol"}, script = "if (window.scapeWebAudio && window.scapeWebAudio.playMidi) window.scapeWebAudio.playMidi(arr, vol);") private static native void jsPlayMidi(Uint8Array arr, float vol); @JSBody(params = {"vol"}, script = "if (window.scapeWebAudio && window.scapeWebAudio.setMidiVolume) window.scapeWebAudio.setMidiVolume(vol);") private static native void jsSetMidiVolume(float vol); @JSBody(script = "if (window.scapeWebAudio && window.scapeWebAudio.stopMidi) window.scapeWebAudio.stopMidi();") private static native void jsStopMidi(); @JSBody(params = {"canvas", "filename"}, script = "var a = document.createElement('a');\n" + "a.download = filename;\n" + "a.href = canvas.toDataURL('image/png');\n" + "a.click();") private static native void downloadCanvasPng(HTMLCanvasElement canvas, String filename); @JSBody(params = {"name"}, script = "var p = new URLSearchParams(window.location.search);\n" + "return p.get(name) || '';") private static native String getUrlParam(String name); @JSBody(params = {"url"}, script = "window.open(url, '_blank');") private static native void openUrl(String url); @JSFunctor public interface HttpFetchCallback extends JSObject { void onComplete(int status, String body); } public static String httpGet(String url) throws java.io.IOException { if (url == null || url.contains(".git/info/refs")) { throw new java.io.IOException("Skipped in browser"); } String res = fetchTextAsync(url); if (res == null) { throw new java.io.IOException("Network error"); } if (res.startsWith("__HTTP_ERR__:")) { throw new java.io.IOException(res.substring("__HTTP_ERR__:".length())); } return res; } @Async private static native String fetchTextAsync(String url); private static void fetchTextAsync(String url, AsyncCallback callback) { jsFetchText(url, (status, body) -> { if (status >= 200 && status < 300) { callback.complete(body != null ? body : ""); } else { callback.complete("__HTTP_ERR__:HTTP " + status); } }); } @JSBody(params = {"url", "cb"}, script = "fetch(url).then(function(r) {\n" + " if (!r.ok) { cb(r.status, ''); return; }\n" + " r.text().then(function(t) { cb(r.status, t); }, function() { cb(-1, ''); });\n" + "}).catch(function() { cb(-1, ''); });") private static native void jsFetchText(String url, HttpFetchCallback cb); public static boolean saveExternalScript(String fileName, String content) { if (fileName == null || content == null) return false; jsSaveScript(fileName, content); return true; } public static boolean deleteExternalScript(String fileName) { if (fileName == null) return false; return jsDeleteScript(fileName); } public static String loadPluginConfig() { return jsLoadPluginConfig(); } public static boolean savePluginConfig(String content) { if (content == null) return false; jsSavePluginConfig(content); jsSyncWebOptionHdFromPluginConfig(content); return true; } @JSBody(script = "return window.localStorage ? window.localStorage.getItem('2006scape_plugin_settings') : null;") private static native String jsLoadPluginConfig(); @JSBody(params = {"content"}, script = "if (window.localStorage) window.localStorage.setItem('2006scape_plugin_settings', content);") private static native void jsSavePluginConfig(String content); @JSBody(params = {"content"}, script = "try {\n" + " if (!window.localStorage || !content) return;\n" + " var m = content.match(/(?:^|\\r?\\n)plugin\\.317_HD\\.enabled\\s*=\\s*(true|false)/i);\n" + " if (!m) return;\n" + " var hdEnabled = m[1].toLowerCase() === 'true';\n" + " var raw = window.localStorage.getItem('2006scape_web_options');\n" + " var opts = raw ? JSON.parse(raw) : {};\n" + " if (!opts || typeof opts !== 'object') opts = {};\n" + " if (opts.hd !== hdEnabled) {\n" + " opts.hd = hdEnabled;\n" + " window.localStorage.setItem('2006scape_web_options', JSON.stringify(opts));\n" + " }\n" + "} catch (_) {}") private static native void jsSyncWebOptionHdFromPluginConfig(String content); public static void yieldUi() { try { Thread.sleep(1L); } catch (InterruptedException ignored) {} } public static Object boxReflectValue(Object v, Class declaredType) { if (v == null) return null; if (v instanceof Number || v instanceof Boolean || v instanceof String) { return v; } if (org.teavm.jso.impl.JSWrapper.isJava(v)) { return v; } JSObject js = org.teavm.jso.impl.JSWrapper.maybeUnwrap(v); if (jsIsUndefined(js)) { return null; } if (jsIsNumber(js)) { double d = jsAsDouble(js); if (declaredType == long.class || declaredType == Long.class) { return Long.valueOf((long) d); } if (declaredType == double.class || declaredType == Double.class) { return Double.valueOf(d); } if (declaredType == float.class || declaredType == Float.class) { return Float.valueOf((float) d); } if (d == Math.floor(d) && d >= Integer.MIN_VALUE && d <= Integer.MAX_VALUE) { return Integer.valueOf((int) d); } return Double.valueOf(d); } if (jsIsBoolean(js)) { return Boolean.valueOf(jsAsBoolean(js)); } if (jsIsString(js)) { return jsAsString(js); } return v; } @JSBody(params = {"v"}, script = "return v === undefined || v === null;") private static native boolean jsIsUndefined(JSObject v); @JSBody(params = {"v"}, script = "return typeof v === 'number';") private static native boolean jsIsNumber(JSObject v); @JSBody(params = {"v"}, script = "return v;") private static native double jsAsDouble(JSObject v); @JSBody(params = {"v"}, script = "return typeof v === 'boolean';") private static native boolean jsIsBoolean(JSObject v); @JSBody(params = {"v"}, script = "return !!v;") private static native boolean jsAsBoolean(JSObject v); @JSBody(params = {"v"}, script = "return typeof v === 'string';") private static native boolean jsIsString(JSObject v); @JSBody(params = {"v"}, script = "return v;") private static native String jsAsString(JSObject v); @JSBody(params = {"name", "content"}, script = "if (window.localStorage) window.localStorage.setItem('2006scape_script_' + name, content);") private static native void jsSaveScript(String name, String content); @JSBody(params = {"name"}, script = "if (!window.localStorage) return false;\n" + "var k = '2006scape_script_' + name;\n" + "if (window.localStorage.getItem(k) === null) return false;\n" + "window.localStorage.removeItem(k);\n" + "return true;") private static native boolean jsDeleteScript(String name); }