-
Notifications
You must be signed in to change notification settings - Fork 83
/
config.go
204 lines (191 loc) · 5.74 KB
/
config.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 oak
import (
"encoding/json"
"io"
"github.com/oakmound/oak/v4/fileutil"
"github.com/oakmound/oak/v4/shiny/driver"
)
// A Config defines the settings oak accepts on initialization. Some of these settings may be ignored depending
// on the target platform.
type Config struct {
Driver Driver `json:"-"`
// Assets defines where assets should be loaded from by default. Defaults to
// 'assets/audio' and 'assets/images'.
Assets Assets `json:"assets"`
Debug Debug `json:"debug"`
Screen Screen `json:"screen"`
BatchLoadOptions BatchLoadOptions `json:"batchLoadOptions"`
// FrameRate, representing the rate enter frame events are triggered, defaults to 60.
FrameRate int `json:"frameRate"`
// DrawFrameRate is ignored on JS. It defaults to 60.
DrawFrameRate int `json:"drawFrameRate"`
// IdleDrawFrameRate defaults to 60. When a window goes out of focus, this setting can be lowered to
// reduce resource consumption by drawing.
IdleDrawFrameRate int `json:"idleDrawFrameRate"`
// Language defines the language oak logs are attempted to be translated to. Defaults to English.
Language string `json:"language"`
// Title defaults to 'Oak Window'.
Title string `json:"title"`
BatchLoad bool `json:"batchLoad"`
GestureSupport bool `json:"gestureSupport"`
LoadBuiltinCommands bool `json:"loadBuiltinCommands"`
TrackInputChanges bool `json:"trackInputChanges"`
// EnableDebugConsole is ignored on JS.
EnableDebugConsole bool `json:"enableDebugConsole"`
TopMost bool `json:"topmost"`
Borderless bool `json:"borderless"`
Fullscreen bool `json:"fullscreen"`
SkipRNGSeed bool `json:"skip_rng_seed"`
// UnlimitedDrawFrameRate is ignored on JS (it is effectively always true).
UnlimitedDrawFrameRate bool `json:"unlimitedDrawFrameRate"`
}
// NewConfig creates a config from a set of transformation options.
func NewConfig(opts ...ConfigOption) (Config, error) {
c := Config{}
c = c.setDefaults()
var err error
for _, o := range opts {
c, err = o(c)
if err != nil {
return c, err
}
}
return c, nil
}
func (c Config) setDefaults() Config {
c.Driver = driver.Main
c.Assets = Assets{
AudioPath: "assets/audio/",
ImagePath: "assets/images/",
}
c.Debug = Debug{
Level: "ERROR",
}
c.Screen = Screen{
Height: 480,
Width: 640,
Scale: 1,
}
c.FrameRate = 60
c.DrawFrameRate = 60
c.IdleDrawFrameRate = 60
c.Language = "English"
c.Title = "Oak Window"
return c
}
// Assets is a json type storing paths to different asset folders
type Assets struct {
AudioPath string `json:"audioPath"`
ImagePath string `json:"imagePath"`
}
// Debug is a json type storing the starting debug filter and level
type Debug struct {
Filter string `json:"filter"`
Level string `json:"level"`
}
// Screen is a json type storing the starting screen width and height
type Screen struct {
X int `json:"X"`
Y int `json:"Y"`
Height int `json:"height"`
Width int `json:"width"`
Scale float64 `json:"scale"`
}
// BatchLoadOptions is a json type storing customizations for batch loading.
// These settings do not take effect unless Config.BatchLoad is true.
type BatchLoadOptions struct {
BlankOutAudio bool `json:"blankOutAudio"`
MaxImageFileSize int64 `json:"maxImageFileSize"`
}
// FileConfig loads a config file, that could exist inside
// oak's binary data storage (see fileutil), to SetupConfig
func FileConfig(filePath string) ConfigOption {
return func(c Config) (Config, error) {
r, err := fileutil.Open(filePath)
if err != nil {
return c, err
}
defer r.Close()
return ReaderConfig(r)(c)
}
}
// A ConfigOption transforms a Config object.
type ConfigOption func(Config) (Config, error)
// ReaderConfig reads a Config as json from the given reader.
func ReaderConfig(r io.Reader) ConfigOption {
return func(c Config) (Config, error) {
c2 := Config{}
decoder := json.NewDecoder(r)
decoder.DisallowUnknownFields()
err := decoder.Decode(&c2)
if err != nil {
return c, err
}
c2 = c.overwriteFrom(c2)
return c2, nil
}
}
func (c Config) overwriteFrom(c2 Config) Config {
if c2.Driver != nil {
c.Driver = c2.Driver
}
if c2.Assets.AudioPath != "" {
c.Assets.AudioPath = c2.Assets.AudioPath
}
if c2.Assets.ImagePath != "" {
c.Assets.ImagePath = c2.Assets.ImagePath
}
if c2.Debug.Filter != "" {
c.Debug.Filter = c2.Debug.Filter
}
if c2.Debug.Level != "" {
c.Debug.Level = c2.Debug.Level
}
if c2.Screen.X != 0 {
c.Screen.X = c2.Screen.X
}
if c2.Screen.Y != 0 {
c.Screen.Y = c2.Screen.Y
}
if c2.Screen.Height != 0 {
c.Screen.Height = c2.Screen.Height
}
if c2.Screen.Width != 0 {
c.Screen.Width = c2.Screen.Width
}
if c2.Screen.Scale != 0 {
c.Screen.Scale = c2.Screen.Scale
}
c.BatchLoadOptions.BlankOutAudio = c2.BatchLoadOptions.BlankOutAudio
if c2.BatchLoadOptions.MaxImageFileSize != 0 {
c.BatchLoadOptions.MaxImageFileSize = c2.BatchLoadOptions.MaxImageFileSize
}
if c2.FrameRate != 0 {
c.FrameRate = c2.FrameRate
}
if c2.DrawFrameRate != 0 {
c.DrawFrameRate = c2.DrawFrameRate
}
if c2.IdleDrawFrameRate != 0 {
c.IdleDrawFrameRate = c2.IdleDrawFrameRate
}
if c2.Language != "" {
c.Language = c2.Language
}
if c2.Title != "" {
c.Title = c2.Title
}
// Booleans can be directly overwritten-- all booleans in a Config
// default to false, if they were unset they will stay false.
c.BatchLoad = c2.BatchLoad
c.GestureSupport = c2.GestureSupport
c.LoadBuiltinCommands = c2.LoadBuiltinCommands
c.TrackInputChanges = c2.TrackInputChanges
c.EnableDebugConsole = c2.EnableDebugConsole
c.TopMost = c2.TopMost
c.Borderless = c2.Borderless
c.Fullscreen = c2.Fullscreen
c.SkipRNGSeed = c2.SkipRNGSeed
c.UnlimitedDrawFrameRate = c2.UnlimitedDrawFrameRate
return c
}