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 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 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 + "ClientScript"); SCRIPTABLE_CLASSES.add(p + "ClientPlugin"); SCRIPTABLE_CLASSES.add(p + "ClientSettings"); SCRIPTABLE_CLASSES.add(p + "ScriptLogger"); 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 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 fields = new ArrayList<>(); for (FieldReader f : cls.getFields()) { fields.add(f.getName()); } return fields; } @Override public Collection 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 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.Collections$EmptyList".equals(className) || "java.util.Collections$SingletonList".equals(className) || "java.util.Arrays$ArrayList".equals(className); if (!SCRIPTABLE_CLASSES.contains(className) && !isLangMath && !isLangSys && !isLangStr && !isUtilColl) { return Collections.emptyList(); } ClassReader cls = context.getClassSource().get(className); if (cls == null) { return Collections.emptyList(); } List methods = new ArrayList<>(); for (MethodReader m : cls.getMethods()) { 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) && !"replace".equals(name) && !"isEmpty".equals(name) && !"valueOf".equals(name)) continue; } else if (isUtilColl) { if (!"size".equals(name) && !"isEmpty".equals(name) && !"get".equals(name) && !"add".equals(name) && !"remove".equals(name) && !"clear".equals(name) && !"contains".equals(name) && !"containsKey".equals(name) && !"put".equals(name)) continue; } methods.add(m.getDescriptor()); } return methods; } }