-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundParser.go
65 lines (50 loc) · 1.15 KB
/
soundParser.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
package main
import (
"encoding/xml"
"fmt"
"os"
)
type SoundParser struct {
instance *SoundParser
}
func (sp *SoundParser) GetInstance() *SoundParser {
if sp.instance == nil {
sp.instance = &SoundParser{}
}
return sp.instance
}
func (sp *SoundParser) Load(src string) error {
if err := sp.parse(src); err != nil {
return fmt.Errorf("failed to parse sounds.xml, %v", err)
}
return nil
}
func (sp *SoundParser) parse(src string) error {
data, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("failed to read the file, %v, %v", src, err)
}
var xmlSounds XMLSounds
err = xml.Unmarshal(data, &xmlSounds)
if err != nil {
return fmt.Errorf("failed to unmarshal xml, %v", err)
}
for _, mus := range xmlSounds.Music {
SoundManagerInstance.GetInstance().LoadMusic(mus.Id, mus.Src)
}
for _, eff := range xmlSounds.Effect {
SoundManagerInstance.GetInstance().LoadEffect(eff.Id, eff.Src)
}
return nil
}
func (sp *SoundParser) Destroy() {
sp.instance = nil
}
type XMLSounds struct {
Music []XMLSound `xml:"music,"`
Effect []XMLSound `xml:"effect,"`
}
type XMLSound struct {
Id string `xml:"id,attr"`
Src string `xml:"src,attr"`
}