2020-02-01 04:18:11 +00:00
|
|
|
package d2config
|
|
|
|
|
|
|
|
import (
|
2020-07-08 13:16:16 +00:00
|
|
|
"encoding/json"
|
2020-02-01 04:18:11 +00:00
|
|
|
"log"
|
2020-07-08 13:16:16 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-03 02:23:07 +00:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-02-01 04:18:11 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
// Config holds the configuration from config.json
|
|
|
|
var Config *Configuration //nolint:gochecknoglobals // Currently global by design
|
|
|
|
|
2020-02-01 23:55:56 +00:00
|
|
|
// Configuration defines the configuration for the engine, loaded from config.json
|
|
|
|
type Configuration struct {
|
2020-07-08 13:16:16 +00:00
|
|
|
MpqLoadOrder []string
|
|
|
|
Language string
|
|
|
|
MpqPath string
|
|
|
|
TicksPerSecond int
|
|
|
|
FpsCap int
|
|
|
|
SfxVolume float64
|
|
|
|
BgmVolume float64
|
|
|
|
FullScreen bool
|
|
|
|
RunInBackground bool
|
|
|
|
VsyncEnabled bool
|
|
|
|
Backend string
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-03 02:23:07 +00:00
|
|
|
LogLevel d2util.LogLevel
|
|
|
|
path string
|
2020-07-04 04:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
// Load loads a configuration object from disk
|
|
|
|
func Load() error {
|
|
|
|
Config = new(Configuration)
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-03 02:23:07 +00:00
|
|
|
|
|
|
|
if Config.Load() != nil {
|
|
|
|
return Config.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-07-04 04:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a configuration object from disk
|
|
|
|
func (c *Configuration) Load() error {
|
2020-02-20 13:41:41 +00:00
|
|
|
configPaths := []string{
|
2020-07-08 13:16:16 +00:00
|
|
|
defaultConfigPath(),
|
|
|
|
localConfigPath(),
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 13:41:41 +00:00
|
|
|
for _, configPath := range configPaths {
|
|
|
|
log.Printf("loading configuration file from %s...", configPath)
|
2020-07-03 18:00:56 +00:00
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
continue
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
2020-02-20 13:41:41 +00:00
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
configFile, err := os.Open(path.Clean(configPath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-03 18:00:56 +00:00
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
if err := json.NewDecoder(configFile).Decode(&Config); err != nil {
|
2020-02-20 13:41:41 +00:00
|
|
|
return err
|
2020-02-08 03:21:15 +00:00
|
|
|
}
|
2020-07-08 13:16:16 +00:00
|
|
|
|
|
|
|
if err := configFile.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-03 02:23:07 +00:00
|
|
|
c.path = configPath
|
2020-10-29 01:42:03 +00:00
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
return nil
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
log.Println("failed to load configuration file, saving default configuration...")
|
|
|
|
|
|
|
|
Config = defaultConfig()
|
|
|
|
|
|
|
|
return Config.Save()
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 04:49:16 +00:00
|
|
|
// Save saves the configuration object to disk
|
|
|
|
func (c *Configuration) Save() error {
|
2020-07-08 13:16:16 +00:00
|
|
|
configPath := defaultConfigPath()
|
2020-02-01 04:18:11 +00:00
|
|
|
log.Printf("saving configuration file to %s...", configPath)
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
configDir := path.Dir(configPath)
|
|
|
|
if err := os.MkdirAll(configDir, 0750); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := os.Create(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.MarshalIndent(Config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
if _, err := configFile.Write(buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return configFile.Close()
|
|
|
|
}
|
|
|
|
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-03 02:23:07 +00:00
|
|
|
// Path returns the path of the config file
|
|
|
|
func (c *Configuration) Path() string {
|
|
|
|
if c.path == "" {
|
|
|
|
c.path = defaultConfigPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.path
|
|
|
|
}
|
|
|
|
|
2020-07-08 13:16:16 +00:00
|
|
|
func defaultConfigPath() string {
|
|
|
|
if configDir, err := os.UserConfigDir(); err == nil {
|
|
|
|
return path.Join(configDir, "OpenDiablo2", "config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return localConfigPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func localConfigPath() string {
|
|
|
|
return path.Join(path.Dir(os.Args[0]), "config.json")
|
2020-02-01 04:18:11 +00:00
|
|
|
}
|