forked from Edru2/DeMuddler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerFunctions.go
66 lines (59 loc) · 1.55 KB
/
TimerFunctions.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
package main
import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func handleTimers(timers *[]Timer, parentDir string) {
if len(*timers) == 0 {
return
}
var jsonFile []Timer
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}
for _, timer := range *timers {
timerFileName := strings.ReplaceAll(timer.Name, " ", "_")
timerFilePath := filepath.Join(parentDir, timerFileName+".lua")
if len(timer.Script) > 0 && !containsIllegalCharacters(timerFileName) {
if err := os.WriteFile(timerFilePath, []byte(timer.Script), 0644); err != nil {
panic(err)
}
timer.Script = ""
}
ConvertTimeToSingleProperties(&timer)
jsonFile = append(jsonFile, timer)
}
jsonFilePath := filepath.Join(parentDir, "timers.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
}
}
func handleTimerGroups(groups *[]TimerGroup, baseDir string) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
handleTimers(&((*groups)[i].Timers), groupPath)
handleTimerGroups(&((*groups)[i].TimerGroup), groupPath)
}
}
func ConvertTimeToSingleProperties(timer *Timer) Timer {
t, err := time.Parse("15:04:05.000", timer.Time)
if err != nil {
panic(err)
}
timer.Hours = strconv.Itoa(t.Hour())
timer.Minutes = strconv.Itoa(t.Minute())
timer.Seconds = strconv.Itoa(t.Second())
timer.Milliseconds = strconv.Itoa(t.Nanosecond() / 1000000)
return *timer
}