gdnative_wrapper.go.tmpl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {{ $data := . -}}
  2. // Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
  3. // Copyright © 2017 - William Edwards
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. {{/* create wrapper boilerplate */}}
  17. package {{ $data.Package }}
  18. // ==================================================================
  19. // This file was autogenerated by PimPam GDNative-Go binding tools
  20. // Please do not modify this file, any change will be lost
  21. // ==================================================================
  22. import (
  23. "fmt"
  24. "git.alfi.li/gamelang/gdnative-go/gdnative"
  25. )
  26. // used for FreeFunc
  27. var emptyFreeFunc = func(_ string) {}
  28. {{ range $className, $class := $data.Classes -}}
  29. // {{ $className }}Wrapper is a wrapper over {{ $className }} that will register it with in godot
  30. type {{ $className }}Wrapper struct {
  31. class *{{ $className }}
  32. }
  33. // LookupInstance implements the gdnative.InstanceRegistry interface for {{ $className }}Wrapper
  34. func (w *{{ $className }}Wrapper) LookupInstance(instance string) *{{ $className }}Wrapper {
  35. wrapper, ok := {{ $className }}Instances[instance]
  36. if !ok {
  37. return nil
  38. }
  39. return wrapper
  40. }
  41. // {{ $className }}Instances is an internal registry of instances for our custom types
  42. var {{ $className }}Instances = map[string]*{{ $className }}Wrapper{}
  43. {{ if $class.Methods -}}
  44. // handle{{ $className }} handles calls from Godot to this instance methods
  45. func handle{{ $className }}(object gdnative.Object, methodData, userData string, numArgs int, args []gdnative.Variant) gdnative.Variant {
  46. // lookup instance on registry, if it does not exists return nil
  47. instance, ok := {{ $className }}Instances[userData]
  48. if !ok {
  49. gdnative.Log.Warning(fmt.Sprintf("could not find instance %s on registry", userData))
  50. return gdnative.NewVariantNil()
  51. }
  52. // find the right method and execute it or return an empty nil value and log it
  53. switch methodData {
  54. {{ range $i, $method := $class.Methods -}}
  55. {{ if $method.Alias -}}
  56. case "{{ $method.Alias }}":
  57. {{ else -}}
  58. case "{{ $method.GodotName }}":
  59. {{ end -}}
  60. {{ range $i, $arg := $method.Arguments -}}
  61. {{ $arg.Name }} := args[{{ $i }}].{{ $arg.ConvertFunction }}
  62. {{ end -}}
  63. {{ if $method.HasReturns -}}
  64. value := instance.class.{{ $method.FunctionCallWithParams }}
  65. return {{ $method.NewVariantType }}
  66. {{ else -}}
  67. instance.class.{{ $method.FunctionCallWithParams }}
  68. return gdnative.NewVariantNil()
  69. {{ end -}}
  70. {{ end -}}
  71. }
  72. // if we are here it means the method being called is unknown to us
  73. gdnative.Log.Warning(fmt.Sprintf("could not find method %s on instance %s", methodData, userData))
  74. return gdnative.NewVariantNil()
  75. }
  76. {{ end -}}
  77. // nativeScriptInit{{ $className }} will run upon NativeScript initialization and its
  78. // responsible for registering all our classes within Godot
  79. func nativeScriptInit{{ $className }}() {
  80. // define an instance creation function, it will be called by Godot
  81. constructor := gdnative.CreateConstructor("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", func(object gdnative.Object, methodData string) string {
  82. // create a new value of this wrapper type
  83. {{ if $class.HasConstructor -}}
  84. instance := {{ $className }}Wrapper{
  85. class: {{ $class.Constructor }}(),
  86. }
  87. {{ else -}}
  88. instance := {{ $className }}Wrapper{
  89. class: ${{ $className }}{},
  90. }
  91. {{ end -}}
  92. // use the pointer address as instance ID
  93. instanceID := fmt.Sprintf("{{ $className }}Wrapper_%p", &instance)
  94. {{ $className }}Instances[instanceID] = &instance
  95. // return the instance ID to Godot
  96. return instanceID
  97. })
  98. // define an instance destruction function, it will be called by Godot
  99. destructor := gdnative.CreateDestructor("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", func(object gdnative.Object, methodData, userData string) {
  100. {{ if $class.HasDestructor -}}
  101. {{ $class.Destructor }}()
  102. {{ end -}}
  103. delete({{ $className }}Instances, userData)
  104. })
  105. // define methods attached to the instance
  106. methods := []gdnative.Method{
  107. {{ range $methodName, $method := $class.Methods -}}
  108. gdnative.NewGodotMethod("{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", "{{ if not $method.Alias }}{{ $method.GodotName }}{{ else }}{{ $method.Alias }}{{ end }}", handle{{ $className }}),
  109. {{ end -}}
  110. }
  111. // define properties attached to the instance
  112. properties := []gdnative.Property{
  113. {{ range $i, $property := $class.Properties -}}
  114. gdnative.NewGodotProperty(
  115. "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}",
  116. "{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
  117. "{{ $property.Hint }}", "{{ $property.HintString }}",
  118. "{{ $property.Usage }}", "{{ $property.RsetType }}",
  119. &gdnative.InstancePropertySet{
  120. SetFunc: func(object gdnative.Object, classProperty, instanceString string, property gdnative.Variant) {
  121. class, ok := SimpleClassInstances[instanceString]
  122. if !ok {
  123. panic(fmt.Sprintf("Set property %s does not exists on instance %s registry", classProperty, instanceString))
  124. }
  125. class.class.{{ $property.Name }} = {{ $property.SetConvert }}
  126. },
  127. MethodData: "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}::{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
  128. FreeFunc: emptyFreeFunc,
  129. },
  130. &gdnative.InstancePropertyGet{
  131. GetFunc: func(object gdnative.Object, classProperty, instanceString string) gdnative.Variant {
  132. class, ok := SimpleClassInstances[instanceString]
  133. if !ok {
  134. panic(fmt.Sprintf("Get property %q does not exists on instance %q registry", classProperty, instanceString))
  135. }
  136. return {{ $property.GetConvert }}
  137. },
  138. MethodData: "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}::{{ if $property.Alias }}{{ $property.Alias }}{{ else }}{{ $property.Name }}{{ end }}",
  139. FreeFunc: emptyFreeFunc,
  140. },
  141. ),
  142. {{ end -}}
  143. }
  144. // signals attached to the instance
  145. signals := []gdnative.GDSignal{
  146. {{ range $i, $signal := $class.Signals -}}
  147. {{ if $class.Alias -}}
  148. gdnative.NewGodotSignal("{{ $class.Alias }}", {{ $signal.Name }}, {{ $signal.Args }}, {{ $signal.Defaults }}),
  149. {{ else -}}
  150. gdnative.NewGodotSignal("{{ $className }}", {{ $signal.Name }}, {{ $signal.Args }}, {{ $signal.Defaults }}),
  151. {{ end -}}
  152. {{ end -}}
  153. }
  154. // register a new class within Godot
  155. gdnative.RegisterNewGodotClass(false, "{{ if $class.Alias }}{{ $class.Alias }}{{ else }}{{ $className }}{{ end }}", "{{ if $class.GetBase }}{{ $class.GetBase }}{{ else }}{{ "Reference" }}{{ end }}", &constructor, &destructor, methods, properties, signals)
  156. }
  157. {{ end -}}{{/* range $className, $class := $data.Classes */ -}}
  158. // The "init()" function is a special Go function that will be called when this library
  159. // is initialized. Here we can register our Godot classes.
  160. func init() {
  161. {{ $data.GDNativeInit }}
  162. }