-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
44 lines (36 loc) · 925 Bytes
/
Copy pathconfig.go
File metadata and controls
44 lines (36 loc) · 925 Bytes
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
package main
import (
"encoding/json"
"io"
"os"
)
const configFileName = "config.json"
type Config struct {
ModConfigSlice []ModConfig
}
func NewConfig() *Config {
modConfigSlice := make([]ModConfig, 1)
modConfigSlice[0] = ModConfig{
Name: "Default",
}
return &Config{ModConfigSlice: modConfigSlice}
}
func (c *Config) Save() {
new_data, _ := json.Marshal(c)
new_string := string(new_data)
configFile, _ := os.OpenFile(configFileName, os.O_RDONLY|os.O_CREATE, 0666)
defer configFile.Close()
old_data, _ := io.ReadAll(configFile)
old_string := string(old_data)
if new_string != old_string {
configFile, _ := os.OpenFile(configFileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
defer configFile.Close()
configFile.Write(new_data)
}
}
func (c *Config) Load() {
configFile, _ := os.Open(configFileName)
defer configFile.Close()
decoder := json.NewDecoder(configFile)
_ = decoder.Decode(&c)
}