This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathencoding.go
108 lines (99 loc) · 2.01 KB
/
encoding.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
package main
import (
"bytes"
"compress/zlib"
"encoding/gob"
)
func init() {
gob.Register(potion(0))
gob.Register(projectile(0))
gob.Register(&simpleEvent{})
gob.Register(&monsterEvent{})
gob.Register(&cloudEvent{})
gob.Register(armour(0))
gob.Register(weapon(0))
gob.Register(shield(0))
}
func (g *game) GameSave() ([]byte, error) {
data := bytes.Buffer{}
enc := gob.NewEncoder(&data)
err := enc.Encode(g)
if err != nil {
return nil, err
}
var buf bytes.Buffer
w := zlib.NewWriter(&buf)
w.Write(data.Bytes())
w.Close()
return buf.Bytes(), nil
}
type config struct {
RuneNormalModeKeys map[rune]keyAction
RuneTargetModeKeys map[rune]keyAction
DarkLOS bool
Small bool
Tiles bool
Version string
}
func (c *config) ConfigSave() ([]byte, error) {
data := bytes.Buffer{}
enc := gob.NewEncoder(&data)
err := enc.Encode(c)
if err != nil {
return nil, err
}
return data.Bytes(), nil
}
func (g *game) DecodeGameSave(data []byte) (*game, error) {
buf := bytes.NewReader(data)
r, err := zlib.NewReader(buf)
if err != nil {
return nil, err
}
dec := gob.NewDecoder(r)
lg := &game{}
err = dec.Decode(lg)
if err != nil {
return nil, err
}
r.Close()
return lg, nil
}
func (g *game) DecodeConfigSave(data []byte) (*config, error) {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
c := &config{}
err := dec.Decode(c)
if err != nil {
return nil, err
}
return c, nil
}
func (g *game) EncodeDrawLog() ([]byte, error) {
data := bytes.Buffer{}
enc := gob.NewEncoder(&data)
err := enc.Encode(&g.DrawLog)
if err != nil {
return nil, err
}
var buf bytes.Buffer
w := zlib.NewWriter(&buf)
w.Write(data.Bytes())
w.Close()
return buf.Bytes(), nil
}
func (g *game) DecodeDrawLog(data []byte) ([]drawFrame, error) {
buf := bytes.NewReader(data)
r, err := zlib.NewReader(buf)
if err != nil {
return nil, err
}
dec := gob.NewDecoder(r)
dl := []drawFrame{}
err = dec.Decode(&dl)
if err != nil {
return nil, err
}
r.Close()
return dl, nil
}