Files
06web/src/main/java/org/scape2006/web/WebReflectionSupplier.java
T
Dark98 e57315b785 feat(web): add Pause in Background option, TeaVM reflection filtering, and HUD drawImage guards
Improve webclient stability, background resource usage, and script reflection compatibility across desktop and mobile browsers.

### Web Client Features & Fixes
- `index.html` & `RSApplet.java`
  - Added a **Pause in Background** (`#opt-pause-bg`) option under **Display & Graphics** persisted in `localStorage` (`2006scape_web_options.pauseInBackground`) and synced live via `window.scapePauseInBackground`.
  - Added `visibilitychange` Web Audio suspend/resume handling in `index.html` and `isPausedInBackground()` loop throttling in `RSApplet.run()` (resetting timing buffers on resume to avoid tick bursts).
- `DesktopHooks.java`
  - Added `naturalWidth > 0` guards to `jsDrawModernMinimap` and `jsDrawModernChat` to prevent `InvalidStateError` on `drawImage` when HUD images are still loading or broken.

### TeaVM Script Reflection
- `WebReflectionSupplier.java`
  - Added `hasUnscriptableParam(...)` to exclude methods referencing `java.util.function.*`, `java.lang.Thread*`, `java.io.*`, or `java.awt.*` from TeaVM reflection metadata.
2026-12-02 17:20:27 +00:00

162 lines
8.1 KiB
Java

package org.scape2006.web;
import org.teavm.classlib.ReflectionContext;
import org.teavm.classlib.ReflectionSupplier;
import org.teavm.model.ClassReader;
import org.teavm.model.FieldReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;
import org.teavm.model.ValueType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* TeaVM ReflectionSupplier ensuring ScriptInterpreter can reflectively invoke
* PluginContext, PluginGraphics, ClientScript, and game entity APIs at runtime.
*/
public final class WebReflectionSupplier implements ReflectionSupplier {
private static final Set<String> SCRIPTABLE_CLASSES = new HashSet<>();
static {
String p = "org.scape2006.web.";
SCRIPTABLE_CLASSES.add(p + "PluginContext");
SCRIPTABLE_CLASSES.add(p + "PluginContext$WorldArea");
SCRIPTABLE_CLASSES.add(p + "PluginContext$NpcWrapper");
SCRIPTABLE_CLASSES.add(p + "PluginContext$PlayerWrapper");
SCRIPTABLE_CLASSES.add(p + "PluginContext$GroundItemWrapper");
SCRIPTABLE_CLASSES.add(p + "PluginContext$SceneObjectWrapper");
SCRIPTABLE_CLASSES.add(p + "PluginContext$InventoryItemWrapper");
SCRIPTABLE_CLASSES.add(p + "PluginContext$Prayer");
SCRIPTABLE_CLASSES.add(p + "PluginContext$Spell");
SCRIPTABLE_CLASSES.add(p + "PluginContext$Query");
SCRIPTABLE_CLASSES.add(p + "PluginContext$NpcQuery");
SCRIPTABLE_CLASSES.add(p + "PluginContext$GroundItemQuery");
SCRIPTABLE_CLASSES.add(p + "PluginContext$ObjectQuery");
SCRIPTABLE_CLASSES.add(p + "PluginContext$InventoryQuery");
SCRIPTABLE_CLASSES.add(p + "PluginContext$PlayerQuery");
SCRIPTABLE_CLASSES.add(p + "PluginGraphics");
SCRIPTABLE_CLASSES.add(p + "PluginConfigItem");
SCRIPTABLE_CLASSES.add(p + "MenuEntry");
SCRIPTABLE_CLASSES.add(p + "ClientScript");
SCRIPTABLE_CLASSES.add(p + "ClientPlugin");
SCRIPTABLE_CLASSES.add(p + "ClientSettings");
SCRIPTABLE_CLASSES.add(p + "ScriptLogger");
SCRIPTABLE_CLASSES.add(p + "WorldWebWalker");
SCRIPTABLE_CLASSES.add(p + "Game");
SCRIPTABLE_CLASSES.add(p + "Player");
SCRIPTABLE_CLASSES.add(p + "NPC");
SCRIPTABLE_CLASSES.add(p + "Item");
SCRIPTABLE_CLASSES.add(p + "ItemDef");
SCRIPTABLE_CLASSES.add(p + "EntityDef");
SCRIPTABLE_CLASSES.add(p + "ObjectDef");
SCRIPTABLE_CLASSES.add(p + "Skills");
SCRIPTABLE_CLASSES.add(p + "RSInterface");
SCRIPTABLE_CLASSES.add(p + "RSColors");
SCRIPTABLE_CLASSES.add(p + "ItemID");
SCRIPTABLE_CLASSES.add(p + "StaticItemList");
SCRIPTABLE_CLASSES.add(p + "SkillID");
SCRIPTABLE_CLASSES.add(p + "NpcID");
SCRIPTABLE_CLASSES.add(p + "StaticNpcList");
SCRIPTABLE_CLASSES.add(p + "ObjectID");
SCRIPTABLE_CLASSES.add(p + "StaticObjectList");
SCRIPTABLE_CLASSES.add(p + "ScriptInterpreter");
SCRIPTABLE_CLASSES.add(p + "ScriptInterpreter$Environment");
SCRIPTABLE_CLASSES.add(p + "ScriptInterpreter$InterpretedScript");
SCRIPTABLE_CLASSES.add(p + "ScriptInterpreter$InterpretedPlugin");
}
@Override
public Collection<String> getAccessibleFields(ReflectionContext context, String className) {
if (!SCRIPTABLE_CLASSES.contains(className)) {
return Collections.emptyList();
}
ClassReader cls = context.getClassSource().get(className);
if (cls == null) {
return Collections.emptyList();
}
List<String> fields = new ArrayList<>();
for (FieldReader f : cls.getFields()) {
fields.add(f.getName());
}
return fields;
}
@Override
public Collection<MethodDescriptor> getAccessibleMethods(ReflectionContext context, String className) {
boolean isLangMath = "java.lang.Math".equals(className);
boolean isLangSys = "java.lang.System".equals(className);
boolean isLangStr = "java.lang.String".equals(className);
boolean isLangSb = "java.lang.StringBuilder".equals(className);
boolean isUtilColl = "java.util.List".equals(className) || "java.util.ArrayList".equals(className)
|| "java.util.Collection".equals(className) || "java.util.Map".equals(className)
|| "java.util.HashMap".equals(className) || "java.util.LinkedHashMap".equals(className)
|| "java.util.Set".equals(className) || "java.util.HashSet".equals(className)
|| "java.util.Collections$EmptyList".equals(className)
|| "java.util.Collections$SingletonList".equals(className)
|| "java.util.Arrays$ArrayList".equals(className);
if (!SCRIPTABLE_CLASSES.contains(className) && !isLangMath && !isLangSys && !isLangStr && !isLangSb && !isUtilColl) {
return Collections.emptyList();
}
ClassReader cls = context.getClassSource().get(className);
if (cls == null) {
return Collections.emptyList();
}
List<MethodDescriptor> methods = new ArrayList<>();
for (MethodReader m : cls.getMethods()) {
if (hasUnscriptableParam(m.getDescriptor())) continue;
String name = m.getName();
if (isLangSys) {
if (!"currentTimeMillis".equals(name) && !"nanoTime".equals(name)) continue;
} else if (isLangMath) {
if (!"max".equals(name) && !"min".equals(name) && !"abs".equals(name)
&& !"round".equals(name) && !"floor".equals(name) && !"ceil".equals(name)
&& !"sqrt".equals(name) && !"pow".equals(name) && !"hypot".equals(name)
&& !"random".equals(name)) continue;
} else if (isLangStr) {
if (!"length".equals(name) && !"equals".equals(name) && !"equalsIgnoreCase".equals(name)
&& !"contains".equals(name) && !"startsWith".equals(name) && !"endsWith".equals(name)
&& !"toLowerCase".equals(name) && !"toUpperCase".equals(name) && !"trim".equals(name)
&& !"substring".equals(name) && !"indexOf".equals(name) && !"lastIndexOf".equals(name)
&& !"replace".equals(name) && !"charAt".equals(name) && !"split".equals(name)
&& !"isEmpty".equals(name) && !"valueOf".equals(name)) continue;
} else if (isLangSb) {
if (!"append".equals(name) && !"toString".equals(name) && !"length".equals(name)
&& !"setLength".equals(name) && !"charAt".equals(name)
&& !"insert".equals(name) && !"delete".equals(name)) continue;
} else if (isUtilColl) {
if (!"size".equals(name) && !"isEmpty".equals(name) && !"get".equals(name)
&& !"set".equals(name) && !"add".equals(name) && !"remove".equals(name)
&& !"clear".equals(name) && !"contains".equals(name) && !"indexOf".equals(name)
&& !"containsKey".equals(name) && !"containsValue".equals(name)
&& !"put".equals(name) && !"keySet".equals(name) && !"values".equals(name)) continue;
}
methods.add(m.getDescriptor());
}
return methods;
}
private static boolean hasUnscriptableParam(MethodDescriptor desc) {
ValueType[] params = desc.getParameterTypes();
for (int i = 0; i < params.length; i++) {
ValueType vt = params[i];
while (vt instanceof ValueType.Array) {
vt = ((ValueType.Array) vt).getItemType();
}
if (vt instanceof ValueType.Object) {
String clsName = ((ValueType.Object) vt).getClassName();
if (clsName.startsWith("java.util.function.") || clsName.startsWith("java.lang.Thread")
|| clsName.startsWith("java.io.") || clsName.startsWith("java.awt.")) {
return true;
}
}
}
return false;
}
}