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")
|
|
|
|
ErrHasInit = errors.New("configuration has already been initialized")
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
if singleton != nil {
|
|
|
|
return ErrHasInit
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
if singleton == nil {
|
|
|
|
return ErrNotInit
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get() (*Configuration, error) {
|
|
|
|
if singleton == nil {
|
|
|
|
return nil, ErrNotInit
|
|
|
|
}
|
|
|
|
return singleton, nil
|
|
|
|
}
|