-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.go
More file actions
230 lines (211 loc) · 5.6 KB
/
annotations.go
File metadata and controls
230 lines (211 loc) · 5.6 KB
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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type FunctionAnnotation struct {
Name string `json:"name,omitempty"`
Comment string `json:"comment,omitempty"`
}
type Annotations struct {
Version int `json:"version"`
Functions map[string]*FunctionAnnotation `json:"functions,omitempty"`
Comments map[string]string `json:"comments,omitempty"`
DecompileComments map[string]string `json:"decompileComments,omitempty"`
Bookmarks []uint32 `json:"bookmarks,omitempty"`
}
func NewAnnotations() *Annotations {
return &Annotations{
Version: 1,
Functions: make(map[string]*FunctionAnnotation),
Comments: make(map[string]string),
DecompileComments: make(map[string]string),
Bookmarks: []uint32{},
}
}
func annotationsPath(wasmPath string) string {
return wasmPath + ".wasmspy"
}
func loadAnnotationsFromFile(wasmPath string) *Annotations {
data, err := os.ReadFile(annotationsPath(wasmPath))
if err != nil {
return NewAnnotations()
}
var ann Annotations
if err := json.Unmarshal(data, &ann); err != nil {
return NewAnnotations()
}
if ann.Functions == nil {
ann.Functions = make(map[string]*FunctionAnnotation)
}
if ann.Comments == nil {
ann.Comments = make(map[string]string)
}
if ann.DecompileComments == nil {
ann.DecompileComments = make(map[string]string)
}
return &ann
}
func saveAnnotationsToFile(wasmPath string, ann *Annotations) error {
data, err := json.MarshalIndent(ann, "", " ")
if err != nil {
return err
}
return os.WriteFile(annotationsPath(wasmPath), data, 0644)
}
func (a *App) GetAnnotations(path string) *Annotations {
if ann := a.annotations[path]; ann != nil {
return ann
}
return NewAnnotations()
}
func (a *App) SetFunctionName(path string, index uint32, name string) error {
ann := a.annotations[path]
if ann == nil {
ann = NewAnnotations()
a.annotations[path] = ann
}
key := fmt.Sprintf("%d", index)
if ann.Functions[key] == nil {
ann.Functions[key] = &FunctionAnnotation{}
}
ann.Functions[key].Name = name
return nil
}
func (a *App) SetFunctionComment(path string, index uint32, comment string) error {
ann := a.annotations[path]
if ann == nil {
ann = NewAnnotations()
a.annotations[path] = ann
}
key := fmt.Sprintf("%d", index)
if ann.Functions[key] == nil {
ann.Functions[key] = &FunctionAnnotation{}
}
ann.Functions[key].Comment = comment
return nil
}
func (a *App) SetOffsetComment(path string, offset uint64, comment string, isDecompile bool) error {
ann := a.annotations[path]
if ann == nil {
ann = NewAnnotations()
a.annotations[path] = ann
}
key := fmt.Sprintf("0x%x", offset)
target := ann.Comments
if isDecompile {
target = ann.DecompileComments
}
if comment == "" {
delete(target, key)
} else {
target[key] = comment
}
return nil
}
func (a *App) SetBookmarks(path string, bookmarks []uint32) error {
ann := a.annotations[path]
if ann == nil {
ann = NewAnnotations()
a.annotations[path] = ann
}
ann.Bookmarks = bookmarks
return nil
}
func (a *App) SaveAnnotations(path string) error {
ann := a.annotations[path]
if ann == nil {
return nil
}
return saveAnnotationsToFile(path, ann)
}
func (a *App) ClearAnnotations(path string) *Annotations {
a.annotations[path] = NewAnnotations()
return a.annotations[path]
}
func (a *App) ExportAnnotations(path string) (string, error) {
ann := a.annotations[path]
if ann == nil {
ann = NewAnnotations()
}
data, err := json.MarshalIndent(ann, "", " ")
if err != nil {
return "", err
}
return string(data), nil
}
func (a *App) ImportAnnotations(path string, data string) error {
var ann Annotations
if err := json.Unmarshal([]byte(data), &ann); err != nil {
return err
}
if ann.Functions == nil {
ann.Functions = make(map[string]*FunctionAnnotation)
}
if ann.Comments == nil {
ann.Comments = make(map[string]string)
}
if ann.DecompileComments == nil {
ann.DecompileComments = make(map[string]string)
}
a.annotations[path] = &ann
return nil
}
func (a *App) ExportAnnotationsToFile(wasmPath string) (string, error) {
savePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Export Annotations",
DefaultFilename: "annotations.wasmspy",
Filters: []runtime.FileFilter{
{DisplayName: "WasmSpy Annotations", Pattern: "*.wasmspy"},
{DisplayName: "JSON Files", Pattern: "*.json"},
},
})
if err != nil || savePath == "" {
return "", err
}
ann := a.annotations[wasmPath]
if ann == nil {
ann = NewAnnotations()
}
data, err := json.MarshalIndent(ann, "", " ")
if err != nil {
return "", err
}
if err := os.WriteFile(savePath, data, 0644); err != nil {
return "", err
}
return savePath, nil
}
func (a *App) ImportAnnotationsFromFile(wasmPath string) (string, error) {
openPath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Import Annotations",
Filters: []runtime.FileFilter{
{DisplayName: "WasmSpy Annotations", Pattern: "*.wasmspy"},
{DisplayName: "JSON Files", Pattern: "*.json"},
},
})
if err != nil || openPath == "" {
return "", err
}
data, err := os.ReadFile(openPath)
if err != nil {
return "", err
}
var ann Annotations
if err := json.Unmarshal(data, &ann); err != nil {
return "", err
}
if ann.Functions == nil {
ann.Functions = make(map[string]*FunctionAnnotation)
}
if ann.Comments == nil {
ann.Comments = make(map[string]string)
}
if ann.DecompileComments == nil {
ann.DecompileComments = make(map[string]string)
}
a.annotations[wasmPath] = &ann
return openPath, nil
}