-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[major] Merge pull request #8 from cfogrady/BEMReading
[major] BEM Reading and Writing
- Loading branch information
Showing
100 changed files
with
2,800 additions
and
1,021 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,13 @@ build | |
bin/ | ||
|
||
test.bin | ||
|
||
modified.bin | ||
|
||
new_background.bmp | ||
|
||
original.bin | ||
|
||
BEM_CARD_IMAGE.bin | ||
|
||
MODIFIED_BEM.bin |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/github/cfogrady/vb/dim/adventure/AdventureLevels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
import lombok.Data; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@SuperBuilder(toBuilder = true) | ||
public class AdventureLevels<T extends AdventureLevels.AdventureLevel> { | ||
|
||
@Data | ||
@SuperBuilder(toBuilder = true) | ||
public static class AdventureLevel { | ||
private final int steps; | ||
private final int bossCharacterIndex; | ||
private final int bossDp; | ||
private final int bossHp; | ||
private final int bossAp; | ||
} | ||
|
||
private final List<T> levels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/github/cfogrady/vb/dim/adventure/BemAdventureConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
public class BemAdventureConstants { | ||
public static final int MAX_TABLE_SIZE = 12; | ||
public static final int ROW_SIZE = 11; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/github/cfogrady/vb/dim/adventure/BemAdventureLevels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Data | ||
@SuperBuilder(toBuilder = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class BemAdventureLevels extends AdventureLevels<BemAdventureLevels.BemAdventureLevel> { | ||
|
||
@Data | ||
@SuperBuilder(toBuilder = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class BemAdventureLevel extends AdventureLevels.AdventureLevel { | ||
private final int showBossIdentity; //1 shows the boss, 2 shows OS secret character icon | ||
private final int smallAttackId; //40+ is BEM specific attack sprite | ||
private final int bigAttackId; //22+ is BEM specific attack sprite | ||
private final int background1; | ||
private final int background2; | ||
private final int giftCharacterIndex; | ||
|
||
public int getBossBp() { | ||
return getBossDp(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/cfogrady/vb/dim/adventure/BemAdventureWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
import com.github.cfogrady.vb.dim.util.ByteOffsetOutputStream; | ||
import com.github.cfogrady.vb.dim.util.RelativeByteOffsetOutputStream; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public class BemAdventureWriter { | ||
public void writeAdventures(BemAdventureLevels adventureLevels, ByteOffsetOutputStream generalOutputStream) { | ||
try { | ||
RelativeByteOffsetOutputStream relativeOutputStream = new RelativeByteOffsetOutputStream(generalOutputStream); | ||
for(BemAdventureLevels.BemAdventureLevel level : adventureLevels.getLevels()) { | ||
relativeOutputStream.write16BitInt(level.getSteps()); | ||
relativeOutputStream.write16BitInt(level.getBossCharacterIndex()); | ||
relativeOutputStream.write16BitInt(level.getShowBossIdentity()); | ||
relativeOutputStream.write16BitInt(level.getBossDp()); | ||
relativeOutputStream.write16BitInt(level.getBossHp()); | ||
relativeOutputStream.write16BitInt(level.getBossAp()); | ||
relativeOutputStream.write16BitInt(level.getSmallAttackId()); | ||
relativeOutputStream.write16BitInt(level.getBigAttackId()); | ||
relativeOutputStream.write16BitInt(level.getBackground1()); | ||
relativeOutputStream.write16BitInt(level.getBackground2()); | ||
relativeOutputStream.write16BitInt(level.getGiftCharacterIndex()); | ||
} | ||
} catch(IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/github/cfogrady/vb/dim/adventure/BemAdventuresReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
import com.github.cfogrady.vb.dim.util.ByteOffsetInputStream; | ||
import com.github.cfogrady.vb.dim.util.ByteUtils; | ||
import com.github.cfogrady.vb.dim.util.RelativeByteOffsetInputStream; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BemAdventuresReader { | ||
public BemAdventureLevels readAdventures(ByteOffsetInputStream generalStream) { | ||
try { | ||
RelativeByteOffsetInputStream relativeInputStream = new RelativeByteOffsetInputStream(generalStream); | ||
List<BemAdventureLevels.BemAdventureLevel> levels = new ArrayList<>(BemAdventureConstants.MAX_TABLE_SIZE); | ||
int[] values = getRowValues(relativeInputStream); | ||
boolean validRow = !ByteUtils.onlyZerosOrMaxValuesInArray(values); | ||
while (validRow) { | ||
levels.add(BemAdventureLevels.BemAdventureLevel.builder() | ||
.steps(values[0]) | ||
.bossCharacterIndex(values[1]) | ||
.showBossIdentity(values[2]) | ||
.bossDp(values[3]) | ||
.bossHp(values[4]) | ||
.bossAp(values[5]) | ||
.smallAttackId(values[6]) | ||
.bigAttackId(values[7]) | ||
.background1(values[8]) | ||
.background2(values[9]) | ||
.giftCharacterIndex(values[10]) | ||
.build()); | ||
values = getRowValues(relativeInputStream); | ||
validRow = !ByteUtils.onlyZerosOrMaxValuesInArray(values); | ||
} | ||
return BemAdventureLevels.builder().levels(levels).build(); | ||
} catch (IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} | ||
} | ||
|
||
private int[] getRowValues(RelativeByteOffsetInputStream relativeInputStream) throws IOException { | ||
byte[] rowBytes = relativeInputStream.readNBytes(BemAdventureConstants.ROW_SIZE * 2); | ||
return ByteUtils.getUnsigned16Bit(rowBytes); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/github/cfogrady/vb/dim/adventure/DimAdventures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.github.cfogrady.vb.dim.adventure; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@SuperBuilder(toBuilder=true) | ||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
public class DimAdventures extends AdventureLevels<AdventureLevels.AdventureLevel> { | ||
public static final int VB_TABLE_SIZE = 15; | ||
private final int dummyRows; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/github/cfogrady/vb/dim/card/BemCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.cfogrady.vb.dim.card; | ||
|
||
import com.github.cfogrady.vb.dim.adventure.BemAdventureLevels; | ||
import com.github.cfogrady.vb.dim.character.BemCharacterStats; | ||
import com.github.cfogrady.vb.dim.fusion.AttributeFusions; | ||
import com.github.cfogrady.vb.dim.fusion.BemSpecificFusions; | ||
import com.github.cfogrady.vb.dim.header.BemHeader; | ||
import com.github.cfogrady.vb.dim.transformation.BemTransformationRequirements; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Data | ||
@SuperBuilder(toBuilder = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
public class BemCard extends Card< | ||
BemHeader, | ||
BemCharacterStats, | ||
BemTransformationRequirements, | ||
BemAdventureLevels, | ||
AttributeFusions, | ||
BemSpecificFusions> { } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/github/cfogrady/vb/dim/card/BemCardConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.github.cfogrady.vb.dim.card; | ||
|
||
public class BemCardConstants { | ||
public static final int CHARACTER_SECTION_START = 0x30000; | ||
public static final int TRANSFORMATION_SECTION_START = 0x40000; | ||
public static final int ADVENTURE_SECTION_START = 0x50000; | ||
public static final int SPRITE_DIMENSIONS_START = 0x60000; | ||
public static final int ATTRIBUTE_FUSION_START = 0x70000; //may never be used again | ||
public static final int SPECIFIC_FUSION_START = 0x80000; | ||
public static final int SPRITE_PACKAGE_START = 0x100000; | ||
public static final int CHECKSUM_LOCATION = 0x3FFFFE; | ||
|
||
public static final int NONE_VALUE = 0xFFFF; | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/com/github/cfogrady/vb/dim/card/BemCardReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.github.cfogrady.vb.dim.card; | ||
|
||
|
||
import com.github.cfogrady.vb.dim.adventure.BemAdventureLevels; | ||
import com.github.cfogrady.vb.dim.adventure.BemAdventuresReader; | ||
import com.github.cfogrady.vb.dim.character.BemCharacterReader; | ||
import com.github.cfogrady.vb.dim.character.BemCharacterStats; | ||
import com.github.cfogrady.vb.dim.fusion.AttributeFusions; | ||
import com.github.cfogrady.vb.dim.fusion.BemFusionReader; | ||
import com.github.cfogrady.vb.dim.fusion.BemSpecificFusions; | ||
import com.github.cfogrady.vb.dim.header.BemHeader; | ||
import com.github.cfogrady.vb.dim.header.BemHeaderReader; | ||
import com.github.cfogrady.vb.dim.sprite.BemSpriteReader; | ||
import com.github.cfogrady.vb.dim.sprite.SpriteData; | ||
import com.github.cfogrady.vb.dim.transformation.BemTransformationReader; | ||
import com.github.cfogrady.vb.dim.transformation.BemTransformationRequirements; | ||
import com.github.cfogrady.vb.dim.util.ByteOffsetInputStream; | ||
import com.github.cfogrady.vb.dim.util.ByteUtils; | ||
import com.github.cfogrady.vb.dim.util.DIMChecksumBuilder; | ||
import com.github.cfogrady.vb.dim.util.InputStreamWithNot; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BemCardReader { | ||
private final BemHeaderReader bemHeaderReader; | ||
private final BemCharacterReader bemCharacterReader; | ||
private final BemTransformationReader bemTransformationReader; | ||
private final BemAdventuresReader bemAdventuresReader; | ||
private final BemFusionReader bemFusionReader; | ||
private final BemSpriteReader bemSpriteReader; | ||
|
||
public BemCardReader() { | ||
bemHeaderReader = new BemHeaderReader(); | ||
bemCharacterReader = new BemCharacterReader(); | ||
bemTransformationReader = new BemTransformationReader(); | ||
bemAdventuresReader = new BemAdventuresReader(); | ||
bemFusionReader = new BemFusionReader(); | ||
bemSpriteReader = new BemSpriteReader(); | ||
} | ||
|
||
public BemCard readBemCard(InputStream inputStream) { | ||
DIMChecksumBuilder checksumBuilder = new DIMChecksumBuilder(); | ||
InputStreamWithNot inputStreamWithNot = new InputStreamWithNot(inputStream, checksumBuilder); | ||
try { | ||
byte[] headerBytes = inputStreamWithNot.readNBytes(0x1030); | ||
return readBemCard(headerBytes, inputStreamWithNot); | ||
} catch (IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} | ||
} | ||
|
||
BemCard readBemCard(byte[] headerBytes, InputStreamWithNot inputStream) { | ||
BemHeader header = bemHeaderReader.readBemHeaderFromHeaderBytes(headerBytes); | ||
try { | ||
inputStream.readToOffset(BemCardConstants.CHARACTER_SECTION_START); | ||
BemCharacterStats characters = bemCharacterReader.readCharacterStats(inputStream); | ||
inputStream.readToOffset(BemCardConstants.TRANSFORMATION_SECTION_START); | ||
BemTransformationRequirements transformations = bemTransformationReader.readTransformations(inputStream); | ||
inputStream.readToOffset(BemCardConstants.ADVENTURE_SECTION_START); | ||
BemAdventureLevels adventureLevels = bemAdventuresReader.readAdventures(inputStream); | ||
inputStream.readToOffset(BemCardConstants.SPRITE_DIMENSIONS_START); | ||
List<SpriteData.SpriteDimensions> spriteDimensions = bemSpriteReader.readSpriteDimensions(inputStream); | ||
inputStream.readToOffset(BemCardConstants.ATTRIBUTE_FUSION_START); | ||
AttributeFusions attributeFusions = bemFusionReader.readAttributeFusion(inputStream); | ||
inputStream.readToOffset(BemCardConstants.SPECIFIC_FUSION_START); | ||
BemSpecificFusions specificFusions = bemFusionReader.readSpecificFusions(inputStream); | ||
inputStream.readToOffset(BemCardConstants.SPRITE_PACKAGE_START); | ||
SpriteData spriteData = bemSpriteReader.getSpriteData(inputStream, spriteDimensions); | ||
int checksumOnCard = readToChecksum(inputStream); | ||
int calculatedChecksum = inputStream.getChecksum(); | ||
if(checksumOnCard != calculatedChecksum) { | ||
log.warn("Checksums don't match! Calculated: {} Received: {}", Integer.toHexString(calculatedChecksum), Integer.toHexString(checksumOnCard)); | ||
} | ||
|
||
return BemCard.builder() | ||
.header(header) | ||
.characterStats(characters) | ||
.transformationRequirements(transformations) | ||
.adventureLevels(adventureLevels) | ||
.attributeFusions(attributeFusions) | ||
.specificFusions(specificFusions) | ||
.spriteData(spriteData) | ||
.calculatedCheckSum(calculatedChecksum) | ||
.checksum(checksumOnCard) | ||
.build(); | ||
} catch (IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} | ||
} | ||
|
||
private int readToChecksum(ByteOffsetInputStream inputStream) throws IOException { | ||
inputStream.readToOffset(BemCardConstants.CHECKSUM_LOCATION); | ||
byte[] checksumBytes = inputStream.readNBytes(2); | ||
int dimChecksum = ByteUtils.getUnsigned16Bit(checksumBytes)[0]; | ||
return dimChecksum; | ||
} | ||
} |
Oops, something went wrong.