2020-01-31 23:18:11 -05:00
|
|
|
package d2config
|
2019-11-08 01:37:21 -05:00
|
|
|
|
|
|
|
import (
|
2020-02-20 08:41:41 -05:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-12-17 21:32:37 -05:00
|
|
|
"os/user"
|
2019-11-08 01:37:21 -05:00
|
|
|
"path"
|
2019-12-14 00:15:52 -05:00
|
|
|
"runtime"
|
2019-11-08 01:37:21 -05:00
|
|
|
)
|
|
|
|
|
2020-02-20 08:41:41 -05:00
|
|
|
func getDefaultConfig() *Configuration {
|
2019-12-17 21:32:37 -05:00
|
|
|
config := &Configuration{
|
|
|
|
Language: "ENG",
|
2020-01-31 23:18:11 -05:00
|
|
|
FullScreen: false,
|
2019-12-17 21:32:37 -05:00
|
|
|
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",
|
2020-02-02 18:58:26 -05:00
|
|
|
"d2data.mpq",
|
2019-12-17 21:32:37 -05:00
|
|
|
"d2char.mpq",
|
|
|
|
"d2music.mpq",
|
|
|
|
"d2sfx.mpq",
|
|
|
|
"d2video.mpq",
|
|
|
|
"d2speech.mpq",
|
|
|
|
},
|
|
|
|
}
|
2019-12-14 00:15:52 -05:00
|
|
|
|
2019-12-17 21:32:37 -05:00
|
|
|
switch runtime.GOOS {
|
2020-01-18 22:55:59 -05:00
|
|
|
case "windows":
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "386":
|
|
|
|
config.MpqPath = "C:/Program Files/Diablo II"
|
|
|
|
}
|
2019-12-17 21:32:37 -05:00
|
|
|
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
|
|
|
|
}
|
2020-02-20 08:41:41 -05:00
|
|
|
|
|
|
|
func getDefaultConfigPath() string {
|
|
|
|
if configDir, err := os.UserConfigDir(); err == nil {
|
|
|
|
return path.Join(configDir, "OpenDiablo2", "config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return getLocalConfigPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLocalConfigPath() string {
|
|
|
|
return path.Join(path.Dir(os.Args[0]), "config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(configPath string) error {
|
|
|
|
configFile, err := os.Open(configPath)
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &singleton); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(configPath string) error {
|
|
|
|
configDir := path.Dir(configPath)
|
|
|
|
if err := os.MkdirAll(configDir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := os.Create(configPath)
|
|
|
|
defer configFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.MarshalIndent(singleton, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := configFile.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|