Skip to content

Commit b7fff6c

Browse files
committed
pr: address comments of #76
1 parent 204ce97 commit b7fff6c

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

src/main/java/entralinked/network/http/pgl/PglHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ private void handleUploadSaveData(PglRequest request, Context ctx) throws IOExce
466466
inputStream.skip(Offsets.ADVENTURE_START_TIME_SUB_OFFSET);
467467
trainerInfo.setAdventureStartTime(TrainerInfoReader.readAdventureStartTime(inputStream));
468468

469-
inputStream.skipTo(Offsets.MONEY_AND_BADGES);
469+
inputStream.skipTo(Offsets.getMoneyAndBadges(request.gameVersion()));
470470
trainerInfo.setMoney(TrainerInfoReader.readLong(inputStream));
471471
trainerInfo.setGymBadges(TrainerInfoReader.readGymBadges(inputStream));
472472
}
473-
473+
474474
// Update and save player information
475475
player.setStatus(PlayerStatus.SLEEPING);
476476
player.setGameVersion(request.gameVersion());

src/main/java/entralinked/savefile/Offsets.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package entralinked.savefile;
22

3+
import entralinked.GameVersion;
4+
35
public class Offsets {
46
public static final int TRAINER_INFO = 0x19400;
57
public static final int TRAINER_INFO_SIZE = 0x67;
@@ -13,10 +15,15 @@ public class Offsets {
1315
public static final int PLAYTIME_OFFSET = 0x24;
1416

1517
public static final int DREAM_WORLD_INFO = 0x1D300;
16-
public static final int POKEMON_INFO_SUB_OFFSET = 0x1D300;
18+
public static final int POKEMON_INFO_SUB_OFFSET = 0x8;
1719

1820
public static final int ADVENTURE_START_TIME_OFFSET = 0x1D900;
19-
public static final int ADVENTURE_START_TIME_SUB_OFFSET = 0x1D900;
21+
public static final int ADVENTURE_START_TIME_SUB_OFFSET = 0x34;
22+
23+
public static final int MONEY_AND_BADGES_VERSION_1 = 0x21200;
24+
public static final int MONEY_AND_BADGES_VERSION_2 = 0x21100;
2025

21-
public static final int MONEY_AND_BADGES = 0x21200;
26+
public static int getMoneyAndBadges(GameVersion gameVersion) {
27+
return gameVersion.isVersion2() ? MONEY_AND_BADGES_VERSION_2 : MONEY_AND_BADGES_VERSION_1;
28+
}
2229
}

src/main/java/entralinked/savefile/PkmnInfoReader.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,23 @@ public static PkmnInfo readPokeInfo(InputStream inputStream) throws IOException
6969
int form = (buffer.getByte(64) >> 3) & 0x1F;
7070
boolean genderless = ((buffer.getByte(64) >> 2) & 1) == 1;
7171
boolean female = ((buffer.getByte(64) >> 1) & 1) == 1;
72+
int natureByte = buffer.getByte(65);
7273
PkmnGender gender = genderless ? PkmnGender.GENDERLESS : female ? PkmnGender.FEMALE : PkmnGender.MALE;
73-
PkmnNature nature = PkmnNature.valueOf(buffer.getByte(65));
74+
PkmnNature nature = PkmnNature.valueOf(natureByte);
7475
String nickname = getString(buffer, 72, 20);
7576
String trainerName = getString(buffer, 104, 14);
7677

7778
// Try release buffer
7879
if(!buffer.release()) {
7980
logger.warn("Buffer was not deallocated!");
8081
}
81-
82+
8283
// Loosely verify data
83-
if(species < 1 || species > 649) throw new IOException("Invalid species");
84-
if(heldItem < 0 || heldItem > 638) throw new IOException("Invalid held item");
85-
if(ability < 1 || ability > 164) throw new IOException("Invalid ability");
86-
if(level < 1 || level > 100) throw new IOException("Level is out of range");
87-
if(nature == null) throw new IOException("Invalid nature");
84+
if(species < 1 || species > 649) throw new IOException(String.format("Invalid species: %d", species));
85+
if(heldItem < 0 || heldItem > 638) throw new IOException(String.format("Invalid held item: %d", heldItem));
86+
if(ability < 1 || ability > 164) throw new IOException(String.format("Invalid ability: %d", ability));
87+
if(level < 1 || level > 100) throw new IOException(String.format("Level is out of range: %d", level));
88+
if(nature == null) throw new IOException(String.format("Invalid nature: %d", natureByte));
8889

8990
// Create record
9091
return new PkmnInfo(nickname, trainerName, nature, gender, species, personality,

src/main/java/entralinked/savefile/TrainerInfoReader.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import io.netty.buffer.ByteBuf;
88
import io.netty.buffer.ByteBufAllocator;
99
import io.netty.buffer.PooledByteBufAllocator;
10+
import org.apache.logging.log4j.LogManager;
11+
import org.apache.logging.log4j.Logger;
1012

1113
import java.io.IOException;
1214
import java.io.InputStream;
@@ -19,6 +21,8 @@ public class TrainerInfoReader {
1921
private static final byte ZERO_BYTE = (byte) 0x00;
2022
private static final byte FF_BYTE = (byte) 0xFF;
2123

24+
25+
private static final Logger logger = LogManager.getLogger();
2226
private static final ByteBufAllocator bufferAllocator = PooledByteBufAllocator.DEFAULT;
2327

2428
public static TrainerInfo readTrainerInfo(InputStream inputStream) throws IOException {
@@ -33,6 +37,11 @@ public static TrainerInfo readTrainerInfo(InputStream inputStream) throws IOExce
3337
TrainerGender gender = TrainerGender.valueOf(buffer.getUnsignedByte(Offsets.GENDER_OFFSET));
3438
Playtime playtime = readPlaytime(buffer);
3539

40+
// Try release buffer
41+
if(!buffer.release()) {
42+
logger.warn("Buffer was not deallocated!");
43+
}
44+
3645
return new TrainerInfo(trainerName, trainerId, secretId, country, region, gender, playtime);
3746
}
3847

@@ -65,7 +74,15 @@ public static long readAdventureStartTime(InputStream inputStream) throws IOExce
6574
public static long readLong(InputStream inputStream) throws IOException {
6675
ByteBuf buffer = bufferAllocator.buffer(4);
6776
buffer.writeBytes(inputStream, 4);
68-
return buffer.getUnsignedIntLE(0);
77+
78+
long read = buffer.getUnsignedIntLE(0);
79+
80+
// Try release buffer
81+
if(!buffer.release()) {
82+
logger.warn("Buffer was not deallocated!");
83+
}
84+
85+
return read;
6986
}
7087

7188
public static List<GymBadge> readGymBadges(InputStream inputStream) throws IOException {
@@ -79,6 +96,12 @@ public static List<GymBadge> readGymBadges(InputStream inputStream) throws IOExc
7996
gymBadges.add(badge);
8097
}
8198
}
99+
100+
// Try release buffer
101+
if(!buffer.release()) {
102+
logger.warn("Buffer was not deallocated!");
103+
}
104+
82105
return gymBadges;
83106
}
84107
}

src/main/java/entralinked/utility/PointedInputStream.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package entralinked.utility;
22

3+
import org.jetbrains.annotations.NotNull;
4+
35
import java.io.FilterInputStream;
46
import java.io.IOException;
57
import java.io.InputStream;
68

79
public class PointedInputStream extends FilterInputStream {
8-
private static long pointer = 0;
10+
private long pointer = 0;
911

1012
public PointedInputStream(InputStream inputStream) {
1113
super(inputStream);
@@ -19,7 +21,7 @@ public int read() throws IOException {
1921
}
2022

2123
@Override
22-
public int read(byte b[], int off, int len) throws IOException {
24+
public int read(@NotNull byte[] b, int off, int len) throws IOException {
2325
int bytes = super.read(b, off, len);
2426
pointer += bytes;
2527
return bytes;

0 commit comments

Comments
 (0)