This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
/
runner.go
239 lines (237 loc) · 6.17 KB
/
runner.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package entrance
import (
"bufio"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gookit/slog"
"github.com/sydneyowl/shx8800-config-editor/internal/radio"
"github.com/sydneyowl/shx8800-config-editor/pkg/filetools"
"os"
"path/filepath"
"strings"
)
func mainRunner() {
var specifyConfigPath string
var configs *radio.ClassTheRadioData
pathExe := filetools.GetCSPath()
if DepPath == "" {
if filetools.Build == "dev" {
slog.Fatalf("未指定依赖包位置!请使用--dep-path指定!")
_, _ = fmt.Scanln()
return
}
if !filetools.IsFileExist(pathExe) {
err := filetools.ReleaseFile()
if err != nil {
slog.Fatalf("无法释放依赖!")
_, _ = fmt.Scanln()
slog.Debug(err)
return
}
slog.Info("释放成功!请重启程序,按下回车确认")
_, _ = fmt.Scanln()
return
}
} else {
pathExe = DepPath
}
if ConfigPath == "" {
fmt.Print("请输入.dat文件路径:")
reader := bufio.NewReader(os.Stdin)
specifyConfigPath, _ = reader.ReadString('\n')
specifyConfigPath = strings.TrimSpace(specifyConfigPath)
} else {
specifyConfigPath = ConfigPath
}
specifyConfigPath = strings.Trim(specifyConfigPath, "\"")
slog.Info("检查文件完整性...")
if !filetools.IsFileExist(specifyConfigPath) {
slog.Fatal("文件路径有误!")
_, _ = fmt.Scanln()
return
}
if !filetools.IsFileExist(pathExe) {
slog.Fatal("C#文件路径有误!")
_, _ = fmt.Scanln()
return
}
configs, err := filetools.ParseData(pathExe, specifyConfigPath)
if err != nil {
slog.Errorf("解析失败:%v", err)
return
} else {
slog.Info("解析成功")
}
for {
swh:
lastStep := make([]radio.ClassTheRadioData, 0)
printMenu()
choose := "q"
_, _ = fmt.Scanln(&choose)
switch choose {
case "m":
for {
wait:
printSubMenu()
var choice string
_, _ = fmt.Scanln(&choice)
for {
switch choice {
case "r":
if len(lastStep) == 0 {
slog.Warn("无法撤销!")
goto wait
}
backto := lastStep[len(lastStep)-1]
*configs = backto
lastStep = lastStep[:len(lastStep)-1]
slog.Notice("已经撤销")
goto wait
case "p":
displayChannels((*configs).ChannelData)
goto wait
case "b":
goto swh
case "c":
backupConfig(configs, &lastStep)
fmt.Printf("请输入要清空的信道号")
channel := "e"
_, _ = fmt.Scanln(&channel)
err = ClearChannel(configs, channel)
if err != nil {
slog.Warn("无法清空:", err)
} else {
slog.Info("清空成功!")
}
goto wait
case "z":
backupConfig(configs, &lastStep)
fmt.Printf("请输入要清空的信道范围,例如1,2-4,9代表清空1,2,3,4,9: ")
channel := "e"
_, _ = fmt.Scanln(&channel)
channelRangesd, err := parseChannel(channel)
if err != nil {
slog.Warn("无法解析:", err)
goto wait
}
err = BatchClear(configs, channelRangesd)
if err != nil {
slog.Warn("无法清空:", err)
} else {
slog.Info("清空成功!")
}
goto wait
case "k":
backupConfig(configs, &lastStep)
RemoveAllEmpty(configs)
slog.Info("删除成功!")
goto wait
case "e":
backupConfig(configs, &lastStep)
fmt.Print("请输入要修改的信道号:")
chanNo := "e"
_, _ = fmt.Scanln(&chanNo)
displayModify()
modNo := 0
fmt.Print("请输入要修改的选项:")
_, _ = fmt.Scanln(&modNo)
fmt.Print("要改成什么?")
rewriteTo := "e"
_, _ = fmt.Scanln(&rewriteTo)
err := ModifyChannel(configs, chanNo, modNo, rewriteTo)
if err != nil {
slog.Error(err)
} else {
slog.Info("成功!")
}
goto wait
case "i":
backupConfig(configs, &lastStep)
fmt.Printf("请输入要在哪一个信道前插入信道(插入信道号)")
channel := "e"
_, _ = fmt.Scanln(&channel)
targetArr, err := AskForDetail()
if err != nil {
slog.Info("用户取消!")
goto wait
}
targetArr[0] = channel
err = AddChannel(configs, channel, *targetArr)
if err != nil {
slog.Warn("错误:", err)
} else {
slog.Info("插入成功!")
}
goto wait
case "m":
channel := "e"
fmt.Print("请输入要复制的信道:")
_, _ = fmt.Scanln(&channel)
err = copyChannel(*configs, channel)
if err != nil {
slog.Warn("错误:", err)
} else {
slog.Info("复制成功!")
}
goto wait
case "d":
ShowCopiedChannel()
goto wait
case "v":
backupConfig(configs, &lastStep)
fmt.Print("复制缓冲区中的哪个信道?")
from := "e"
_, _ = fmt.Scanln(&from)
fmt.Print("要复制到哪个信道?")
dst := "e"
_, _ = fmt.Scanln(&dst)
err = pasteChannelWithCheck(configs, from, dst)
if err != nil {
slog.Warn("错误:", err)
} else {
slog.Info("粘贴成功!")
}
goto wait
case "a":
fmt.Println(configs.ChannelData)
goto wait
default:
fmt.Println("输入错误,请重新输入")
_, _ = fmt.Scanln(&choice)
}
}
}
case "q":
os.Exit(0)
case "s":
jsonstring, err := json.Marshal(configs)
out := jsonstring
//out, _ := simplifiedchinese.GB18030.NewEncoder().Bytes(jsonstring)
if err != nil {
slog.Errorf("无法反序列:%v", err)
return
}
fmt.Print("请输入要保存到的目标目录,输入英文句号(.)保存到当前目录:")
saveto := "./"
reader := bufio.NewReader(os.Stdin)
saveto, _ = reader.ReadString('\n')
saveto = strings.TrimSpace(saveto)
saveto = strings.Trim(saveto, "\"")
if !filetools.IsFileExist(saveto) {
slog.Warn("文件路径有误!")
continue
}
id := uuid.New().String()[0:12] + ".dat"
saveto = filepath.Join(saveto, id)
err = filetools.WriteBackData(pathExe, saveto, string(out))
if err != nil {
slog.Errorf("无法保存:%v", err)
continue
}
slog.Infof("保存成功!文件名为:%s", saveto)
default:
fmt.Println("输入错误!")
}
}
}