forked from OpenDiablo2/HellSpawner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
183 lines (145 loc) · 4.29 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package hsconfig
import (
"encoding/json"
"fmt"
"image/color"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsenum"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsstate"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsutil"
"github.com/kirsle/configdir"
)
// default background color value
const (
DefaultBGColor = 0x0a0a0aff
)
const (
newFileMode = 0o644
)
const (
maxRecentOpenedProjectsCount = 5
)
// Config represents HellSpawner's config
type Config struct {
Path string `json:"-"`
RecentProjects []string `json:"recentProjects"`
AbyssEnginePath string `json:"abyssEnginePath"`
AuxiliaryMpqPath string `json:"auxiliaryMpqPath"`
ExternalListFile string `json:"externalListFile"`
OpenMostRecentOnStartup bool `json:"openMostRecentOnStartup"`
ProjectStates map[string]hsstate.AppState `json:"projectStates"`
LoggingToFile bool `json:"loggingToFile"`
LogFilePath string `json:"logFile"`
Locale hsenum.Locale `json:"locale"`
BGColor color.RGBA `json:"bgColor"`
}
// GetConfigPath returns default config path
func GetConfigPath() string {
configPath := configdir.LocalConfig("hellspawner")
if err := configdir.MakePath(configPath); err != nil {
log.Fatal(err)
}
return filepath.Join(configPath, "environment.json")
}
func generateDefaultConfig(path string) *Config {
result := &Config{
Path: path,
RecentProjects: []string{},
OpenMostRecentOnStartup: true,
ProjectStates: make(map[string]hsstate.AppState),
LoggingToFile: false,
LogFilePath: filepath.Join(filepath.Dir(path), "output.log"),
Locale: hsenum.LocaleEnglish,
BGColor: hsutil.Color(DefaultBGColor),
}
if err := result.Save(); err != nil {
log.Fatalf("filed to save config: %s", err)
}
return result
}
// Load loads config
func Load(optionalPath string) *Config {
var configFile string
if optionalPath == "" {
configFile = GetConfigPath()
} else {
configFile = optionalPath
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return generateDefaultConfig(configFile)
}
var err error
var data []byte
if data, err = ioutil.ReadFile(filepath.Clean(configFile)); err != nil {
return generateDefaultConfig(configFile)
}
result := generateDefaultConfig(configFile)
if err = json.Unmarshal(data, &result); err != nil {
return generateDefaultConfig(configFile)
}
return result
}
// Save saves a new config
func (c *Config) Save() error {
var err error
var data []byte
if data, err = json.MarshalIndent(c, "", " "); err != nil {
return fmt.Errorf("cannot marshal config: %w", err)
}
if err := ioutil.WriteFile(c.Path, data, os.FileMode(newFileMode)); err != nil {
return fmt.Errorf("cannot write config at %s: %w", c.Path, err)
}
return nil
}
// AddToRecentProjects adds a path to recent opened projects
func (c *Config) AddToRecentProjects(filePath string) {
found := false
for idx := range c.RecentProjects {
if c.RecentProjects[idx] == filePath {
found = true
if idx != 0 {
old := c.RecentProjects[0]
c.RecentProjects[0] = filePath
c.RecentProjects[idx] = old
}
}
}
if !found {
recent := []string{filePath}
for idx := range c.RecentProjects {
if idx == maxRecentOpenedProjectsCount {
break
}
recent = append(recent, c.RecentProjects[idx])
}
c.RecentProjects = recent
}
if err := c.Save(); err != nil {
log.Fatalf("failed to save config: %s", err)
}
}
// GetAuxMPQs returns paths to auxiliary mpq's
func (c *Config) GetAuxMPQs() []string {
if c.AuxiliaryMpqPath == "" {
return []string{}
}
result := make([]string, 0)
err := filepath.Walk(c.AuxiliaryMpqPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext == ".mpq" {
result = append(result, path)
}
return nil
})
if err != nil {
log.Printf("failed to walk path for aux MPQs %s: %s", c.AuxiliaryMpqPath, err)
}
return result
}