-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
52 lines (42 loc) · 1.01 KB
/
Copy pathconfig.go
File metadata and controls
52 lines (42 loc) · 1.01 KB
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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
type Config struct {
GamePath string `json:"game_path"`
LaunchTimeout int `json:"launch_timeout"`
AutoClose bool `json:"auto_close"`
LastMode string `json:"last_mode"`
}
func getConfigPath() string {
exe, _ := os.Executable()
return filepath.Join(filepath.Dir(exe), "config.json")
}
func loadConfig() (*Config, error) {
configPath := getConfigPath()
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return &Config{
LaunchTimeout: 30,
AutoClose: false,
LastMode: "normal",
}, nil
}
data, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}
func saveConfig(config *Config) error {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(getConfigPath(), data, 0644)
}