From af237a3bd2a9d46f24a8ccb2f43c1e43cb8be902 Mon Sep 17 00:00:00 2001 From: Pedro Diaz Date: Wed, 28 Sep 2016 16:52:35 -0400 Subject: [PATCH] Rename seems to work... Also, there's no design pattern here, refactor is coming --- .../com/icecream/snorlax/common/Strings.java | 10 +- .../module/feature/mitm/MitmInputStream.java | 3 - .../module/feature/mitm/MitmProvider.java | 104 ++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/icecream/snorlax/common/Strings.java b/app/src/main/java/com/icecream/snorlax/common/Strings.java index a4e0aa1..01a2b82 100644 --- a/app/src/main/java/com/icecream/snorlax/common/Strings.java +++ b/app/src/main/java/com/icecream/snorlax/common/Strings.java @@ -33,7 +33,15 @@ public static String emptyToNull(String string) { } public static boolean isNullOrEmpty(String string) { - return string == null || string.trim().length() == 0; + return isNull(string) || isEmpty(string); + } + + public static boolean isNull(String string) { + return string == null; + } + + public static boolean isEmpty(String string) { + return string.trim().length() == 0; } public static String valueOrDefault(String string, String defaultString) { diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmInputStream.java b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmInputStream.java index 67586cb..d57972d 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmInputStream.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmInputStream.java @@ -100,12 +100,9 @@ private void mitmStream() { mByteBuffer.hasRemaining() ); - // Lets keep it read only for now - /* if (fromMitm != null) { mByteBuffer = fromMitm; } - */ mMitmDone = true; } } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmProvider.java b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmProvider.java index caa3b45..acb3ec0 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmProvider.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/mitm/MitmProvider.java @@ -17,15 +17,35 @@ package com.icecream.snorlax.module.feature.mitm; import java.nio.ByteBuffer; +import java.util.List; +import android.util.LongSparseArray; + +import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import com.icecream.snorlax.common.Strings; import com.icecream.snorlax.module.util.Log; +import static POGOProtos.Data.PokemonDataOuterClass.PokemonData; +import static POGOProtos.Enums.PokemonIdOuterClass.PokemonId; +import static POGOProtos.Inventory.InventoryDeltaOuterClass.InventoryDelta; +import static POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData; +import static POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem; import static POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope; import static POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass.ResponseEnvelope; +import static POGOProtos.Networking.Requests.RequestOuterClass.Request; +import static POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType; +import static POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse; +import static android.R.attr.id; final class MitmProvider { + static { + sRequests = new LongSparseArray<>(); + } + + private static LongSparseArray> sRequests; + static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk) { if (!connectionOk) return null; @@ -39,6 +59,8 @@ static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk synchronized (MitmProvider.class) { RequestEnvelope envelope = RequestEnvelope.parseFrom(buffer); MitmRelay.getInstance().call(envelope); + + processOutBuffer(envelope); } } catch (InvalidProtocolBufferException ignored) { @@ -50,6 +72,13 @@ static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk return null; } + private static void processOutBuffer(RequestEnvelope envelope) { + sRequests.put( + envelope.getRequestId(), + envelope.getRequestsList() + ); + } + static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk) { if (!connectionOk) return null; @@ -63,6 +92,12 @@ static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk) synchronized (MitmProvider.class) { ResponseEnvelope envelope = ResponseEnvelope.parseFrom(buffer); MitmRelay.getInstance().call(envelope); + + ByteBuffer processed = processInBuffer(envelope); + + if (processed != null) { + return processed; + } } } catch (InvalidProtocolBufferException ignored) { @@ -74,6 +109,75 @@ static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk) return null; } + private static ByteBuffer processInBuffer(ResponseEnvelope envelope) throws InvalidProtocolBufferException { + List requests = sRequests.get(envelope.getRequestId()); + + if (requests == null) { + return null; + } + + boolean isDone = false; + for (int i = 0; i < requests.size(); i++) { + if (requests.get(i).getRequestType() == RequestType.GET_INVENTORY) { + ByteString processed = processInventoryResponse(GetInventoryResponse.parseFrom(envelope.getReturns(i))); + + if (processed != null) { + ResponseEnvelope.Builder builder = envelope.toBuilder(); + builder.setReturns(i, processed); + envelope = builder.build(); + isDone = true; + } + } + } + sRequests.remove(id); + + if (!isDone) { + return null; + } + + return ByteBuffer.wrap(envelope.toByteArray()); + } + + private static ByteString processInventoryResponse(GetInventoryResponse response) { + if (!response.getSuccess() || !response.hasInventoryDelta()) { + return null; + } + + boolean isDone = false; + GetInventoryResponse.Builder inventory = response.toBuilder(); + InventoryDelta.Builder inventoryDelta = inventory.getInventoryDelta().toBuilder(); + for (int i = 0; i < inventoryDelta.getInventoryItemsCount(); i++) { + InventoryItem.Builder inventoryItem = inventoryDelta.getInventoryItems(i).toBuilder(); + InventoryItemData.Builder itemData = inventoryItem.getInventoryItemData().toBuilder(); + + if (itemData.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) { + PokemonData.Builder Pokemon = itemData.getPokemonData().toBuilder(); + String nickname = Pokemon.getNickname(); + + if (Strings.isEmpty(nickname)) { + Pokemon.setNickname( + String.format( + "%s/%s/%s", + String.valueOf(Pokemon.getIndividualAttack()), + String.valueOf(Pokemon.getIndividualDefense()), + String.valueOf(Pokemon.getIndividualStamina()) + ) + ); + itemData.setPokemonData(Pokemon); + inventoryItem.setInventoryItemData(itemData); + inventoryDelta.setInventoryItems(i, inventoryItem); + inventory.setInventoryDelta(inventoryDelta); + isDone = true; + } + } + } + if (isDone) { + return inventory.build().toByteString(); + } + + return null; + } + private MitmProvider() { throw new AssertionError("No instances"); }