mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-05 16:17:45 -05:00
Configuration cleanup (#257)
* Cleanup config file loading, move defaults to code * Default to fullscreen * Cleanup * Store configuration in UserConfigDir * print config path on load and save
This commit is contained in:
parent
cead000c3f
commit
d5a0038125
37
config.json
37
config.json
@ -1,37 +0,0 @@
|
||||
{
|
||||
"Language": "ENG",
|
||||
"FullScreen": true,
|
||||
"Scale": 1,
|
||||
"TicksPerSecond": -1,
|
||||
"RunInBackground": true,
|
||||
"VsyncEnabled": true,
|
||||
"SfxVolume": 1.0,
|
||||
"BgmVolume": 0.3,
|
||||
"MpqPath": "C:/Program Files (x86)/Diablo II",
|
||||
"MpqLoadOrder": [
|
||||
"Patch_D2.mpq",
|
||||
"d2exp.mpq",
|
||||
"d2xmusic.mpq",
|
||||
"d2xtalk.mpq",
|
||||
"d2xvideo.mpq",
|
||||
"d2data.mpq",
|
||||
"d2char.mpq",
|
||||
"d2music.mpq",
|
||||
"d2sfx.mpq",
|
||||
"d2video.mpq",
|
||||
"d2speech.mpq"
|
||||
],
|
||||
"MpqMacLoadOrder": [
|
||||
"Diablo II Patch",
|
||||
"Diablo II Expansion Data",
|
||||
"Diablo II Expansion Movies",
|
||||
"Diablo II Expansion Music",
|
||||
"Diablo II Expansion Speech",
|
||||
"Diablo II Game Data",
|
||||
"Diablo II Graphics",
|
||||
"Diablo II Movies",
|
||||
"Diablo II Music",
|
||||
"Diablo II Sounds",
|
||||
"Diablo II Speech"
|
||||
]
|
||||
}
|
@ -56,7 +56,12 @@ type Engine struct {
|
||||
// CreateEngine creates and instance of the OpenDiablo2 engine
|
||||
func CreateEngine() Engine {
|
||||
var result Engine
|
||||
result.loadConfigurationFile()
|
||||
|
||||
result.Settings = d2corecommon.LoadConfiguration()
|
||||
if err := result.Settings.Save(); err != nil {
|
||||
log.Printf("could not load settings: %v", err)
|
||||
}
|
||||
|
||||
result.assetManager = createAssetManager(result.Settings)
|
||||
d2resource.LanguageCode = result.Settings.Language
|
||||
d2datadict.LoadPalettes(nil, &result)
|
||||
@ -84,11 +89,6 @@ func CreateEngine() Engine {
|
||||
return result
|
||||
}
|
||||
|
||||
func (v *Engine) loadConfigurationFile() {
|
||||
log.Println("Loading configuration file")
|
||||
v.Settings = d2corecommon.LoadConfiguration()
|
||||
}
|
||||
|
||||
func (v *Engine) LoadFile(fileName string) []byte {
|
||||
return v.assetManager.LoadFile(fileName)
|
||||
}
|
||||
@ -194,7 +194,7 @@ func (v Engine) Draw(screen *ebiten.Image) {
|
||||
ebitenutil.DebugPrintAt(screen, "HeapSys "+strconv.FormatInt(int64(m.HeapSys/1024/1024), 10), 680, 20)
|
||||
ebitenutil.DebugPrintAt(screen, "NumGC "+strconv.FormatInt(int64(m.NumGC), 10), 680, 30)
|
||||
cx, cy := ebiten.CursorPosition()
|
||||
ebitenutil.DebugPrintAt(screen, "Coords "+strconv.FormatInt(int64(cx), 10) + ","+strconv.FormatInt(int64(cy), 10), 680, 40)
|
||||
ebitenutil.DebugPrintAt(screen, "Coords "+strconv.FormatInt(int64(cx), 10)+","+strconv.FormatInt(int64(cy), 10), 680, 40)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,14 +2,11 @@ package d2corecommon
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
// Configuration defines the configuration for the engine, loaded from config.json
|
||||
@ -23,44 +20,116 @@ type Configuration struct {
|
||||
VsyncEnabled bool
|
||||
MpqPath string
|
||||
MpqLoadOrder []string
|
||||
MpqMacLoadOrder []string
|
||||
SfxVolume float64
|
||||
BgmVolume float64
|
||||
}
|
||||
|
||||
// ConfigBasePath is used for tests to find the base config json file
|
||||
var ConfigBasePath = "./"
|
||||
|
||||
func LoadConfiguration() *Configuration {
|
||||
configJSON, err := ioutil.ReadFile(path.Join(ConfigBasePath, "config.json"))
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return getDefaultConfiguration()
|
||||
}
|
||||
var config Configuration
|
||||
err = json.Unmarshal(configJSON, &config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if runtime.GOOS == "darwin" {
|
||||
if config.MpqPath[0] != '/' {
|
||||
config.MpqPath = "/Applications/Diablo II/"
|
||||
}
|
||||
config.MpqLoadOrder = config.MpqMacLoadOrder
|
||||
} else {
|
||||
// Path fixup for wine-installed diablo 2 in linux
|
||||
if config.MpqPath[0] != '/' {
|
||||
if _, err := os.Stat(config.MpqPath); os.IsNotExist(err) {
|
||||
homeDir, _ := homedir.Dir()
|
||||
newPath := strings.ReplaceAll(config.MpqPath, `C:\`, homeDir+"/.wine/drive_c/")
|
||||
newPath = strings.ReplaceAll(newPath, "C:/", homeDir+"/.wine/drive_c/")
|
||||
newPath = strings.ReplaceAll(newPath, `\`, "/")
|
||||
if _, err := os.Stat(newPath); !os.IsNotExist(err) {
|
||||
config.MpqPath = newPath
|
||||
}
|
||||
}
|
||||
|
||||
configDir = path.Join(configDir, "OpenDiablo2")
|
||||
configPath := path.Join(configDir, "config.json")
|
||||
log.Printf("loading configuration file from %s...", configPath)
|
||||
configFile, err := os.Open(configPath)
|
||||
defer configFile.Close()
|
||||
|
||||
if err == nil {
|
||||
var config Configuration
|
||||
decoder := json.NewDecoder(configFile)
|
||||
if err := decoder.Decode(&config); err == nil {
|
||||
return &config
|
||||
}
|
||||
}
|
||||
return &config
|
||||
|
||||
return getDefaultConfiguration()
|
||||
}
|
||||
|
||||
func (c *Configuration) Save() error {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configDir = path.Join(configDir, "OpenDiablo2")
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configPath := path.Join(configDir, "config.json")
|
||||
log.Printf("saving configuration file to %s...", configPath)
|
||||
configFile, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(configFile)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getConfigurationPath() string {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return "config.json"
|
||||
}
|
||||
|
||||
return path.Join(configDir, "OpenDiablo2/config.json")
|
||||
}
|
||||
|
||||
func getDefaultConfiguration() *Configuration {
|
||||
config := &Configuration{
|
||||
Language: "ENG",
|
||||
FullScreen: true,
|
||||
Scale: 1,
|
||||
TicksPerSecond: -1,
|
||||
RunInBackground: true,
|
||||
VsyncEnabled: true,
|
||||
SfxVolume: 1.0,
|
||||
BgmVolume: 0.3,
|
||||
MpqPath: "C:/Program Files (x86)/Diablo II",
|
||||
MpqLoadOrder: []string{
|
||||
"Patch_D2.mpq",
|
||||
"d2exp.mpq",
|
||||
"d2xmusic.mpq",
|
||||
"d2xtalk.mpq",
|
||||
"d2xvideo.mpq",
|
||||
"d2data.mpq",
|
||||
"d2char.mpq",
|
||||
"d2music.mpq",
|
||||
"d2sfx.mpq",
|
||||
"d2video.mpq",
|
||||
"d2speech.mpq",
|
||||
},
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
config.MpqPath = "/Applications/Diablo II/"
|
||||
config.MpqLoadOrder = []string{
|
||||
"Diablo II Patch",
|
||||
"Diablo II Expansion Data",
|
||||
"Diablo II Expansion Movies",
|
||||
"Diablo II Expansion Music",
|
||||
"Diablo II Expansion Speech",
|
||||
"Diablo II Game Data",
|
||||
"Diablo II Graphics",
|
||||
"Diablo II Movies",
|
||||
"Diablo II Music",
|
||||
"Diablo II Sounds",
|
||||
"Diablo II Speech",
|
||||
}
|
||||
case "linux":
|
||||
if usr, err := user.Current(); err == nil {
|
||||
config.MpqPath = path.Join(usr.HomeDir, ".wine/drive_c/Program Files (x86)/Diablo II")
|
||||
}
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user