Commit 995747d3 authored by Oscar Campos's avatar Oscar Campos Committed by Oscar Campos
Browse files

fix: eerything working in auto generation but signals make editor to crash...

fix: eerything working in auto generation but signals make editor to crash (probably due pointer conversion into array)
parent 639962dd
......@@ -24,11 +24,13 @@ package {{ $data.Package }}
import (
"fmt"
"reflect"
"gitlab.com/pimpam-games-studio/gdnative-go/gdnative"
)
// used for FreeFunc
var emptyFreeFunc = func(_ string) {}
{{ range $className, $class := $data.Classes -}}
// {{ $className }}Wrapper is a wrapper over {{ $className }} that will register it with in godot
type {{ $className }}Wrapper struct {
......@@ -131,55 +133,44 @@ func nativeScriptInit{{ $className }}() {
// define properties attached to the instance
properties := []gdnative.Property{
{{ range $i, $property := $class.Properties -}}
{{ if $class.Alias -}}
gdnative.NewGodotProperty("{{ $class.Alias }}", "{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}", "{{ $property.Hint }}", "{{ $property.HintString }}", "{{ $property.Usage }}", "{{ $property.RsetType }}", nil, nil),
{{ else -}}
setter.MethodData = fmt.Sprintf("{{ $className }}::%s", propertyString)
getter.MethodData = fmt.Sprintf("{{ $className }}::%s", propertyString)
gdnative.NewGodotProperty("{{ $className }}", "{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}", "{{ $property.Hint }}", "{{ $property.HintString }}", "{{ $property.Usage }}", "{{ $property.RsetType }}", nil, nil),
{{ end -}}
gdnative.NewGodotProperty(
"{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}",
"{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
"{{ $property.Hint }}", "{{ $property.HintString }}",
"{{ $property.Usage }}", "{{ $property.RsetType }}",
&gdnative.InstancePropertySet{
SetFunc: func(object gdnative.Object, classProperty, instanceString string, property gdnative.Variant) {
class, ok := SimpleClassInstances[instanceString]
if !ok {
panic(fmt.Sprintf("Set property %s does not exists on instance %s registry", classProperty, instanceString))
}
class.class.{{ $property.Name }} = {{ $property.SetConvert }}
},
MethodData: "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}::{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
FreeFunc: emptyFreeFunc,
},
&gdnative.InstancePropertyGet{
GetFunc: func(object gdnative.Object, classProperty, instanceString string) gdnative.Variant {
class, ok := SimpleClassInstances[instanceString]
if !ok {
panic(fmt.Sprintf("Get property %q does not exists on instance %q registry", classProperty, instanceString))
}
return {{ $property.GetConvert }}
},
MethodData: "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}::{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
FreeFunc: emptyFreeFunc,
},
),
{{ end -}}
}
{{/* second pass to add the setter/getters */}}
for i := range properties {
var setter gdnative.InstancePropertySet
setter.SetFunc = func(object gdnative.Object, classProperty, instanceString string, property gdnative.Variant) {
class, ok := {{ $className }}Instances[instanceString]
if !ok {
panic(fmt.Sprintf("Set property %s does not exists on instance %s registry", classProperty, instanceString))
}
propertyField := reflect.ValueOf(reflect.ValueOf(class)).Elem().FieldByName(properties[i].GetName())
propertyField.Set(reflect.ValueOf(property))
}
setter.FreeFunc = func(methodData string) {}
var getter gdnative.InstancePropertyGet
getter.GetFunc = func(object gdnative.Object, classProperty, instanceString string) gdnative.Variant {
class, ok := {{ $className }}Instances[instanceString]
if !ok {
panic(fmt.Sprintf("Set property %s does not exists on instance %s registry", classProperty, instanceString))
}
propertyField := reflect.ValueOf(reflect.ValueOf(class)).Elem().FieldByName(properties[i].GetName())
return propertyField.Interface().(gdnative.Variant)
}
getter.FreeFunc = func(methodData string) {}
{{ if $class.Alias -}}
setter.MethodData = fmt.Sprintf("{{ $class.Alias }}::%s", properties[i].GetName())
getter.MethodData = fmt.Sprintf("{{ $class.Alias }}::%s", properties[i].GetName())
{{ else -}}
setter.MethodData = fmt.Sprintf("{{ $className }}::%s", properties[i].GetName())
getter.MethodData = fmt.Sprintf("{{ $className }}::%s", properties[i].GetName())
{{ end -}}
properties[i].SetSetter(&setter)
properties[i].SetGetter(&getter)
}
// signals attached to the instance
signals := []gdnative.GDSignal{
......
......@@ -501,6 +501,7 @@ func lookupProperties(className string, file *ast.File) []*registryProperty {
continue
}
gdnativeKind := ""
kind := parseDefault(field.Type, "")
switch kind {
case "":
......@@ -510,6 +511,7 @@ func lookupProperties(className string, file *ast.File) []*registryProperty {
// this is a signal skip it
continue
default:
gdnativeKind = kind
typeFormat := fmt.Sprintf("VariantType%s", strings.ReplaceAll(kind, "gdnative.", ""))
_, ok := VariantTypeLookupMap[typeFormat]
if ok {
......@@ -529,9 +531,10 @@ func lookupProperties(className string, file *ast.File) []*registryProperty {
}
tmpProperties = append(tmpProperties, &registryProperty{
name: name.String(),
alias: alias,
kind: kind,
name: name.String(),
alias: alias,
kind: kind,
gdnativeKind: gdnativeKind,
})
}
......@@ -591,7 +594,7 @@ func mustSetPropertyTagRset(property *registryProperty, value string) {
)
os.Exit(1)
}
property.rset = fmt.Sprintf("gdnative.%s", rpcMode)
property.rset = strings.Title(value)
}
func mustSetPropertyTagHint(property *registryProperty, value string) {
......@@ -606,7 +609,7 @@ func mustSetPropertyTagHint(property *registryProperty, value string) {
fmt.Printf("on property %s: unknown hint %s, it must be one of %s", property.name, value, strings.Join(valid, ", "))
os.Exit(1)
}
property.hint = fmt.Sprintf("gdnative.%s", hint)
property.hint = strings.Title(value)
}
func mustSetPropertyTagUsage(property *registryProperty, value string) {
......@@ -621,7 +624,7 @@ func mustSetPropertyTagUsage(property *registryProperty, value string) {
fmt.Printf("on property %s: unknown usage %s, it must be one of %s", property.name, value, strings.Join(valid, ", "))
os.Exit(1)
}
property.usage = fmt.Sprintf("gdnative.%s", usage)
property.usage = strings.Title(value)
}
func extractExportedAndAliasFromDoc(doc *ast.CommentGroup) (string, bool) {
......
......@@ -388,10 +388,10 @@ func (n *nativeScript) RegisterProperty(name, path string, attributes *PropertyA
func (n *nativeScript) RegisterSignal(name string, signal *Signal) {
// Construct the C struct based on the signal Go wrapper
var base C.godot_signal
signal.base = &base
signal.base = (*C.godot_signal)(unsafe.Pointer(&base))
signal.base.name = *(signal.Name.getBase())
signal.base.num_args = C.int(signal.NumArgs.getBase())
signal.base.num_default_args = C.int(signal.NumDefaultArgs.getBase())
signal.base.num_args = signal.NumArgs.getBase()
signal.base.num_default_args = signal.NumDefaultArgs.getBase()
// Build the arguments
argsArray := C.go_godot_signal_argument_build_array(C.int(signal.NumArgs))
......@@ -404,16 +404,17 @@ func (n *nativeScript) RegisterSignal(name string, signal *Signal) {
cArg.hint_string = *(arg.HintString.getBase())
cArg.usage = arg.Usage.getBase()
C.go_godot_signal_argument_add_element(argsArray, &cArg, C.int(i))
C.go_godot_signal_argument_add_element(argsArray, (*C.godot_signal_argument)(unsafe.Pointer(&cArg)), C.int(i))
}
signal.base.args = *argsArray
signal.base.args = *(**C.godot_signal_argument)(unsafe.Pointer(&argsArray))
// Build the default arguments
variantArray := C.go_godot_variant_build_array(C.int(signal.NumDefaultArgs))
for i, variant := range signal.DefaultArgs {
C.go_godot_variant_add_element(variantArray, variant.getBase(), C.int(i))
}
signal.base.default_args = *variantArray
signal.base.default_args = *(**C.godot_variant)(unsafe.Pointer(&variantArray))
// Register the signal with Godot.
C.go_godot_nativescript_register_signal(
......@@ -572,7 +573,7 @@ func go_method_func(godotObject *C.godot_object, methodData unsafe.Pointer, user
// Append the variant to our list of variants
variantArgs = append(variantArgs, variant)
// Convert the pointer into a uintptr so we can perform artithmetic on it.
// Convert the pointer into a uintptr so we can perform arithmetic on it.
arrayPtr := uintptr(unsafe.Pointer(arg))
// Add the size of the godot_variant pointer to our array pointer to get the position
......
......@@ -110,7 +110,7 @@ func (c *Class) register() {
// finally iterate over any defined signal and register them
for _, signal := range c.signals {
signal.register()
signal.register(c.name)
}
}
......@@ -190,9 +190,9 @@ func NewGodotSignal(className, name string, args []SignalArgument, defaults []Va
}
// registers a Signal value with in Godot
func (s *GDSignal) register() {
func (s *GDSignal) register(name string) {
NativeScript.RegisterSignal(s.signalName, s.signal)
NativeScript.RegisterSignal(s.name, s.signal)
}
// NewGodotMethod creates a new ready to go Godot method for us and return it back
......@@ -245,7 +245,7 @@ func NewGodotProperty(className, name, hint, hintString, usage, rset string,
for key := range PropertyHintLookupMap {
allowed = append(allowed, strings.Replace(key, "PropertyHint", "", 1))
}
panic(fmt.Sprintf("unknown property hint %s(%s), allowed types: %s", hint, hintKey, strings.Join(allowed, ", ")))
panic(fmt.Sprintf("unknown property hint %q, allowed types: %s", hint, strings.Join(allowed, ", ")))
}
} else {
attributes.Hint = PropertyHintNone
......@@ -264,7 +264,7 @@ func NewGodotProperty(className, name, hint, hintString, usage, rset string,
for key := range PropertyUsageFlagsLookupMap {
allowed = append(allowed, strings.Replace(key, "PropertyUsage", "", 1))
}
panic(fmt.Sprintf("unknown property usage %s, allowed types: %s", usage, strings.Join(allowed, ", ")))
panic(fmt.Sprintf("unknown property usage %q, allowed types: %s", usage, strings.Join(allowed, ", ")))
}
} else {
attributes.Usage = PropertyUsageDefault
......@@ -283,7 +283,7 @@ func NewGodotProperty(className, name, hint, hintString, usage, rset string,
for key, _ := range MethodRpcModeLookupMap {
validTypes = fmt.Sprintf("%s %s", validTypes, strings.Replace(key, "MethodRpcMode", "", 1))
}
panic(fmt.Errorf("rset must be one of the allowed types %s", validTypes))
panic(fmt.Sprintf("unknown rset %q, allowed types: %s", rset, validTypes))
}
} else {
attributes.RsetType = MethodRpcModeDisabled
......
......@@ -337,7 +337,7 @@ func (rm *registryMethod) NewVariantType() string {
}
type registryProperty struct {
name, alias, kind, hint, hintString, usage, rset, setFunc, getFunc string
name, alias, kind, gdnativeKind, hint, hintString, usage, rset, setFunc, getFunc string
}
// Name returns the name of the property back
......@@ -345,7 +345,12 @@ func (rp *registryProperty) Name() string {
return rp.name
}
// Alias returns the porperty alias back
// Kind returns the property type back
func (rp *registryProperty) Kind() string {
return rp.gdnativeKind
}
// Alias returns the property alias back
func (rp *registryProperty) Alias() string {
return rp.alias
}
......@@ -370,6 +375,28 @@ func (rp *registryProperty) RsetType() string {
return rp.rset
}
// SetConvert writes right syntax for conversion from gdnative.Variant into Go type
func (rp *registryProperty) SetConvert() string {
switch rp.gdnativeKind {
case "gdnative.Int":
return "gdnative.Int(property.AsInt())"
default:
return fmt.Sprintf("property.As%s()", strings.Replace(rp.gdnativeKind, "gdnative.", "", -1))
}
}
// GetConvert writes right syntax for conversion from Go type into gdnative.Variant
func (rp *registryProperty) GetConvert() string {
switch rp.gdnativeKind {
case "gdnative.Int":
return fmt.Sprintf("gdnative.NewVariant%s(gdnative.Int64T(class.class.%s))", rp.gdnativeKind[9:], rp.name)
default:
return fmt.Sprintf("gdnative.NewVariant%s(class.class.%s)", rp.gdnativeKind[9:], rp.name)
}
}
// SetFunc returns this property set function or default one
func (rp *registryProperty) SetFunc(class, instance string) string {
if rp.setFunc == "" {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment