Skip to content

Commit

Permalink
v4 release!
Browse files Browse the repository at this point in the history
  • Loading branch information
dingdongg committed Jun 8, 2024
1 parent 468a77f commit 6e5954c
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 63 deletions.
14 changes: 7 additions & 7 deletions crypt/crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import (
"encoding/binary"
"log"

"github.com/dingdongg/pkmn-rom-parser/v3/prng"
"github.com/dingdongg/pkmn-rom-parser/v4/prng"
)

// Computes a checksum via the CRC16-CCITT algorithm on the given data
func CRC16_CCITT(data []byte) uint16 {
sum := uint(0xFFFF)

for _, b := range data {
sum = (sum << 8) ^ seeds[b ^ byte((sum >> 8))]
sum = (sum << 8) ^ seeds[b^byte((sum>>8))]
}

return uint16(sum)
}

// Encrypts the given pokemon. Checksum will be updated as part of encryption
func EncryptPokemon(plaintext []byte) []byte {
personality := binary.LittleEndian.Uint32(plaintext[0 : 4])
personality := binary.LittleEndian.Uint32(plaintext[0:4])
// do i use the previous checksum? or the new plaintextSum calculated below?
// UPDATE: I think im supposed to use the new checksum
// checksum := binary.LittleEndian.Uint16(plaintext[6 : 8])
// checksum := binary.LittleEndian.Uint16(plaintext[6 : 8])

buffer := make([]byte, 8)
copy(buffer, plaintext[:8])
Expand Down Expand Up @@ -62,8 +62,8 @@ func EncryptBattleStats(plaintext []byte, personality uint32) []byte {
}

func DecryptPokemon(ciphertext []byte) []byte {
personality := binary.LittleEndian.Uint32(ciphertext[0 : 4])
checksum := binary.LittleEndian.Uint16(ciphertext[6 : 8])
personality := binary.LittleEndian.Uint32(ciphertext[0:4])
checksum := binary.LittleEndian.Uint16(ciphertext[6:8])

rand := prng.Init(checksum, personality)

Expand Down Expand Up @@ -98,4 +98,4 @@ func DecryptBattleStats(ciphertext []byte, personality uint32) []byte {
}

return plaintext
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/dingdongg/pkmn-rom-parser/v3
module github.com/dingdongg/pkmn-rom-parser/v4

go 1.22.3

Expand Down
2 changes: 1 addition & 1 deletion items/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strconv"
"strings"

"github.com/dingdongg/pkmn-rom-parser/v3/path_resolver"
"github.com/dingdongg/pkmn-rom-parser/v4/path_resolver"
)

// fetch item names and cache in RAM to prevent multiple file IO operations
Expand Down
14 changes: 7 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package parser

import (
"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v3/rom_reader"
"github.com/dingdongg/pkmn-rom-parser/v3/rom_writer"
"github.com/dingdongg/pkmn-rom-parser/v3/rom_writer/req"
"github.com/dingdongg/pkmn-rom-parser/v3/validator"
"github.com/dingdongg/pkmn-rom-parser/v3/validator/locator"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/rom_reader"
"github.com/dingdongg/pkmn-rom-parser/v4/rom_writer"
"github.com/dingdongg/pkmn-rom-parser/v4/rom_writer/req"
"github.com/dingdongg/pkmn-rom-parser/v4/validator"
"github.com/dingdongg/pkmn-rom-parser/v4/validator/locator"
)

func Parse(savefile []byte) ([]rom_reader.Pokemon, error) {
Expand All @@ -27,4 +27,4 @@ func Write(savefile []byte, newBytes []req.WriteRequest) ([]byte, error) {

chunk := locator.GetLatestSaveChunk(savefile)
return rom_writer.UpdatePartyPokemon(savefile, *chunk, newBytes)
}
}
10 changes: 5 additions & 5 deletions rom_reader/rom_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"encoding/binary"
"log"

"github.com/dingdongg/pkmn-rom-parser/v3/char"
"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v3/crypt"
"github.com/dingdongg/pkmn-rom-parser/v3/data"
"github.com/dingdongg/pkmn-rom-parser/v3/shuffler"
"github.com/dingdongg/pkmn-rom-parser/v4/char"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/crypt"
"github.com/dingdongg/pkmn-rom-parser/v4/data"
"github.com/dingdongg/pkmn-rom-parser/v4/shuffler"
)

type Stats struct {
Expand Down
4 changes: 2 additions & 2 deletions rom_reader/rom_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -90,4 +90,4 @@ func TestParseUpdatedPokemon(t *testing.T) {
if !cmp.Equal(firstPokemon, expectedPokemon) {
t.Fatalf("expected %+v, but got %+v\n", expectedPokemon, firstPokemon)
}
}
}
8 changes: 4 additions & 4 deletions rom_writer/req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package req
import (
"fmt"

"github.com/dingdongg/pkmn-rom-parser/v3/char"
"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v3/shuffler"
"github.com/dingdongg/pkmn-rom-parser/v4/char"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/shuffler"
)

const (
Expand Down Expand Up @@ -172,4 +172,4 @@ func GetWriteLocation(request string) (dataOffset int, blockIndex int, err error
}

return dataOffset, blockIndex, nil
}
}
16 changes: 8 additions & 8 deletions rom_writer/rom_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package rom_writer
import (
"encoding/binary"

"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v3/crypt"
"github.com/dingdongg/pkmn-rom-parser/v3/rom_writer/req"
"github.com/dingdongg/pkmn-rom-parser/v3/shuffler"
"github.com/dingdongg/pkmn-rom-parser/v3/validator"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/crypt"
"github.com/dingdongg/pkmn-rom-parser/v4/rom_writer/req"
"github.com/dingdongg/pkmn-rom-parser/v4/shuffler"
"github.com/dingdongg/pkmn-rom-parser/v4/validator"
)

/*
Expand Down Expand Up @@ -70,10 +70,10 @@ func UpdatePartyPokemon(savefile []byte, chunk validator.Chunk, newData []req.Wr
return []byte{}, err
}
}

if _, ok := changes[wr.PartyIndex]; !ok {
changes[wr.PartyIndex] = crypt.DecryptPokemon(savefile[offset:])
}
changes[wr.PartyIndex] = crypt.DecryptPokemon(savefile[offset:])
}
copy(changes[wr.PartyIndex][blockAddress+uint(dataOffset):], bytes)

if _, seen := updatedPokemonIndexes[wr.PartyIndex]; !seen {
Expand Down
12 changes: 6 additions & 6 deletions shuffler/shuffler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
)

// from https://projectpokemon.org/home/docs/gen-4/pkm-structure-r65/
Expand Down Expand Up @@ -89,20 +89,20 @@ func GetPokemonBlock(buf []byte, block uint, personality uint32) ([]byte, error)

// Used to get the absolute memory address location of the block. Mainly for writing purposes ATM
func GetPokemonBlockLocation(block uint, personality uint32) (uint, error) {
if block >= A && block <= D {
if block >= A && block <= D {
shiftValue := ((personality & 0x03E000) >> 0x0D) % 24
unshuffleInfo := unshuffleTable[shiftValue]
startAddr := unshuffleInfo.GetUnshuffledPos(block)

return startAddr, nil
}
return 0, errors.New("invalid block index")
}

/*
block is one of 0, 1, 2, 3
/*
block is one of 0, 1, 2, 3
metadata consists of a pokemon's PID & checksum
metadata consists of a pokemon's PID & checksum
*/
func (bo blockOrder) GetUnshuffledPos(block uint) uint {
metadataOffset := uint(0x8)
Expand Down
4 changes: 2 additions & 2 deletions shuffler/shuffler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package shuffler
import (
"testing"

"github.com/dingdongg/pkmn-rom-parser/v3/consts"
"github.com/dingdongg/pkmn-rom-parser/v4/consts"
)

func TestGetUnshuffledPos(t *testing.T) {
Expand Down Expand Up @@ -48,4 +48,4 @@ func TestGetPokemonBlock(t *testing.T) {
t.Fatalf("BUF LENGTH: expected %d but got %d\n", consts.BLOCK_SIZE_BYTES, len(res))
}
}
}
}
6 changes: 3 additions & 3 deletions validator/locator/locator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package locator

import "github.com/dingdongg/pkmn-rom-parser/v3/validator"
import "github.com/dingdongg/pkmn-rom-parser/v4/validator"

// return address to the start of the latest save chunk
// REQUIREMENT: savefile must be the entire .SAV file
Expand All @@ -24,6 +24,6 @@ func GetLatestSaveChunk(savefile []byte) *validator.Chunk {

return &validator.Chunk{
SmallBlock: latestSmallBlock,
BigBlock: latestBigBlock,
BigBlock: latestBigBlock,
}
}
}
34 changes: 17 additions & 17 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/dingdongg/pkmn-rom-parser/v3/crypt"
"github.com/dingdongg/pkmn-rom-parser/v4/crypt"
)

/*
Expand All @@ -23,22 +23,22 @@ POKEMON PLATINUM
// a "chunk" denotes a pair of small + big block that are adjacent in memory
type Chunk struct {
SmallBlock Block
BigBlock Block
BigBlock Block
}

type Block struct {
BlockData []byte
Footer Footer
Address uint
Footer Footer
Address uint
}

type Footer struct {
Identifier uint32
SaveNumber uint32
BlockSize uint32
K uint32
T uint16
Checksum uint16
SaveNumber uint32
BlockSize uint32
K uint32
T uint16
Checksum uint16
}

const savefileSize int = 1 << 19
Expand All @@ -48,11 +48,11 @@ const secondChunkOffset uint = 0x40000
func getFooter(buf []byte) Footer {
return Footer{
binary.LittleEndian.Uint32(buf[:0x4]),
binary.LittleEndian.Uint32(buf[0x4 : 0x8]),
binary.LittleEndian.Uint32(buf[0x8 : 0xC]),
binary.LittleEndian.Uint32(buf[0xC : 0x10]),
binary.LittleEndian.Uint16(buf[0x10 : 0x12]),
binary.LittleEndian.Uint16(buf[0x12 : 0x14]),
binary.LittleEndian.Uint32(buf[0x4:0x8]),
binary.LittleEndian.Uint32(buf[0x8:0xC]),
binary.LittleEndian.Uint32(buf[0xC:0x10]),
binary.LittleEndian.Uint16(buf[0x10:0x12]),
binary.LittleEndian.Uint16(buf[0x12:0x14]),
}
}

Expand Down Expand Up @@ -85,18 +85,18 @@ func GetChunk(savefile []byte, offset uint) Chunk {
bigBlockStart := uint(0xCF2C) + offset

smallBlock := Block{
savefile[offset : smallBlockFooterAddr],
savefile[offset:smallBlockFooterAddr],
getFooter(savefile[smallBlockFooterAddr : smallBlockFooterAddr+footerSize]),
offset,
}

bigBlock := Block{
savefile[bigBlockStart : bigBlockFooterAddr],
savefile[bigBlockStart:bigBlockFooterAddr],
getFooter(savefile[bigBlockFooterAddr : bigBlockFooterAddr+footerSize]),
bigBlockStart,
}

return Chunk{ smallBlock, bigBlock }
return Chunk{smallBlock, bigBlock}
}

// validates the given .sav file
Expand Down

0 comments on commit 6e5954c

Please sign in to comment.