~moody/pse

pse/save.go -rw-r--r-- 3.3 KiB
e4de17d5 — Jacob Moody Introduce pse(1) 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package pse

import (
	"io"
)

type GameType int

const (
	RSType GameType = iota //Ruby/Sapphire
	RGType                 //Fire Red/Leaf Green
	EType                  //Emerald
)

type Save struct {
	Type  GameType
	Info  *TrainerInfo
	Inv   *Inventory
	Boxes *Boxes

	active   []Section
	unactive []Section
	//TODO: Parse these properly
	hof      [8192]byte
	gift     [4096]byte
	recorded [4096]byte
}

func (s *Save) Write(f io.WriteSeeker) error {
	_, err := f.Seek(0, io.SeekStart)
	if err != nil {
		return err
	}

	err = s.Info.Commit()
	if err != nil {
		return err
	}
	s.Inv.Commit()
	err = s.Boxes.Commit()
	if err != nil {
		return err
	}
	for i := 0; i < 14; i++ {
		_, err = f.Write(s.active[i].data[:])
		if err != nil {
			return err
		}
	}

	_, err = f.Seek(0xE000, io.SeekStart)
	if err != nil {
		return err
	}
	for i := 0; i < 14; i++ {
		_, err = f.Write(s.unactive[i].data[:])
		if err != nil {
			return err
		}
	}

	_, err = f.Seek(0x1C000, io.SeekStart)
	if err != nil {
		return err
	}
	_, err = f.Write(s.hof[:])
	if err != nil {
		return err
	}

	_, err = f.Seek(0x1E000, io.SeekStart)
	if err != nil {
		return err
	}
	_, err = f.Write(s.gift[:])
	if err != nil {
		return err
	}

	_, err = f.Seek(0x1F000, io.SeekStart)
	if err != nil {
		return err
	}
	_, err = f.Write(s.recorded[:])
	if err != nil {
		return err
	}

	return nil
}

func NewSave(f io.ReadSeeker) (*Save, error) {
	s := &Save{Boxes: &Boxes{}}
	_, err := f.Seek(0, io.SeekStart)
	if err != nil {
		return nil, err
	}
	var block [28]Section
	var section [4096]byte
	for i := 0; i < 14; i++ {
		_, err = f.Read(section[:])
		if err != nil {
			return nil, err
		}
		block[i].Load(section)
	}
	_, err = f.Seek(0xE000, io.SeekStart)
	if err != nil {
		return nil, err
	}
	for i := 14; i < 28; i++ {
		_, err = f.Read(section[:])
		if err != nil {
			return nil, err
		}
		block[i].Load(section)
	}

	_, err = f.Seek(0x1C000, io.SeekStart)
	if err != nil {
		return nil, err
	}
	_, err = f.Read(s.hof[:])
	if err != nil {
		return nil, err
	}

	_, err = f.Seek(0x1E000, io.SeekStart)
	if err != nil {
		return nil, err
	}
	_, err = f.Read(s.gift[:])
	if err != nil {
		return nil, err
	}

	_, err = f.Seek(0x1F000, io.SeekStart)
	if err != nil {
		return nil, err
	}
	_, err = f.Read(s.recorded[:])
	if err != nil {
		return nil, err
	}

	if block[0].Index > block[14].Index {
		s.active = block[0:14]
		s.unactive = block[14:]
	} else {
		s.unactive = block[0:14]
		s.active = block[14:]
	}

	//Due to differences in save data format we do two passes over the active
	//save blocks, this is used to determine what type of game the save file is for
	//and stores that in the save struct.
	for i := range s.active {
		switch s.active[i].ID {
		case SectionTI:
			s.Info, err = NewTrainerInfo(&s.active[i])
			if err != nil {
				return nil, err
			}
		}
	}
	switch v := GameType(s.Info.GameCode); v {
	case RSType, RGType:
		s.Type = v
	default:
		s.Type = EType
	}

	for i := range s.active {
		switch s.active[i].ID {
		case SectionInv:
			s.Inv = NewInventory(&s.active[i], s.Type, s.Info.SecurityKey)
		case SectionPCA, SectionPCB, SectionPCC, SectionPCD, SectionPCE, SectionPCF, SectionPCG, SectionPCH, SectionPCI:
			err = s.Boxes.Load(int(s.active[i].ID), &s.active[i])
			if err != nil {
				return nil, err
			}
		}
	}

	return s, s.Boxes.Parse()
}