-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
564 lines (521 loc) · 14.4 KB
/
generator.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//go:generate generator.go
package plugify
import "C"
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// Plugin represents the structure of the .pplugin file
type Plugin struct {
Methods []Method `json:"exportedMethods"`
}
// Method represents a single exported method
type Method struct {
Name string `json:"name"`
FuncName string `json:"funcName"`
ParamTypes []PropertyType `json:"paramTypes"`
RetType PropertyType `json:"retType"`
Group string `json:"group,omitempty"`
Description string `json:"description,omitempty"`
}
// EnumValue represents a single enumeration value
type EnumValue struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Value int64 `json:"value"`
}
// Enum represents an enumeration
type Enum struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Values []EnumValue `json:"values"`
}
// PropertyType represents a parameter type
type PropertyType struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Ref bool `json:"ref,omitempty"`
Prototype *Method `json:"prototype,omitempty"`
Enumerator *Enum `json:"enum,omitempty"`
}
// invalidNames contains Go's reserved keywords and predeclared identifiers
var invalidNames = map[string]struct{}{
"break": {}, "case": {}, "chan": {}, "const": {}, "continue": {},
"default": {}, "defer": {}, "else": {}, "fallthrough": {}, "for": {},
"func": {}, "go": {}, "goto": {}, "if": {}, "import": {}, "interface": {},
"map": {}, "package": {}, "range": {}, "return": {}, "select": {}, "struct": {},
"switch": {}, "type": {}, "var": {}, "append": {}, "bool": {}, "byte": {},
"cap": {}, "close": {}, "complex": {}, "complex64": {}, "complex128": {},
"copy": {}, "delete": {}, "error": {}, "false": {}, "float32": {}, "float64": {},
"imag": {}, "int": {}, "int8": {}, "int16": {}, "int32": {}, "int64": {},
"iota": {}, "len": {}, "make": {}, "new": {}, "nil": {}, "panic": {},
"print": {}, "println": {}, "real": {}, "recover": {}, "rune": {}, "string": {},
"true": {}, "uint": {}, "uint8": {}, "uint16": {}, "uint32": {}, "uint64": {},
"uintptr": {},
}
// generateName returns a valid Go identifier by appending an underscore if the name is reserved
func generateName(name string) string {
if _, exists := invalidNames[name]; exists {
return name + "_"
}
return name
}
func Generate(packageName string, rootFolder string) {
code := []string{fmt.Sprintf(`package %s
// #include "autoexports.h"
import "C"
import (
"github.com/untrustedmodders/go-plugify"
"reflect"
"unsafe"
)
// Exported methods
`, packageName)}
// Walk through the directory recursively
err := filepath.Walk(rootFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the file is a .pplugin file
if !info.IsDir() && strings.HasSuffix(info.Name(), ".pplugin") {
// Read the file
fileContent, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file %s: %v", path, err)
}
// Parse the JSON content
var plugin Plugin
if err := json.Unmarshal(fileContent, &plugin); err != nil {
return fmt.Errorf("error parsing JSON in file %s: %v", path, err)
}
// Generate export function wrappers
for _, method := range plugin.Methods {
code = append(code, generateWrapper(method))
}
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path %v: %v\n", rootFolder, err)
}
// Write the generated file
err = os.WriteFile("autoexports.go", []byte(strings.Join(code, "")), 0644)
if err != nil {
fmt.Println("Error writing file:", err)
os.Exit(1)
}
header := `#pragma once
// autoexports.h
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <uchar.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct String { char* data; size_t size; size_t cap; } String;
typedef struct Vector { void* begin; void* end; void* capacity; } Vector;
typedef struct Vector2 { float x, y; } Vector2;
typedef struct Vector3 { float x, y, z; } Vector3;
typedef struct Vector4 { float x, y, z, w; } Vector4;
typedef struct Matrix4x4 { float m[4][4]; } Matrix4x4;
typedef struct Variant {
union {
bool boolean;
char char8;
char16_t char16;
int8_t int8;
int16_t int16;
int32_t int32;
int64_t int64;
uint8_t uint8;
uint16_t uint16;
uint32_t uint32;
uint64_t uint64;
void* ptr;
float flt;
double dbl;
String str;
Vector vec;
Vector2 vec2;
Vector3 vec3;
Vector4 vec4;
};
uint8_t current;
} Variant;
#ifdef __cplusplus
}
#endif
`
// Write the generated file
err = os.WriteFile("autoexports.h", []byte(header), 0644)
if err != nil {
fmt.Println("Error writing file:", err)
os.Exit(1)
}
fmt.Println("Generated autoexports.go!")
}
func mapToFunc(jsonType string) string {
switch jsonType {
case "void":
return ""
case "bool":
return "Bool"
case "char8":
return "Int8"
case "char16":
return "UInt16"
case "int8":
return "Int8"
case "int16":
return "Int16"
case "int32":
return "Int32"
case "int64":
return "Int64"
case "uint8":
return "UInt8"
case "uint16":
return "UInt16"
case "uint32":
return "UInt32"
case "uint64":
return "UInt64"
case "ptr64", "ptr32":
return "Pointer"
case "float":
return "Float"
case "double":
return "Double"
case "string":
return "String"
case "any":
return "Variant"
case "vec2":
return "Vector2"
case "vec3":
return "Vector3"
case "vec4":
return "Vector4"
case "mat4x4":
return "Matrix4x4"
case "bool[]":
return "Bool"
case "char8[]":
return "Char8"
case "char16[]":
return "Char16"
case "int8[]":
return "Int8"
case "int16[]":
return "Int16"
case "int32[]":
return "Int32"
case "int64[]":
return "Int64"
case "uint8[]":
return "UInt8"
case "uint16[]":
return "UInt16"
case "uint32[]":
return "UInt32"
case "uint64[]":
return "UInt64"
case "ptr32[]":
return "Pointer"
case "ptr64[]":
return "Pointer"
case "float[]":
return "Float"
case "double[]":
return "Double"
case "string[]":
return "String"
case "any[]":
return "Variant"
case "vec2[]":
return "Vector2"
case "vec3[]":
return "Vector3"
case "vec4[]":
return "Vector4"
case "mat4x4[]":
return "Matrix4x4"
default:
return "Function" // Function
}
}
func mapToCType(jsonType string) string {
switch jsonType {
case "void":
return ""
case "bool":
return "bool"
case "char8":
return "int8"
case "char16":
return "uint16"
case "int8":
return "int8"
case "int16":
return "int16"
case "int32":
return "int32"
case "int64":
return "int64"
case "uint8":
return "uint8"
case "uint16":
return "uint16"
case "uint32":
return "uint32"
case "uint64":
return "uint64"
case "ptr64", "ptr32":
return "uintptr"
case "float":
return "float32"
case "double":
return "float64"
case "string":
return "C.String"
case "any":
return "C.Variant"
case "vec2":
return "C.Vector2"
case "vec3":
return "C.Vector3"
case "vec4":
return "C.Vector4"
case "mat4x4":
return "C.Matrix4x4"
case "bool[]", "char8[]", "char16[]", "int8[]", "int16[]", "int32[]", "int64[]", "uint8[]", "uint16[]", "uint32[]", "uint64[]", "ptr32[]", "ptr64[]", "float[]", "double[]", "string[]", "any[]", "vec2[]", "vec3[]", "vec4[]", "mat4x4[]":
return "C.Vector"
default:
return "unsafe.Pointer" // Function
}
}
func mapToGoType(jsonType string) string {
switch jsonType {
case "void":
return ""
case "bool":
return "bool"
case "char8":
return "int8"
case "char16":
return "uint16"
case "int8":
return "int8"
case "int16":
return "int16"
case "int32":
return "int32"
case "int64":
return "int64"
case "uint8":
return "uint8"
case "uint16":
return "uint16"
case "uint32":
return "uint32"
case "uint64":
return "uint64"
case "ptr64", "ptr32":
return "uintptr"
case "float":
return "float32"
case "double":
return "float64"
case "string":
return "string"
case "any":
return "interface{}"
case "bool[]":
return "[]bool"
case "int8[]":
return "[]int8"
case "int16[]":
return "[]int16"
case "int32[]":
return "[]int32"
case "int64[]":
return "[]int64"
case "uint8[]":
return "[]uint8"
case "uint16[]":
return "[]uint16"
case "uint32[]":
return "[]uint32"
case "uint64[]":
return "[]uint64"
case "ptr32[]", "ptr64[]":
return "[]uintptr"
case "float[]":
return "[]float32"
case "double[]":
return "[]float64"
case "string[]":
return "[]string"
case "any[]":
return "[]interface{}"
case "vec2[]":
return "[]plugify.Vector2"
case "vec3[]":
return "[]plugify.Vector3"
case "vec4[]":
return "[]plugify.Vector4"
case "mat4x4[]":
return "[]plugify.Matrix4x4"
case "vec2":
return "plugify.Vector2"
case "vec3":
return "plugify.Vector3"
case "vec4":
return "plugify.Vector4"
case "mat4x4":
return "plugify.Matrix4x4"
default:
return "interface{}"
}
}
func generateWrapper(method Method) string {
// Generate the function signature
var params []string
var callParams []string
var tempVars []string // Temporary variables for ref parameters
var assignBack []string // Code to assign back ref parameters
for _, param := range method.ParamTypes {
cType := mapToCType(param.Type)
goType := mapToGoType(param.Type)
varType := mapToFunc(param.Type)
name := generateName(param.Name)
// Handle non-ref parameters
if param.Ref || strings.HasPrefix(cType, "C.") {
cType = "*" + cType
}
params = append(params, fmt.Sprintf("%s %s", name, cType))
castName := fmt.Sprintf("(*plugify.Plg%s)(unsafe.Pointer(%s))", cType[3:], name)
// Handle ref parameters
if param.Ref && (cType == "*C.String" || cType == "*C.Vector" || cType == "*C.Variant") {
// Create a temporary variable for the ref parameter
tempVar := fmt.Sprintf("_%s", name)
// Pass the address of the temporary variable to the inner function
callParams = append(callParams, fmt.Sprintf("&%s", tempVar))
// Handle special cases for strings, arrays, and functions
switch param.Type {
case "string", "any":
tempVars = append(tempVars, fmt.Sprintf("\t%s := plugify.Get%sData(%s)\n", tempVar, varType, castName))
assignBack = append(assignBack, fmt.Sprintf("\tplugify.Assign%s(%s, %s)\n", varType, castName, tempVar))
default:
if strings.HasSuffix(param.Type, "[]") {
typeName := ""
if param.Enumerator != nil {
typeName = fmt.Sprintf("T[%s]", param.Enumerator.Name)
}
tempVars = append(tempVars, fmt.Sprintf("\t%s := plugify.GetVectorData%s%s(%s)\n", tempVar, varType, typeName, castName))
assignBack = append(assignBack, fmt.Sprintf("\tplugify.AssignVector%s(%s, %s)\n", varType, castName, tempVar))
} else {
tempVars = append(tempVars, fmt.Sprintf("\t%s := %s\n", tempVar, name))
assignBack = append(assignBack, fmt.Sprintf("\t%s = %s\n", name, tempVar))
}
}
} else {
// Handle special cases for strings, arrays, and functions
switch param.Type {
case "function":
funcName := generateName(param.Prototype.Name)
callParams = append(callParams, fmt.Sprintf("plugify.GetDelegateForFunctionPointer(%s, reflect.TypeOf(%s(nil))).(%s)", name, funcName, funcName))
case "string", "any":
callParams = append(callParams, fmt.Sprintf("plugify.Get%sData(%s)", varType, castName))
case "vec2", "vec3", "vec4", "mat4x4":
deref := "*" // Dereference the pointer
if param.Ref {
deref = ""
}
callParams = append(callParams, fmt.Sprintf("%s(*%s)(unsafe.Pointer(%s))", deref, goType, name))
default:
if strings.HasSuffix(param.Type, "[]") {
typeName := ""
if param.Enumerator != nil {
typeName = fmt.Sprintf("T[%s]", param.Enumerator.Name)
}
callParams = append(callParams, fmt.Sprintf("plugify.GetVectorData%s%s(%s)", varType, typeName, castName))
} else {
if param.Enumerator != nil {
if param.Ref {
callParams = append(callParams, fmt.Sprintf("(*%s)(%s)", param.Enumerator.Name, name))
} else {
callParams = append(callParams, fmt.Sprintf("%s(%s)", param.Enumerator.Name, name))
}
} else {
callParams = append(callParams, name)
}
}
}
}
}
paramList := strings.Join(params, ", ")
callParamList := strings.Join(callParams, ", ")
// Generate the return type
resultDest := ""
returnType := mapToCType(method.RetType.Type)
if returnType != "" {
returnType = " " + returnType
resultDest = "__result := "
}
// Generate the function wrapper
wrapper := []string{fmt.Sprintf(`
//export %s
func %s(%s)%s {
`, method.FuncName, method.FuncName, paramList, returnType)}
// Add temporary variable declarations for ref parameters
if len(tempVars) > 0 {
wrapper = append(wrapper, strings.Join(tempVars, ""))
}
// Call the inner function
wrapper = append(wrapper, fmt.Sprintf("\t%s%s(%s)\n", resultDest, method.Name, callParamList))
// Assign back ref parameters
if len(assignBack) > 0 {
wrapper = append(wrapper, strings.Join(assignBack, ""))
}
// Handle return type conversion
if resultDest != "" {
cType := mapToCType(method.RetType.Type)
varType := mapToFunc(method.RetType.Type)
switch method.RetType.Type {
case "function":
wrapper = append(wrapper, "\treturn plugify.GetFunctionPointerForDelegate(__result)\n")
case "string", "any":
wrapper = append(wrapper,
fmt.Sprintf("\t__return := plugify.Construct%s(__result)\n", varType),
fmt.Sprintf("\treturn *(*%s)(unsafe.Pointer(&__return))\n", cType))
case "vec2", "vec3", "vec4", "mat4x4":
wrapper = append(wrapper, fmt.Sprintf("\treturn *(*%s)(unsafe.Pointer(&__result))\n", cType))
default:
if strings.HasSuffix(method.RetType.Type, "[]") {
typeName := ""
if method.RetType.Enumerator != nil {
typeName = fmt.Sprintf("[%s]", method.RetType.Enumerator.Name)
}
wrapper = append(wrapper,
fmt.Sprintf("\t__return := plugify.ConstructVector%s%s(__result)\n", varType, typeName),
fmt.Sprintf("\treturn *(*%s)(unsafe.Pointer(&__return))\n", cType))
} else {
if method.RetType.Enumerator != nil {
wrapper = append(wrapper, fmt.Sprintf("\treturn %s(__result)\n", method.RetType.Enumerator.Name))
} else {
wrapper = append(wrapper, fmt.Sprintf("\treturn __result\n"))
}
}
}
}
wrapper = append(wrapper, "}\n")
return strings.Join(wrapper, "")
}