import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
//lib 1005011 // mochadoom
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
class main {
// mochadoom with key mapping
static int escapeKeyCode = KeyEvent.VK_END; // put zero to not remap
static String defaultCfg = "\r\nalwaysrun true\r\nchatmacro0 \"No\"\r\nchatmacro1 \"I'm ready to kick butt!\"\r\nchatmacro2 \"I'm OK.\"\r\nchatmacro3 \"I'm not looking too good!\"\r\nchatmacro4 \"Help!\"\r\nchatmacro5 \"You suck!\"\r\nchatmacro6 \"Next time, scumbag...\"\r\nchatmacro7 \"Come here!\"\r\nchatmacro8 \"I'll take care of it.\"\r\nchatmacro9 \"Yes\"\r\ndetaillevel 0\r\nfullscreen true\r\njoyb_fire 0\r\njoyb_speed 2\r\njoyb_strafe 1\r\njoyb_use 3\r\nkey_fire 97 // 157\r\nkey_left 172\r\nkey_right 174\r\nkey_speed 182\r\nkey_strafe 184\r\nkey_strafeleft 105 // 97\r\nkey_straferight 106 // 100\r\nkey_up 103 // 119\r\nkey_down 108 // 115\r\nkey_use 57 // 32\r\nmb_used 2\r\nmouse_sensitivity 5\r\nmouseb_fire 0\r\nmouseb_forward 2\r\nmouseb_strafe 1\r\nmusic_volume 8\r\nscreenblocks 10\r\nsfx_volume 8\r\nshow_messages true\r\nsnd_channels 3\r\nuse_joystick false\r\nuse_mouse true\r\nusegamma 0\r\n";
public static void main(String[] args) { try {
renameConsole("Mocha DOOM!");
File iwad = loadLibrary("#1005008");
File iwad2 = prepareProgramFile("doom1.wad");
copyFile(iwad, iwad2);
File iwad3 = new File(iwad2.getName());
if (!iwad3.exists()) {
if (eq(first(args), "CDed"))
throw fail("Can't move to correct directory! " + f2s(iwad3.getAbsoluteFile().getParentFile()) + " (should be " + f2s(programDir()) + ")");
else {
classicNohupJavaxWithChdir(programID() + " CDed " + smartJoin(args));
System.exit(0);
}
}
File cfgFile = prepareProgramFile("mochadoom.cfg");
if (!cfgFile.exists() || cic(args, "-overwritecfg")) {
args = toStringArray(listMinus(toList(args), "-overwritecfg"));
saveTextFile(cfgFile, trimAllLines(dropJavaComments(defaultCfg)));
}
String[] argz = eq(first(args), "CDed") ? dropFirst(args) : args;
if (!cic(argz, "-iwad"))
argz = concatStringArrays(new String[] {"-iwad", f2s(iwad2)}, argz);
printStructure(argz);
g.Signals.keyToMapToEscape = escapeKeyCode;
mochadoom.Engine.main(argz);
} catch (Exception __e) { throw rethrow(__e); } }
static void renameConsole(String title) {
setConsoleTitle(title);
}
static File loadLibrary(String snippetID) {
return loadBinarySnippet(snippetID);
}
static File prepareProgramFile(String name) {
return mkdirsForFile(getProgramFile(name));
}
static File prepareProgramFile(String progID, String name) {
return mkdirsForFile(getProgramFile(progID, name));
}
public static void copyFile(File src, File dest) { try {
FileInputStream inputStream = new FileInputStream(src.getPath());
FileOutputStream outputStream = newFileOutputStream(dest.getPath());
try {
copyStream(inputStream, outputStream);
inputStream.close();
} finally {
outputStream.close();
}
} catch (Exception __e) { throw rethrow(__e); } }
static boolean eq(Object a, Object b) {
return a == b || (a == null ? b == null : b != null && a.equals(b));
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static String f2s(String s) { return f2s(newFile(s)); }
static String f2s(java.nio.file.Path p) {
return p == null ? null : f2s(p.toFile());
}
static File programDir_mine; // set this to relocate program's data
static File programDir() {
return programDir(getProgramID());
}
static File programDir(String snippetID) {
boolean me = sameSnippetID(snippetID, programID());
if (programDir_mine != null && me)
return programDir_mine;
File dir = new File(javaxDataDir(), formatSnippetIDOpt(snippetID));
if (me) {
String c = caseID();
if (nempty(c)) dir = newFile(dir, c);
}
return dir;
}
static File programDir(String snippetID, String subPath) {
return new File(programDir(snippetID), subPath);
}
static void classicNohupJavaxWithChdir(String javaxargs) {
classicNohupJavaxWithChdir(javaxargs, "");
}
static void classicNohupJavaxWithChdir(String javaxargs, String vmArgs) { try {
int x = latestInstalledJavaX();
File xfile = new File(userHome(), ".javax/x" + Math.max(x, 30) + ".jar");
if (!xfile.isFile()) {
String url = "http://tinybrain.de/x30.jar";
byte[] data = loadBinaryPage(url);
if (data.length < 1000000)
throw fail("Could not load " + url);
saveBinaryFile(xfile.getPath(), data);
}
String jarPath = xfile.getPath();
if (javaxargs.startsWith("#")) javaxargs = javaxargs.substring(1);
String progID = onlyUntilSpace(javaxargs);
File progDir = programDir(progID);
mkdir(progDir);
print("ProgDir: " + platformQuote(f2s(progDir)));
String javaCmd = "java " + vmArgs + " -jar " + platformQuote(jarPath) + " " + javaxargs;
String cmds;
if (isWindows())
cmds = "(cd " + platformQuote(f2s(progDir)) + " & " + javaCmd + ")";
else
cmds = "/bin/bash -c " + platformQuote(
"cd " + platformQuote(f2s(progDir)) + " ; " + javaCmd);
nohup(cmds);
} catch (Exception __e) { throw rethrow(__e); } }
static String programID() {
return getProgramID();
}
static String programID(Object o) {
return getProgramID(o);
}
// Try to get the quoting right...
static String smartJoin(String[] args) {
if (empty(args)) return "";
if (args.length == 1) return args[0];
String[] a = new String[args.length];
for (int i = 0; i < a.length; i++)
a[i] = !isJavaIdentifier(args[i]) && !isQuoted(args[i]) ? quote(args[i]) : args[i];
return join(" ", a);
}
static String smartJoin(List args) {
return smartJoin(toStringArray(args));
}
static boolean cic(Collection l, String s) {
return containsIgnoreCase(l, s);
}
static boolean cic(String[] l, String s) {
return containsIgnoreCase(l, s);
}
static boolean cic(String s, char c) {
return containsIgnoreCase(s, c);
}
static boolean cic(String a, String b) {
return containsIgnoreCase(a, b);
}
static String[] toStringArray(Collection c) {
String[] a = new String[l(c)];
Iterator it = c.iterator();
for (int i = 0; i < l(a); i++)
a[i] = it.next();
return a;
}
static String[] toStringArray(Object o) {
if (o instanceof String[])
return (String[]) o;
else if (o instanceof Collection)
return toStringArray((Collection) o);
else
throw fail("Not a collection or array: " + getClassName(o));
}
static List listMinus(Collection l, Object... stuff) {
if (l == null) return null;
List l2 = similarEmptyList(l);
Set set = lithashset(stuff);
for (Object o : l)
if (!set.contains(o))
l2.add(o);
return l2;
}
static ArrayList toList(A[] a) { return asList(a); }
static ArrayList toList(int[] a) { return asList(a); }
static ArrayList toList(Set s) { return asList(s); }
static ArrayList toList(Iterable s) { return asList(s); }
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
try {
File file = new File(fileName);
mkdirsForFile(file);
String tempFileName = fileName + "_temp";
File tempFile = new File(tempFileName);
if (contents != null) {
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
vmBus_send("wroteFile", file);
return file;
} finally {
action.done();
}
}
static File saveTextFile(File fileName, String contents) { try {
saveTextFile(fileName.getPath(), contents);
return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
static String trimAllLines(String s) {
return lines(trimAll(lines(s)));
}
static String dropJavaComments(String s) {
return javaDropAllComments(s);
}
static List dropJavaComments(List tok) {
return javaDropAllComments(tok);
}
static String[] dropFirst(int n, String[] a) {
return drop(n, a);
}
static String[] dropFirst(String[] a) {
return drop(1, a);
}
static Object[] dropFirst(Object[] a) {
return drop(1, a);
}
static List dropFirst(List l) {
return dropFirst(1, l);
}
static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); }
static List dropFirst(Iterable i) { return dropFirst(toList(i)); }
static List dropFirst(int n, List l) {
return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}
static List dropFirst(List l, int n) {
return dropFirst(n, l);
}
static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s, int n) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static String[] concatStringArrays(String[]... arrays) {
int l = 0;
for (String[] a : arrays) l += l(a);
String[] x = new String[l];
int i = 0;
for (String[] a : arrays) if (a != null) {
System.arraycopy(a, 0, x, i, l(a));
i += l(a);
}
return x;
}
static void printStructure(String prefix, Object o) {
if (endsWithLetter(prefix)) prefix += ": ";
print(prefix + structureForUser(o));
}
static void printStructure(Object o) {
print(structureForUser(o));
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static void setConsoleTitle(String title) {
callOpt(consoleFrame_gen(), "setTitle", title);
}
static File loadBinarySnippet(String snippetID) { try {
IResourceLoader rl = vm_getResourceLoader();
if (rl != null)
return rl.loadLibrary(snippetID);
long id = parseSnippetID(snippetID);
if (isImageServerSnippet(id)) return loadImageAsFile(snippetID);
File f = DiskSnippetCache_getLibrary(id);
if (fileSize(f) == 0)
f = loadDataSnippetToFile(snippetID);
return f;
} catch (Exception __e) { throw rethrow(__e); } }
public static File mkdirsForFile(File file) {
File dir = file.getParentFile();
if (dir != null) { // is null if file is in current dir
dir.mkdirs();
if (!dir.isDirectory())
if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!");
else throw fail("Unknown IO exception during mkdirs of " + f2s(file));
}
return file;
}
public static String mkdirsForFile(String path) {
mkdirsForFile(new File(path));
return path;
}
static File getProgramFile(String progID, String fileName) {
if (new File(fileName).isAbsolute())
return new File(fileName);
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
static FileOutputStream newFileOutputStream(File path) throws IOException {
return newFileOutputStream(path.getPath());
}
static FileOutputStream newFileOutputStream(String path) throws IOException {
return newFileOutputStream(path, false);
}
static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException {
return newFileOutputStream(path.getPath(), append);
}
static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException {
mkdirsForFile(path);
FileOutputStream f = new FileOutputStream(path, append);
_registerIO(f, path, true);
return f;
}
static void copyStream(InputStream in, OutputStream out) { try {
byte[] buf = new byte[65536];
while (true) {
int n = in.read(buf);
if (n <= 0) return;
out.write(buf, 0, n);
}
} catch (Exception __e) { throw rethrow(__e); } }
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static File newFile(File base, String... names) {
for (String name : names) base = new File(base, name);
return base;
}
static File newFile(String name) {
return name == null ? null : new File(name);
}
static File newFile(String base, String... names) {
return newFile(newFile(base), names);
}
static String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetIDOpt(programID) : "?";
}
// TODO: ask JavaX instead
static String getProgramID(Class c) {
String id = (String) getOpt(c, "programID");
if (nempty(id))
return formatSnippetID(id);
return "?";
}
static String getProgramID(Object o) {
return getProgramID(getMainClass(o));
}
static boolean sameSnippetID(String a, String b) {
if (!isSnippetID(a) || !isSnippetID(b)) return false;
return parseSnippetID(a) == parseSnippetID(b);
}
static File javaxDataDir_dir; // can be set to work on different base dir
static File javaxDataDir() {
return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data");
}
static File javaxDataDir(String... subs) {
return newFile(javaxDataDir(), subs);
}
static String formatSnippetIDOpt(String s) {
return isSnippetID(s) ? formatSnippetID(s) : s;
}
static volatile String caseID_caseID;
static String caseID() { return caseID_caseID; }
static void caseID(String id) {
caseID_caseID = id;
}
static boolean nempty(Collection c) {
return !empty(c);
}
static boolean nempty(CharSequence s) {
return !empty(s);
}
static boolean nempty(Object[] o) { return !empty(o); }
static boolean nempty(byte[] o) { return !empty(o); }
static boolean nempty(int[] o) { return !empty(o); }
static boolean nempty(Map m) {
return !empty(m);
}
static boolean nempty(Iterator i) {
return i != null && i.hasNext();
}
static boolean nempty(Object o) { return !empty(o); }
static int latestInstalledJavaX() {
File[] files = new File(userHome(), ".javax").listFiles();
int v = 0;
if (files != null) for (File f : files) {
Matcher m = Pattern.compile("x(\\d\\d\\d?)\\.jar").matcher(f.getName());
if (m.matches())
v = Math.max(v, Integer.parseInt(m.group(1)));
}
return v;
}
static String _userHome;
static String userHome() {
if (_userHome == null)
return actualUserHome();
return _userHome;
}
static File userHome(String path) {
return new File(userDir(), path);
}
static ThreadLocal