2020-01-31 23:18:11 -05:00
|
|
|
package d2config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-02-07 22:21:15 -05:00
|
|
|
"path/filepath"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotInit = errors.New("configuration is not initialized")
|
2020-02-09 14:12:04 -05:00
|
|
|
ErrWasInit = errors.New("configuration has already been initialized")
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
// Configuration defines the configuration for the engine, loaded from config.json
|
|
|
|
type Configuration struct {
|
|
|
|
Language string
|
|
|
|
FullScreen bool
|
|
|
|
Scale float64
|
|
|
|
RunInBackground bool
|
|
|
|
TicksPerSecond int
|
|
|
|
FpsCap int
|
|
|
|
VsyncEnabled bool
|
|
|
|
MpqPath string
|
|
|
|
MpqLoadOrder []string
|
|
|
|
SfxVolume float64
|
|
|
|
BgmVolume float64
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
var singleton *Configuration
|
|
|
|
|
|
|
|
func Initialize() error {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyNotInit()
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
configDir, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
singleton = getDefaultConfiguration()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir = path.Join(configDir, "OpenDiablo2")
|
|
|
|
configPath := path.Join(configDir, "config.json")
|
|
|
|
log.Printf("loading configuration file from %s...", configPath)
|
|
|
|
configFile, err := os.Open(configPath)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
var config Configuration
|
|
|
|
decoder := json.NewDecoder(configFile)
|
|
|
|
defer configFile.Close()
|
|
|
|
if err := decoder.Decode(&config); err == nil {
|
|
|
|
singleton = &config
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-07 22:21:15 -05:00
|
|
|
} else {
|
|
|
|
log.Printf("configuration file not found, writing default")
|
|
|
|
os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
|
|
|
|
configFile, err := os.Create(configPath)
|
|
|
|
if err == nil {
|
|
|
|
encoder := json.NewEncoder(configFile)
|
|
|
|
defer configFile.Close()
|
|
|
|
encoder.Encode(getDefaultConfiguration())
|
|
|
|
} else {
|
|
|
|
log.Printf("failed to write default configuration (%s)", err)
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
singleton = getDefaultConfiguration()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Save() error {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
configDir, err := os.UserConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir = path.Join(configDir, "OpenDiablo2")
|
|
|
|
if err := os.MkdirAll(configDir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(configDir, "config.json")
|
|
|
|
log.Printf("saving configuration file to %s...", configPath)
|
|
|
|
configFile, err := os.Create(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(configFile)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
if err := encoder.Encode(singleton); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func Get() *Configuration {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyWasInit() {
|
2020-01-31 23:18:11 -05:00
|
|
|
if singleton == nil {
|
2020-02-09 14:12:04 -05:00
|
|
|
panic(ErrNotInit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyNotInit() {
|
|
|
|
if singleton != nil {
|
|
|
|
panic(ErrWasInit)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
}
|