/
xmlconfig.go
74 lines (62 loc) · 1.78 KB
/
xmlconfig.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
package gamesys
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
// Configuration setting collection
type Configuration struct {
XMLName xml.Name `xml:"configuration"`
System System `xml:"system"`
Default Default `xml:"default"`
}
// System configuration setting.
type System struct {
XMLName xml.Name `xml:"system"`
Window Window `xml:"window"`
Scripting Scripting `xml:"scripting"`
Directory Directory `xml:"directory"`
}
// Window is the options for starting pixel window.
type Window struct {
XMLName xml.Name `xml:"window"`
Width float64 `xml:"width,attr"`
Height float64 `xml:"height,attr"`
Title string `xml:"title,attr"`
}
// Scripting sets customizable script options.
type Scripting struct {
XMLName xml.Name `xml:"scripting"`
Dir string `xml:"dir,attr"`
Extension string `xml:"extension,attr"`
}
// Directory will set default directories not set elsewhere
type Directory struct {
XMLName xml.Name `xml:"directory"`
Characters string `xml:"characters,attr"`
}
// Default object values when not provided.
type Default struct {
XMLName xml.Name `xml:"default"`
Scene Scene `xml:"scene"`
Actor Actor `xml:"actor"`
MessageBox MessageBox `xml:"messagebox"`
}
// LoadConfiguration loads a configuration from the provided XML file.
func LoadConfiguration(file string) (*Configuration, error) {
// New empty configuration
newconfig := &Configuration{}
// Open our file, ensuring it closes later.
xmlFile, err := os.Open(file)
if err != nil {
fmt.Println(err)
return newconfig, err
}
defer xmlFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(xmlFile)
// Process XML file in the simplest way possible.
err = xml.Unmarshal(byteValue, &newconfig)
return newconfig, err
}