-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
121 lines (106 loc) · 2.82 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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
)
type Config struct {
Path string `json:"path"`
EmbeddedBitmap bool `json:"embeddedbitmap"`
PreferOutline bool `json:"preferoutline"`
DoSubstitutions bool `json:"dosubstitutions"`
BitmapMonospace bool `json:"bitmapmonospace"`
ForceAutohint bool `json:"forceautohint"`
QtUseSubpixelPositioning bool `json:"qtusesubpixelpositioning"`
Dpi int `json:"dpi"`
Font string `json:"font"`
Pixelsize float64 `json:"pixelsize"`
Weight int `json:"weight"`
Customize bool `json:"customize"`
Fonts []FontStruct `json:"fonts"`
}
type FontStruct struct {
ValveFont string `json:"valvefont"`
ReplaceFont string `json:"replacefont"`
Pixelsize float64 `json:"pixelsize"`
Weight int `json:"weight"`
EmbeddedBitmap bool `json:"embeddedbitmap"`
PreferOutline bool `json:"prefer_outline"`
DoSubstitutions bool `json:"do_substitutions"`
BitmapMonospace bool `json:"bitmap_monospace"`
ForceAutohint bool `json:"force_autohint"`
Dpi int `json:"dpi"`
QtUseSubpixelPositioning bool `json:"qt_use_subpixel_positioning"`
}
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func (c *Config) Init() {
found, e := Exists("./config.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
if !found {
c.newConfig()
} else {
c.loadConfigFile()
}
}
func (c *Config) loadConfigFile() {
file, e := os.ReadFile("./config.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
err := json.Unmarshal(file, &c)
if err != nil {
log.Println(err)
c.newConfig()
}
if c.Customize && len(c.Fonts) == 0 {
c.Customize = false
}
}
func (c *Config) SaveConfigFile(apppath string) error { // https://github.com/spddl/csgo-reporter/blob/master/Config/Config.go#L147
if !c.Customize {
c.Fonts = []FontStruct{}
}
bytes, err := json.MarshalIndent(c, "", "\t")
if err != nil {
return errors.New("config.json could not be saved (JSON error)")
} else {
err = os.WriteFile(filepath.Join(apppath, "config.json"), bytes, 0644)
if err == nil {
return nil
} else {
return err
}
}
}
func (c *Config) newConfig() {
jsonBlob := json.RawMessage(`{
"path": "",
"font": "",
"embeddedbitmap": false,
"preferoutline": true,
"dosubstitutions": true,
"bitmapmonospace": false,
"forceautohint": false,
"qtusesubpixelpositioning": false,
"dpi": 96
}`)
if err := json.Unmarshal(jsonBlob, c); err != nil {
panic(err)
}
}