1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-07-01 19:35:22 +00:00
OpenDiablo2/d2core/d2config/d2config.go
gravestench 5ac03d6f49
refactored game bootstrap, removed d2config.Config singleton (#899)
* Remove d2config.Config singleton

* refactored config file bootstrap
* `d2loader.Loader` adds the config directories during init
* `d2asset.AssetManager` loads the config file during init
* mpq verification logic removed from d2config; this is done by d2loader
* added `errorMessage` to `d2app.App` for setting the error message for the error screen.

* fixed loader test
2020-11-03 07:54:15 -05:00

72 lines
1.4 KiB
Go

package d2config
import (
"encoding/json"
"os"
"path"
"path/filepath"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
)
// Configuration defines the configuration for the engine, loaded from config.json
type Configuration struct {
MpqLoadOrder []string
Language string
MpqPath string
TicksPerSecond int
FpsCap int
SfxVolume float64
BgmVolume float64
FullScreen bool
RunInBackground bool
VsyncEnabled bool
Backend string
LogLevel d2util.LogLevel
path string
}
// Save saves the configuration object to disk
func (c *Configuration) Save() error {
configDir := path.Dir(c.path)
if err := os.MkdirAll(configDir, 0750); err != nil {
return err
}
configFile, err := os.Create(c.path)
if err != nil {
return err
}
buf, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
if _, err := configFile.Write(buf); err != nil {
return err
}
return configFile.Close()
}
// Dir returns the directory component of the path
func (c *Configuration) Dir() string {
return filepath.Dir(c.path)
}
// Base returns the base component of the path
func (c *Configuration) Base() string {
return filepath.Base(c.path)
}
// Path returns the config file path
func (c *Configuration) Path() string {
return c.path
}
// SetPath sets where the config file is saved to (a full path)
func (c *Configuration) SetPath(p string) {
c.path = p
}