2020-01-31 23:18:11 -05:00
|
|
|
package d2config
|
|
|
|
|
|
|
|
import (
|
2020-07-08 09:16:16 -04:00
|
|
|
"encoding/json"
|
2020-01-31 23:18:11 -05:00
|
|
|
"log"
|
2020-07-08 09:16:16 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
// Config holds the configuration from config.json
|
|
|
|
var Config *Configuration //nolint:gochecknoglobals // Currently global by design
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
// Configuration defines the configuration for the engine, loaded from config.json
|
|
|
|
type Configuration struct {
|
2020-07-08 09:16: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-07-04 00:49:16 -04:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
// Load loads a configuration object from disk
|
|
|
|
func Load() error {
|
|
|
|
Config = new(Configuration)
|
|
|
|
return Config.Load()
|
2020-07-04 00:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a configuration object from disk
|
|
|
|
func (c *Configuration) Load() error {
|
2020-02-20 08:41:41 -05:00
|
|
|
configPaths := []string{
|
2020-07-08 09:16:16 -04:00
|
|
|
defaultConfigPath(),
|
|
|
|
localConfigPath(),
|
2020-01-31 23:18:11 -05: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-07-08 09:16:16 -04:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
continue
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
2020-02-20 08:41:41 -05:00
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
configFile, err := os.Open(path.Clean(configPath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
if err := json.NewDecoder(configFile).Decode(&Config); err != nil {
|
2020-02-20 08:41:41 -05:00
|
|
|
return err
|
2020-02-07 22:21:15 -05:00
|
|
|
}
|
2020-07-08 09:16:16 -04:00
|
|
|
|
|
|
|
if err := configFile.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
log.Println("failed to load configuration file, saving default configuration...")
|
|
|
|
|
|
|
|
Config = defaultConfig()
|
|
|
|
|
|
|
|
return Config.Save()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-04 00:49:16 -04:00
|
|
|
// Save saves the configuration object to disk
|
|
|
|
func (c *Configuration) Save() error {
|
2020-07-08 09:16:16 -04:00
|
|
|
configPath := defaultConfigPath()
|
2020-01-31 23:18:11 -05:00
|
|
|
log.Printf("saving configuration file to %s...", configPath)
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
configDir := path.Dir(configPath)
|
|
|
|
if err := os.MkdirAll(configDir, 0750); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := os.Create(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.MarshalIndent(Config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
if _, err := configFile.Write(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return configFile.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultConfigPath() string {
|
|
|
|
if configDir, err := os.UserConfigDir(); err == nil {
|
|
|
|
return path.Join(configDir, "OpenDiablo2", "config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return localConfigPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func localConfigPath() string {
|
|
|
|
return path.Join(path.Dir(os.Args[0]), "config.json")
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|