Skip to content

Commit e068351

Browse files
authored
Support for PokeHash hasher (#833)
1 parent 6be3594 commit e068351

25 files changed

+778
-249
lines changed

library/src/main/java/com/pokegoapi/api/PokemonGo.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.pokegoapi.util.ClientInterceptor;
5656
import com.pokegoapi.util.SystemTimeImpl;
5757
import com.pokegoapi.util.Time;
58+
import com.pokegoapi.util.hash.HashProvider;
5859
import lombok.Getter;
5960
import lombok.Setter;
6061
import okhttp3.OkHttpClient;
@@ -128,6 +129,9 @@ public class PokemonGo {
128129
@Getter
129130
private Heartbeat heartbeat = new Heartbeat(this);
130131

132+
@Getter
133+
private HashProvider hashProvider;
134+
131135
/**
132136
* Instantiates a new Pokemon go.
133137
*
@@ -186,17 +190,22 @@ public PokemonGo(OkHttpClient client) {
186190
* Login user with the provided provider
187191
*
188192
* @param credentialProvider the credential provider
193+
* @param hashProvider to provide hashes
189194
* @throws LoginFailedException When login fails
190195
* @throws RemoteServerException When server fails
191196
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
192197
*/
193-
public void login(CredentialProvider credentialProvider)
198+
public void login(CredentialProvider credentialProvider, HashProvider hashProvider)
194199
throws LoginFailedException, CaptchaActiveException, RemoteServerException {
195200
this.loggingIn = true;
196201
if (credentialProvider == null) {
197-
throw new NullPointerException("Credential Provider is null");
202+
throw new NullPointerException("Credential Provider can not be null!");
203+
} else if (hashProvider == null) {
204+
throw new NullPointerException("Hash Provider can not be null!");
198205
}
199206
this.credentialProvider = credentialProvider;
207+
this.hashProvider = hashProvider;
208+
200209
startTime = currentTimeMillis();
201210
inventories = new Inventories(this);
202211
settings = new Settings(this);
@@ -209,7 +218,7 @@ private void initialize() throws RemoteServerException, CaptchaActiveException,
209218
playerProfile.updateProfile();
210219

211220
ServerRequest downloadConfigRequest = new ServerRequest(RequestType.DOWNLOAD_REMOTE_CONFIG_VERSION,
212-
CommonRequests.getDownloadRemoteConfigVersionMessageRequest());
221+
CommonRequests.getDownloadRemoteConfigVersionMessageRequest(this));
213222
fireRequestBlock(downloadConfigRequest, RequestType.GET_BUDDY_WALKED);
214223
getAssetDigest();
215224

@@ -310,7 +319,7 @@ private void fireRequestBlock(ServerRequest request, RequestType... exclude)
310319
*/
311320
public void getAssetDigest() throws RemoteServerException, CaptchaActiveException, LoginFailedException {
312321
fireRequestBlock(new ServerRequest(RequestType.GET_ASSET_DIGEST,
313-
CommonRequests.getGetAssetDigestMessageRequest()).exclude(RequestType.GET_BUDDY_WALKED));
322+
CommonRequests.getGetAssetDigestMessageRequest(this)).exclude(RequestType.GET_BUDDY_WALKED));
314323
}
315324

316325
/**
@@ -625,4 +634,11 @@ public void awaitChallenge() throws InterruptedException {
625634
public void enqueueTask(Runnable task) {
626635
heartbeat.enqueueTask(task);
627636
}
637+
638+
/**
639+
* @return the version of the API being used
640+
*/
641+
public int getVersion() {
642+
return hashProvider.getHashVersion();
643+
}
628644
}

library/src/main/java/com/pokegoapi/api/inventory/Inventories.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
package com.pokegoapi.api.inventory;
1717

18+
import POGOProtos.Data.PokemonDataOuterClass.PokemonData;
1819
import POGOProtos.Enums.PokemonFamilyIdOuterClass;
1920
import POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
2021
import POGOProtos.Inventory.AppliedItemOuterClass.AppliedItem;
2122
import POGOProtos.Inventory.AppliedItemsOuterClass.AppliedItems;
2223
import POGOProtos.Inventory.EggIncubatorOuterClass;
24+
import POGOProtos.Inventory.EggIncubatorsOuterClass.EggIncubators;
2325
import POGOProtos.Inventory.InventoryItemDataOuterClass;
2426
import POGOProtos.Inventory.InventoryItemOuterClass;
2527
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
@@ -146,12 +148,13 @@ public void updateInventories(GetInventoryResponse response) {
146148
InventoryItemDataOuterClass.InventoryItemData itemData = inventoryItem.getInventoryItemData();
147149

148150
// hatchery
149-
if (itemData.getPokemonData().getPokemonId() == PokemonId.MISSINGNO && itemData.getPokemonData().getIsEgg()) {
150-
hatchery.addEgg(new EggPokemon(itemData.getPokemonData()));
151+
PokemonData pokemonData = itemData.getPokemonData();
152+
if (pokemonData.getPokemonId() == PokemonId.MISSINGNO && pokemonData.getIsEgg()) {
153+
hatchery.addEgg(new EggPokemon(pokemonData));
151154
}
152155

153156
// pokebank
154-
if (itemData.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) {
157+
if (pokemonData.getPokemonId() != PokemonId.MISSINGNO) {
155158
pokebank.addPokemon(new Pokemon(api, inventoryItem.getInventoryItemData().getPokemonData()));
156159
}
157160

@@ -183,7 +186,8 @@ public void updateInventories(GetInventoryResponse response) {
183186
}
184187

185188
if (itemData.hasEggIncubators()) {
186-
for (EggIncubatorOuterClass.EggIncubator incubator : itemData.getEggIncubators().getEggIncubatorList()) {
189+
EggIncubators eggIncubators = itemData.getEggIncubators();
190+
for (EggIncubatorOuterClass.EggIncubator incubator : eggIncubators.getEggIncubatorList()) {
187191
EggIncubator eggIncubator = new EggIncubator(api, incubator);
188192
synchronized (this.lock) {
189193
incubators.remove(eggIncubator);

library/src/main/java/com/pokegoapi/api/inventory/PokeBank.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ public Pokemon getPokemonById(final Long id) {
141141
/**
142142
* Releases multiple pokemon in a single request
143143
*
144-
* @param pokemons the pokemon to release
144+
* @param releasePokemon the pokemon to release
145145
* @return the amount of candies for each pokemon family
146146
* @throws CaptchaActiveException if a captcha is active and a message cannot be sent
147147
* @throws LoginFailedException the login fails
148148
* @throws RemoteServerException if the server errors
149149
*/
150-
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... pokemons)
150+
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon)
151151
throws CaptchaActiveException, LoginFailedException, RemoteServerException {
152152
ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
153-
for (Pokemon pokemon : pokemons) {
153+
for (Pokemon pokemon : releasePokemon) {
154154
if (!pokemon.isDeployed()) {
155155
releaseBuilder.addPokemonIds(pokemon.getId());
156156
}
@@ -167,6 +167,14 @@ public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... pokemons)
167167
ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
168168
Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
169169
if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
170+
synchronized (this.lock) {
171+
for (Pokemon pokemon : releasePokemon) {
172+
this.pokemons.remove(pokemon);
173+
}
174+
}
175+
for (Pokemon pokemon : releasePokemon) {
176+
api.getInventories().getPokebank().removePokemon(pokemon);
177+
}
170178
List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
171179
for (InventoryItem item : items) {
172180
InventoryItemData data = item.getInventoryItemData();

library/src/main/java/com/pokegoapi/api/pokemon/Pokemon.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public boolean canPowerUp() {
185185
*
186186
* @param considerMaxCPLimitForPlayerLevel Consider max cp limit for actual player level
187187
* @return the boolean
188-
* @throws NoSuchItemException If the PokemonId value cannot be found in the {@link PokemonMetaRegistry}.
188+
* @throws NoSuchItemException If the PokemonId value cannot be found in the {@link com.pokegoapi.main.PokemonMeta}.
189189
*/
190190
public boolean canPowerUp(boolean considerMaxCPLimitForPlayerLevel)
191191
throws NoSuchItemException {
@@ -464,4 +464,14 @@ public int getCPInPercentageMaxPlayerLevel() throws NoSuchItemException {
464464
public double getIvInPercentage() {
465465
return ((Math.floor((this.getIvRatio() * 100) * 100)) / 100);
466466
}
467+
468+
@Override
469+
public int hashCode() {
470+
return (int) getId();
471+
}
472+
473+
@Override
474+
public boolean equals(Object obj) {
475+
return obj instanceof Pokemon && ((Pokemon) obj).getId() == getId();
476+
}
467477
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package com.pokegoapi.exceptions.hash;
17+
18+
public class HashException extends Exception {
19+
public HashException() {
20+
super();
21+
}
22+
23+
public HashException(String reason) {
24+
super(reason);
25+
}
26+
27+
public HashException(Throwable exception) {
28+
super(exception);
29+
}
30+
31+
public HashException(String reason, Throwable exception) {
32+
super(reason, exception);
33+
}
34+
}

library/src/main/java/com/pokegoapi/util/Constant.java renamed to library/src/main/java/com/pokegoapi/exceptions/hash/HashLimitExceededException.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1414
*/
1515

16-
package com.pokegoapi.util;
16+
package com.pokegoapi.exceptions.hash;
1717

18-
/**
19-
* Created by iGio90 on 27/08/16.
20-
*/
18+
public class HashLimitExceededException extends HashException {
19+
public HashLimitExceededException() {
20+
super();
21+
}
2122

22-
public class Constant {
23-
public static final int APP_VERSION = 4500;
23+
public HashLimitExceededException(String reason) {
24+
super(reason);
25+
}
2426

25-
public static final long UNK25 = -1553869577012279119L;
27+
public HashLimitExceededException(Throwable exception) {
28+
super(exception);
29+
}
2630
}

library/src/main/java/com/pokegoapi/main/CommonRequests.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.pokegoapi.exceptions.CaptchaActiveException;
4040
import com.pokegoapi.exceptions.LoginFailedException;
4141
import com.pokegoapi.exceptions.RemoteServerException;
42-
import com.pokegoapi.util.Constant;
4342

4443
import java.util.HashMap;
4544
import java.util.LinkedHashMap;
@@ -158,25 +157,27 @@ public void parse(PokemonGo api, ByteString data, RequestType requestType)
158157
/**
159158
* Constant for repetitive usage of DownloadRemoteConfigVersionMessage request
160159
*
160+
* @param api the current API instance
161161
* @return DownloadRemoteConfigVersionMessage
162162
*/
163-
public static DownloadRemoteConfigVersionMessage getDownloadRemoteConfigVersionMessageRequest() {
163+
public static DownloadRemoteConfigVersionMessage getDownloadRemoteConfigVersionMessageRequest(PokemonGo api) {
164164
return DownloadRemoteConfigVersionMessage
165165
.newBuilder()
166166
.setPlatform(Platform.IOS)
167-
.setAppVersion(Constant.APP_VERSION)
167+
.setAppVersion(api.getVersion())
168168
.build();
169169
}
170170

171171
/**
172172
* Constant for repetitive usage of GetAssetDigestMessage request
173173
*
174+
* @param api the current API instance
174175
* @return GetAssetDigestMessage
175176
*/
176-
public static GetAssetDigestMessage getGetAssetDigestMessageRequest() {
177+
public static GetAssetDigestMessage getGetAssetDigestMessageRequest(PokemonGo api) {
177178
return GetAssetDigestMessage.newBuilder()
178179
.setPlatform(Platform.IOS)
179-
.setAppVersion(Constant.APP_VERSION)
180+
.setAppVersion(api.getVersion())
180181
.build();
181182
}
182183

library/src/main/java/com/pokegoapi/main/RequestHandler.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.pokegoapi.exceptions.CaptchaActiveException;
2929
import com.pokegoapi.exceptions.LoginFailedException;
3030
import com.pokegoapi.exceptions.RemoteServerException;
31+
import com.pokegoapi.exceptions.hash.HashException;
3132
import com.pokegoapi.util.AsyncHelper;
3233
import com.pokegoapi.util.Log;
3334
import com.pokegoapi.util.Signature;
@@ -177,9 +178,10 @@ public void sendServerRequests(ServerRequest... serverRequests)
177178
* @throws RemoteServerException the remote server exception
178179
* @throws LoginFailedException the login failed exception
179180
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
181+
* @throws HashException if hashing fails
180182
*/
181183
private AuthTicket internalSendServerRequests(AuthTicket authTicket, ServerRequest... serverRequests)
182-
throws RemoteServerException, CaptchaActiveException, LoginFailedException {
184+
throws RemoteServerException, CaptchaActiveException, LoginFailedException, HashException {
183185
AuthTicket newAuthTicket = authTicket;
184186
if (serverRequests.length == 0) {
185187
return authTicket;
@@ -384,7 +386,7 @@ public void run() {
384386
continue;
385387
}
386388
continue;
387-
} catch (RemoteServerException | LoginFailedException | CaptchaActiveException e) {
389+
} catch (RemoteServerException | LoginFailedException | CaptchaActiveException | HashException e) {
388390
for (AsyncServerRequest request : requests) {
389391
resultMap.put(request.getId(), ResultOrException.getError(e));
390392
}

0 commit comments

Comments
 (0)