2020-01-31 23:18:11 -05:00
|
|
|
package d2config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
// Configuration defines the configuration for the engine, loaded from config.json
|
|
|
|
type Configuration struct {
|
2020-07-04 00:49:16 -04:00
|
|
|
mpqLoadOrder []string
|
|
|
|
language string
|
|
|
|
mpqPath string
|
|
|
|
ticksPerSecond int
|
|
|
|
fpsCap int
|
|
|
|
sfxVolume float64
|
|
|
|
bgmVolume float64
|
|
|
|
fullScreen bool
|
|
|
|
runInBackground bool
|
|
|
|
vsyncEnabled bool
|
|
|
|
backend string
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-04 00:49:16 -04:00
|
|
|
func (c *Configuration) MpqLoadOrder() []string {
|
|
|
|
return c.mpqLoadOrder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) Language() string {
|
|
|
|
return c.language
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) MpqPath() string {
|
|
|
|
return c.mpqPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) TicksPerSecond() int {
|
|
|
|
return c.ticksPerSecond
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-07-04 00:49:16 -04:00
|
|
|
func (c *Configuration) FpsCap() int {
|
|
|
|
return c.fpsCap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) SfxVolume() float64 {
|
|
|
|
return c.sfxVolume
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) BgmVolume() float64 {
|
|
|
|
return c.bgmVolume
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) FullScreen() bool {
|
|
|
|
return c.fullScreen
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) RunInBackground() bool {
|
|
|
|
return c.runInBackground
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) VsyncEnabled() bool {
|
|
|
|
return c.vsyncEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) Backend() string {
|
|
|
|
return c.backend
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a configuration object from disk
|
|
|
|
func (c *Configuration) Load() error {
|
2020-02-20 08:41:41 -05:00
|
|
|
configPaths := []string{
|
|
|
|
getDefaultConfigPath(),
|
2020-07-04 00:49:16 -04:00
|
|
|
getLocalConfigPath(),
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
var loaded bool
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
for _, configPath := range configPaths {
|
|
|
|
log.Printf("loading configuration file from %s...", configPath)
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
if err := load(configPath); err == nil {
|
|
|
|
loaded = true
|
|
|
|
break
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
2020-02-20 08:41:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !loaded {
|
|
|
|
log.Println("failed to load configuration file, saving default configuration...")
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
if err := Save(); err != nil {
|
|
|
|
return err
|
2020-02-07 22:21:15 -05:00
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-04 00:49:16 -04:00
|
|
|
// Save saves the configuration object to disk
|
|
|
|
func (c *Configuration) Save() error {
|
2020-02-20 08:41:41 -05:00
|
|
|
configPath := getDefaultConfigPath()
|
2020-01-31 23:18:11 -05:00
|
|
|
log.Printf("saving configuration file to %s...", configPath)
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
var err error
|
|
|
|
if err = save(configPath); err != nil {
|
|
|
|
log.Printf("failed to write configuration file (%s)", err)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
return err
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|