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-06-24 10:13:11 -04:00
|
|
|
MpqLoadOrder []string
|
2020-02-01 18:55:56 -05:00
|
|
|
Language string
|
2020-06-24 10:13:11 -04:00
|
|
|
MpqPath string
|
2020-02-01 18:55:56 -05:00
|
|
|
TicksPerSecond int
|
|
|
|
FpsCap int
|
|
|
|
SfxVolume float64
|
|
|
|
BgmVolume float64
|
2020-06-24 10:13:11 -04:00
|
|
|
FullScreen bool
|
|
|
|
RunInBackground bool
|
|
|
|
VsyncEnabled bool
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 10:13:11 -04:00
|
|
|
var singleton = getDefaultConfig()
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
func Load() error {
|
|
|
|
configPaths := []string{
|
|
|
|
getLocalConfigPath(),
|
|
|
|
getDefaultConfigPath(),
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
var loaded bool
|
|
|
|
for _, configPath := range configPaths {
|
|
|
|
log.Printf("loading configuration file from %s...", configPath)
|
|
|
|
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...")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func 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
|
|
|
}
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
func Get() Configuration {
|
2020-01-31 23:18:11 -05:00
|
|
|
if singleton == nil {
|
2020-02-20 08:41:41 -05:00
|
|
|
panic("configuration is not initialized")
|
2020-02-09 14:12:04 -05:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
return *singleton
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|