105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/knadh/koanf"
|
|
"github.com/knadh/koanf/providers/env"
|
|
"github.com/knadh/koanf/providers/posflag"
|
|
input "github.com/quasilyte/ebitengine-input"
|
|
flag "github.com/spf13/pflag"
|
|
)
|
|
|
|
const Usage string = `openquell [options]
|
|
-t --tps <ticks> Set ticks per second, default 60.
|
|
-d --debug Enable debugging output.
|
|
`
|
|
|
|
type Config struct {
|
|
TPS int `koanf:"tps"`
|
|
Loglevel string `koanf:"loglevel"`
|
|
Debug bool `koanf:"debug"` // loglevel=debug
|
|
Startlevel int
|
|
Keymap input.Keymap `koanf:"keymap"` // we put it here so we can eventually customize it later
|
|
}
|
|
|
|
func InitConfig() (*Config, error) {
|
|
var kloader = koanf.New(".")
|
|
|
|
// Load default values using the confmap provider.
|
|
/*
|
|
if err := kloader.Load(confmap.Provider(map[string]interface{}{
|
|
"keymap": input.Keymap{
|
|
InputMoveLeft: {input.KeyGamepadLeft, input.KeyLeft, input.KeyA},
|
|
InputMoveRight: {input.KeyGamepadRight, input.KeyRight, input.KeyD},
|
|
InputMoveUp: {input.KeyGamepadUp, input.KeyUp, input.KeyW},
|
|
InputMoveDown: {input.KeyGamepadDown, input.KeyDown, input.KeyS},
|
|
},
|
|
}, "."), nil); err != nil {
|
|
return nil, fmt.Errorf("failed to load default values into koanf: %w", err)
|
|
}
|
|
*/
|
|
|
|
flagset := flag.NewFlagSet("config", flag.ContinueOnError)
|
|
flagset.BoolP("debug", "d", false, "enable debug log")
|
|
|
|
flagset.Usage = func() {
|
|
fmt.Println(Usage)
|
|
os.Exit(0)
|
|
}
|
|
|
|
flagset.IntP("tps", "t", 0, "TPS")
|
|
|
|
if err := flagset.Parse(os.Args[1:]); err != nil {
|
|
return nil, fmt.Errorf("failed to parse program arguments: %w", err)
|
|
}
|
|
|
|
if err := kloader.Load(env.Provider("OC_", ".", func(s string) string {
|
|
return strings.ReplaceAll(strings.ToLower(
|
|
strings.TrimPrefix(s, "OC_")), "_", ".")
|
|
}), nil); err != nil {
|
|
return nil, fmt.Errorf("error loading environment: %w", err)
|
|
}
|
|
|
|
if err := kloader.Load(posflag.Provider(flagset, ".", kloader), nil); err != nil {
|
|
return nil, fmt.Errorf("error loading flags: %w", err)
|
|
}
|
|
|
|
conf := &Config{}
|
|
if err := kloader.Unmarshal("", &conf); err != nil {
|
|
return nil, fmt.Errorf("error unmarshalling: %w", err)
|
|
}
|
|
|
|
switch conf.Loglevel {
|
|
case "debug":
|
|
conf.Debug = true
|
|
}
|
|
|
|
// are there any args left on commandline? if so threat them as adlinks
|
|
if len(flagset.Args()) > 0 {
|
|
level, err := strconv.Atoi(flagset.Args()[0])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse start level: %w", err)
|
|
}
|
|
|
|
conf.Startlevel = level
|
|
}
|
|
|
|
conf.Keymap = input.Keymap{
|
|
MoveLeft: {input.KeyGamepadLeft, input.KeyLeft, input.KeyA},
|
|
MoveRight: {input.KeyGamepadRight, input.KeyRight, input.KeyD},
|
|
MoveUp: {input.KeyGamepadUp, input.KeyUp, input.KeyW},
|
|
MoveDown: {input.KeyGamepadDown, input.KeyDown, input.KeyS},
|
|
SwitchPlayer: {input.KeyTab},
|
|
Abort: {input.KeyEscape},
|
|
Activate: {input.KeyEnter, input.KeyMouseLeft},
|
|
Any: {},
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|