From aa43011e8d9edc0a0583a00c858983f1a5acec9a Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 2 Apr 2020 01:21:32 +0100 Subject: [PATCH] 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. --- internal/assetstore/assetstore.go | 1 + internal/assetstore/data.go | 62 +++++++++++++++++++++++++++++++ internal/config/config.go | 22 ++++++++++- internal/ordoor/ordoor.go | 12 ++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 internal/assetstore/data.go diff --git a/internal/assetstore/assetstore.go b/internal/assetstore/assetstore.go index 55167f4..ecf8cd3 100644 --- a/internal/assetstore/assetstore.go +++ b/internal/assetstore/assetstore.go @@ -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 diff --git a/internal/assetstore/data.go b/internal/assetstore/data.go new file mode 100644 index 0000000..4487417 --- /dev/null +++ b/internal/assetstore/data.go @@ -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 +} diff --git a/internal/config/config.go b/internal/config/config.go index 5bd9dd7..2acf187 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 diff --git a/internal/ordoor/ordoor.go b/internal/ordoor/ordoor.go index 5348eb3..f7f3705 100644 --- a/internal/ordoor/ordoor.go +++ b/internal/ordoor/ordoor.go @@ -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) }