2018-10-13 03:23:55 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-04-02 01:21:32 +01:00
|
|
|
"errors"
|
2020-03-22 22:12:59 +00:00
|
|
|
"os"
|
2018-10-13 03:23:55 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
2020-03-22 17:56:24 +00:00
|
|
|
type Ordoor struct {
|
2018-10-13 03:23:55 +01:00
|
|
|
DataDir string `toml:"data_dir"`
|
|
|
|
VideoPlayer []string `toml:"video_player"`
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:33:26 +00:00
|
|
|
// Things set in the options hash
|
|
|
|
// TODO: load defaults from Data/GenericData.dat if they're not set
|
2020-03-22 22:12:59 +00:00
|
|
|
type Options struct {
|
|
|
|
PlayMovies bool `toml:"play_movies"`
|
|
|
|
Animations bool `toml:"animations"`
|
|
|
|
PlayMusic bool `toml:"play_music"`
|
|
|
|
CombatVoices bool `toml:"combat_voices"`
|
|
|
|
ShowGrid bool `toml:"show_grid"`
|
|
|
|
ShowPaths bool `toml:"show_paths"`
|
|
|
|
PointSaving bool `toml:"point_saving"`
|
|
|
|
AutoCutLevel bool `toml:"auto_cut_level"`
|
|
|
|
|
|
|
|
XRes int `toml:"x_resolution"`
|
|
|
|
YRes int `toml:"y_resolution"`
|
|
|
|
|
|
|
|
MusicVolume int `toml:"music_volume"`
|
|
|
|
SFXVolume int `toml:"sfx_volume"`
|
|
|
|
|
|
|
|
UnitSpeed int `toml:"unit_speed"`
|
|
|
|
AnimSpeed int `toml:"animation_speed"`
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:23:55 +01:00
|
|
|
type Config struct {
|
2020-03-22 22:12:59 +00:00
|
|
|
filename string `toml:"-"`
|
|
|
|
|
2020-04-02 01:21:32 +01:00
|
|
|
Defaults *Options `toml:"-"`
|
|
|
|
Ordoor `toml:"ordoor"`
|
|
|
|
Options `toml:"options"`
|
2018-10-13 03:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(filename string) (*Config, error) {
|
|
|
|
var out Config
|
|
|
|
|
|
|
|
_, err := toml.DecodeFile(filename, &out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-22 22:12:59 +00:00
|
|
|
out.filename = filename
|
|
|
|
|
2018-10-13 03:23:55 +01:00
|
|
|
return &out, err
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:21:32 +01:00
|
|
|
func (c *Config) HasUnsetOptions() bool {
|
|
|
|
var empty Options
|
|
|
|
|
|
|
|
return c.Options == empty
|
|
|
|
}
|
|
|
|
|
2020-03-22 22:12:59 +00:00
|
|
|
func (c *Config) Save() error {
|
|
|
|
f, err := os.OpenFile(c.filename, os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return toml.NewEncoder(f).Encode(c)
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:23:55 +01:00
|
|
|
// TODO: case-insensitive lookup
|
|
|
|
func (c *Config) DataFile(path string) string {
|
|
|
|
return filepath.Join(c.DataDir, path)
|
|
|
|
}
|
2020-03-24 23:11:37 +00:00
|
|
|
|
2020-04-02 01:21:32 +01:00
|
|
|
func (c *Config) ResetDefaults() error {
|
|
|
|
if c.Defaults == nil {
|
|
|
|
return errors.New("Defaults not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Options = *c.Defaults
|
|
|
|
|
|
|
|
return c.Save()
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:11:37 +00:00
|
|
|
func (o *Options) ResolutionIndex() int {
|
|
|
|
if o.XRes == 640 && o.YRes == 480 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.XRes == 800 && o.YRes == 600 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.XRes == 1024 && o.YRes == 768 {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
return 4 // Magic value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) SetResolutionIndex(value int) {
|
|
|
|
switch value {
|
|
|
|
case 1:
|
|
|
|
o.XRes = 640
|
|
|
|
o.YRes = 480
|
|
|
|
case 2:
|
|
|
|
o.XRes = 800
|
|
|
|
o.YRes = 600
|
|
|
|
case 3:
|
|
|
|
o.XRes = 1024
|
|
|
|
o.YRes = 768
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the value isn't recognised, silently ignore the request to avoid
|
|
|
|
// overwriting options the resolution slider doesn't know about
|
|
|
|
}
|