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 java.util.function.*;
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 java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
import java.awt.geom.*;
class main {
static void dm_profileThisThread(Runnable r) { dm_profileThisThread(poorMansProfiling_defaultInterval(), r); }
static void dm_profileThisThread(int interval, Runnable r) {
dm_showText("Profile", pairB(profileThisThreadToString(r)));
}
// in ms
static int poorMansProfiling_defaultInterval() {
return 25;
}
static String dm_showText(String text) {
return dm_showNewModuleWithFields("#1016122/TextArea", "text", text);
}
static String dm_showText(String title, String text) {
String mod = dm_showText(text);
dm_setModuleName(mod, title);
return mod;
}
// convenience synonym for dm_showLines
static String dm_showText(Iterable l) {
return dm_showLines(l);
}
static B pairB(Pair p) {
return p == null ? null : p.b;
}
static String profileThisThreadToString(Runnable r) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), r); }
static String profileThisThreadToString(int interval, Runnable r) {
poorMansProfiling(interval, currentThread());
try {
r.run();
} catch (Throwable __e) { printStackTrace(__e); }
return poorMansProfiling_stopAndRenderResults();
}
static Pair profileThisThreadToString(IF0 f) { return profileThisThreadToString(poorMansProfiling_defaultInterval(), f); }
static Pair profileThisThreadToString(int interval, IF0 f) {
Var var = new Var();
String profile = profileThisThreadToString(interval, new Runnable() { public void run() { try { var.set(callF(f)) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(callF(f))"; }});
return pair(var.get(), profile);
}
static String dm_showNewModuleWithFields(String moduleLibID, Object... params) {
return dm_showNewModuleWithParams(moduleLibID, params);
}
static void dm_setModuleName(Object module, String name) {
dm_callModule(module, "setModuleName", name);
}
static void dm_setModuleName(String name) {
dm_setModuleName(dm_current_mandatory_generic(), name);
}
static String dm_showLines(Iterable l) {
return dm_showText(lines(l));
}
static MultiSet poorMansProfiling_results = new MultiSet();
static int poorMansProfiling_samples;
static java.util.Timer poorMansProfiling_timer;
static Lock poorMansProfiling_lock = lock();
static Thread poorMansProfiling_threadToSample; // otherwise sample all threads
static void poorMansProfiling() {
poorMansProfiling(poorMansProfiling_defaultInterval());
}
static void poorMansProfiling(Thread thread) {
poorMansProfiling(poorMansProfiling_defaultInterval(), thread);
}
static void poorMansProfiling(int interval) {
poorMansProfiling(interval, null);
}
static void poorMansProfiling(int interval, Thread thread) {
Lock __0 = poorMansProfiling_lock; lock(__0); try {
poorMansProfiling_threadToSample = thread;
poorMansProfiling_stop();
poorMansProfiling_clear();
poorMansProfiling_timer = doEvery_daemon(interval, new Runnable() { public void run() { try {
Map map =
poorMansProfiling_threadToSample != null
? litmap(poorMansProfiling_threadToSample, poorMansProfiling_threadToSample.getStackTrace())
: runnableThreadsWithStackTraces();
Lock __1 = poorMansProfiling_lock; lock(__1); try {
poorMansProfiling_samples++;
for (Thread t : keys(map)) {
{ if (isSystemThread(t)) continue; }
StringBuilder buf = new StringBuilder();
for (StackTraceElement e : map.get(t))
buf.append(e).append("\n");
poorMansProfiling_results.add(str(buf));
}
} finally { unlock(__1); }
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Map map =\r\n poorMansProfiling_threadToSampl..."; }});
} finally { unlock(__0); } }
static void poorMansProfiling_stop() {
Lock __2 = poorMansProfiling_lock; lock(__2); try {
if (poorMansProfiling_timer != null) {
stopTimer(poorMansProfiling_timer);
poorMansProfiling_timer = null;
}
} finally { unlock(__2); } }
static void poorMansProfiling_clear() {
Lock __3 = poorMansProfiling_lock; lock(__3); try {
poorMansProfiling_results.clear();
poorMansProfiling_samples = 0;
} finally { unlock(__3); } }
static MultiSet poorMansProfiling_results() {
return new MultiSet(poorMansProfiling_results);
}
static MultiSet poorMansProfiling_stopAndGetResults() {
Lock __4 = poorMansProfiling_lock; lock(__4); try {
poorMansProfiling_stop();
return poorMansProfiling_results();
} finally { unlock(__4); } }
static Thread currentThread() {
return Thread.currentThread();
}
static A printStackTrace(A e) {
// we go to system.out now - system.err is nonsense
if (e != null) print(getStackTrace(e));
return e;
}
static void printStackTrace() {
printStackTrace(new Throwable());
}
static void printStackTrace(String msg) {
printStackTrace(new Throwable(msg));
}
static void printStackTrace(String msg, Throwable e) {
printStackTrace(new Throwable(msg, e));
}
static String poorMansProfiling_stopAndRenderResults() {
return poorMansProfiling_renderFullResults(poorMansProfiling_stopAndGetResults());
}
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 B callF(A a, IF1 f) {
return f == null ? null : f.get(a);
}
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 void callF(A a, IVF1 f) {
if (f != null) f.get(a);
}
static void callF(IVF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Runnable r) { { if (r != null) r.run(); } return null; }
static Object callF(Object f, Object... args) {
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) {
if (f instanceof String)
throw fail("Legacy call: " + f);
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 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 Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static String dm_showNewModuleWithParams(String moduleLibID, Object... params) {
String moduleID = dm_makeNewModuleWithParams(moduleLibID, params);
dm_showModule(moduleID);
return moduleID;
}
static Object dm_callModule(Object moduleOrID, String method, Object... args) {
Object mod = dm_getModule(moduleOrID);
if (mod == null) return null;
AutoCloseable __1 = dm_enter(mod); try {
return call(mod, method, args);
} finally { _close(__1); }}
static Object dm_current_mandatory_generic() {
return assertNotNull("No module set", dm_current_generic());
}
static String lines(Iterable lines) { return fromLines(lines); }
static String lines(Object[] lines) { return fromLines(asList(lines)); }
static List lines(String s) { return toLines(s); }
// convenience map call
static String lines(Iterable l, IF1 f) {
return mapToLines(l, f);
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
// firstDelay = delay
static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, final Object r) {
return doEvery_daemon(timerName, delay, delay, r);
}
static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, firstDelay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(true);
timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
return timer;
}
static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) {
return doEvery_daemon(toMS(delaySeconds), r);
}
static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
static void litmap_impl(Map map, Object... x) {
if (x != null) for (int i = 0; i < x.length-1; i += 2)
if (x[i+1] != null)
map.put(x[i], x[i+1]);
}
static Map runnableThreadsWithStackTraces() {
Map map = filterMap((__21, __22) -> isThreadRunnable_x(__21, __22), Thread.getAllStackTraces());
map.remove(currentThread());
return map;
}
static Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
// convenience shortcut for keys_gen
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiSet ms) {
return ms.keySet();
}
static Set keys(MultiMap mm) {
return mm.keySet();
}
static boolean isSystemThread(Thread t) {
ThreadGroup g = t.getThreadGroup();
return g != null && g.getName().equals("system");
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static void unlock(Lock lock, String msg) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
print("Unlocked: " + msg); // print afterwards to make sure the lock is always unlocked
}
static void unlock(Lock lock) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
}
static void stopTimer(java.util.Timer timer) {
if (timer != null) timer.cancel();
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
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