-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
319 lines (274 loc) · 8.97 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"fmt"
"slices"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
generateFile(gen, f)
}
return nil
})
}
func getEnumTypeFromKind(kind protoreflect.Kind) (string, bool) {
switch kind {
case protoreflect.BoolKind:
return "BOOL", true
case protoreflect.Int32Kind:
return "INT32", true
case protoreflect.Sint32Kind:
return "SINT32", true
case protoreflect.Sfixed32Kind:
return "SFIXED32", true
case protoreflect.Int64Kind:
return "INT64", true
case protoreflect.Sint64Kind:
return "SINT64", true
case protoreflect.Sfixed64Kind:
return "SFIXED64", true
case protoreflect.Uint32Kind:
return "UINT32", true
case protoreflect.Fixed32Kind:
return "FIXED32", true
case protoreflect.Uint64Kind:
return "UINT64", true
case protoreflect.Fixed64Kind:
return "FIXED64", true
case protoreflect.FloatKind:
return "FLOAT", true
case protoreflect.DoubleKind:
return "DOUBLE", true
case protoreflect.StringKind:
return "STRING", true
case protoreflect.BytesKind:
return "BYTES", true
case protoreflect.MessageKind:
return "MESSAGE", true
case protoreflect.EnumKind:
return "ENUM", true
}
return "UNKNOWN KIND " + kind.String(), false
}
func getEnumTypeFromField(field *protogen.Field) (string, bool) {
if field.Desc.IsMap() {
return "MAP", true
}
typeName, ok := getEnumTypeFromKind(field.Desc.Kind())
if ok {
return typeName, true
}
return "Unknown " + field.Desc.Kind().String(), false
}
func getGodotFieldType(field *protogen.Field) string {
if field.Desc.IsList() && !field.Desc.IsPacked() {
return "Array"
}
switch field.Desc.Kind() {
case protoreflect.BoolKind:
return "bool"
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind,
protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind,
protoreflect.Uint32Kind, protoreflect.Fixed32Kind,
protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return "int"
case protoreflect.FloatKind, protoreflect.DoubleKind:
return "float"
case protoreflect.StringKind:
return "String"
case protoreflect.BytesKind:
return "PackedByteArray"
case protoreflect.MessageKind:
// If it's a real message
if !field.Desc.IsMap() {
return string(field.Desc.Message().FullName().Name())
}
// If it's a map
return "Dictionary"
case protoreflect.EnumKind:
return string(field.Desc.Enum().FullName().Name())
}
return "Unknown " + field.Desc.Kind().String()
}
// func getGodotDefaultValue(field *protogen.Field) string {
// switch field.Desc.Kind() {
// case protoreflect.BoolKind:
// return "false"
// case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
// case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
// case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
// case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
// return "0"
// case protoreflect.FloatKind:
// case protoreflect.DoubleKind:
// return "0.0"
// case protoreflect.StringKind:
// return "\"\""
// case protoreflect.BytesKind:
// return "PackedByteArray()"
// case protoreflect.MessageKind:
// return "null"
// // Maybe this would be handled differently?
// case protoreflect.EnumKind:
// return "0"
// }
// return "null"
// }
func generateFile(gen *protogen.Plugin, file *protogen.File) {
filename := file.GeneratedFilenamePrefix + "_pb.gd"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("## @generated by protoc-gen-godot")
g.P("## @generated from ", file.Desc.Path())
g.P("##")
g.P("## Proto syntax: ", file.Desc.Syntax())
if file.Proto.Edition != nil {
g.P("## Edition: ", file.Proto.Edition)
}
g.P()
g.P("const proto = preload(\"res://addons/godot-protobuf/proto.gd\")")
g.P()
// Generate the enum definitions
for _, enum := range file.Enums {
g.P("## @generated from enum ", enum.Desc.Name())
g.P("enum ", enum.GoIdent.GoName, " {")
for _, value := range enum.Values {
g.P("\t", value.Desc.Name(), " = ", value.Desc.Number(), ", ## @generated from enum value: ", value.Desc.Name(), " = ", value.Desc.Number())
}
g.P("}")
g.P()
g.P()
}
// Generate Message definitions
for _, msg := range file.Messages {
g.P("## @generated from message ", msg.Desc.Name())
g.P("class ", msg.GoIdent.GoName, " extends proto.ProtobufMessage:")
g.P("\tconst __NAME__ := \"", msg.GoIdent.GoName, "\"")
g.P()
g.P("\tstatic func from_bytes(bytes: PackedByteArray) -> ", msg.GoIdent.GoName, ":")
g.P("\t\treturn proto.ProtobufDecoder.decode_message(", msg.GoIdent.GoName, ".new(), bytes)")
g.P()
if len(msg.Fields) <= 0 {
continue
}
g.P("\tfunc _init_fields():")
for _, field := range msg.Fields {
fieldType, ok := getEnumTypeFromField(field)
if !ok {
println("Invalid field type: ", field.Desc.Kind())
return
}
fieldMethod := "\t\tadd_field(\"" + string(field.Desc.Name()) + "\", " + fmt.Sprintf("%d", field.Desc.Number()) + ", proto.DATA_TYPE." + fieldType
switch field.Desc.Kind() {
case protoreflect.MessageKind:
// Regular messages that aren't maps
if !field.Desc.IsMap() {
fieldMethod = fieldMethod + ", " + string(field.Desc.Message().FullName().Name())
if field.Desc.IsList() {
fieldMethod = fieldMethod + ", true"
if !field.Desc.IsPacked() {
fieldMethod = fieldMethod + ", false"
} else {
fieldMethod = fieldMethod + ", true"
}
if field.Oneof != nil {
fieldMethod = fieldMethod + ", -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
} else if field.Oneof != nil {
fieldMethod = fieldMethod + ", false, true, -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
break
}
// Map key and value types
keyType, ok := getEnumTypeFromKind(field.Desc.MapKey().Kind())
if !ok {
println("Invalid map key type: ", field.Desc.MapKey().Kind())
return
}
valueType, ok := getEnumTypeFromKind(field.Desc.MapValue().Kind())
if !ok {
println("Invalid map value type: ", field.Desc.MapValue().Kind())
return
}
if keyType == "MESSAGE" {
keyType = string(field.Desc.MapKey().Message().FullName().Name())
} else {
keyType = "proto.DATA_TYPE." + keyType
}
if valueType == "MESSAGE" {
valueType = string(field.Desc.MapValue().Message().FullName().Name())
} else {
valueType = "proto.DATA_TYPE." + valueType
}
fieldMethod = fieldMethod + ", null, true, false, " + keyType + ", " + valueType
if field.Oneof != nil {
fieldMethod = fieldMethod + ", \"" + string(field.Oneof.Desc.Name()) + "\""
}
case protoreflect.EnumKind:
fieldMethod = fieldMethod + ", " + string(field.Desc.Enum().FullName().Name())
if field.Oneof != nil {
fieldMethod = fieldMethod + ", false, true, -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
default:
if field.Desc.IsList() {
fieldMethod = fieldMethod + ", null, true"
if !field.Desc.IsPacked() {
fieldMethod = fieldMethod + ", false"
} else {
fieldMethod = fieldMethod + ", true"
}
if field.Oneof != nil {
fieldMethod = fieldMethod + ", -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
}
if field.Desc.IsPacked() {
fieldMethod = fieldMethod + ", true"
if field.Oneof != nil {
fieldMethod = fieldMethod + ", -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
}
if !field.Desc.IsList() && !field.Desc.IsPacked() && field.Oneof != nil {
fieldMethod = fieldMethod + ", null , false, false, -1, -1, \"" + string(field.Oneof.Desc.Name()) + "\""
}
}
g.P(fieldMethod, ")", " ## @generated from field: ", field.Desc.Kind().String(), " ", field.Desc.Name(), " = ", field.Desc.Number())
}
g.P()
for _, field := range msg.Fields {
g.P("\tfunc get_", field.Desc.Name(), "() -> ", getGodotFieldType(field), ":")
g.P("\t\treturn get_field(\"", field.Desc.Name(), "\") as ", getGodotFieldType(field))
g.P()
g.P("\tfunc set_", field.Desc.Name(), "(value: ", getGodotFieldType(field), "):")
g.P("\t\tset_field(\"", field.Desc.Name(), "\", value)")
g.P()
// Potentially in the future we could add a set method for maps to set the key and value
// if field.Desc.IsMap() {
// g.P("\tfunc set_", field.Desc.Name(), "(value: ", getGodotFieldType(field), "):")
// g.P("\t\treturn fields[\"", field.Desc.Name(), "\"].set_value(value)")
// g.P()
// }
}
var oneofs []string
for _, field := range msg.Fields {
if field.Oneof == nil {
continue
}
oneofName := string(field.Oneof.Desc.Name())
if slices.Contains(oneofs, oneofName) {
continue
}
oneofs = append(oneofs, oneofName)
g.P("\tfunc get_", oneofName, "():")
g.P("\t\treturn get_field(\"", oneofName, "\")")
g.P()
g.P("\tfunc set_", oneofName, "(value):")
g.P("\t\tset_field(\"", oneofName, "\", value)")
g.P()
}
g.P()
}
}