diff --git a/src/main/java/Connection.java b/src/main/java/Connection.java index 746ce1c..eee7f20 100644 --- a/src/main/java/Connection.java +++ b/src/main/java/Connection.java @@ -9,155 +9,155 @@ import java.net.Socket; public class Connection implements Runnable { - public final Socket aSocket421; - public final GameShell anShell_423; - public InputStream anInputStream419; - public OutputStream anOutputStream420; - public boolean closed = false; - public byte[] aByteArray424; - public int anInt425; - public int anInt426; - public boolean aBoolean427 = false; - public boolean aBoolean428 = false; + public final Socket socket; + public final GameShell shell; + public InputStream in; + public OutputStream out; + public boolean closed = false; + public byte[] buf; + public int tcycl; + public int tnum; + public boolean writer = false; + public boolean ioerror = false; - public Connection(GameShell shell, Socket socket) throws IOException { - anShell_423 = shell; - aSocket421 = socket; - aSocket421.setSoTimeout(30000); - aSocket421.setTcpNoDelay(true); - anInputStream419 = aSocket421.getInputStream(); - anOutputStream420 = aSocket421.getOutputStream(); - } + public Connection(GameShell shell, Socket socket) throws IOException { + this.shell = shell; + this.socket = socket; + this.socket.setSoTimeout(30000); + this.socket.setTcpNoDelay(true); + in = this.socket.getInputStream(); + out = this.socket.getOutputStream(); + } - public void method267() { - closed = true; - try { - if (anInputStream419 != null) { - anInputStream419.close(); - } - if (anOutputStream420 != null) { - anOutputStream420.close(); - } - if (aSocket421 != null) { - aSocket421.close(); - } - } catch (IOException _ex) { - System.out.println("Error closing stream"); - } - aBoolean427 = false; - synchronized (this) { - notify(); - } - aByteArray424 = null; - } + public void close() { + closed = true; + try { + if (in != null) { + in.close(); + } + if (out != null) { + out.close(); + } + if (socket != null) { + socket.close(); + } + } catch (IOException _ex) { + System.out.println("Error closing stream"); + } + writer = false; + synchronized (this) { + notify(); + } + buf = null; + } - public int method268() throws IOException { - if (closed) { - return 0; - } else { - return anInputStream419.read(); - } - } + public int read() throws IOException { + if (closed) { + return 0; + } else { + return in.read(); + } + } - public int available() throws IOException { - if (closed) { - return 0; - } else { - return anInputStream419.available(); - } - } + public int available() throws IOException { + if (closed) { + return 0; + } else { + return in.available(); + } + } - public void method270(byte[] abyte0, int i, int j) throws IOException { - if (closed) { - return; - } - int k; - for (; j > 0; j -= k) { - k = anInputStream419.read(abyte0, i, j); - if (k <= 0) { - throw new IOException("EOF"); - } - i += k; - } - } + public void read(byte[] dst, int off, int len) throws IOException { + if (closed) { + return; + } + int k; + for (; len > 0; len -= k) { + k = in.read(dst, off, len); + if (k <= 0) { + throw new IOException("EOF"); + } + off += k; + } + } - public void method271(int i, byte[] abyte0, int k) throws IOException { - if (closed) { - return; - } - if (aBoolean428) { - aBoolean428 = false; - throw new IOException("Error in writer thread"); - } - if (aByteArray424 == null) { - aByteArray424 = new byte[5000]; - } - synchronized (this) { - for (int l = 0; l < i; l++) { - aByteArray424[anInt426] = abyte0[l + k]; - anInt426 = (anInt426 + 1) % 5000; - if (anInt426 == ((anInt425 + 4900) % 5000)) { - throw new IOException("buffer overflow"); - } - } - if (!aBoolean427) { - aBoolean427 = true; - anShell_423.startThread(this, 3); - } - notify(); - } - } + public void write(byte[] src, int off, int len) throws IOException { + if (closed) { + return; + } + if (ioerror) { + ioerror = false; + throw new IOException("Error in writer thread"); + } + if (buf == null) { + buf = new byte[5000]; + } + synchronized (this) { + for (int i = 0; i < len; i++) { + buf[tnum] = src[i + off]; + tnum = (tnum + 1) % 5000; + if (tnum == ((tcycl + 4900) % 5000)) { + throw new IOException("buffer overflow"); + } + } + if (!writer) { + writer = true; + shell.startThread(this, 3); + } + notify(); + } + } - @Override - public void run() { - while (aBoolean427) { - int i; - int j; - synchronized (this) { - if (anInt426 == anInt425) { - try { - wait(); - } catch (InterruptedException ignored) { - } - } - if (!aBoolean427) { - return; - } - j = anInt425; - if (anInt426 >= anInt425) { - i = anInt426 - anInt425; - } else { - i = 5000 - anInt425; - } - } - if (i > 0) { - try { - anOutputStream420.write(aByteArray424, j, i); - } catch (IOException _ex) { - aBoolean428 = true; - } - anInt425 = (anInt425 + i) % 5000; - try { - if (anInt426 == anInt425) { - anOutputStream420.flush(); - } - } catch (IOException _ex) { - aBoolean428 = true; - } - } - } - } + @Override + public void run() { + while (writer) { + int i; + int j; + synchronized (this) { + if (tnum == tcycl) { + try { + wait(); + } catch (InterruptedException ignored) { + } + } + if (!writer) { + return; + } + j = tcycl; + if (tnum >= tcycl) { + i = tnum - tcycl; + } else { + i = 5000 - tcycl; + } + } + if (i > 0) { + try { + out.write(buf, j, i); + } catch (IOException _ex) { + ioerror = true; + } + tcycl = (tcycl + i) % 5000; + try { + if (tnum == tcycl) { + out.flush(); + } + } catch (IOException _ex) { + ioerror = true; + } + } + } + } - public void method272() { - System.out.println("closed:" + closed); - System.out.println("tcycl:" + anInt425); - System.out.println("tnum:" + anInt426); - System.out.println("writer:" + aBoolean427); - System.out.println("ioerror:" + aBoolean428); - try { - System.out.println("available:" + available()); - } catch (IOException ignored) { - } - } + public void debug() { + System.out.println("closed:" + closed); + System.out.println("tcycl:" + tcycl); + System.out.println("tnum:" + tnum); + System.out.println("writer:" + writer); + System.out.println("ioerror:" + ioerror); + try { + System.out.println("available:" + available()); + } catch (IOException ignored) { + } + } } diff --git a/src/main/java/Game.java b/src/main/java/Game.java index e9d7aca..64c4051 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -4,7 +4,6 @@ import org.apache.commons.math3.random.ISAACRandom; -import java.applet.AppletContext; import java.awt.*; import java.io.DataInputStream; import java.io.EOFException; @@ -576,21 +575,6 @@ public class Game extends GameShell { public Game() { } - @Override - public void init() { - nodeId = Integer.parseInt(getParameter("nodeid")); - portOffset = Integer.parseInt(getParameter("portoff")); - String s = getParameter("lowmem"); - if ((s != null) && s.equals("1")) { - setLowmem(); - } else { - setHighmem(); - } - String s1 = getParameter("free"); - members = (s1 == null) || !s1.equals("1"); - initApplet(765, 503); - } - @Override public void run() { if (aBoolean880) { @@ -600,25 +584,16 @@ public class Game extends GameShell { } } - @Override - public AppletContext getAppletContext() { - return super.getAppletContext(); - } - - @Override public URL getCodeBase() { try { - if (super.frame != null) { - return new URL("http://127.0.0.1:" + (80 + portOffset)); - } + return new URL("http://lucas.xenorune.com:" + (80 + portOffset)); } catch (Exception ignored) { } - return super.getCodeBase(); + return null; } - @Override public String getParameter(String s) { - return super.getParameter(s); + throw new UnsupportedOperationException(); } @Override @@ -921,31 +896,31 @@ public class Game extends GameShell { aImageArray1219[l4] = new Image8(archiveMedia, "mod_icons", l4); } Image24 image = new Image24(archiveMedia, "backleft1", 0); - aArea_903 = new DrawArea(image.width, image.height, getComponent()); + aArea_903 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backleft2", 0); - aArea_904 = new DrawArea(image.width, image.height, getComponent()); + aArea_904 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backright1", 0); - aArea_905 = new DrawArea(image.width, image.height, getComponent()); + aArea_905 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backright2", 0); - aArea_906 = new DrawArea(image.width, image.height, getComponent()); + aArea_906 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backtop1", 0); - aArea_907 = new DrawArea(image.width, image.height, getComponent()); + aArea_907 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backvmid1", 0); - aArea_908 = new DrawArea(image.width, image.height, getComponent()); + aArea_908 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backvmid2", 0); - aArea_909 = new DrawArea(image.width, image.height, getComponent()); + aArea_909 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backvmid3", 0); - aArea_910 = new DrawArea(image.width, image.height, getComponent()); + aArea_910 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); image = new Image24(archiveMedia, "backhmid2", 0); - aArea_911 = new DrawArea(image.width, image.height, getComponent()); + aArea_911 = new DrawArea(image.width, image.height, this); image.blitOpaque(0, 0); int i5 = (int) (Math.random() * 21D) - 10; int j5 = (int) (Math.random() * 21D) - 10; @@ -1068,7 +1043,7 @@ public class Game extends GameShell { Signlink.reporterror = false; try { if (connection != null) { - connection.method267(); + connection.close(); } } catch (Exception ignored) { } @@ -1217,18 +1192,6 @@ public class Game extends GameShell { aBoolean1255 = true; } - @Override - public java.awt.Component getComponent() { - if (Signlink.mainapp != null) { - return Signlink.mainapp; - } - if (super.frame != null) { - return super.frame; - } else { - return this; - } - } - @Override public void startThread(Runnable runnable, int priority) { if (priority > 10) { @@ -1948,18 +1911,6 @@ public class Game extends GameShell { public void method28(String s) { System.out.println(s); - try { - getAppletContext().showDocument(new URL(getCodeBase(), "loaderror_" + s + ".html")); - } catch (Exception exception) { - exception.printStackTrace(); - } - //noinspection InfiniteLoopStatement - while (true) { - try { - Thread.sleep(1000L); - } catch (Exception ignored) { - } - } } public void handleParentComponentInput(int x, Component parent, int mouseX, int y, int mouseY, int scrollY) { @@ -2874,7 +2825,7 @@ public class Game extends GameShell { public void method44() { try { if (connection != null) { - connection.method267(); + connection.close(); } } catch (Exception ignored) { } @@ -3946,7 +3897,7 @@ public class Game extends GameShell { } try { if ((connection != null) && (aBuffer_1192.position > 0)) { - connection.method271(aBuffer_1192.position, aBuffer_1192.data, 0); + connection.write(aBuffer_1192.data, 0, aBuffer_1192.position); aBuffer_1192.position = 0; anInt1010 = 0; } @@ -3980,23 +3931,23 @@ public class Game extends GameShell { aArea_1123 = null; aArea_1124 = null; aArea_1125 = null; - imageTitle0 = new DrawArea(128, 265, getComponent()); + imageTitle0 = new DrawArea(128, 265, this); Draw2D.clear(); - imageTitle1 = new DrawArea(128, 265, getComponent()); + imageTitle1 = new DrawArea(128, 265, this); Draw2D.clear(); - imageTitle2 = new DrawArea(509, 171, getComponent()); + imageTitle2 = new DrawArea(509, 171, this); Draw2D.clear(); - imageTitle3 = new DrawArea(360, 132, getComponent()); + imageTitle3 = new DrawArea(360, 132, this); Draw2D.clear(); - imageTitle4 = new DrawArea(360, 200, getComponent()); + imageTitle4 = new DrawArea(360, 200, this); Draw2D.clear(); - imageTitle5 = new DrawArea(202, 238, getComponent()); + imageTitle5 = new DrawArea(202, 238, this); Draw2D.clear(); - imageTitle6 = new DrawArea(203, 238, getComponent()); + imageTitle6 = new DrawArea(203, 238, this); Draw2D.clear(); - imageTitle7 = new DrawArea(74, 94, getComponent()); + imageTitle7 = new DrawArea(74, 94, this); Draw2D.clear(); - imageTitle8 = new DrawArea(75, 94, getComponent()); + imageTitle8 = new DrawArea(75, 94, this); Draw2D.clear(); if (archiveTitle != null) { createTitleBackground(); @@ -4208,12 +4159,12 @@ public class Game extends GameShell { Connection connection = this.connection; ingame = false; anInt1038 = 0; - method84(aString1173, aString1174, true); + login(aString1173, aString1174, true); if (!ingame) { method44(); } try { - connection.method267(); + connection.close(); } catch (Exception ignored) { } } @@ -5202,7 +5153,7 @@ public class Game extends GameShell { System.out.println("scene-state:" + sceneState); System.out.println("draw-state:" + method54()); if (connection != null) { - connection.method272(); + connection.debug(); } super.debug = true; } @@ -5962,16 +5913,16 @@ public class Game extends GameShell { imageTitle6 = null; imageTitle7 = null; imageTitle8 = null; - areaChatback = new DrawArea(479, 96, getComponent()); - aArea_1164 = new DrawArea(172, 156, getComponent()); + areaChatback = new DrawArea(479, 96, this); + aArea_1164 = new DrawArea(172, 156, this); Draw2D.clear(); imageMapback.blit(0, 0); - areaInvback = new DrawArea(190, 261, getComponent()); - areaViewport = new DrawArea(512, 334, getComponent()); + areaInvback = new DrawArea(190, 261, this); + areaViewport = new DrawArea(512, 334, this); Draw2D.clear(); - aArea_1123 = new DrawArea(496, 50, getComponent()); - aArea_1124 = new DrawArea(269, 37, getComponent()); - aArea_1125 = new DrawArea(249, 45, getComponent()); + aArea_1123 = new DrawArea(496, 50, this); + aArea_1124 = new DrawArea(269, 37, this); + aArea_1125 = new DrawArea(249, 45, this); aBoolean1255 = true; } @@ -6068,27 +6019,27 @@ public class Game extends GameShell { return (((((src & 0xff00ff) * invAlpha) + ((dst & 0xff00ff) * alpha)) & 0xff00ff00) + ((((src & 0xff00) * invAlpha) + ((dst & 0xff00) * alpha)) & 0xff0000)) >> 8; } - public void method84(String s, String s1, boolean flag) { + public void login(String username, String password, boolean reconnect) { try { - if (!flag) { + if (!reconnect) { aString1266 = ""; aString1267 = "Connecting to server..."; method135(true); } connection = new Connection(this, openSocket(43594 + portOffset)); - long l = StringUtil.toBase37(s); + long l = StringUtil.toBase37(username); int i = (int) ((l >> 16) & 31L); aBuffer_1192.position = 0; aBuffer_1192.put1(14); aBuffer_1192.put1(i); - connection.method271(2, aBuffer_1192.data, 0); + connection.write(aBuffer_1192.data, 0, 2); for (int j = 0; j < 8; j++) { - connection.method268(); + connection.read(); } - int k = connection.method268(); + int k = connection.read(); int i1 = k; if (k == 0) { - connection.method270(in.data, 0, 8); + connection.read(in.data, 0, 8); in.position = 0; aLong1215 = in.get8(); @@ -6106,11 +6057,11 @@ public class Game extends GameShell { aBuffer_1192.put4(seed[2]); aBuffer_1192.put4(seed[3]); aBuffer_1192.put4(Signlink.uid); - aBuffer_1192.put(s); - aBuffer_1192.put(s1); + aBuffer_1192.put(username); + aBuffer_1192.put(password); aBuffer_1192.encrypt(RSA_PUBLIC_KEY, RSA_MODULUS); aBuffer_847.position = 0; - if (flag) { + if (reconnect) { aBuffer_847.put1(18); } else { aBuffer_847.put1(16); @@ -6128,20 +6079,20 @@ public class Game extends GameShell { seed[j2] += 50; } randomIn = new ISAACRandom(seed); - connection.method271(aBuffer_847.position, aBuffer_847.data, 0); - k = connection.method268(); + connection.write(aBuffer_847.data, 0, aBuffer_847.position); + k = connection.read(); } if (k == 1) { try { Thread.sleep(2000L); } catch (Exception ignored) { } - method84(s, s1, flag); + login(username, password, reconnect); return; } if (k == 2) { - rights = connection.method268(); - aBoolean1205 = connection.method268() == 1; + rights = connection.read(); + aBoolean1205 = connection.read() == 1; aLong1220 = 0L; anInt1022 = 0; aMouseRecorder_879.anInt810 = 0; @@ -6318,7 +6269,7 @@ public class Game extends GameShell { return; } if (k == 21) { - for (int k1 = connection.method268(); k1 >= 0; k1--) { + for (int k1 = connection.read(); k1 >= 0; k1--) { aString1266 = "You have only just left another world"; aString1267 = "Your profile will be transferred in: " + k1 + " seconds"; method135(true); @@ -6327,7 +6278,7 @@ public class Game extends GameShell { } catch (Exception ignored) { } } - method84(s, s1, flag); + login(username, password, reconnect); return; } if (k == -1) { @@ -6338,7 +6289,7 @@ public class Game extends GameShell { } catch (Exception ignored) { } anInt1038++; - method84(s, s1, flag); + login(username, password, reconnect); } else { aString1266 = "No response from loginserver"; aString1267 = "Please wait 1 minute and try again."; @@ -6967,7 +6918,7 @@ public class Game extends GameShell { } public void method94() { - Graphics g = getComponent().getGraphics(); + Graphics g = this.getGraphics(); g.setColor(Color.black); g.fillRect(0, 0, 765, 503); setFrameRate(1); @@ -9715,7 +9666,7 @@ public class Game extends GameShell { k1 += 20; if ((super.mousePressButton == 1) && (super.mousePressX >= (i1 - 75)) && (super.mousePressX <= (i1 + 75)) && (super.mousePressY >= (k1 - 20)) && (super.mousePressY <= (k1 + 20))) { anInt1038 = 0; - method84(aString1173, aString1174, false); + login(aString1173, aString1174, false); if (ingame) { return; } @@ -9925,7 +9876,7 @@ public class Game extends GameShell { } if (ptype == -1) { - connection.method270(in.data, 0, 1); + connection.read(in.data, 0, 1); ptype = in.data[0] & 0xff; if (randomIn != null) { ptype = (ptype - randomIn.nextInt()) & 0xff; @@ -9936,7 +9887,7 @@ public class Game extends GameShell { if (psize == -1) { if (available > 0) { - connection.method270(in.data, 0, 1); + connection.read(in.data, 0, 1); psize = in.data[0] & 0xff; available--; } else { @@ -9946,7 +9897,7 @@ public class Game extends GameShell { if (psize == -2) { if (available > 1) { - connection.method270(in.data, 0, 2); + connection.read(in.data, 0, 2); in.position = 0; psize = in.get2U(); available -= 2; @@ -9960,7 +9911,7 @@ public class Game extends GameShell { } in.position = 0; - connection.method270(in.data, 0, psize); + connection.read(in.data, 0, psize); anInt1009 = 0; anInt843 = anInt842; anInt842 = anInt841; diff --git a/src/main/java/GameFrame.java b/src/main/java/GameFrame.java index ee7ff28..e7b4011 100644 --- a/src/main/java/GameFrame.java +++ b/src/main/java/GameFrame.java @@ -2,36 +2,23 @@ // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) +import javax.swing.*; import java.awt.*; -public class GameFrame extends Frame { +public class GameFrame extends JFrame { public final GameShell shell; - public GameFrame(GameShell shell, int width, int height) { + public GameFrame(GameShell shell) { this.shell = shell; setTitle("Jagex"); setResizable(false); - show(); - toFront(); - resize(width + 8, height + 28); - } - - @Override - public Graphics getGraphics() { - Graphics g = super.getGraphics(); - g.translate(4, 24); - return g; - } - - @Override - public void update(Graphics g) { - shell.update(g); - } - - @Override - public void paint(Graphics g) { - shell.paint(g); + setLayout(new BorderLayout()); + add(shell,BorderLayout.CENTER); + pack(); + setLocationRelativeTo(null); + setVisible(true); + transferFocus(); } } diff --git a/src/main/java/GameShell.java b/src/main/java/GameShell.java index 103626c..8f47910 100644 --- a/src/main/java/GameShell.java +++ b/src/main/java/GameShell.java @@ -2,546 +2,520 @@ // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) -import java.applet.Applet; +import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; -public abstract class GameShell extends Applet implements Runnable, MouseListener, MouseMotionListener, KeyListener, FocusListener, WindowListener { - - public final long[] otim = new long[10]; - public final int[] actionKey = new int[128]; - public final int[] anIntArray31 = new int[128]; - public int state; - public int deltime = 20; - public int mindel = 1; - public int fps; - public boolean debug = false; - public int screenWidth; - public int screenHeight; - public Graphics graphics; - public GameFrame frame; - public boolean refresh = true; - public boolean aBoolean17 = true; - public int idleCycles; - public int mouseButton; - public int mouseX; - public int mouseY; - public int lastMousePressButton; - public int lastMousePressX; - public int lastMousePressY; - public long lastMousePressTime; - public int mousePressButton; - public int mousePressX; - public int mousePressY; - public long mousePressTime; - public int anInt32; - public int anInt33; - - public void init(int width, int height) { - screenWidth = width; - screenHeight = height; - frame = new GameFrame(this, screenWidth, screenHeight); - graphics = getComponent().getGraphics(); - startThread(this, 1); - } - - public void initApplet(int width, int height) { - screenWidth = width; - screenHeight = height; - graphics = getComponent().getGraphics(); - startThread(this, 1); - } - - @Override - public void run() { - try { - getComponent().addMouseListener(this); - getComponent().addMouseMotionListener(this); - getComponent().addKeyListener(this); - getComponent().addFocusListener(this); - - if (frame != null) { - frame.addWindowListener(this); - } - - showProgress(0, "Loading..."); - load(); - - int opos = 0; - int ratio = 256; - int delta = 1; - int count = 0; - int intex = 0; // interrupt exceptions - - for (int k1 = 0; k1 < 10; k1++) { - otim[k1] = System.currentTimeMillis(); - } - - while (state >= 0) { - if (state > 0) { - state--; - if (state == 0) { - shutdown(); - return; - } - } - - int lastRatio = ratio; - int lastDelta = delta; - - ratio = 300; - delta = 1; - - long ntime = System.currentTimeMillis(); - - if (otim[opos] == 0L) { - ratio = lastRatio; - delta = lastDelta; - } else if (ntime > otim[opos]) { - ratio = (int) ((2560L * deltime) / (ntime - otim[opos])); - } - - if (ratio < 25) { - ratio = 25; - } - - if (ratio > 256) { - ratio = 256; - delta = (int) ((long) deltime - ((ntime - otim[opos]) / 10L)); - } - - if (delta > deltime) { - delta = deltime; - } - - otim[opos] = ntime; - opos = (opos + 1) % 10; - - if (delta > 1) { - for (int k2 = 0; k2 < 10; k2++) { - if (otim[k2] != 0L) { - otim[k2] += delta; - } - } - } - - if (delta < mindel) { - delta = mindel; - } - - try { - Thread.sleep(delta); - } catch (InterruptedException e) { - intex++; - } - - for (; count < 256; count += ratio) { - mousePressButton = lastMousePressButton; - mousePressX = lastMousePressX; - mousePressY = lastMousePressY; - mousePressTime = lastMousePressTime; - lastMousePressButton = 0; - update(); - anInt32 = anInt33; - } - - count &= 0xff; - - if (deltime > 0) { - fps = (1000 * ratio) / (deltime * 256); - } - - draw(); - - if (debug) { - System.out.println("ntime:" + ntime); - for (int i = 0; i < 10; i++) { - int o = ((opos - i - 1) + 20) % 10; - System.out.println("otim" + o + ":" + otim[o]); - } - System.out.println("fps:" + fps + " ratio:" + ratio + " count:" + count); - System.out.println("del:" + delta + " deltime:" + deltime + " mindel:" + mindel); - System.out.println("intex:" + intex + " opos:" + opos); - debug = false; - intex = 0; - } - } - } catch (Exception e) { - state = -1; - e.printStackTrace(); - } - - if (state == -1) { - shutdown(); - } - } - - public void shutdown() { - state = -2; - unload(); - - if (frame != null) { - try { - Thread.sleep(1000L); - } catch (Exception ignored) { - } - try { - System.exit(0); - } catch (Throwable ignored) { - } - } - } - - public void setFrameRate(int fps) { - deltime = 1000 / fps; - } - - @Override - public void start() { - if (state >= 0) { - state = 0; - } - } - - @Override - public void stop() { - if (state >= 0) { - state = 4000 / deltime; - } - } - - @Override - public void destroy() { - state = -1; - - try { - Thread.sleep(5000L); - } catch (Exception ignored) { - } - - if (state == -1) { - shutdown(); - } - } - - @Override - public void update(Graphics g) { - if (graphics == null) { - graphics = g; - } - refresh = true; - refresh(); - } - - @Override - public void paint(Graphics g) { - if (graphics == null) { - graphics = g; - } - refresh = true; - refresh(); - } - - @Override - public void mousePressed(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - - if (frame != null) { - x -= 4; - y -= 22; - } - - idleCycles = 0; - lastMousePressX = x; - lastMousePressY = y; - lastMousePressTime = System.currentTimeMillis(); - - if (e.isMetaDown()) { - lastMousePressButton = 2; - mouseButton = 2; - } else { - lastMousePressButton = 1; - mouseButton = 1; - } - } - - @Override - public void mouseReleased(MouseEvent e) { - idleCycles = 0; - mouseButton = 0; - } - - @Override - public void mouseClicked(MouseEvent e) { - } - - @Override - public void mouseEntered(MouseEvent e) { - } - - @Override - public void mouseExited(MouseEvent e) { - idleCycles = 0; - mouseX = -1; - mouseY = -1; - } - - @Override - public void mouseDragged(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - if (frame != null) { - x -= 4; - y -= 22; - } - idleCycles = 0; - mouseX = x; - mouseY = y; - } - - @Override - public void mouseMoved(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - if (frame != null) { - x -= 4; - y -= 22; - } - idleCycles = 0; - mouseX = x; - mouseY = y; - } - - @Override - public void keyPressed(KeyEvent e) { - idleCycles = 0; - int code = e.getKeyCode(); - int ch = e.getKeyChar(); - - if (ch < 30) { - ch = 0; - } - - if (code == KeyEvent.VK_LEFT) { - ch = 1; - } else if (code == KeyEvent.VK_RIGHT) { - ch = 2; - } else if (code == KeyEvent.VK_UP) { - ch = 3; - } else if (code == KeyEvent.VK_DOWN) { - ch = 4; - } else if (code == KeyEvent.VK_CONTROL) { - ch = 5; - } else if (code == KeyEvent.VK_BACK_SPACE) { - ch = 8; - } else if (code == KeyEvent.VK_DELETE) { - ch = 8; - } else if (code == KeyEvent.VK_TAB) { - ch = 9; - } else if (code == KeyEvent.VK_ENTER) { - ch = 10; - } else if ((code >= KeyEvent.VK_F1) && (code <= KeyEvent.VK_F12)) { - ch = (1008 + code) - KeyEvent.VK_F1; - } else if (code == KeyEvent.VK_HOME) { - ch = 1000; - } else if (code == KeyEvent.VK_END) { - ch = 1001; - } else if (code == KeyEvent.VK_PAGE_UP) { - ch = 1002; - } else if (code == KeyEvent.VK_PAGE_DOWN) { - ch = 1003; - } - - if ((ch > 0) && (ch < 128)) { - actionKey[ch] = 1; - } - - if (ch > 4) { - anIntArray31[anInt33] = ch; - anInt33 = (anInt33 + 1) & 0x7f; - } - } - - @Override - public void keyReleased(KeyEvent e) { - idleCycles = 0; - - int code = e.getKeyCode(); - char action = e.getKeyChar(); - - if (action < 30) { - action = 0; - } - - if (code == KeyEvent.VK_LEFT) { - action = 1; - } else if (code == KeyEvent.VK_RIGHT) { - action = 2; - } else if (code == KeyEvent.VK_UP) { - action = 3; - } else if (code == KeyEvent.VK_DOWN) { - action = 4; - } else if (code == KeyEvent.VK_CONTROL) { - action = 5; - } else if (code == KeyEvent.VK_BACK_SPACE) { - action = 8; - } else if (code == KeyEvent.VK_DELETE) { - action = 8; - } else if (code == KeyEvent.VK_TAB) { - action = 9; - } else if (code == KeyEvent.VK_ENTER) { - action = 10; - } - - if ((action > 0) && (action < 128)) { - actionKey[action] = 0; - } - } - - @Override - public void keyTyped(KeyEvent e) { - } - - public int method5() { - int k = -1; - if (anInt33 != anInt32) { - k = anIntArray31[anInt32]; - anInt32 = (anInt32 + 1) & 0x7f; - } - return k; - } - - @Override - public void focusGained(FocusEvent e) { - aBoolean17 = true; - refresh = true; - refresh(); - } - - @Override - public void focusLost(FocusEvent e) { - aBoolean17 = false; - for (int i = 0; i < 128; i++) { - actionKey[i] = 0; - } - } - - @Override - public void windowActivated(WindowEvent e) { - } - - @Override - public void windowClosed(WindowEvent e) { - } - - @Override - public void windowClosing(WindowEvent e) { - destroy(); - } - - @Override - public void windowDeactivated(WindowEvent e) { - } - - @Override - public void windowDeiconified(WindowEvent e) { - } - - @Override - public void windowIconified(WindowEvent e) { - } - - @Override - public void windowOpened(WindowEvent e) { - } - - /** - * Called when the {@link GameShell} thread starts. - * - * @see #run() - */ - public abstract void load() throws IOException; - - /** - * @see #run() - */ - public abstract void update() throws IOException; - - /** - * @see #run() - */ - public abstract void unload(); - - /** - * @see #run() - */ - public abstract void draw() throws IOException; - - /** - * Refresh is invoked whenever the shell is expected to potentially lost its display image. - * - * @see #update(Graphics) - * @see #paint(Graphics) - * @see #focusGained(FocusEvent) - */ - public abstract void refresh(); - - /** - * Gets the draw component of this {@link GameShell}. - * - * @return the component. - */ - public java.awt.Component getComponent() { - if (frame != null) { - return frame; - } else { - return this; - } - } - - public void startThread(Runnable runnable, int priority) { - Thread thread = new Thread(runnable); - thread.start(); - thread.setPriority(priority); - } - - public void showProgress(int percent, String message) throws IOException { - while (graphics == null) { - graphics = getComponent().getGraphics(); - try { - getComponent().repaint(); - } catch (Exception ignored) { - } - - try { - Thread.sleep(1000L); - } catch (Exception ignored) { - } - } - - java.awt.Font helvetica = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 13); - FontMetrics metrics = getComponent().getFontMetrics(helvetica); - - if (refresh) { - graphics.setColor(Color.black); - graphics.fillRect(0, 0, screenWidth, screenHeight); - refresh = false; - } - - int y = (screenHeight / 2) - 18; - - graphics.setColor(new Color(140, 17, 17)); - graphics.drawRect((screenWidth / 2) - 152, y, 304, 34); - graphics.fillRect((screenWidth / 2) - 150, y + 2, percent * 3, 30); - - graphics.setColor(Color.black); - graphics.fillRect(((screenWidth / 2) - 150) + (percent * 3), y + 2, 300 - (percent * 3), 30); - - graphics.setFont(helvetica); - graphics.setColor(Color.white); - graphics.drawString(message, (screenWidth - metrics.stringWidth(message)) / 2, y + 22); - } +public abstract class GameShell extends JComponent implements Runnable, MouseListener, MouseMotionListener, KeyListener, FocusListener, WindowListener { + + public final long[] otim = new long[10]; + public final int[] actionKey = new int[128]; + public final int[] anIntArray31 = new int[128]; + public int state; + public int deltime = 20; + public int mindel = 1; + public int fps; + public boolean debug = false; + public int screenWidth; + public int screenHeight; + public Graphics graphics; + public GameFrame frame; + public boolean refresh = true; + public boolean aBoolean17 = true; + public int idleCycles; + public int mouseButton; + public int mouseX; + public int mouseY; + public int lastMousePressButton; + public int lastMousePressX; + public int lastMousePressY; + public long lastMousePressTime; + public int mousePressButton; + public int mousePressX; + public int mousePressY; + public long mousePressTime; + public int anInt32; + public int anInt33; + + public void init(int width, int height) { + screenWidth = width; + screenHeight = height; + setSize(width,height); + setPreferredSize(getSize()); + frame = new GameFrame(this); + graphics = this.getGraphics(); + startThread(this, 1); + requestFocus(); + } + + public void initApplet(int width, int height) { + screenWidth = width; + screenHeight = height; + graphics = this.getGraphics(); + startThread(this, 1); + } + + @Override + public void run() { + try { + this.addMouseListener(this); + this.addMouseMotionListener(this); + this.addKeyListener(this); + this.addFocusListener(this); + + if (frame != null) { + frame.addWindowListener(this); + } + + showProgress(0, "Loading..."); + load(); + + int opos = 0; + int ratio = 256; + int delta = 1; + int count = 0; + int intex = 0; // interrupt exceptions + + for (int k1 = 0; k1 < 10; k1++) { + otim[k1] = System.currentTimeMillis(); + } + + while (state >= 0) { + if (state > 0) { + state--; + if (state == 0) { + shutdown(); + return; + } + } + + int lastRatio = ratio; + int lastDelta = delta; + + ratio = 300; + delta = 1; + + long ntime = System.currentTimeMillis(); + + if (otim[opos] == 0L) { + ratio = lastRatio; + delta = lastDelta; + } else if (ntime > otim[opos]) { + ratio = (int) ((2560L * deltime) / (ntime - otim[opos])); + } + + if (ratio < 25) { + ratio = 25; + } + + if (ratio > 256) { + ratio = 256; + delta = (int) ((long) deltime - ((ntime - otim[opos]) / 10L)); + } + + if (delta > deltime) { + delta = deltime; + } + + otim[opos] = ntime; + opos = (opos + 1) % 10; + + if (delta > 1) { + for (int k2 = 0; k2 < 10; k2++) { + if (otim[k2] != 0L) { + otim[k2] += delta; + } + } + } + + if (delta < mindel) { + delta = mindel; + } + + try { + Thread.sleep(delta); + } catch (InterruptedException e) { + intex++; + } + + for (; count < 256; count += ratio) { + mousePressButton = lastMousePressButton; + mousePressX = lastMousePressX; + mousePressY = lastMousePressY; + mousePressTime = lastMousePressTime; + lastMousePressButton = 0; + update(); + anInt32 = anInt33; + } + + count &= 0xff; + + if (deltime > 0) { + fps = (1000 * ratio) / (deltime * 256); + } + + draw(); + + if (debug) { + System.out.println("ntime:" + ntime); + for (int i = 0; i < 10; i++) { + int o = ((opos - i - 1) + 20) % 10; + System.out.println("otim" + o + ":" + otim[o]); + } + System.out.println("fps:" + fps + " ratio:" + ratio + " count:" + count); + System.out.println("del:" + delta + " deltime:" + deltime + " mindel:" + mindel); + System.out.println("intex:" + intex + " opos:" + opos); + debug = false; + intex = 0; + } + } + } catch (Exception e) { + state = -1; + e.printStackTrace(); + } + + if (state == -1) { + shutdown(); + } + } + + public void shutdown() { + state = -2; + unload(); + + if (frame != null) { + try { + Thread.sleep(1000L); + } catch (Exception ignored) { + } + try { + System.exit(0); + } catch (Throwable ignored) { + } + } + } + + public void setFrameRate(int fps) { + deltime = 1000 / fps; + } + + public void destroy() { + state = -1; + + try { + Thread.sleep(5000L); + } catch (Exception ignored) { + } + + if (state == -1) { + shutdown(); + } + } + + @Override + public void update(Graphics g) { + if (graphics == null) { + graphics = g; + } + refresh = true; + refresh(); + } + + @Override + public void paint(Graphics g) { + if (graphics == null) { + graphics = g; + } + refresh = true; + refresh(); + } + + @Override + public void mousePressed(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + + if (frame != null) { + x -= 4; + y -= 22; + } + + idleCycles = 0; + lastMousePressX = x; + lastMousePressY = y; + lastMousePressTime = System.currentTimeMillis(); + + if (SwingUtilities.isRightMouseButton(e)) { + lastMousePressButton = 2; + mouseButton = 2; + } else { + lastMousePressButton = 1; + mouseButton = 1; + } + } + + @Override + public void mouseReleased(MouseEvent e) { + idleCycles = 0; + mouseButton = 0; + } + + @Override + public void mouseClicked(MouseEvent e) { + } + + @Override + public void mouseEntered(MouseEvent e) { + } + + @Override + public void mouseExited(MouseEvent e) { + idleCycles = 0; + mouseX = -1; + mouseY = -1; + } + + @Override + public void mouseDragged(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + if (frame != null) { + x -= 4; + y -= 22; + } + idleCycles = 0; + mouseX = x; + mouseY = y; + } + + @Override + public void mouseMoved(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + if (frame != null) { + x -= 4; + y -= 22; + } + idleCycles = 0; + mouseX = x; + mouseY = y; + } + + @Override + public void keyPressed(KeyEvent e) { + idleCycles = 0; + int code = e.getKeyCode(); + int ch = e.getKeyChar(); + + if (ch < 30) { + ch = 0; + } + + if (code == KeyEvent.VK_LEFT) { + ch = 1; + } else if (code == KeyEvent.VK_RIGHT) { + ch = 2; + } else if (code == KeyEvent.VK_UP) { + ch = 3; + } else if (code == KeyEvent.VK_DOWN) { + ch = 4; + } else if (code == KeyEvent.VK_CONTROL) { + ch = 5; + } else if (code == KeyEvent.VK_BACK_SPACE) { + ch = 8; + } else if (code == KeyEvent.VK_DELETE) { + ch = 8; + } else if (code == KeyEvent.VK_TAB) { + ch = 9; + } else if (code == KeyEvent.VK_ENTER) { + ch = 10; + } else if ((code >= KeyEvent.VK_F1) && (code <= KeyEvent.VK_F12)) { + ch = (1008 + code) - KeyEvent.VK_F1; + } else if (code == KeyEvent.VK_HOME) { + ch = 1000; + } else if (code == KeyEvent.VK_END) { + ch = 1001; + } else if (code == KeyEvent.VK_PAGE_UP) { + ch = 1002; + } else if (code == KeyEvent.VK_PAGE_DOWN) { + ch = 1003; + } + + if ((ch > 0) && (ch < 128)) { + actionKey[ch] = 1; + } + + if (ch > 4) { + anIntArray31[anInt33] = ch; + anInt33 = (anInt33 + 1) & 0x7f; + } + } + + @Override + public void keyReleased(KeyEvent e) { + idleCycles = 0; + + int code = e.getKeyCode(); + char action = e.getKeyChar(); + + if (action < 30) { + action = 0; + } + + if (code == KeyEvent.VK_LEFT) { + action = 1; + } else if (code == KeyEvent.VK_RIGHT) { + action = 2; + } else if (code == KeyEvent.VK_UP) { + action = 3; + } else if (code == KeyEvent.VK_DOWN) { + action = 4; + } else if (code == KeyEvent.VK_CONTROL) { + action = 5; + } else if (code == KeyEvent.VK_BACK_SPACE) { + action = 8; + } else if (code == KeyEvent.VK_DELETE) { + action = 8; + } else if (code == KeyEvent.VK_TAB) { + action = 9; + } else if (code == KeyEvent.VK_ENTER) { + action = 10; + } + + if ((action > 0) && (action < 128)) { + actionKey[action] = 0; + } + } + + @Override + public void keyTyped(KeyEvent e) { + } + + public int method5() { + int k = -1; + if (anInt33 != anInt32) { + k = anIntArray31[anInt32]; + anInt32 = (anInt32 + 1) & 0x7f; + } + return k; + } + + @Override + public void focusGained(FocusEvent e) { + aBoolean17 = true; + refresh = true; + refresh(); + } + + @Override + public void focusLost(FocusEvent e) { + aBoolean17 = false; + for (int i = 0; i < 128; i++) { + actionKey[i] = 0; + } + } + + @Override + public void windowActivated(WindowEvent e) { + } + + @Override + public void windowClosed(WindowEvent e) { + } + + @Override + public void windowClosing(WindowEvent e) { + destroy(); + } + + @Override + public void windowDeactivated(WindowEvent e) { + } + + @Override + public void windowDeiconified(WindowEvent e) { + } + + @Override + public void windowIconified(WindowEvent e) { + } + + @Override + public void windowOpened(WindowEvent e) { + } + + /** + * Called when the {@link GameShell} thread starts. + * + * @see #run() + */ + public abstract void load() throws IOException; + + /** + * @see #run() + */ + public abstract void update() throws IOException; + + /** + * @see #run() + */ + public abstract void unload(); + + /** + * @see #run() + */ + public abstract void draw() throws IOException; + + /** + * Refresh is invoked whenever the shell is expected to potentially lost its display image. + * + * @see #update(Graphics) + * @see #paint(Graphics) + * @see #focusGained(FocusEvent) + */ + public abstract void refresh(); + + public void startThread(Runnable runnable, int priority) { + Thread thread = new Thread(runnable); + thread.start(); + thread.setPriority(priority); + } + + public void showProgress(int percent, String message) throws IOException { + while (graphics == null) { + graphics = this.getGraphics(); + try { + this.repaint(); + } catch (Exception ignored) { + } + try { + Thread.sleep(1000L); + } catch (Exception ignored) { + } + } + + java.awt.Font helvetica = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 13); + FontMetrics metrics = this.getFontMetrics(helvetica); + + if (refresh) { + graphics.setColor(Color.black); + graphics.fillRect(0, 0, screenWidth, screenHeight); + refresh = false; + } + + int y = (screenHeight / 2) - 18; + + graphics.setColor(new Color(140, 17, 17)); + graphics.drawRect((screenWidth / 2) - 152, y, 304, 34); + graphics.fillRect((screenWidth / 2) - 150, y + 2, percent * 3, 30); + + graphics.setColor(Color.black); + graphics.fillRect(((screenWidth / 2) - 150) + (percent * 3), y + 2, 300 - (percent * 3), 30); + + graphics.setFont(helvetica); + graphics.setColor(Color.white); + graphics.drawString(message, (screenWidth - metrics.stringWidth(message)) / 2, y + 22); + } }