|
| 1 | +package wtf.zani.vanillamenu.accessors.lunar; |
| 2 | + |
| 3 | +import org.objectweb.asm.signature.SignatureReader; |
| 4 | +import org.objectweb.asm.signature.SignatureVisitor; |
| 5 | +import org.objectweb.asm.tree.ClassNode; |
| 6 | +import org.objectweb.asm.tree.MethodNode; |
| 7 | +import wtf.zani.vanillamenu.Util; |
| 8 | +import wtf.zani.vanillamenu.accessors.Accessor; |
| 9 | +import wtf.zani.vanillamenu.hooks.delegations.AccountManagerHook; |
| 10 | + |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.*; |
| 14 | +import java.util.concurrent.atomic.AtomicReference; |
| 15 | + |
| 16 | +import static org.objectweb.asm.Opcodes.ACC_PUBLIC; |
| 17 | +import static org.objectweb.asm.Opcodes.ASM9; |
| 18 | + |
| 19 | +public class AccountManagerAccessor extends Accessor { |
| 20 | + private final Method getAccountsMethod; |
| 21 | + private final Method getCurrentAccountMethod; |
| 22 | + private final Method setCurrentAccountMethod; |
| 23 | + |
| 24 | + public AccountManagerAccessor(Object wrapped) throws NoSuchMethodException { |
| 25 | + super(wrapped); |
| 26 | + |
| 27 | + final AtomicReference<String> accountClass = new AtomicReference<>(); |
| 28 | + |
| 29 | + final ClassNode managerNode = AccountManagerHook.getInstance().node; |
| 30 | + final MethodNode getAccountsMethodNode = Util.findMethod(managerNode, methodNode -> { |
| 31 | + if ((methodNode.access & ACC_PUBLIC) <= 0 || methodNode.signature == null) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + final List<String> types = new ArrayList<>(); |
| 36 | + |
| 37 | + final SignatureVisitor visitor = new SignatureVisitor(ASM9) { |
| 38 | + @Override |
| 39 | + public void visitClassType(String name) { |
| 40 | + types.add(name); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + final SignatureReader reader = new SignatureReader(methodNode.signature); |
| 45 | + |
| 46 | + reader.accept(visitor); |
| 47 | + visitor.visitReturnType(); |
| 48 | + |
| 49 | + accountClass.set(types.get(2)); |
| 50 | + |
| 51 | + return types.get(0).equals("java/util/Map"); |
| 52 | + }); |
| 53 | + |
| 54 | + this.getAccountsMethod = wrapped.getClass().getMethod(getAccountsMethodNode.name); |
| 55 | + this.getCurrentAccountMethod = Arrays.stream(wrapped.getClass().getMethods()) |
| 56 | + .filter(method -> |
| 57 | + method.getReturnType().getName().replace('.', '/').equals(accountClass.get())) |
| 58 | + .findFirst() |
| 59 | + .orElseThrow(); |
| 60 | + this.setCurrentAccountMethod = Arrays.stream(wrapped.getClass().getMethods()) |
| 61 | + .filter(method -> { |
| 62 | + if (method.getParameterCount() != 1) return false; |
| 63 | + |
| 64 | + return method.getReturnType().getName().equals(Void.TYPE.getName()) && method.getParameterTypes()[0].getName().replace('.', '/').equals(accountClass.get()); |
| 65 | + }) |
| 66 | + .findFirst() |
| 67 | + .orElseThrow(); |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("unused") |
| 71 | + public static void create(Object accountManager) throws NoSuchMethodException { |
| 72 | + Accessor.accountAccessor = new AccountManagerAccessor(accountManager); |
| 73 | + } |
| 74 | + |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public Map<String, AccountAccessor> getAccounts() { |
| 77 | + try { |
| 78 | + final Map<String, Object> accounts = (Map<String, Object>) this.getAccountsMethod.invoke(this.wrapped); |
| 79 | + final Map<String, AccountAccessor> mappedAccounts = new HashMap<>(); |
| 80 | + |
| 81 | + accounts.forEach((uuid, account) -> mappedAccounts.put(uuid, new AccountAccessor(account))); |
| 82 | + |
| 83 | + return mappedAccounts; |
| 84 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public AccountAccessor getCurrentAccount() { |
| 90 | + try { |
| 91 | + final Object account = this.getCurrentAccountMethod.invoke(this.wrapped); |
| 92 | + |
| 93 | + return account != null ? new AccountAccessor(account) : null; |
| 94 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 95 | + throw new RuntimeException(e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void setAccount(AccountAccessor account) { |
| 100 | + try { |
| 101 | + this.setCurrentAccountMethod.invoke(this.wrapped, account.getWrapped()); |
| 102 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments