-
Notifications
You must be signed in to change notification settings - Fork 0
/
th8_replay.go
204 lines (198 loc) · 4.64 KB
/
th8_replay.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package threp
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/pkg/errors"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"strconv"
"strings"
)
func DecodeTh8Replay(fin io.Reader) (*TH8RepInfo, error) {
dat := make([]byte, 4)
n, err := fin.Read(dat)
if err != nil {
return nil, err
}
if n < 4 {
return nil, errors.New("not a replay")
}
if string(dat) != "T8RP" {
return nil, errors.New("not a th08 replay")
}
return decodeTh8Replay(fin)
}
func decodeTh8Replay(fin io.Reader) (*TH8RepInfo, error) {
// read data size
buf := make([]byte, 8)
n, err := fin.Read(buf)
if err != nil {
return nil, err
}
if n != 8 {
return nil, errors.New("decompress failed")
}
buf = buf[:4]
n, err = fin.Read(buf)
if err != nil {
return nil, err
}
if n != 4 {
return nil, errors.New("decompress failed")
}
// move to fileinfo block.
err = seek(fin, int64(binary.LittleEndian.Uint32(buf)-4-8-4))
if err != nil {
return nil, err
}
// cut USER????????
buf = make([]byte, 12)
_, err = fin.Read(buf)
if err != nil {
return nil, err
}
buf, err = io.ReadAll(fin)
if err != nil {
return nil, err
}
ret, ok, err := parseJP(buf)
if ok {
return ret, err
}
ret, ok, err = parseCN(buf)
if ok {
return ret, err
}
return nil, errors.New("parse failed")
}
func parseJP(buf []byte) (*TH8RepInfo, bool, error) {
m, err := linesToMap(transform.NewReader(bytes.NewReader(buf), japanese.ShiftJIS.NewDecoder()), "\t")
if err != nil {
return nil, false, err
}
if _, ok := m["プレイヤー名"]; !ok {
return nil, false, nil
}
ret := &TH8RepInfo{}
ret.game = "8"
// line1: プレイヤー名\t(name)\r\n
ret.Player = m["プレイヤー名"]
// line2: プレイ時刻\t(yyyy/mm/dd hh24:mi:ss)\r\n
ret.Date = m["プレイ時刻"]
// line3: キャラ名\t(char)\r\n
ret.Char = m["キャラ名"]
// line4: スコア\t\t(score)\r\n
score, ok := m["スコア"]
if ok {
ret.Score, err = strconv.ParseInt(score, 10, 64)
if err != nil {
return nil, true, err
}
}
// line5: 難易度\t\t(level)\r\n
ret.Rank = m["難易度"]
// line6: 最終ステージ\t(stage)\r\n
ret.Stage = m["最終ステージ"]
// line7: ミス回数\t(miss)\r\n
miss, ok := m["ミス回数"]
if ok {
ret.Miss, err = strconv.Atoi(miss)
if err != nil {
return nil, true, err
}
}
// line8: ボム回数\t(bomb)\r\n
bomb, ok := m["ボム回数"]
if ok {
ret.Bomb, err = strconv.Atoi(bomb)
if err != nil {
return nil, true, err
}
}
// line9: 処理落ち率\t(slow rate)\r\n
drop, ok := m["処理落ち率"]
if ok {
_, err = fmt.Sscanf(drop, "%f%%", &ret.Drop)
if err != nil {
return nil, true, err
}
}
// line10: 人間率\t\t(human rate)\r\n
human, ok := m["人間率"]
if ok {
_, err = fmt.Sscanf(strings.ReplaceAll(human, "%", "%"), "%f%%", &ret.Human)
if err != nil {
return nil, true, err
}
}
// line11: ゲームのバージョン\t(version)\r\n
ret.Version = m["ゲームのバージョン"]
return ret, true, nil
}
func parseCN(buf []byte) (*TH8RepInfo, bool, error) {
m, err := linesToMap(transform.NewReader(bytes.NewBuffer(buf), simplifiedchinese.GBK.NewDecoder()), "\t")
if err != nil {
return nil, false, err
}
if _, ok := m["玩家名"]; !ok {
return nil, false, nil
}
ret := &TH8RepInfo{}
ret.game = "8"
// line1: プレイヤー名\t(name)\r\n
ret.Player = m["玩家名"]
// line2: プレイ時刻\t(yyyy/mm/dd hh24:mi:ss)\r\n
ret.Date = m["游戏时间"]
// line3: キャラ名\t(char)\r\n
ret.Char = m["角色名"]
// line4: スコア\t\t(score)\r\n
score, ok := m["分数"]
if ok {
ret.Score, err = strconv.ParseInt(score, 10, 64)
if err != nil {
return nil, true, err
}
}
// line5: 難易度\t\t(level)\r\n
ret.Rank = m["难易度"]
// line6: 最終ステージ\t(stage)\r\n
ret.Stage = m["最终面"]
// line7: ミス回数\t(miss)\r\n
miss, ok := m["miss回数"]
if ok {
ret.Miss, err = strconv.Atoi(miss)
if err != nil {
return nil, true, err
}
}
// line8: ボム回数\t(bomb)\r\n
bomb, ok := m["Bomb回数"]
if ok {
ret.Bomb, err = strconv.Atoi(bomb)
if err != nil {
return nil, true, err
}
}
// line9: 処理落ち率\t(slow rate)\r\n
drop, ok := m["処理落率"]
if ok {
_, err = fmt.Sscanf(drop, "%f%%", &ret.Drop)
if err != nil {
return nil, true, err
}
}
// line10: 人間率\t\t(human rate)\r\n
human, ok := m["人间率"]
if ok {
_, err = fmt.Sscanf(strings.ReplaceAll(human, "%", "%"), "%f%%", &ret.Human)
if err != nil {
return nil, true, err
}
}
// line11: ゲームのバージョン\t(version)\r\n
ret.Version = m["游戏版本"]
return ret, true, nil
}