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:
@@ -35,6 +35,7 @@ type AssetStore struct {
|
||||
|
||||
// These members are used to store things we've already loaded
|
||||
fonts map[string]*Font
|
||||
generic *data.Generic
|
||||
maps map[string]*Map
|
||||
menus map[string]*Menu
|
||||
objs map[string]*Object
|
||||
|
62
internal/assetstore/data.go
Normal file
62
internal/assetstore/data.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package assetstore
|
||||
|
||||
import (
|
||||
"code.ur.gs/lupine/ordoor/internal/config"
|
||||
"code.ur.gs/lupine/ordoor/internal/data"
|
||||
)
|
||||
|
||||
// Generic returns a struct containing a grab-bag of otherwise-unrelated data
|
||||
// TODO: it would be nice if this could be cleaner
|
||||
func (a *AssetStore) Generic() (*data.Generic, error) {
|
||||
if a.generic != nil {
|
||||
return a.generic, nil
|
||||
}
|
||||
|
||||
filename, err := a.lookup("GenericData", "dat", "Data")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
generic, err := data.LoadGeneric(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a.generic = generic
|
||||
|
||||
return generic, nil
|
||||
}
|
||||
|
||||
func (a *AssetStore) DefaultOptions() (*config.Options, error) {
|
||||
cfg := &config.Options{}
|
||||
g, err := a.Generic()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg.PlayMovies = intToBool(g.Options[data.OptionMovies])
|
||||
cfg.PlayMusic = intToBool(g.Options[data.OptionMusic])
|
||||
cfg.CombatVoices = intToBool(g.Options[data.OptionCombatVoices])
|
||||
cfg.ShowGrid = intToBool(g.Options[data.OptionGrid])
|
||||
cfg.ShowPaths = intToBool(g.Options[data.OptionShowPaths])
|
||||
cfg.PointSaving = intToBool(g.Options[data.OptionPointSave])
|
||||
cfg.AutoCutLevel = intToBool(g.Options[data.OptionAutoCutLevel])
|
||||
cfg.Animations = intToBool(g.Options[data.OptionShowUnitAnimations])
|
||||
|
||||
// These are overrides of data.OptionCombatResolution. *This* default from
|
||||
// 1998 is no good at all!
|
||||
cfg.XRes = 1280
|
||||
cfg.YRes = 1024
|
||||
|
||||
cfg.MusicVolume = g.Options[data.OptionMusicVolume]
|
||||
cfg.SFXVolume = g.Options[data.OptionSoundEffectsVolume]
|
||||
|
||||
cfg.UnitSpeed = g.Options[data.OptionUnitAnimationSpeed]
|
||||
cfg.AnimSpeed = g.Options[data.OptionEffectAnimationSpeed]
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func intToBool(i int) bool {
|
||||
return i > 0
|
||||
}
|
@@ -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
|
||||
|
@@ -53,6 +53,18 @@ func Run(configFile string, overrideX, overrideY int) error {
|
||||
return fmt.Errorf("Failed to initialize asset store: %v", err)
|
||||
}
|
||||
|
||||
defaults, err := assets.DefaultOptions()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to read option defaults: %v", err)
|
||||
}
|
||||
|
||||
cfg.Defaults = defaults
|
||||
if cfg.HasUnsetOptions() {
|
||||
if err := cfg.ResetDefaults(); err != nil {
|
||||
return fmt.Errorf("Failed to set options on first-start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := audio.NewContext(48000); err != nil {
|
||||
return fmt.Errorf("Failed to set up audio context: %v", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user