Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
PimPam Games Studio
gdnative-go
Commits
8419ba6c
Commit
8419ba6c
authored
Oct 10, 2020
by
Oscar Campos
Committed by
Oscar Campos
Oct 11, 2020
Browse files
fix: fix linting errors and rename gdnativego compiler into gogdc
parent
0f2165e1
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
41 additions
and
90 deletions
+41
-90
cmd/generate/gdnative/generate.go
cmd/generate/gdnative/generate.go
+8
-14
cmd/generate/types/generate.go
cmd/generate/types/generate.go
+12
-35
cmd/generate/types/model.go
cmd/generate/types/model.go
+1
-1
cmd/gogdc/gdnativego.go
cmd/gogdc/gdnativego.go
+4
-6
cmd/gogdc/generate.go
cmd/gogdc/generate.go
+0
-0
cmd/gogdc/list.go
cmd/gogdc/list.go
+0
-0
gdnative/ast.go
gdnative/ast.go
+2
-3
gdnative/gdnative.go
gdnative/gdnative.go
+3
-15
gdnative/nativescript.go
gdnative/nativescript.go
+1
-3
gdnative/pimpam.go
gdnative/pimpam.go
+7
-5
gdnative/string.go
gdnative/string.go
+0
-4
gdnative/util.go
gdnative/util.go
+3
-3
gdnative/variant.go
gdnative/variant.go
+0
-1
No files found.
cmd/generate/gdnative/generate.go
View file @
8419ba6c
...
...
@@ -4,6 +4,7 @@ package gdnative
import
(
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
...
...
@@ -21,30 +22,21 @@ type View struct {
// not the given element is the last in the slice or not. This is so we can
// correctly insert commas for argument lists.
func
(
v
View
)
NotLastElement
(
n
int
,
slice
[][]
string
)
bool
{
if
n
!=
(
len
(
slice
)
-
1
)
{
return
true
}
return
false
return
n
!=
(
len
(
slice
)
-
1
)
}
// NotVoid checks to see if the return string is void or not. This is used inside
// our template so we can determine if we need to use the `return` keyword in
// the function body.
func
(
v
View
)
NotVoid
(
ret
string
)
bool
{
if
ret
!=
"void"
{
return
true
}
return
false
return
ret
!=
"void"
}
// HasArgs is a function we use inside the template to test whether or not the
// function has arguments. This is so we can determine if we need to place a
// comma.
func
(
v
View
)
HasArgs
(
args
[][]
string
)
bool
{
if
len
(
args
)
!=
0
{
return
true
}
return
false
return
len
(
args
)
!=
0
}
// Generate generates the bindings from the JSON definition
...
...
@@ -114,7 +106,9 @@ func Parse(packagePath string) APIs {
// Unmarshal the JSON into our struct.
var
apis
APIs
json
.
Unmarshal
(
body
,
&
apis
)
if
err
:=
json
.
Unmarshal
(
body
,
&
apis
);
err
!=
nil
{
panic
(
errors
.
New
(
"could not unmarshal Godot JSON API"
))
}
return
apis
}
...
...
@@ -129,10 +123,10 @@ func WriteTemplate(templatePath, outputPath string, view View) {
// Open the output file for writing
f
,
err
:=
os
.
Create
(
outputPath
)
defer
f
.
Close
()
if
err
!=
nil
{
panic
(
err
)
}
defer
f
.
Close
()
// Write the template with the given view.
err
=
t
.
Execute
(
f
,
view
)
...
...
cmd/generate/types/generate.go
View file @
8419ba6c
...
...
@@ -36,19 +36,13 @@ func (v View) Debug(itm string) string {
// IsValidProperty will determine if we should be generating the given property
// in our Go structure.
func
(
v
View
)
IsValidProperty
(
prop
TypeDef
)
bool
{
if
strings
.
Contains
(
prop
.
Name
,
"_touch_that"
)
{
return
false
}
return
true
return
strings
.
Contains
(
prop
.
Name
,
"_touch_that"
)
}
// IsGodotBaseType will check to see if the given simple type definition is defining
// a built-in C type or a Godot type.
func
(
v
View
)
IsGodotBaseType
(
typeDef
TypeDef
)
bool
{
if
strings
.
Contains
(
typeDef
.
Base
,
"godot_"
)
{
return
true
}
return
false
return
strings
.
Contains
(
typeDef
.
Base
,
"godot_"
)
}
// ToGoBaseType will convert a base type name to the correct Go base type.
...
...
@@ -82,18 +76,12 @@ func (v View) ToGoReturnType(str string) string {
// HasReturn returns true if the given string is void
func
(
v
View
)
HasReturn
(
str
string
)
bool
{
if
str
==
"void"
||
str
==
"Void"
||
strings
.
Contains
(
str
,
"void"
)
{
return
false
}
return
true
return
str
==
"void"
||
str
==
"Void"
||
strings
.
Contains
(
str
,
"void"
)
}
// HasPointerReturn returns true if the given string contains an indirection operator
func
(
v
View
)
HasPointerReturn
(
str
string
)
bool
{
if
strings
.
Contains
(
str
,
"*"
)
{
return
true
}
return
false
return
strings
.
Contains
(
str
,
"*"
)
}
// IsVoidPointerType returns true if the given string matches godot object void types
...
...
@@ -107,19 +95,13 @@ func (v View) IsVoidPointerType(str string) bool {
// IsWcharT returns true if the given strig contains wchar_t type
func
(
v
View
)
IsWcharT
(
str
string
)
bool
{
if
strings
.
Contains
(
str
,
"wchar_t"
)
{
return
true
}
return
false
return
strings
.
Contains
(
str
,
"wchar_t"
)
}
// IsDoublePointer returns true if the given string contains two indirection
// operators one beside another
func
(
v
View
)
IsDoublePointer
(
str
string
)
bool
{
if
strings
.
Contains
(
str
,
"**"
)
{
return
true
}
return
false
return
strings
.
Contains
(
str
,
"**"
)
}
// ToGoArgType converts arguments types to Go valid types
...
...
@@ -244,18 +226,12 @@ func (v View) MethodsList(typeDef TypeDef) []Method {
// MethodIsConstructor returns true if the given method contains the `_new` sub string
func
(
v
View
)
MethodIsConstructor
(
method
Method
)
bool
{
if
strings
.
Contains
(
method
.
Name
,
"_new"
)
{
return
true
}
return
false
return
strings
.
Contains
(
method
.
Name
,
"_new"
)
}
// NotSelfArg return false if the given string contains any reference to self or p_self
func
(
v
View
)
NotSelfArg
(
str
string
)
bool
{
if
str
==
"self"
||
str
==
"p_self"
{
return
false
}
return
true
return
str
==
"self"
||
str
==
"p_self"
}
// StripPointer strips the indirection operator from a given string
...
...
@@ -363,7 +339,8 @@ func Generate() {
// Organize the type definitions by header name
for
_
,
typeDef
:=
range
allTypeDefinitions
{
if
_
,
ok
:=
defMap
[
typeDef
.
HeaderName
];
ok
{
_
,
ok
:=
defMap
[
typeDef
.
HeaderName
]
if
ok
{
defMap
[
typeDef
.
HeaderName
]
=
append
(
defMap
[
typeDef
.
HeaderName
],
typeDef
)
}
else
{
defMap
[
typeDef
.
HeaderName
]
=
[]
TypeDef
{
typeDef
}
...
...
@@ -411,7 +388,7 @@ func Generate() {
// Run gofmt on the generated Go file.
log
.
Println
(
" Running gofmt on output:"
,
outFileName
+
"..."
)
if
noGoImport
==
false
{
if
!
noGoImport
{
GoFmt
(
packagePath
+
"/gdnative/"
+
outFileName
)
}
...
...
@@ -437,10 +414,10 @@ func WriteTemplate(templatePath, outputPath string, view View) {
// Open the output file for writing
f
,
err
:=
os
.
Create
(
outputPath
)
defer
f
.
Close
()
if
err
!=
nil
{
panic
(
err
)
}
defer
f
.
Close
()
// Write the template with the given view.
err
=
t
.
Execute
(
f
,
view
)
...
...
cmd/generate/types/model.go
View file @
8419ba6c
...
...
@@ -7,8 +7,8 @@ type TypeDef struct {
Comment
string
// Contains the comment on the line of the struct
GoName
string
// The Go type name in camelCase
HeaderName
string
// The header file this type shows up in
IsPointer
bool
// Usually for properties; defines if it is a pointer type
Name
string
// The C type name in snake_case
Properties
[]
TypeDef
// Optional C struct fields
IsPointer
bool
// Usually for properties; defines if it is a pointer type
SimpleType
bool
// Whether or not the definition is just one line long (e.g. bool, int, etc.)
}
cmd/g
dnativego
/gdnativego.go
→
cmd/g
ogdc
/gdnativego.go
View file @
8419ba6c
...
...
@@ -24,19 +24,17 @@ type context struct {
Verbose
bool
}
type
generateCmd
struct
{
path
string
}
type
generateCmd
struct
{}
type
listCmd
struct
{}
// cli defines our command line structure using Kong
var
cli
struct
{
Generate
generateCmd
`cmd help:"Generates autotoregistration boilerplate Go code for user defined structures"`
//nolint:govet
List
listCmd
`cmd help:"List user defined autoregistrable data structures"`
//nolint:govet
Path
string
`type:"path" default:"." help:"Path where execute the command"`
Verbose
bool
`help:"Verbose output"`
Generate
generateCmd
`cmd help:"Generates autotoregistration boilerplate Go code for user defined structures"`
List
listCmd
`cmd help:"List user defined autoregistrable data structures"`
}
func
main
()
{
...
...
cmd/g
dnativego
/generate.go
→
cmd/g
ogdc
/generate.go
View file @
8419ba6c
File moved
cmd/g
dnativego
/list.go
→
cmd/g
ogdc
/list.go
View file @
8419ba6c
File moved
gdnative/ast.go
View file @
8419ba6c
...
...
@@ -211,7 +211,6 @@ func validateConstructor(structName string, fd *ast.FuncDecl) (*registryConstruc
structName
,
structName
,
funcName
,
t
.
X
.
(
*
ast
.
Ident
)
.
Name
,
)
}
break
default
:
return
nil
,
fmt
.
Errorf
(
"constructors of %s values must return a pointer to *%s but %s returns %v"
,
structName
,
structName
,
funcName
,
t
)
}
...
...
@@ -361,7 +360,7 @@ func lookupMethods(className string, file *ast.File) []*registryMethod {
// lookupSignals look up for every signal that is owned by the type and fill
// a registration data structure with it
func
lookupSignals
(
className
string
,
file
*
ast
.
Fil
e
)
[]
*
registrySignal
{
func
lookupSignals
(
className
string
,
file
ast
.
Nod
e
)
[]
*
registrySignal
{
signals
:=
[]
*
registrySignal
{}
ast
.
Inspect
(
file
,
func
(
node
ast
.
Node
)
(
cont
bool
)
{
...
...
@@ -693,7 +692,7 @@ func parseMap(field *ast.MapType) string {
return
fmt
.
Sprintf
(
result
,
key
,
value
)
}
func
parseKeyValueExpr
(
expr
*
ast
.
KeyValueExpr
)
(
string
,
string
)
{
func
parseKeyValueExpr
(
expr
*
ast
.
KeyValueExpr
)
(
string
,
string
)
{
//nolint:unused
var
value
string
key
:=
expr
.
Key
.
(
*
ast
.
Ident
)
.
Name
...
...
gdnative/gdnative.go
View file @
8419ba6c
...
...
@@ -252,7 +252,7 @@ func (i Int64T) getBase() C.int64_t {
// SignedChar is a Godot C schar wrapper
type
SignedChar
int8
func
(
s
SignedChar
)
getBase
()
*
C
.
schar
{
func
(
s
SignedChar
)
getBase
()
*
C
.
schar
{
//nolint:unused
intVal
:=
int8
(
s
)
return
(
*
C
.
schar
)(
unsafe
.
Pointer
(
&
intVal
))
}
...
...
@@ -260,7 +260,7 @@ func (s SignedChar) getBase() *C.schar {
// Uint is a Godot C uint wrapper
type
Uint
uint
func
(
u
Uint
)
getBase
()
C
.
uint
{
func
(
u
Uint
)
getBase
()
C
.
uint
{
//nolint:unused
return
C
.
uint
(
u
)
}
...
...
@@ -274,7 +274,7 @@ func (u Uint8T) getBase() C.uint8_t {
// Uint32T is a Godot C uint32_t wrapper
type
Uint32T
uint32
func
(
u
Uint32T
)
getBase
()
C
.
uint32_t
{
func
(
u
Uint32T
)
getBase
()
C
.
uint32_t
{
//nolint:unused
return
C
.
uint32_t
(
u
)
}
...
...
@@ -294,18 +294,6 @@ func newWcharT(str *C.wchar_t) WcharT {
return
WcharT
(
goStr
)
}
// newWcharTWithLength will convert the given C.wchar_t into a Go string
func
newWcharTWithLength
(
str
*
C
.
wchar_t
,
length
int
)
WcharT
{
goStr
,
err
:=
wchar
.
WcharStringPtrNToGoString
(
unsafe
.
Pointer
(
str
),
length
)
if
err
!=
nil
{
log
.
Println
(
"Error converting wchar_t to Go string:"
,
err
)
}
if
len
(
goStr
)
!=
length
{
goStr
=
truncateString
(
goStr
,
length
)
}
return
WcharT
(
goStr
)
}
// WcharT is a Godot C wchar_t wrapper
type
WcharT
string
...
...
gdnative/nativescript.go
View file @
8419ba6c
...
...
@@ -435,9 +435,7 @@ var nativeScriptInit = []func(){}
// This is used so you can define a function that will run to register all of the
// classes that you want exposed to Godot.
func
SetNativeScriptInit
(
initFunc
...
func
())
{
for
_
,
init
:=
range
initFunc
{
nativeScriptInit
=
append
(
nativeScriptInit
,
init
)
}
nativeScriptInit
=
append
(
nativeScriptInit
,
initFunc
...
)
}
/*------------------------------------------------------------------------------
...
...
gdnative/pimpam.go
View file @
8419ba6c
...
...
@@ -105,7 +105,9 @@ func (c *Class) register() {
// then iterate over any defined property and register them
for
_
,
property
:=
range
c
.
properties
{
property
.
register
()
if
err
:=
property
.
register
();
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"could not register class properties: %w"
,
err
))
}
}
// finally iterate over any defined signal and register them
...
...
@@ -280,7 +282,7 @@ func NewGodotProperty(className, name, hint, hintString, usage, rset string,
if
attributes
.
RsetType
,
ok
=
MethodRpcModeLookupMap
[
rsetType
];
!
ok
{
var
validTypes
string
for
key
,
_
:=
range
MethodRpcModeLookupMap
{
for
key
:=
range
MethodRpcModeLookupMap
{
validTypes
=
fmt
.
Sprintf
(
"%s %s"
,
validTypes
,
strings
.
Replace
(
key
,
"MethodRpcMode"
,
""
,
1
))
}
panic
(
fmt
.
Sprintf
(
"unknown rset %q, allowed types: %s"
,
rset
,
validTypes
))
...
...
@@ -307,7 +309,7 @@ func (p *Property) register() error {
// if set and get functions are not defined generate generic ones
if
p
.
setFunc
==
nil
{
p
.
setFunc
=
p
.
c
reateGenericSetter
()
p
.
setFunc
=
p
.
C
reateGenericSetter
()
}
if
p
.
setFunc
==
nil
||
p
.
getFunc
==
nil
{
return
fmt
.
Errorf
(
"you can not register a property that does not defines both setter and getter functions"
)
...
...
@@ -318,7 +320,7 @@ func (p *Property) register() error {
}
// creates a generic setter method to set property values if none is provided
func
(
p
*
Property
)
c
reateGenericSetter
()
*
InstancePropertySet
{
func
(
p
*
Property
)
C
reateGenericSetter
()
*
InstancePropertySet
{
propertySetter
:=
func
(
object
Object
,
classProperty
,
instanceString
string
,
property
Variant
)
{
Log
.
Println
(
fmt
.
Sprintf
(
"Creating Go generic property setter for %s.%s"
,
p
.
name
,
p
.
propertyName
))
...
...
@@ -334,7 +336,7 @@ func (p *Property) createGenericSetter() *InstancePropertySet {
}
// created a generic getter method to get property values if none is provided
func
(
p
*
Property
)
c
reateGenericGetter
()
*
InstancePropertyGet
{
func
(
p
*
Property
)
C
reateGenericGetter
()
*
InstancePropertyGet
{
propertyGetter
:=
func
(
object
Object
,
classProperty
,
instanceString
string
)
Variant
{
log
.
Println
(
fmt
.
Sprintf
(
"Creating Go generic property getter for %s.%s"
,
p
.
name
,
p
.
propertyName
))
...
...
gdnative/string.go
View file @
8419ba6c
...
...
@@ -15,7 +15,3 @@ func NewStringWithWideString(str string) String {
func
NewString
()
String
{
return
""
}
func
truncateString
(
str
string
,
num
int
)
string
{
return
str
[
0
:
num
]
}
gdnative/util.go
View file @
8419ba6c
...
...
@@ -22,7 +22,7 @@ func unsafeToGoString(p unsafe.Pointer) string {
// Attribution:
// Author: https://github.com/mantenie
// Source: https://github.com/serenize/snaker
func
camelToSnake
(
s
string
)
string
{
func
camelToSnake
(
s
string
)
string
{
//nolint:deadcode,unused
var
result
string
var
words
[]
string
var
lastPos
int
...
...
@@ -60,7 +60,7 @@ func camelToSnake(s string) string {
}
// startsWithInitialism returns the initialism if the given string begins with it
func
startsWithInitialism
(
s
string
)
string
{
func
startsWithInitialism
(
s
string
)
string
{
//nolint:unused
var
initialism
string
// the longest initialism is 5 char, the shortest 2
for
i
:=
1
;
i
<=
5
;
i
++
{
...
...
@@ -73,7 +73,7 @@ func startsWithInitialism(s string) string {
// commonInitialisms, taken from
// https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
var
commonInitialisms
=
map
[
string
]
bool
{
var
commonInitialisms
=
map
[
string
]
bool
{
//nolint:unused
"ACL"
:
true
,
"API"
:
true
,
"ASCII"
:
true
,
...
...
gdnative/variant.go
View file @
8419ba6c
...
...
@@ -24,7 +24,6 @@ func (gdt *Variant) GetType() VariantType {
// VariantArray is a wrapper around Godot C **godot_variant
type
VariantArray
struct
{
base
**
C
.
godot_variant
array
[]
Variant
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment