~moody/pse

7fab316f0f7b8a1c7164ec29601f9dce59926357 — Jacob Moody 8 months ago 58b16e3
Don't store lang field within PkSave
Start work on text marshaling/unmarshaling
4 files changed, 90 insertions(+), 15 deletions(-)

M box.go
M pokemon.go
M trainer.go
M util.go
M box.go => box.go +12 -1
@@ 3,6 3,7 @@ package pse
import (
	"errors"
	"fmt"
	"strings"
)

type Boxes struct {


@@ 21,6 22,16 @@ type Boxes struct {
	data     [9]*Section
}

func (b *Boxes) MarshalTab() string {
	sb := &strings.Builder{}
	for _, p := range b.Pokemon {
		if p != nil {
			fmt.Fprintf(sb, "%s\n", p.MarshalTab())
		}
	}
	return sb.String()
}

func (b *Boxes) Load(index int, s *Section) error {
	if index < SectionPCA || index > SectionPCI {
		return errors.New("Boxes.Load: non box index")


@@ 36,7 47,7 @@ func (b *Boxes) Commit() error {
	//First we commit things to our contigious buffer
	var err error
	for _, s := range b.Names {
		err = s.Commit()
		err = s.Commit(LangEN)
		if err != nil {
			return err
		}

M pokemon.go => pokemon.go +26 -2
@@ 3,6 3,9 @@ package pse

import (
	"errors"
	"fmt"
	"io"
	"strings"
)

type PokeDataG struct {


@@ 234,6 237,27 @@ type Pokemon struct {
	data    []byte //Backing data store
}

func (p *Pokemon) MarshalTab() string {
	b := &strings.Builder{}
	pd := &p.PokeData
	fmt.Fprintf(b, "%v\t%v\t%s\t%v\t%s\t%v\t", p.Personality, p.OTID, p.Name.string, p.Lang, p.OTName.string, p.Marks)
	fmt.Fprintf(b, "%v\t%v\t%v\t%v\t%v\t", pd.Species, pd.Item, pd.EXP, pd.PPBonus, pd.Friendship)
	fmt.Fprintf(b, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t", pd.Move1, pd.Move2, pd.Move3, pd.Move4, pd.PP1, pd.PP2, pd.PP3, pd.PP4)
	fmt.Fprintf(b, "%v\t%v\t", pd.Pokerus, pd.IV)
	fmt.Fprintf(b, "%v\t%v\t%v\t%v\t%v\t%v", pd.HP, pd.Atk, pd.Def, pd.Spd, pd.SpAtk, pd.SpDef)
	return b.String()
}

func (p *Pokemon) UnmarshalTab(r io.Reader) error {
	pd := &p.PokeData
	_, err := fmt.Scanln(&p.Personality, &p.OTID, &p.Name.string, &p.Lang, &p.OTName.string, &p.Marks,
		&pd.Species, &pd.Item, &pd.EXP, &pd.PPBonus, &pd.Friendship,
		&pd.Move1, &pd.Move2, &pd.Move3, &pd.Move4, &pd.PP1, &pd.PP2, &pd.PP3, &pd.PP4,
		&pd.Pokerus, &pd.IV,
		&pd.HP, &pd.Atk, &pd.Def, &pd.Spd, &pd.SpAtk, &pd.SpDef)
	return err
}

func NewPokemon(data []byte, isParty bool) (*Pokemon, error) {
	p := &Pokemon{
		Personality: get4(data[0:]),


@@ 274,14 298,14 @@ func (p *Pokemon) Commit() error {
	put4(p.Personality, p.data[0:])
	put4(p.OTID, p.data[4:])

	err := p.Name.Commit()
	err := p.Name.Commit(int(p.Lang))
	if err != nil {
		return err
	}

	put2(p.Lang, p.data[18:])

	err = p.OTName.Commit()
	err = p.OTName.Commit(int(p.Lang))
	if err != nil {
		return err
	}

M trainer.go => trainer.go +30 -7
@@ 2,6 2,7 @@ package pse

import (
	"fmt"
	"io"
	"strings"
)



@@ 52,7 53,7 @@ func NewTrainerInfo(s *Section) (*TrainerInfo, error) {

func (ti *TrainerInfo) Commit() error {
	s := ti.data
	err := ti.Name.Commit()
	err := ti.Name.Commit(LangEN)
	if err != nil {
		return err
	}


@@ 74,16 75,18 @@ func (ti *TrainerInfo) Commit() error {
	return nil
}

func (ti *TrainerInfo) String() string {
	var b strings.Builder
	var gender string
func (ti *TrainerInfo) GenderString() string {
	if ti.Gender == 0 {
		gender = "male"
		return "male"
	} else {
		gender = "female"
		return "female"
	}
}

func (ti *TrainerInfo) String() string {
	var b strings.Builder
	fmt.Fprintf(&b, "Name: %s\n", ti.Name.string)
	fmt.Fprintf(&b, "Gender: %s\n", gender)
	fmt.Fprintf(&b, "Gender: %s\n", ti.GenderString())
	fmt.Fprintf(&b, "Time Played: %v-%v-%v-%v\n", ti.HoursPlayed, ti.MinPlayed, ti.SecPlayed, ti.FramePlayed)
	fmt.Fprintf(&b, "Trainer ID: %v\n", ti.ID)
	fmt.Fprintf(&b, "Secret ID: %v\n", ti.SecretID)


@@ 97,3 100,23 @@ func (ti *TrainerInfo) String() string {
	}
	return b.String()
}

func (ti *TrainerInfo) MarshalTab() string {
	return fmt.Sprintf("%s\t%s\t%v\t%v\t%v\t%v\t%v\t%v", ti.Name.string, ti.GenderString(),
		ti.HoursPlayed, ti.MinPlayed, ti.SecPlayed, ti.FramePlayed,
		ti.ID, ti.SecretID)
}

func (ti *TrainerInfo) UnmarshalTab(r io.Reader) error {
	var gender string
	_, err := fmt.Fscanln(r, &ti.Name.string, &gender,
		&ti.HoursPlayed, &ti.MinPlayed, &ti.SecPlayed, &ti.FramePlayed,
		&ti.ID, &ti.SecretID)
	switch gender {
	case "male":
		ti.Gender = 0
	case "female":
		ti.Gender = 1
	}
	return err
}

M util.go => util.go +22 -5
@@ 1,6 1,7 @@
package pse

import (
	"encoding/json"
	"fmt"
)



@@ 150,15 151,31 @@ func rtogen3(i rune, lang int) (byte, error) {
type PkString struct {
	string

	lang int
	data []byte
}

//Encoding embedded strings doesn't work nicely.
//Plus this cleans up the json output for structs
//that contain PkStrings.
func (p *PkString) MarshalJSON() ([]byte, error) {
	return json.Marshal(p.string)
}

func (p *PkString) UnmarshalJSON(b []byte) error {
	var tmp string
	err := json.Unmarshal(b, &tmp)
	if err != nil {
		return err
	}
	p.string = tmp
	return nil
}

func NewPkString(data []byte, lang int) (*PkString, error) {
	var tmp []rune
	p := &PkString{lang: lang, data: data}
	p := &PkString{data: data}
	for i, b := range data {
		n, err := gen3tor(b, p.lang)
		n, err := gen3tor(b, lang)
		if err != nil {
			return nil, err
		}


@@ 172,10 189,10 @@ func NewPkString(data []byte, lang int) (*PkString, error) {
	return p, nil
}

func (p *PkString) Commit() error {
func (p *PkString) Commit(lang int) error {
	var tmp []byte
	for _, r := range append([]rune(p.string), 0x00) {
		n, err := rtogen3(r, p.lang)
		n, err := rtogen3(r, lang)
		if err != nil {
			return err
		}