import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.image.BufferedImage; import java.awt.image.PixelGrabber; import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiSystem; import javax.sound.midi.Sequence; import javax.sound.midi.Sequencer; import javax.sound.midi.ShortMessage; import javax.sound.midi.Synthesizer; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; public final class DesktopHooks { private DesktopHooks() {} public static void initHdBackend() { if (!ClientSettings.ANDROID && HD317Pipeline.backend == null) { HD317Pipeline.backend = new DesktopHd317Renderer(); } } public static void decodeImage(Sprite sprite, byte[] abyte0, Object component) { try { Component comp = (component instanceof Component) ? (Component) component : null; Image image = Toolkit.getDefaultToolkit().createImage(abyte0); if (comp != null) { MediaTracker mediatracker = new MediaTracker(comp); mediatracker.addImage(image, 0); mediatracker.waitForAll(); sprite.width = image.getWidth(comp); sprite.height = image.getHeight(comp); } else { Canvas canvas = new Canvas(); MediaTracker mediatracker = new MediaTracker(canvas); mediatracker.addImage(image, 0); mediatracker.waitForAll(); sprite.width = image.getWidth(canvas); sprite.height = image.getHeight(canvas); } sprite.trimWidth = sprite.width; sprite.trimHeight = sprite.height; sprite.anInt1442 = 0; sprite.anInt1443 = 0; sprite.pixels = new int[sprite.width * sprite.height]; PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, sprite.width, sprite.height, sprite.pixels, 0, sprite.width); pixelgrabber.grabPixels(); } catch (Exception _ex) { System.out.println("Error converting jpg"); } } public static String getClipBoard() { String myString = ""; try { myString = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); } catch (Exception e) { e.printStackTrace(); } String output = ""; for (int i = 0; i < myString.length(); i++) { int j = (int) myString.charAt(i); if (j >= 32 && j <= 122) { output += (char) j; } } return output; } public static void takeScreenshot(Game game, boolean sendMessage, String... subfolders) { try { // getGameComponent() is the applet itself, so capture the frame that contains it Window window = SwingUtilities.getWindowAncestor(game.getGameComponent()); if (window == null) { return; } Point point = window.getLocationOnScreen(); int x = (int) point.getX(); int y = (int) point.getY(); int w = window.getWidth(); int h = window.getHeight(); Robot robot = new Robot(window.getGraphicsConfiguration().getDevice()); Rectangle captureSize = new Rectangle(x, y, w, h); BufferedImage bufferedimage = robot.createScreenCapture(captureSize); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss"); String dateTime = dateFormat.format(new Date()); String fileExtension = game.myUsername != null && !game.myUsername.isEmpty() ? game.myUsername : ClientSettings.SERVER_NAME; String subfolderPath = String.join(File.separator, subfolders); if (!subfolderPath.isEmpty()) { subfolderPath += File.separator; } String screenshotDir = System.getProperty("user.home") + File.separatorChar + ClientSettings.SERVER_NAME + File.separatorChar + "screenshots" + File.separatorChar + subfolderPath; File dir = new File(screenshotDir); if (!dir.exists()) { dir.mkdirs(); } File file = new File(screenshotDir, fileExtension + "_" + dateTime + ".png"); if (!file.exists()) { ImageIO.write(bufferedimage, "png", file); if (sendMessage) { game.pushMessage("A picture has been saved in your screenshots folder.", 0, ""); } } } catch (Exception e) { e.printStackTrace(); } } public static void showErrorScreen(Game game, boolean loadingError, boolean genericLoadingError, boolean rsAlreadyLoaded) { Component comp = game.getGameComponent(); if (comp == null) return; Graphics g = comp.getGraphics(); if (g == null) return; g.setColor(Color.black); g.fillRect(0, 0, 765, 503); game.method4(1); if (loadingError) { g.setFont(new Font("Helvetica", 1, 16)); g.setColor(Color.yellow); int k = 35; g.drawString("Sorry, an error has occured whilst loading " + ClientSettings.SERVER_NAME + "", 30, k); k += 50; g.setColor(Color.white); g.drawString("To fix this try the following (in order):", 30, k); k += 50; g.setColor(Color.white); g.setFont(new Font("Helvetica", 1, 12)); g.drawString("1: Try closing ALL open web-browser windows, and reloading", 30, k); k += 30; g.drawString("2: Try clearing your web-browsers cache from tools->internet options", 30, k); k += 30; g.drawString("3: Try using a different game-world", 30, k); k += 30; g.drawString("4: Try rebooting your computer", 30, k); k += 30; g.drawString("5: Try selecting a different version of Java from the play-game menu", 30, k); } if (genericLoadingError) { g.setFont(new Font("Helvetica", 1, 20)); g.setColor(Color.white); g.drawString("Error - unable to load game!", 50, 50); g.drawString("To play " + ClientSettings.SERVER_NAME + " make sure you play from", 50, 100); g.drawString("" + ClientSettings.SERVER_WEBSITE + "", 50, 150); } if (rsAlreadyLoaded) { g.setColor(Color.yellow); int l = 35; g.drawString("Error a copy of " + ClientSettings.SERVER_NAME + " already appears to be loaded", 30, l); l += 50; g.setColor(Color.white); g.drawString("To fix this try the following (in order):", 30, l); l += 50; g.setColor(Color.white); g.setFont(new Font("Helvetica", 1, 12)); g.drawString("1: Try closing ALL open web-browser windows, and reloading", 30, l); l += 30; g.drawString("2: Try rebooting your computer, and reloading", 30, l); } } public static void checkJavaVersion() { if (!System.getProperty("sun.arch.data.model", "").contains("64")) { JOptionPane.showMessageDialog(null, "You're running 32-bit java. This will definitely cause problems.\nYou can get the right Java 8 at AdoptOpenJDK.net", "You're running 32-bit Java!", JOptionPane.INFORMATION_MESSAGE); System.out.println("Please upgrade to 64-bit java to avoid problems! (AdoptOpenJDK.net)"); } try { if (Double.parseDouble(System.getProperty("java.specification.version", "1.8")) >= 1.9) { JOptionPane.showMessageDialog(null, "You're not running Java 8. If you're using Parabot, this will cause problems!\nYou can get Java 8 from AdoptOpenJDK.net", "You're not running Java 8!", JOptionPane.INFORMATION_MESSAGE); System.out.println("Please downgrade to Java 8 to avoid problems! (AdoptOpenJDK.net)"); } } catch (Exception ignored) {} } public static void playSound(byte[] buffer, int offset, int length, int level, int delay) { new SoundPlayer(new ByteArrayInputStream(buffer, offset, length), level, delay); } public static void setSoundVolume(int config) { SoundPlayer.setVolume(config); } public static String getParameter(String s) { if (Signlink.mainapp instanceof java.applet.Applet) { return ((java.applet.Applet) Signlink.mainapp).getParameter(s); } return ""; } public static void showDocument(Object caller, String s) { try { if (caller instanceof java.applet.Applet) { java.applet.Applet a = (java.applet.Applet) caller; java.applet.AppletContext ctx = a.getAppletContext(); if (ctx != null && caller instanceof Game) { ctx.showDocument(new URL(((Game) caller).getCodeBase(), "loaderror_" + s + ".html")); } } } catch (Exception ignored) {} } // Midi support for Signlink public static Sequencer music = null; public static Synthesizer synthesizer = null; public static Sequence sequence = null; public static synchronized void playSignlinkMidi(String location, int midiVolume) { stopSignlinkMidi(); File midiFile = new File(location); try { sequence = MidiSystem.getSequence(midiFile); music = MidiSystem.getSequencer(); music.open(); music.setSequence(sequence); } catch (Exception e) { System.err.println("Problem loading MIDI file."); e.printStackTrace(); return; } if (music instanceof Synthesizer) { synthesizer = (Synthesizer) music; } else { try { synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); if (synthesizer.getDefaultSoundbank() == null) { music.getTransmitter().setReceiver(MidiSystem.getReceiver()); } else { music.getTransmitter().setReceiver(synthesizer.getReceiver()); } } catch (Exception e) { e.printStackTrace(); return; } } music.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); music.start(); setSignlinkVolume(midiVolume); } public static boolean setSignlinkVolume(int midiVolume) { if (synthesizer == null) { return false; } int CHANGE_VOLUME = 7; if (synthesizer.getDefaultSoundbank() == null) { try (javax.sound.midi.Receiver receiver = MidiSystem.getReceiver()) { ShortMessage volumeMessage = new ShortMessage(); for (int i = 0; i < 16; i++) { volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, CHANGE_VOLUME, midiVolume); receiver.send(volumeMessage, -1); volumeMessage.setMessage(ShortMessage.CONTROL_CHANGE, i, 39, midiVolume); receiver.send(volumeMessage, -1); } } catch (Exception e) { e.printStackTrace(); return false; } } else { try { MidiChannel[] channels = synthesizer.getChannels(); for (int c = 0; channels != null && c < channels.length; c++) { channels[c].controlChange(CHANGE_VOLUME, midiVolume); channels[c].controlChange(39, midiVolume); } } catch (Exception e) { e.printStackTrace(); return false; } } return true; } public static synchronized void stopSignlinkMidi() { if (music != null) { try { if (music.isRunning()) { music.stop(); } music.close(); } catch (Exception ignored) {} music = null; } if (synthesizer != null) { try { synthesizer.close(); } catch (Exception ignored) {} synthesizer = null; } sequence = null; } public static void showKeyboard() {} public static void hideKeyboard() {} private static BufferedImage modernMinimapBezel = null; private static BufferedImage modernMinimapHole = null; private static BufferedImage modernMinimapComposite = null; private static BufferedImage modernMinimapContent = null; private static BufferedImage modernChatFrame = null; private static BufferedImage modernChatComposite = null; private static BufferedImage modernChatContent = null; private static BufferedImage modernChatButtons = null; private static BufferedImage modernTabComposite = null; private static int[] modernTabPixels = null; private static boolean[] modernTabMask = null; private static final Object loginLock = new Object(); private static BufferedImage loginScreenBuffer = null; private static BufferedImage offscreenBuffer = null; private static Graphics2D offscreenGraphics = null; public static void updateLoginTile(int[] tilePixels, int fixedX, int fixedY, int width, int height) { if (tilePixels == null || tilePixels.length < width * height) { return; } synchronized (loginLock) { if (loginScreenBuffer == null) { loginScreenBuffer = new BufferedImage(765, 503, BufferedImage.TYPE_INT_RGB); } loginScreenBuffer.setRGB(fixedX, fixedY, width, height, tilePixels, 0, width); } } public static void drawScaledLoginTile(Graphics screenGraphics, int[] tilePixels, int fixedX, int fixedY, int width, int height) { updateLoginTile(tilePixels, fixedX, fixedY, width, height); // Only flip once after tile 1111 (right torch). Tile 1110 (left torch) updates buffer only. if (fixedX == 0 && fixedY == 0 && width == 128 && height == 265) { return; } redrawScaledLoginScreen(screenGraphics); } public static void redrawScaledLoginScreen(Graphics screenGraphics) { if (screenGraphics == null) { return; } synchronized (loginLock) { if (loginScreenBuffer == null) { return; } int w = ModernLayout.windowWidth; int h = ModernLayout.windowHeight; if (w <= 0 || h <= 0) { return; } if (offscreenBuffer == null || offscreenBuffer.getWidth() != w || offscreenBuffer.getHeight() != h) { if (offscreenGraphics != null) { offscreenGraphics.dispose(); } offscreenBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); offscreenGraphics = offscreenBuffer.createGraphics(); } Graphics2D g2d = offscreenGraphics; int[] bounds = ModernLayout.getScaledScreenBounds(w, h); g2d.setColor(Color.BLACK); g2d.fillRect(0, 0, w, h); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.drawImage(loginScreenBuffer, bounds[0], bounds[1], bounds[2], bounds[3], null); screenGraphics.drawImage(offscreenBuffer, 0, 0, null); } } public static void checkResize(Component comp) { if (ClientSettings.ANDROID || !ClientSettings.RESIZABLE || comp == null) { return; } int w = comp.getWidth(); int h = comp.getHeight(); if (w > 0 && h > 0 && (w != ModernLayout.windowWidth || h != ModernLayout.windowHeight)) { ModernLayout.updateWindowSize(w, h); ClientSettings.VIEWPORT_WIDTH = ClientSettings.isDesktopModern() ? w : 512; ClientSettings.VIEWPORT_HEIGHT = ClientSettings.isDesktopModern() ? h : 334; } } public static Graphics getRenderGraphics(Graphics screenGraphics) { if (!ClientSettings.isDesktopModern()) { return screenGraphics; } int w = ModernLayout.windowWidth; int h = ModernLayout.windowHeight; if (offscreenBuffer == null || offscreenBuffer.getWidth() != w || offscreenBuffer.getHeight() != h) { if (offscreenGraphics != null) { offscreenGraphics.dispose(); } offscreenBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); offscreenGraphics = offscreenBuffer.createGraphics(); } return offscreenGraphics; } public static void flip(Graphics screenGraphics) { if (!ClientSettings.isDesktopModern() || screenGraphics == null || offscreenBuffer == null) { return; } screenGraphics.drawImage(offscreenBuffer, 0, 0, null); } public static void drawModernMinimap(Object graphics, int[] minimapPixels, int windowWidth) { if (!(graphics instanceof Graphics) || minimapPixels == null) { return; } Graphics g = (Graphics) graphics; if (modernMinimapBezel == null) { try { modernMinimapBezel = ImageIO.read(DesktopHooks.class.getResource("/hud_minimap_bezel.png")); modernMinimapHole = ImageIO.read(DesktopHooks.class.getResource("/hud_minimap_hole.png")); modernMinimapComposite = new BufferedImage(249, 168, BufferedImage.TYPE_INT_ARGB); modernMinimapContent = new BufferedImage(172, 156, BufferedImage.TYPE_INT_RGB); } catch (Exception e) { e.printStackTrace(); } } if (modernMinimapBezel == null || modernMinimapHole == null) { return; } modernMinimapContent.setRGB(0, 0, 172, 156, minimapPixels, 0, 172); Graphics2D g2d = modernMinimapComposite.createGraphics(); g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, 249, 168); g2d.setComposite(AlphaComposite.SrcOver); g2d.drawImage(modernMinimapContent, 34, 4, null); g2d.setComposite(AlphaComposite.DstIn); g2d.drawImage(modernMinimapHole, 0, 0, null); g2d.setComposite(AlphaComposite.SrcOver); g2d.drawImage(modernMinimapBezel, 0, 0, null); g2d.dispose(); g.drawImage(modernMinimapComposite, windowWidth - 249, 0, null); } public static void drawModernChat(Object graphics, int[] chatContentPixels, int[] chatButtonsPixels, int windowHeight) { if (!(graphics instanceof Graphics)) { return; } Graphics g = (Graphics) graphics; if (modernChatFrame == null) { try { modernChatFrame = ImageIO.read(DesktopHooks.class.getResource("/hud_chat_frame.png")); modernChatComposite = new BufferedImage(519, 165, BufferedImage.TYPE_INT_ARGB); modernChatContent = new BufferedImage(479, 96, BufferedImage.TYPE_INT_RGB); modernChatButtons = new BufferedImage(496, 50, BufferedImage.TYPE_INT_RGB); } catch (Exception e) { e.printStackTrace(); } } if (modernChatFrame == null) { return; } if (chatContentPixels != null && chatContentPixels.length >= 479 * 96) { modernChatContent.setRGB(0, 0, 479, 96, chatContentPixels, 0, 479); } if (chatButtonsPixels != null && chatButtonsPixels.length >= 496 * 50) { modernChatButtons.setRGB(0, 0, 496, 50, chatButtonsPixels, 0, 496); } Graphics2D g2d = modernChatComposite.createGraphics(); g2d.setComposite(AlphaComposite.Clear); g2d.fillRect(0, 0, 519, 165); g2d.setComposite(AlphaComposite.SrcOver); g2d.drawImage(modernChatFrame, 0, 0, null); if (chatContentPixels != null) { g2d.drawImage(modernChatContent, 17, 19, null); } if (chatButtonsPixels != null) { g2d.drawImage(modernChatButtons, 0, 115, null); } g2d.dispose(); g.drawImage(modernChatComposite, 0, windowHeight - 165, null); } public static void drawModernTab(Object graphics, int[] topTabPixels, int[] leftPillarTopPixels, int[] leftPillarMidPixels, int[] leftPillarBotPixels, int[] tabBodyPixels, int[] rightPillarPixels, int[] bottomTabPixels, int windowWidth, int windowHeight) { if (!(graphics instanceof Graphics)) { return; } Graphics g = (Graphics) graphics; if (modernTabComposite == null) { modernTabComposite = new BufferedImage(249, 343, BufferedImage.TYPE_INT_ARGB); 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); } } } // 1. Top tabs: 249x45 at y = 0..44 if (topTabPixels != null && topTabPixels.length >= 249 * 45) { System.arraycopy(topTabPixels, 0, modernTabPixels, 0, 249 * 45); } // 2. Middle area: y from 45 to 306 (height 261, total width 249) // Left vertical stone pillar is assembled from 3 fixed-mode producers: // - backVmidIP2 (37x133 at y = 45..177) // - backVmidIP2_2 (553x19 at y = 178..196, rightmost 37px starting at x = 516) // - backVmidIP3 (57x109 at y = 197..305, rightmost 37px starting at x = 20) 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); } } // Tab body (inventory/interface): 190x261 at x = 37..226 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); } } // Right pillar: 22x261 at x = 227..248 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); } } // 3. Bottom tabs: 249x37 at y = 306..342 (cropped by 20px on left from 269x37 producer) 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); } } // 4. Mask out fixed-mode outer margins and 4 corner triangles to 100% transparent int total = 249 * 343; for (int i = 0; i < total; i++) { modernTabPixels[i] = modernTabMask[i] ? (0xFF000000 | (modernTabPixels[i] & 0xFFFFFF)) : 0x00000000; } modernTabComposite.setRGB(0, 0, 249, 343, modernTabPixels, 0, 249); g.drawImage(modernTabComposite, windowWidth - 249, windowHeight - 343, null); } public static String httpGet(String url) throws java.io.IOException { return null; } public static boolean saveExternalScript(String fileName, String content) { return false; } public static boolean deleteExternalScript(String fileName) { return false; } public static void yieldUi() {} public static Object boxReflectValue(Object v, Class declaredType) { return v; } public static String loadPluginConfig() { return null; } public static boolean savePluginConfig(String content) { return false; } }