package org.newdawn.touchapi.applet;
import edu.stanford.ejalbert.BrowserLauncher;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.newdawn.touchapi.Game;
import org.newdawn.touchapi.Sound;
public class TouchFrame
implements GameCanvasContainer
{
private static final int TIMEOUT = 3000;
private GameCanvas canvas;
private Properties save = new Properties();
private String className;
private int width;
private int height;
private boolean cheat;
private Properties props;
private boolean downloadingFailed = false;
private URLConnection connection;
private int total;
private String[] check = { "KEVGLASS.DSA", "KEVGLASS.SF", "MANIFEST.MF", "INDEX.LIST" };
private boolean fullscreen;
private DisplayMode originalMode;
private JFrame frame;
private String title;
public TouchFrame(Properties props, String title, String className, boolean big, boolean cheat)
{
this.title = title;
this.originalMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
this.props = props;
this.cheat = cheat;
if (!cheat) {
for (int i = 0; i < this.check.length; i++) {
URL url = TouchFrame.class.getClassLoader().getResource("META-INF/" + this.check[i]);
if (url == null) {
System.exit(0);
}
if (!url.getProtocol().equals("jar")) {
System.exit(0);
}
}
}
this.className = className;
this.canvas = new GameCanvas(this, cheat);
this.width = 480;
this.height = 320;
if (big) {
this.width = 800;
this.height = 600;
}
if ("true".equals(props.getProperty("game.portrait"))) {
this.width = 320;
this.height = 480;
}
this.canvas.setMinimumSize(new Dimension(this.width, this.height));
this.canvas.setSize(new Dimension(this.width, this.height));
this.canvas.setMaximumSize(new Dimension(this.width, this.height));
this.canvas.setPreferredSize(new Dimension(this.width, this.height));
loadProperties();
createFrame();
}
private void createFrame() {
if (this.frame != null) {
this.frame.setVisible(false);
this.frame.dispose();
}
this.frame = new JFrame(this.title);
this.frame.setBackground(Color.black);
Image im = Toolkit.getDefaultToolkit().getImage("icon.png");
this.frame.setIconImage(im);
this.frame.getContentPane().add(this.canvas);
this.frame.setDefaultCloseOperation(0);
this.frame.addWindowListener(new TouchFrame.1(this));
}
private void initFrame()
{
this.frame.pack();
this.frame.setResizable(false);
this.frame.setLocationRelativeTo(null);
this.frame.setVisible(true);
this.canvas.createStrategy();
}
public void start() {
boolean enabledScreenShot = true;
initFrame();
this.canvas.start(this.className, new Rectangle(this.width, this.height), enabledScreenShot);
}
public Sound loadSound(String ref)
{
if (ref.endsWith(".ogg")) {
try {
return new OggSound(this.canvas.getResource(ref));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
try
{
return new ApplicationSound(ref);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void loadProperties() {
try {
FileInputStream fin = new FileInputStream(this.canvas.modded() ? "local.properties.dat" : "save.properties.dat");
int ver = fin.read();
fin.read();
fin.read();
GZIPInputStream zin = new GZIPInputStream(fin);
if (this.canvas.modded()) {
int value = zin.read();
if (value != 11) {
return;
}
}
this.save.load(zin);
zin.close();
fin.close();
if (ver == 1) {
String value = this.save.getProperty("__u");
if (!getUUID().equals(value)) {
this.save.setProperty("__a", "");
store();
}
}
} catch (Throwable e) {
System.err.println("Failed to load/find save games");
}
}
private void store() {
try {
FileOutputStream out = new FileOutputStream(this.canvas.modded() ? "local.properties.dat" : "save.properties.dat");
out.write(new byte[] { 1, 8, 1 });
GZIPOutputStream zout = new GZIPOutputStream(out);
if (this.canvas.modded()) {
zout.write(11);
}
this.save.setProperty("__u", getUUID());
this.save.store(zout, "");
zout.close();
out.close();
} catch (IOException e) {
System.err.println("Failed to save to: save.properties");
JOptionPane.showMessageDialog(this.frame, "Failed to save game!");
}
}
public void save(String name, String value)
{
if (value == null)
this.save.remove(name);
else {
this.save.setProperty(name, value);
}
store();
}
public String load(String name)
{
return this.save.getProperty(name);
}
public static void main(String[] argv) {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("run.properties"));
String osname = System.getProperty("os.name");
boolean newWindows = osname.startsWith("Windows");
if ((osname.endsWith("95")) || (osname.endsWith("98")) || (osname.endsWith("XP"))) {
newWindows = false;
}
if ((osname.endsWith("NT")) || (osname.endsWith("2000")) || (osname.endsWith("ME"))) {
newWindows = false;
}
if ((!newWindows) &&
("true".equals(properties.getProperty("use.opengl")))) {
System.setProperty("sun.java2d.opengl", "true");
}
TouchFrame frame = new TouchFrame(properties, properties.getProperty("game.title"), properties.getProperty("game.class"), "true".equals(properties.getProperty("game.big")), "true".equals(properties.getProperty("game.cheat")));
frame.start();
} catch (IOException e) {
System.err.println("Unable to locate run.properties");
}
}
public String getUUID()
{
try
{
Enumeration ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface)ifaces.nextElement();
if (!iface.isLoopback()) {
byte[] data = iface.getHardwareAddress();
if (data != null) {
String value = "";
for (int i = 0; i < data.length; i++) {
value = value + data[i];
}
return value;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
return "" + new File(".").getTotalSpace();
}
return "" + new File(".").getTotalSpace();
}
public boolean keyPressed(KeyEvent event)
{
if ((this.cheat) &&
(event.getKeyCode() == 45)) {
String input = JOptionPane.showInputDialog(null);
this.canvas.getGame().inputCode(input);
return true;
}
String keyCode = "key." + event.getKeyChar();
String mapping = this.props.getProperty(keyCode);
if (mapping != null) {
if (mapping.equalsIgnoreCase("LEFT")) {
this.canvas.controlPressed(2);
return true;
}
if (mapping.equalsIgnoreCase("RIGHT")) {
this.canvas.controlPressed(3);
return true;
}
if (mapping.equalsIgnoreCase("UP")) {
this.canvas.controlPressed(4);
return true;
}
if (mapping.equalsIgnoreCase("DOWN")) {
this.canvas.controlPressed(5);
return true;
}
}
return false;
}
public void getRemoteResource(String u)
{
if (this.downloadingFailed) {
return;
}
try
{
URL url = new URL(u);
String[] segments = url.getPath().split("/");
String filename = segments[(segments.length - 1)];
File local = new File(filename);
if (local.exists()) {
return;
}
System.out.println("Download: " + url);
this.total = -1;
Thread t = new TouchFrame.2(this, url);
t.start();
long startTime = System.currentTimeMillis();
while (this.total == -1) {
try { Thread.sleep(100L); } catch (Exception e) {
}if (System.currentTimeMillis() - startTime > 3000L) {
break;
}
}
if (this.total == -1) {
this.downloadingFailed = true;
System.out.println("Download failed (timeout): " + u);
return;
}
byte[] buffer = new byte[100000];
InputStream in = this.connection.getInputStream();
FileOutputStream out = new FileOutputStream(local);
while (this.total > 0) {
int count = in.read(buffer, 0, buffer.length);
if (count < 0) {
in.close();
out.close();
local.delete();
this.downloadingFailed = true;
System.out.println("Download failed: " + u);
return;
}
out.write(buffer, 0, count);
this.total -= count;
}
out.flush();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
this.downloadingFailed = true;
System.out.println("Download failed: " + u);
return;
}
}
public InputStream getCachedResource(String ref)
{
try {
File file = new File(ref);
if (!file.exists()) {
return null;
}
return new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}return null;
}
public boolean resourceExists(String ref)
{
boolean existsInClassPath = TouchFrame.class.getClassLoader().getResourceAsStream(ref) != null;
boolean existsInLocal = new File(ref).exists();
return (existsInClassPath) || (existsInLocal);
}
public void setResolution(int w, int h)
{
this.width = w;
this.height = h;
this.canvas.setMinimumSize(new Dimension(this.width, this.height));
this.canvas.setSize(new Dimension(this.width, this.height));
this.canvas.setMaximumSize(new Dimension(this.width, this.height));
this.canvas.setPreferredSize(new Dimension(this.width, this.height));
this.frame.pack();
this.frame.setLocationRelativeTo(null);
}
public int getPlatform()
{
return 4;
}
public void show(String url)
{
try {
BrowserLauncher launcher = new BrowserLauncher();
launcher.openURLinBrowser(url);
} catch (Throwable e) {
e.printStackTrace();
try {
Process process = Runtime.getRuntime().exec("open " + url);
if (process.exitValue() != 0)
JOptionPane.showMessageDialog(this.frame, "Failed to launch browser\n\nNavigate to:" + url);
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
public boolean isFullScreen()
{
return this.fullscreen;
}
public void setFullScreen(boolean full)
{
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (full) {
try {
DisplayMode[] modes = device.getDisplayModes();
DisplayMode newMode = null;
for (int i = 0; i < modes.length; i++) {
DisplayMode current = modes[i];
if ((current.getWidth() == 800) && (current.getHeight() == 600)) {
newMode = current;
}
}
if (newMode == null) {
JOptionPane.showMessageDialog(this.frame, "No 800x600 screen mode available!");
return;
}
createFrame();
this.frame.setUndecorated(true);
this.canvas.setResolution(800, 600);
this.canvas.createStrategy();
device.setFullScreenWindow(this.frame);
device.setDisplayMode(newMode);
this.fullscreen = true;
} catch (Throwable e) {
e.printStackTrace();
setFullScreen(false);
JOptionPane.showMessageDialog(this.frame, "Failed to switch to fullscreen");
return;
}
} else {
device.setDisplayMode(this.originalMode);
device.setFullScreenWindow(null);
createFrame();
this.frame.setUndecorated(false);
this.canvas.setResolution(800, 600);
initFrame();
this.fullscreen = false;
return;
}
}
public Graphics getGraphics()
{
return this.frame.getGraphics();
}
public void addKeyListener(KeyListener keyListener)
{
this.frame.addKeyListener(keyListener);
}
public boolean isVisible()
{
return this.frame.isActive();
}
public boolean isActive()
{
return this.frame.isActive();
}
public void exit()
{
System.exit(0);
}
}