Set options from defaults on first start

Data/GenericData.dat specifies what the default values for some options
should be. Respect them on startup if the options in config are unset.
This commit is contained in:
2020-04-02 01:21:32 +01:00
parent 8ce24ce5f8
commit aa43011e8d
4 changed files with 95 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"errors"
"os"
"path/filepath"
@@ -37,8 +38,9 @@ type Options struct {
type Config struct {
filename string `toml:"-"`
Ordoor `toml:"ordoor"`
Options `toml:"options"`
Defaults *Options `toml:"-"`
Ordoor `toml:"ordoor"`
Options `toml:"options"`
}
func Load(filename string) (*Config, error) {
@@ -54,6 +56,12 @@ func Load(filename string) (*Config, error) {
return &out, err
}
func (c *Config) HasUnsetOptions() bool {
var empty Options
return c.Options == empty
}
func (c *Config) Save() error {
f, err := os.OpenFile(c.filename, os.O_WRONLY, 0644)
if err != nil {
@@ -69,6 +77,16 @@ func (c *Config) DataFile(path string) string {
return filepath.Join(c.DataDir, path)
}
func (c *Config) ResetDefaults() error {
if c.Defaults == nil {
return errors.New("Defaults not available")
}
c.Options = *c.Defaults
return c.Save()
}
func (o *Options) ResolutionIndex() int {
if o.XRes == 640 && o.YRes == 480 {
return 1