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.*;
import java.text.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import oshi.SystemInfo;
import oshi.software.os.OSProcess;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import java.text.NumberFormat;
class main {
static String iconID = "#1101427";
static double interval = 60.0, timeout = 30.0; // change for fast computers
static int consecutiveFailsThreshold = 3; // TODO: change if loading
static String osBotName = "Discord Bots OS";
static String vmArgs = "--add-opens java.base/jdk.internal.loader=ALL-UNNAMED --add-opens java.base/jdk.internal.module=ALL-UNNAMED --add-opens java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/jdk.internal.reflect=ALL-UNNAMED --add-opens java.base/java.lang.module=ALL-UNNAMED --illegal-access=permit -Xmx1g";
static boolean v6 = false;
static int consecutiveFails;
static volatile String status;
static TrayIcon trayIcon;
static String osProgramID;
public static void main(final String[] args) throws Exception {
// TODO
osProgramID = or(get(args, 0), "#1025342");
// TODO: put "Restart OS Now" in consoleMemoryView()'s popup menu
String myName = "Watch dog for " + osBotName;
bot(myName);
trayIcon = installTrayIcon(iconID, myName,
new Runnable() { public void run() { try { showConsole();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "showConsole();"; }},
"Restart " + osBotName + " Now", new Runnable() { public void run() { try { restartOSNow();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "restartOSNow();"; }},
"Show Watch Dog Window", new Runnable() { public void run() { try { showConsole();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "showConsole();"; }},
"Hide Watch Dog Window", new Runnable() { public void run() { try { hideConsole();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "hideConsole();"; }},
"Exit Watch Dog", runnableThread(new Runnable() { public void run() { try { cleanKill();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "cleanKill();"; }}));
consoleIcon(iconID);
//printWithDateAndTimeInThisThread();
doEvery(interval, new Runnable() { public void run() { try {
boolean ok = false;
try {
long time = sysNow();
String s = sendOptWithTimeout(timeout, osBotName, "live check");
if (match("i'm alive", s)) {
hideConsole();
ok = true;
consecutiveFails = 0;
setStatus(osBotName + " OK");
} else
showConsole();
consoleStatus(ok ? "OK" : "FAIL");
} catch (Throwable __e) { _handleException(__e); }
if (!ok) {
++consecutiveFails;
printAndProgramLog(status = osBotName + " #" + consecutiveFails + " of " + consecutiveFailsThreshold);
/*S stackTraces = sendOptWithTimeout(timeout, osBotName, "stack traces");
if (nempty(stackTraces)) {
stackTraces = unquote(stackTraces);
print(stackTraces);
saveTextFile(newFile(stefansOS_watchDogStackTracesDir(), "discord-audio-stack-trace-" + ymd_minus_hms() + ".txt"), stackTraces);
}*/
try { osFailActivity(); } catch (Throwable __e) { _handleException(__e); }
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "boolean ok = false;\r\n pcall {\r\n long time = sysNow();\r\n String s..."; }});
hideConsole();
sleep();
}
static void osFailActivity() {
if (consecutiveFails >= consecutiveFailsThreshold)
restartOSNow();
}
static void restartOSNow() {
printAndProgramLog("RESTARTING " + osBotName + ".");
consecutiveFails = 0;
hardKillProgram(osProgramID, "level" , 9);
nohupJavax(osProgramID, vmArgs);
}
static String answer(String s) {
final Matches m = new Matches();
if (match("status", s)) return status;
return null;
}
static void setStatus(String status) {
printAndProgramLog(main.status = status);
setTrayIconToolTip(trayIcon, status);
}
static A or(A a, A b) {
return a != null ? a : b;
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return ((DynamicObject) o).fieldValues.get(field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static Android3 bot(String greeting) {
return makeAndroid3(greeting);
}
static Android3 bot(Android3 a) {
return makeBot(a);
}
static Android3 bot(String greeting, Object responder) {
return makeBot(greeting, responder);
}
static Android3 bot() {
return makeBot();
}
static TrayIcon installTrayIcon(String imageID, String tooltip) {
return installTrayIcon(imageID, tooltip, null);
}
// menuItems can also just be a PopupMenu
// also, first of menuItems can be a Runnable for left-click on icon
static TrayIcon installTrayIcon(String imageID, String tooltip, Object... menuItems) { try {
// convenience parameter swap :-)
if (!isSnippetID(imageID) && isSnippetID(tooltip)) {
String s = tooltip;
tooltip = imageID;
imageID = s;
}
return installTrayIcon(imageIcon(imageID).getImage(), tooltip, menuItems);
} catch (Exception __e) { throw rethrow(__e); } }
static TrayIcon installTrayIcon(final Image image, final String tooltip, final Object... _menuItems) { try {
return swing(new F0() { public TrayIcon get() { try {
Runnable leftClick = null;
Object[] menuItems = params_unpackList(_menuItems);
if (l(menuItems) > 0 && menuItems[0] instanceof Runnable) {
leftClick = (Runnable) menuItems[0];
menuItems = dropFirst(menuItems);
}
PopupMenu menu = makePopupMenu(menuItems);
TrayIcon trayIcon = new TrayIcon(image, tooltip, menu);
trayIcon.setImageAutoSize(true);
if (leftClick != null) {
final Runnable _leftClick = leftClick;
onLeftClick(trayIcon, _leftClick);
}
SystemTray.getSystemTray().add(trayIcon);
return trayIcon;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "Runnable leftClick = null;\r\n O[] menuItems = params_unpackList(_menuItems)..."; }});
} catch (Exception __e) { throw rethrow(__e); } }
static void showConsole() {
callOpt(get(javax(), "console"), "showConsole");
}
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 hideConsole() {
final JFrame frame = consoleFrame();
if (frame != null) {
autoVMExit();
swingLater(new Runnable() { public void run() { try {
frame.setVisible(false);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "frame.setVisible(false);"; }});
}
}
static Runnable runnableThread(final Runnable r) {
return new Runnable() { public void run() { try { startThread(r) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "startThread(r)"; }};
}
static void cleanKill() {
cleanKillVM();
}
static void consoleIcon(String imageID) {
frameIcon(consoleFrame(), or2(imageID, javaxDefaultIcon()));
}
// firstDelay = delay
static FixedRateTimer doEvery(long delay, final Object r) {
return doEvery(delay, delay, r);
}
static FixedRateTimer doEvery(long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(shorten(programID() + ": " + r, 80));
timer.scheduleAtFixedRate(smartTimerTask(r, timer, toInt(delay)), toInt(firstDelay), toInt(delay));
return vmBus_timerStarted(timer);
}
// reversed argument order for fun
static FixedRateTimer doEvery(double initialSeconds, double delaySeconds, final Object r) {
return doEvery(toMS(delaySeconds), toMS(initialSeconds), r);
}
static FixedRateTimer doEvery(double delaySeconds, final Object r) {
return doEvery(toMS(delaySeconds), r);
}
static long sysNow() {
ping();
return System.nanoTime()/1000000;
}
static String sendOptWithTimeout(double timeoutSeconds, final String bot, final String text, final Object... args) {
return sendOptWithTimeout(toMS_int(timeoutSeconds), bot, text, args);
}
static String sendOptWithTimeout(int timeout, final String bot, final String text, final Object... args) {
return evalWithTimeoutOrNull(timeout, new F0() { public String get() { try {
return sendOpt(bot, text, args);
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret sendOpt(bot, text, args);"; }});
}
static boolean match(String pat, String s) {
return match3(pat, s);
}
static boolean match(String pat, String s, Matches matches) {
return match3(pat, s, matches);
}
static boolean match(String pat, List toks, Matches matches) {
return match3(pat, toks, matches);
}
static void consoleStatus(String status) {
consoleTitleStatus(status);
}
static volatile PersistableThrowable _handleException_lastException;
static List _handleException_onException = synchroList(ll("printStackTrace2"));
static void _handleException(Throwable e) {
_handleException_lastException = persistableThrowable(e);
Throwable e2 = innerException(e);
if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException)
return;
for (Object f : cloneList(_handleException_onException)) try {
callF(f, e);
} catch (Throwable e3) {
try {
printStackTrace2(e3); // not using pcall here - it could lead to endless loops
} catch (Throwable e4) {
System.out.println(getStackTrace(e3));
System.out.println(getStackTrace(e4));
}
}
}
static A printAndProgramLog(String s, A o) {
printAndProgramLog((endsWithLetterOrDigit(s) ? s + ": " : s) + o);
return o;
}
static A printAndProgramLog(A a) {
String s = str(a);
print("[" + localDateWithMilliseconds() + "] " + s);
logQuotedWithDate(programLog(), s);
return a;
}
static volatile boolean sleep_noSleep = false;
static void sleep(long ms) {
ping();
if (ms < 0) return;
// allow spin locks
if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
try {
Thread.sleep(ms);
} catch (Exception e) { throw new RuntimeException(e); }
}
static void sleep() { try {
if (sleep_noSleep) throw fail("nosleep");
print("Sleeping.");
sleepQuietly();
} catch (Exception __e) { throw rethrow(__e); } }
static List hardKillProgram(final String progID, Object... __) {
print("Trying to hard-kill program " + fsI(progID));
return os_killAll(new F1() { public Boolean get(String cmd) { try {
if (regexpFinds("[/ ]" + psI(progID), cmd)) {
print("! KILLING " + cmd);
return true;
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "if (regexpFinds(\"[/ ]\" + psI(progID), cmd)) {\r\n print(\"! KILLING \" + cmd..."; }}, __);
}
static void nohupJavax(final String javaxargs) {
{ startThread(new Runnable() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "call(hotwireOnce(\"#1008562\"), \"nohupJavax\", javaxargs);"; }}); }
}
static void nohupJavax(final String javaxargs, final String vmArgs) {
{ startThread(new Runnable() { public void run() { try { call(hotwireOnce("#1008562"), "nohupJavax", javaxargs, vmArgs);
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "call(hotwireOnce(\"#1008562\"), \"nohupJavax\", javaxargs, vmArgs);"; }}); }
}
static void setTrayIconToolTip(TrayIcon trayIcon, String toolTip) {
if (trayIcon != null) trayIcon.setToolTip(toolTip);
}
static Throwable printStackTrace2(Throwable e) {
// we go to system.out now - system.err is nonsense
print(getStackTrace2(e));
return e;
}
static void printStackTrace2() {
printStackTrace2(new Throwable());
}
static void printStackTrace2(String msg) {
printStackTrace2(new Throwable(msg));
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(IntRange r) { return r == null ? 0 : r.length(); }
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error",e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error",e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error",e, c);
}
return c;
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
// An "Android" is a program that accepts text questions (on console or TCP) and outputs one response text per question
//please include function myJavaSource. // for getting my known commands
static boolean makeAndroid3_disable = false; // disable all android making
static class Android3 implements AutoCloseable {
String greeting;
boolean publicOverride = false; // optionally set this in client
int startPort = 5000; // optionally set this in client
Responder responder;
boolean console = true;
boolean quiet = false; // no messages on console
boolean daemon = false;
boolean incomingSilent = false;
int incomingPrintLimit = 200;
boolean useMultiPort = true;
boolean recordHistory = false;
boolean verbose = false;
int answerPrintLimit = 500;
boolean newLineAboveAnswer, newLineBelowAnswer;
// set by system
int port;
long vport;
DialogHandler handler;
ServerSocket server;
Android3(String greeting) {
this.greeting = greeting;}
Android3() {}
public void close() { dispose(); }
synchronized void dispose() {
if (server != null) {
try {
server.close();
} catch (IOException e) {
print("[internal] " + e);
}
server = null;
}
if (vport != 0) { try {
print("Disposing " + this);
removeFromMultiPort(vport);
vport = 0;
} catch (Throwable __e) { _handleException(__e); }}
}
public String toString() { return "Bot: " + greeting + " [vport " + vport + "]"; }
}
static abstract class Responder {
abstract String answer(String s, List history);
}
static Android3 makeAndroid3(final String greeting) {
return makeAndroid3(new Android3(greeting));
}
static Android3 makeAndroid3(final String greeting, Responder responder) {
Android3 android = new Android3(greeting);
android.responder = responder;
return makeAndroid3(android);
}
static Android3 makeAndroid3(final Android3 a) {
if (makeAndroid3_disable) return a;
if (a.responder == null)
a.responder = new Responder() {
String answer(String s, List history) {
return callStaticAnswerMethod(s, history);
}
};
if (!a.quiet)
print("[bot] " + a.greeting);
if (a.console && (readLine_noReadLine || makeAndroid3_consoleInUse()))
a.console = false;
record(a);
if (a.useMultiPort)
a.vport = addToMultiPort(a.greeting,
makeAndroid3_verboseResponder(a));
if (a.console)
makeAndroid3_handleConsole(a);
if (a.useMultiPort) return a;
a.handler = makeAndroid3_makeDialogHandler(a);
if (a.quiet) startDialogServer_quiet.set(true);
try {
a.port = a.daemon
? startDialogServerOnPortAboveDaemon(a.startPort, a.handler)
: startDialogServerOnPortAbove(a.startPort, a.handler);
} finally {
startDialogServer_quiet.set(null);
}
a.server = startDialogServer_serverSocket;
return a;
}
static void makeAndroid3_handleConsole(final Android3 a) {
// Console handling stuff
if (!a.quiet)
print("You may also type on this console.");
{ startThread(new Runnable() { public void run() { try { List history = new ArrayList();
while (licensed()) {
String line;
try {
line = readLine();
} catch (Throwable e) {
print(getInnerMessage(e));
break;
}
if (line == null) break;
/*if (eq(line, "bye")) {
print("> bye stranger");
history = new ArrayList();
} else*/ {
history.add(line);
history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too
}
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "List history = new ArrayList();\r\n while (licensed()) {\r\n Stri..."; }}); }
}
static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) {
return new DialogHandler() {
public void run(final DialogIO io) {
if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) {
io.sendLine("Sorry, not allowed");
return;
}
String dialogID = randomID(8);
io.sendLine(a.greeting + " / Your ID: " + dialogID);
List history = new ArrayList();
while (io.isStillConnected()) {
if (io.waitForLine()) {
final String line = io.readLineNoBlock();
String s = dialogID + " at " + now() + ": " + quote(line);
if (!a.incomingSilent)
print(shorten(s, a.incomingPrintLimit));
if (eq(line, "bye")) {
io.sendLine("bye stranger");
return;
}
Matches m = new Matches();
if (a.recordHistory)
history.add(line);
String answer;
if (match3("this is a continuation of talk *", s, m)
|| match3("hello bot! this is a continuation of talk *", s, m)) {
dialogID = unquote(m.m[0]);
answer = "ok";
} else try {
makeAndroid3_io.set(io);
answer = makeAndroid3_getAnswer(line, history, a);
} finally {
makeAndroid3_io.set(null);
}
if (a.recordHistory)
history.add(answer);
io.sendLine(answer);
//appendToLog(logFile, s);
}
}
}};
}
static String makeAndroid3_getAnswer(String line, List history, Android3 a) {
String answer, originalAnswer;
try {
originalAnswer = a.responder.answer(line, history);
answer = makeAndroid3_fallback(line, history, originalAnswer);
} catch (Throwable e) {
e = getInnerException(e);
printStackTrace(e);
originalAnswer = answer = e.toString();
}
if (!a.incomingSilent) {
if (originalAnswer == null) originalAnswer = "?";
if (a.newLineAboveAnswer) print();
print(">" + dropFirst(indentx(2, shorten(rtrim(originalAnswer), a.answerPrintLimit))));
if (a.newLineBelowAnswer) print();
}
return answer;
}
static String makeAndroid3_fallback(String s, List history, String answer) {
// Now we only do the safe thing instead of VM inspection - give out our process ID
if (answer == null && match3("what is your pid", s))
return getPID();
if (answer == null && match3("what is your program id", s)) // should be fairly safe, right?
return getProgramID();
if (match3("get injection id", s))
return getInjectionID();
if (answer == null) answer = "?";
if (answer.indexOf('\n') >= 0 || answer.indexOf('\r') >= 0)
answer = quote(answer);
return answer;
}
static boolean makeAndroid3_consoleInUse() {
if (isTrue(vm_generalMap_get("consoleInUse"))) return true;
for (Object o : record_list)
if (o instanceof Android3 && ((Android3) o).console)
return true;
return false;
}
static Responder makeAndroid3_verboseResponder(final Android3 a) {
return new Responder() {
String answer(String s, List history) {
if (a.verbose)
print("> " + shorten(s, a.incomingPrintLimit));
String answer = a.responder.answer(s, history);
if (a.verbose)
print("< " + shorten(answer, a.incomingPrintLimit));
return answer;
}
};
}
static ThreadLocal makeAndroid3_io = new ThreadLocal();
static Android3 makeAndroid3() {
return makeAndroid3(getProgramTitle() + ".");
}
static int makeBot(String greeting) {
return makeAndroid3(greeting).port;
}
static Android3 makeBot(Android3 a) {
makeAndroid3(a);
return a;
}
static Android3 makeBot(String greeting, Object responder) {
Android3 a = new Android3(greeting);
a.responder = makeResponder(responder);
makeBot(a);
return a;
}
static Android3 makeBot() {
return makeAndroid3(defaultBotName());
}
public static boolean isSnippetID(String s) {
try {
parseSnippetID(s);
return true;
} catch (RuntimeException e) {
return false;
}
}
static int imageIcon_cacheSize = 10;
static boolean imageIcon_verbose = false;
static Map imageIcon_cache;
static Lock imageIcon_lock = lock();
static ThreadLocal imageIcon_fixGIF = new ThreadLocal();
// not going through BufferedImage preserves animations
static ImageIcon imageIcon(String imageID) { try {
if (imageID == null) return null;
Lock __0 = imageIcon_lock; lock(__0); try {
if (imageIcon_cache == null)
imageIcon_cache = new MRUCache(imageIcon_cacheSize);
imageID = fsI(imageID);
ImageIcon ii = imageIcon_cache.get(imageID);
if (ii == null) {
if (imageIcon_verbose) print("Loading image icon: " + imageID);
File f = loadBinarySnippet(imageID);
Boolean b = imageIcon_fixGIF.get();
if (!isFalse(b))
ii = new ImageIcon(loadBufferedImageFixingGIFs(f));
else
ii = new ImageIcon(f.toURI().toURL());
} else
imageIcon_cache.remove(imageID); // move to front of cache on access
imageIcon_cache.put(imageID, ii);
return ii;
} finally { unlock(__0); } } catch (Exception __e) { throw rethrow(__e); } }
// doesn't fix GIFs
static ImageIcon imageIcon(File f) { try {
return new ImageIcon(f.toURI().toURL());
} catch (Exception __e) { throw rethrow(__e); } }
static ImageIcon imageIcon(Image img) {
return new ImageIcon(img);
}
static Object swing(Object f) {
return swingAndWait(f);
}
static A swing(F0 f) {
return (A) swingAndWait(f);
}
static A swing(IF0 f) {
return (A) swingAndWait(f);
}
static Object[] params_unpackList(Object[] params) {
if (l(params) == 1 && params[0] instanceof List)
return toObjectArray((List) params[0]);
return params;
}
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 PopupMenu makePopupMenu(Object... x) {
if (x == null) return null;
if (l(x) == 1 && x[0] instanceof PopupMenu)
return (PopupMenu) x[0];
PopupMenu m = new PopupMenu();
for (int i = 0; i < l(x); i++) {
Object o = x[i];
if (eqOneOf(o, "***", "---", "===", ""))
m.addSeparator();
else if (o instanceof String && get(x, i+1) instanceof Runnable)
m.add(menuItem((String) o, get(x, ++i)));
else if (o instanceof String || o instanceof Menu || o instanceof MenuItem)
call(m, "add", o);
else
print("Unknown menu item: " + o);
}
return m;
}
// runnable takes an argument
static A onLeftClick(final A c, final Object runnable) {
{ swing(new Runnable() { public void run() { try {
c.addMouseListener(leftClickMouseAdapter(runnable));
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.addMouseListener(leftClickMouseAdapter(runnable));"; }}); }
return c;
}
// runnable takes no args
static TrayIcon onLeftClick(final TrayIcon c, final Object runnable) {
{ swing(new Runnable() { public void run() { try {
c.addMouseListener(leftClickMouseAdapter_noPt(runnable));
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "c.addMouseListener(leftClickMouseAdapter_noPt(runnable));"; }}); }
return c;
}
static A onLeftClick(final Object runnable, A c) {
return onLeftClick(c, runnable);
}
static Object callOpt(Object o) {
return callF(o);
}
static A callOpt(Object o, String method, Object... args) {
return (A) callOpt_withVarargs(o, method, args);
}
static Class javax() {
return getJavaX();
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
static JFrame consoleFrame() {
return (JFrame) getOpt(get(getJavaX(), "console"), "frame");
}
static void autoVMExit() {
call(getJavaX(), "autoVMExit");
}
static void swingLater(long delay, final Object r) {
javax.swing.Timer timer = new javax.swing.Timer(toInt(delay), actionListener(wrapAsActivity(r)));
timer.setRepeats(false);
timer.start();
}
static void swingLater(Object r) {
SwingUtilities.invokeLater(toRunnable(r));
}
static Thread startThread(Object runnable) {
return startThread(defaultThreadName(), runnable);
}
static Thread startThread(String name, Object runnable) {
runnable = wrapAsActivity(runnable);
return startThread(newThread(toRunnable(runnable), name));
}
static Thread startThread(Thread t) {
_registerThread(t);
t.start();
return t;
}
static void cleanKillVM() { try {
ping();
assertNotOnAWTThread();
cleanKillVM_noSleep();
Object o = new Object();
synchronized(o) { o.wait(); }
} catch (Exception __e) { throw rethrow(__e); } }
static void cleanKillVM_noSleep() {
call(getJavaX(), "cleanKill");
}
static Component frameIcon(Component c, String imageID) {
setFrameIconLater(c, imageID);
return c;
}
static Component frameIcon(String imageID, Component c) {
setFrameIconLater(c, imageID);
return c;
}
static String or2(String a, String b) {
return nempty(a) ? a : b;
}
static String or2(String a, String b, String c) {
return or2(or2(a, b), c);
}
static String javaxDefaultIcon() {
return "#1005557";
}
static int shorten_default = 100;
static String shorten(CharSequence s) { return shorten(s, shorten_default); }
static String shorten(CharSequence s, int max) {
return shorten(s, max, "...");
}
static String shorten(CharSequence s, int max, String shortener) {
if (s == null) return "";
if (max < 0) return str(s);
return s.length() <= max ? str(s) : subCharSequence(s, 0, min(s.length(), max-l(shortener))) + shortener;
}
static String shorten(int max, CharSequence s) { return shorten(s, max); }
static String programID() {
return getProgramID();
}
static String programID(Object o) {
return getProgramID(o);
}
// r may return false to cancel timer
static TimerTask smartTimerTask(Object r, java.util.Timer timer, long delay) {
return new SmartTimerTask(r, timer, delay, _threadInfo());
}
static class SmartTimerTask extends TimerTask implements IFieldsToList{
Object r;
java.util.Timer timer;
long delay;
Object threadInfo;
SmartTimerTask() {}
SmartTimerTask(Object r, java.util.Timer timer, long delay, Object threadInfo) {
this.threadInfo = threadInfo;
this.delay = delay;
this.timer = timer;
this.r = r;}
public String toString() { return shortClassName(this) + "(" + r + ", " + timer + ", " + delay + ", " + threadInfo + ")"; }public Object[] _fieldsToList() { return new Object[] {r, timer, delay, threadInfo}; }
long lastRun;
public void run() {
if (!licensed())
timer.cancel();
else {
_threadInheritInfo(threadInfo);
AutoCloseable __1 = tempActivity(r); try {
lastRun = fixTimestamp(lastRun);
long now = now();
if (now >= lastRun + delay*0.9) {
lastRun = now;
if (eq(false, pcallF(r)))
timer.cancel();
}
} finally { _close(__1); }}
}
}
static int toInt(Object o) {
if (o == null) return 0;
if (o instanceof Number)
return ((Number) o).intValue();
if (o instanceof String)
return parseInt(((String) o));
if (o instanceof Boolean)
return boolToInt(((Boolean) o));
throw fail("woot not int: " + getClassName(o));
}
static int toInt(long l) {
if (l != (int) l) throw fail("Too large for int: " + l);
return (int) l;
}
static A vmBus_timerStarted(A timer) {
vmBus_send("timerStarted",timer, costCenter());
return timer;
}
static long toMS(double seconds) {
return (long) (seconds*1000);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static int toMS_int(double seconds) {
return toInt_checked((long) (seconds*1000));
}
static Object evalWithTimeoutOrNull(final Object f, int timeoutMS) {
return evalWithTimeoutOrNull(timeoutMS, f);
}
static Object evalWithTimeoutOrNull(int timeoutMS, final Object f) {
return eitherAOpt(evalWithTimeout(timeoutMS, f));
}
static Object evalWithTimeoutOrNull(double timeoutSeconds, final Object f) {
return eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}
static A evalWithTimeoutOrNull(int timeoutMS, F0 f) {
return (A) eitherAOpt(evalWithTimeout(timeoutMS, f));
}
static A evalWithTimeoutOrNull(double timeoutSeconds, F0 f) {
return (A) eitherAOpt(evalWithTimeout(timeoutSeconds, f));
}
static String sendOpt(String bot, String text, Object... args) {
return sendToLocalBotOpt(bot, text, args);
}
static boolean match3(String pat, String s) {
return match3(pat, s, null);
}
static boolean match3(String pat, String s, Matches matches) {
if (pat == null || s == null) return false;
return match3(pat, parse3_cachedInput(s), matches);
}
static boolean match3(String pat, List toks, Matches matches) {
List tokpat = parse3_cachedPattern(pat);
return match3(tokpat, toks, matches);
}
static boolean match3(List tokpat, List toks, Matches matches) {
String[] m = match2(tokpat, toks);
//print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
if (m == null) return false;
if (matches != null) matches.m = m; return true;
}
static void consoleTitleStatus(String status) {
titleStatus(consoleFrame(), status);
}
static List synchroList() {
return Collections.synchronizedList(new ArrayList());
}
static List synchroList(List l) {
return Collections.synchronizedList(l);
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static PersistableThrowable persistableThrowable(Throwable e) {
return e == null ? null : new PersistableThrowable(e);
}
static Throwable innerException(Throwable e) {
return getInnerException(e);
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
static ArrayList cloneList(Iterable l) {
return l instanceof Collection ? cloneList((Collection) l) : asList(l);
}
static ArrayList cloneList(Collection l) {
if (l == null) return new ArrayList();
synchronized(collectionMutex(l)) {
return new ArrayList(l);
}
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static C callF(F2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Object f, Object... args) {
if (f instanceof String)
return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static String getStackTrace(Throwable throwable) {
lastException(throwable);
return getStackTrace_noRecord(throwable);
}
static String getStackTrace_noRecord(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return hideCredentials(writer.toString());
}
static String getStackTrace() {
return getStackTrace_noRecord(new Throwable());
}
static boolean endsWithLetterOrDigit(String s) {
return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1));
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal