Verified Commit 552d344e authored by Oscar Campos's avatar Oscar Campos
Browse files

fix: fixed errors and added more examples

parent 7761e89e
......@@ -43,14 +43,33 @@ func (cmd *listCmd) Run(ctx *context) error {
}
fmt.Printf("Found Structure: %s%s\n", key, base)
for _, property := range data.GetProperties() {
properties := data.GetProperties()
if len(properties) > 0 {
fmt.Printf("\tProperties:\n")
}
for _, property := range properties {
fmt.Printf("\t\t%s\n", property)
}
fmt.Printf("\tConstructor: %s\n", data.GetConstructor())
fmt.Printf("\tDestructor: %s\n", data.GetDestructor())
for _, method := range data.GetMethods() {
fmt.Printf("\t%s\n", method)
fmt.Printf("\tConstructor:\n\t\t%s\n", data.GetConstructor())
fmt.Printf("\tDestructor:\n\t\t%s\n", data.GetDestructor())
methods := data.GetMethods()
if len(methods) > 0 {
fmt.Printf("\tMethods:\n")
}
for _, method := range methods {
fmt.Printf("\t\t%s\n", method)
}
signals := data.Signals()
if len(signals) > 0 {
fmt.Printf("\tSignals:\n")
}
for _, signal := range data.Signals() {
fmt.Printf("\t\tsignal %s", signal.Name())
}
fmt.Println()
}
......
{{ $view := . -}}
/*------------------------------------------------------------------------------
// This code was generated by a tool.
// Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
// Copyright © 2017 - William Edwards
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. Any updates should be done in
// "gdnative.c.tmpl" so they can be included in the generated
// code.
//----------------------------------------------------------------------------*/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "gdnative.gen.h"
#include <gdnative/aabb.h>
#include <gdnative/array.h>
......@@ -29,6 +36,11 @@
#include <gdnative/vector3.h>
#include <gdnative_api_struct.gen.h>
/*------------------------------------------------------------------------------
// This file was autogenerated by PimPam GDNative-Go binding tools
// Please do not modify this file, any change will be lost
//----------------------------------------------------------------------------*/
{{/* Define the Core API */}}
/* GDNative {{ $view.API.Type }} {{ $view.API.Version.Major }}.{{ $view.API.Version.Minor }} */
{{ range $i, $api := $view.API.API -}}
......
{{ $view := . -}}
// Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
// Copyright © 2017 - William Edwards
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CGDNATIVE_{{ $view.API.Type }}_H
#define CGDNATIVE_{{ $view.API.Type }}_H
/*------------------------------------------------------------------------------
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. Any updates should be done in
// "gdnative.h.tmpl" so they can be included in the generated
// code.
//----------------------------------------------------------------------------*/
#include <gdnative/aabb.h>
#include <gdnative/array.h>
#include <gdnative/basis.h>
......@@ -30,6 +38,10 @@
#include <gdnative/vector3.h>
#include <gdnative_api_struct.gen.h>
/*------------------------------------------------------------------------------
// This file was autogenerated by PimPam GDNative-Go binding tools
// Please do not modify this file, any change will be lost
//----------------------------------------------------------------------------*/
{{/* Define the Core API */}}
/* GDNative {{ $view.API.Type }} {{ $view.API.Version.Major }}.{{ $view.API.Version.Minor }} */
......
{{ $data := . -}}
// Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
// Copyright © 2017 - William Edwards
//
......@@ -15,16 +14,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
{{/* create wrapper boilerplate */}}
package {{ $data.Package }}
// ==================================================================
// This file was autogenerated by PimPam GDNative-Go binding tools
// Please do not modify this file, any change will be lost
// ==================================================================
{{/* create wrapper boilerplate */}}
package {{ $data.Package }}
import (
"fmt"
"reflect"
"gitlab.com/pimpam-games-studio/gdnative-go/gdnative"
)
......@@ -35,9 +35,21 @@ type {{ $className }}Wrapper struct {
class *{{ $className }}
}
// LookupInstance implements the gdnative.InstanceRegistry interface for {{ $className }}Wrapper
func (w *{{ $className }}Wrapper) LookupInstance(instance string) *{{ $className }}Wrapper {
wrapper, ok := {{ $className }}Instances[instance]
if !ok {
return nil
}
return wrapper
}
// {{ $className }}Instances is an internal registry of instances for our custom types
var {{ $className }}Instances = map[string]*{{ $className }}Wrapper{}
{{ if $class.Methods -}}
// handle{{ $className }} handles calls from Godot to this instance methods
func handle{{ $className }}(object gdnative.Object, methodData, userData string, numArgs int, args []gdnative.Variant) gdnative.Variant {
......@@ -51,7 +63,11 @@ func handle{{ $className }}(object gdnative.Object, methodData, userData string,
// find the right method and execute it or return an empty nil value and log it
switch methodData {
{{ range $i, $method := $class.Methods -}}
{{ if $method.Alias -}}
case "{{ $method.Alias }}":
{{ else -}}
case "{{ $method.GodotName }}":
{{ end -}}
{{ range $i, $arg := $method.Arguments -}}
{{ $arg.Name }} := args[{{ $i }}].{{ $arg.ConvertFunction }}
{{ end -}}
......@@ -70,13 +86,14 @@ func handle{{ $className }}(object gdnative.Object, methodData, userData string,
gdnative.Log.Warning(fmt.Sprintf("could not find method %s on instance %s", methodData, userData))
return gdnative.NewVariantNil()
}
{{ end -}}
// nativeScriptInit{{ $className }} will run upon NativeScript initialization and its
// responsible for registering all our classes within Godot
func nativeScriptInit{{ $className }}() {
// define an instance creation function, it will be called by Godot
constructor := gdnative.CreateConstructor("{{ $className }}", func(object gdnative.Object, methodData string) string {
constructor := gdnative.CreateConstructor("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", func(object gdnative.Object, methodData string) string {
// create a new value of this wrapper type
{{ if $class.HasConstructor -}}
instance := {{ $className }}Wrapper{
......@@ -97,7 +114,7 @@ func nativeScriptInit{{ $className }}() {
})
// define an instance destruction function, it will be called by Godot
destructor := gdnative.CreateDestructor("{{ $className }}", func(object gdnative.Object, methodData, userData string) {
destructor := gdnative.CreateDestructor("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", func(object gdnative.Object, methodData, userData string) {
{{ if $class.HasDestructor -}}
{{ $class.Destructor }}()
{{ end -}}
......@@ -107,19 +124,76 @@ func nativeScriptInit{{ $className }}() {
// define methods attached to the instance
methods := []gdnative.Method{
{{ range $methodName, $method := $class.Methods -}}
gdnative.NewGodotMethod("{{ $className }}", "{{ $method.GodotName }}", handle{{ $className }}),
gdnative.NewGodotMethod("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", "{{ if not $method.Alias }}{{ $method.GodotName }}{{ else }}{{ $method.Alias }}{{ end }}", handle{{ $className }}),
{{ end -}}
}
// define properties attached to the instance
properties := []gdnative.Property{
{{ range $propertyName, $property := $class.GetProperties -}}
gdnative.NewGodotProperty("{{ $className }}", {{ $property.Name }}, $property.Hint, $property.HintString, $property.Usage, $property.RsetType, $property.SetFunc, $property.GetFunc),
{{ 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 -}}
{{ 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 gdnative.GoTypeToVariant(propertyField)
}
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{
{{ range $i, $signal := $class.Signals -}}
{{ if $class.Alias -}}
gdnative.NewGodotSignal("{{ $class.Alias }}", {{ $signal.Name }}, {{ $signal.Args }}, {{ $signal.Defaults }}),
{{ else -}}
gdnative.NewGodotSignal("{{ $className }}", {{ $signal.Name }}, {{ $signal.Args }}, {{ $signal.Defaults }}),
{{ end -}}
{{ end -}}
}
// register a new class within Godot
gdnative.RegisterNewGodotClass(false, "{{ $className }}", "{{ $class.GetBase }}", &constructor, &destructor, methods, properties)
gdnative.RegisterNewGodotClass(false, "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", "{{ if $class.GetBase }}{{ $class.GetBase }}{{ else }}{{ "Reference" }}{{ end }}", &constructor, &destructor, methods, properties, signals)
}
{{ end -}}{{/* range $className, $class := $data.Classes */ -}}
......
{{ $view := . -}}
// Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
// Copyright © 2017 - William Edwards
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gdnative
/*------------------------------------------------------------------------------
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated. Any updates should be done in
// "types.go.tmpl" so they can be included in the generated
// code.
//----------------------------------------------------------------------------*/
// ==================================================================
// This file was autogenerated by PimPam GDNative-Go binding tools
// Please do not modify this file, any change will be lost
// ==================================================================
/*
#include "gdnative.gen.h"
......@@ -45,7 +55,7 @@ import "C"
{{/* Handle struct definitions */}}
{{ if or (eq $typedef.Base "struct") (eq $typedef.Base "void") }}
// NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
// initialized {{ $typedef.GoName }}. This is primarily used in
// initialized {{ $typedef.GoName }}. This is primarily used in
// conjunction with MethodBindPtrCall.
func NewEmpty{{ $typedef.GoName }}() Pointer {
var obj C.{{ $typedef.Name }}
......@@ -58,7 +68,7 @@ import "C"
return Pointer{base: unsafe.Pointer(obj.getBase())}
}
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
{{/* String structs should be handled differently -*/}}
......@@ -82,7 +92,7 @@ import "C"
{{/* The String struct is a special case to be compatible with Go strings -*/}}
{{ if (eq $typedef.GoName "String") }}
type {{ $typedef.GoName }} string
type {{ $typedef.GoName }} string
func (s String) getBase() *C.godot_string {
return stringAsGodotString(string(s))
......@@ -200,11 +210,11 @@ import "C"
{{ if eq $typedef.Base "enum" }}
// {{ $typedef.GoName }} is a Go wrapper for the C.{{ $typedef.Name }} enum type.
type {{ $typedef.GoName }} int
func (e {{ $typedef.GoName }}) getBase() C.{{ $typedef.Name }} {
return C.{{ $typedef.Name }}(e)
}
const (
{{ range $j, $propdef := $typedef.Properties -}}
{{ if (ne $propdef.GoName "") -}}
......@@ -221,7 +231,7 @@ import "C"
{{ end -}}
{{ end -}}
}
{{ end }}
{{/* Handle base type definitions */}}
......@@ -229,7 +239,7 @@ import "C"
{{/* This segment handles godot_pool_*_access type definitions */}}
{{ if ($view.IsGodotBaseType $typedef) }}
// NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
// initialized {{ $typedef.GoName }}. This is primarily used in
// initialized {{ $typedef.GoName }}. This is primarily used in
// conjunction with MethodBindPtrCall.
func NewEmpty{{ $typedef.GoName }}() Pointer {
var obj C.{{ $typedef.Name }}
......@@ -241,14 +251,14 @@ import "C"
func NewPointerFrom{{ $typedef.GoName }}(obj {{ $typedef.GoName }}) Pointer {
return Pointer{base: unsafe.Pointer(obj.getBase())}
}
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
obj := (*C.{{ $typedef.Name }})(ptr.getBase())
return {{ $typedef.GoName }}{base: obj}
}
type {{ $typedef.GoName }} struct {
base *C.{{ $typedef.Name }}
{{/* Handle struct properties */}}
......@@ -258,14 +268,14 @@ import "C"
{{ end -}}
{{ end -}}
}
func (gdt {{ $typedef.GoName }}) getBase() *C.{{ $typedef.Name }} {
return gdt.base
}
{{/* This segment handles base types like godot_bool */}}
{{ else }}
// NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
// initialized {{ $typedef.GoName }}. This is primarily used in
// initialized {{ $typedef.GoName }}. This is primarily used in
// conjunction with MethodBindPtrCall.
func NewEmpty{{ $typedef.GoName }}() Pointer {
var obj C.{{ $typedef.Name }}
......@@ -279,7 +289,7 @@ import "C"
return Pointer{base: unsafe.Pointer(&base)}
}
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
// given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
base := ptr.getBase()
......
all: build
all: generate build
generate:
/home/damnwidget/development/pimpam/engines/godot/gdnative-go/gdnativego generate --path="./src"
build:
go build -v -buildmode=c-shared -o ./bin/libsimple.so ./src/*.go
clean:
go clean
rm ./bin/libsimple.so
rm ./src/*_registrable.gen.go
.PHONY: all
package main
import (
"fmt"
"gitlab.com/pimpam-games-studio/gdnative-go/gdnative"
)
type Pepe struct{}
// SimpleClass is a structure that we can register with Godot.
//godot::register as SIMPLE
type SimpleClass struct {
base gdnative.Object
Hit gdnative.Signal
HP gdnative.Int `hint:"range" hint_string:"The player's Hit Points" usage:"Default"`
Mana, Blood gdnative.Int `hint:"range" hint_string:"The player points to cast spells"`
IgnoreMe gdnative.Float `-` // this property will be ignored
}
// Instances is a map of our created Godot classes. This will be populated when
// Godot calls the CreateFunc.
var Instances = map[string]*SimpleClass{}
// NativeScriptInit will run on NativeScript initialization. It is responsible
// for registering all our classes with Godot.
func nativeScriptInit() {
gdnative.Log.Warning("Initializing nativescript from Go!")
// Define an instance creation function. This will be called when Godot
// creates a new instance of our class.
createFunc := gdnative.InstanceCreateFunc{
CreateFunc: simpleConstructor,
MethodData: "SIMPLE",
FreeFunc: func(methodData string) {},
}
// Define an instance destroy function. This will be called when Godot
// asks our library to destroy our class instance.
destroyFunc := gdnative.InstanceDestroyFunc{
DestroyFunc: simpleDestructor,
MethodData: "SIMPLE",
FreeFunc: func(methodData string) {},
}
// Register our class with Godot.
gdnative.Log.Warning("Registering SIMPLE class...")
gdnative.NativeScript.RegisterClass(
"SIMPLE",
"Reference",
&createFunc,
&destroyFunc,
)
// Register a method with Godot.
gdnative.Log.Warning("Registering SIMPLE method...")
gdnative.NativeScript.RegisterMethod(
"SIMPLE",
"get_data",
&gdnative.MethodAttributes{
RPCType: gdnative.MethodRpcModeDisabled,
},
&gdnative.InstanceMethod{
Method: simpleMethod,
MethodData: "SIMPLE",
FreeFunc: func(methodData string) {},
// New creates a new SimpleClass value and returns a pointer to it
//godot::constructor(SimpleClass)
func New() *SimpleClass {
sc := SimpleClass{
// Signals must be defined as literals or they will be ignored by the gdnativego compiler
Hit: gdnative.Signal{
Name: "hit",
NumArgs: gdnative.Int(1),
NumDefaultArgs: gdnative.Int(1),
Args: []gdnative.SignalArgument{
{
Name: gdnative.String("power"),
Type: gdnative.Int(gdnative.VariantTypeInt),
Hint: gdnative.PropertyHintRange,
HintString: "Hit power value",
Usage: gdnative.PropertyUsageDefault,
DefaultValue: gdnative.NewVariantInt(gdnative.Int64T(0)),
},
},
DefaultArgs: []gdnative.Variant{
gdnative.NewVariantInt(gdnative.Int64T(0)),
},
},
)
}
func simpleConstructor(object gdnative.Object, methodData string) string {
gdnative.Log.Println("Creating new SimpleClass...")
// Create a new instance of our struct.
instance := &SimpleClass{
base: object,
}
// Use the pointer address as the instance ID
instanceID := fmt.Sprintf("%p", instance)
Instances[instanceID] = instance
// Return the instanceID
return instanceID
return &sc
}
func simpleDestructor(object gdnative.Object, methodData, userData string) {
gdnative.Log.Println("Destroying SimpleClass with ID:", userData, "...")
// Delete the instance from our map of instances
delete(Instances, userData)
}
// GetData is automatically registered to SimpleClass on Godot
//godot::export as get_data
func (sc *SimpleClass) GetData() gdnative.Variant {
func simpleMethod(object gdnative.Object, methodData, userData string, numArgs int, args []gdnative.Variant) gdnative.Variant {
gdnative.Log.Println("SIMPLE.get_data() called!")
data := gdnative.NewStringWithWideString("World from godot-go from instance: " + object.ID() + "!")
ret := gdnative.NewVariantWithString(data)
return ret
}
// The "init()" function is a special Go function that will be called when this library
// is initialized. Here we can register our Godot classes.
func init() {
// Set the initialization script that will run upon NativeScript initialization.
// This function will handle using the NativeScript API to register all of our
// classes.
gdnative.SetNativeScriptInit(nativeScriptInit)
data := gdnative.NewStringWithWideString("Hello World from gdnative-go instance!")
return gdnative.NewVariantWithString(data)
}
// This never gets called, but it necessary to export as a shared library.
......