Skip to content

Commit

Permalink
Rename seems to work... Also, there's no design pattern here, refacto…
Browse files Browse the repository at this point in the history
…r is coming
  • Loading branch information
Pedro Diaz committed Sep 28, 2016
1 parent c865acd commit af237a3
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/com/icecream/snorlax/common/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ private void mitmStream() {
mByteBuffer.hasRemaining()
);

// Lets keep it read only for now
/*
if (fromMitm != null) {
mByteBuffer = fromMitm;
}
*/
mMitmDone = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Request>> sRequests;

static ByteBuffer processOutboundPackage(ByteBuffer roData, boolean connectionOk) {
if (!connectionOk)
return null;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -74,6 +109,75 @@ static ByteBuffer processInboundPackage(ByteBuffer roData, boolean connectionOk)
return null;
}

private static ByteBuffer processInBuffer(ResponseEnvelope envelope) throws InvalidProtocolBufferException {
List<Request> 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");
}
Expand Down

0 comments on commit af237a3

Please sign in to comment.