-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
196 lines (169 loc) · 4.16 KB
/
main.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
package main
import (
"fmt"
log "github.com/codecat/go-libs/log"
"github.com/eikarna/GoDat/Components/Decoder"
"github.com/eikarna/GoDat/Components/Encoder"
. "github.com/eikarna/GoDat/Components/Enums"
"github.com/eikarna/GoDat/Components/ProtonHash"
"github.com/eikarna/GoDat/Components/UI"
"github.com/goccy/go-json"
"io/ioutil"
"os"
"strconv"
"strings"
)
var (
cachedItemInfo *ItemInfo
)
func main() {
for {
UI.ClearScreen()
UI.PrintBanner(true, "GoDat", "Version: 1.1.0", "By: Eikarna")
fmt.Println("Please select Method (ex. 1, or \"Decode\"):")
fmt.Println("1. Decode")
fmt.Println("2. Encode")
fmt.Println("3. Search Items (Name/ID)")
fmt.Println("4. ProtonHash")
fmt.Println("Type 'exit' to quit the program.")
method, err := UI.Read(false, "> ")
if err != nil {
log.Error(err.Error())
continue
}
method = strings.TrimSpace(strings.ToLower(method))
if method == "exit" {
fmt.Println("Exiting program.")
break
}
switch method {
case "1", "decode":
handleDecode()
case "2", "encode":
handleEncode()
case "3", "search items":
handleSearchItems()
case "4", "protonhash":
handleProtonHash()
default:
fmt.Println("Invalid option. Please try again.")
}
}
}
func handleDecode() {
for {
targetDecodeFile, err := UI.Read(false, "File Name to be Decoded (or 'back' to return): ")
if err != nil {
log.Error(err.Error())
return
}
if strings.TrimSpace(strings.ToLower(targetDecodeFile)) == "back" {
return
}
decoded, err := Decoder.Decode(targetDecodeFile)
if err != nil {
log.Error("Error when trying to decode \"%s\": %v", targetDecodeFile, err)
continue
}
data, _ := json.MarshalIndent(decoded, "", " ")
_ = ioutil.WriteFile("items.json", data, 0644)
fmt.Println("File decoded successfully. Output saved to items.json.")
return
}
}
func handleEncode() {
for {
targetEncodeFile, err := UI.Read(false, "File Name to be Encoded (Only Valid JSON) (or 'back' to return): ")
if err != nil {
log.Error(err.Error())
return
}
if strings.TrimSpace(strings.ToLower(targetEncodeFile)) == "back" {
return
}
outputNameFile, err := UI.Read(false, "Output File Name: ")
if err != nil {
log.Error(err.Error())
return
}
b, err := os.ReadFile(targetEncodeFile)
if err != nil {
log.Error(err.Error())
continue
}
data := &ItemInfo{}
err = json.Unmarshal(b, data)
if err != nil {
log.Error(err.Error())
continue
}
err = Encoder.Encode(data, outputNameFile)
if err != nil {
log.Error("Error when trying to encode \"%s\": %v", targetEncodeFile, err)
continue
}
fmt.Println("File encoded successfully. Output saved to", outputNameFile)
return
}
}
func handleSearchItems() {
for {
isId := false
var itemId int32
targetItem, err := UI.Read(false, "Items Name/ID (or 'back' to return): ")
if err != nil {
log.Error(err.Error())
return
}
if strings.TrimSpace(strings.ToLower(targetItem)) == "back" {
return
}
if id, err := strconv.Atoi(targetItem); err == nil {
fmt.Printf("%q looks like a number.\n", targetItem)
isId = true
itemId = int32(id)
}
if cachedItemInfo == nil {
data, err := os.ReadFile("items.json")
if err != nil {
log.Error(err.Error())
return
}
err = json.Unmarshal(data, &cachedItemInfo)
if err != nil {
log.Error(err.Error())
return
}
}
found := false
for _, key := range cachedItemInfo.Items {
if isId && key.ItemID == int32(itemId) {
fmt.Println("Got Items with ID:", strconv.Itoa(int(key.ItemID)))
fmt.Printf("%v\n", key)
found = true
break
} else if !isId && strings.Contains(strings.ToLower(key.Name), strings.ToLower(targetItem)) {
fmt.Println("Got Items with Name:", key.Name)
fmt.Printf("%v\n", key)
found = true
break
}
}
if !found {
fmt.Println("No items found. Please try again.")
}
}
}
func handleProtonHash() {
for {
targetFile, err := UI.Read(false, "Target File (or 'back' to return): ")
if err != nil {
log.Error(err.Error())
return
}
if strings.TrimSpace(strings.ToLower(targetFile)) == "back" {
return
}
fmt.Printf("ProtonHash: %d\n", ProtonHash.GetFileHash(targetFile))
}
}