Skip to content

Commit 1790059

Browse files
committed
Make @export work with GArray and Variant values, fixes #410
1 parent ebc26d0 commit 1790059

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

Sources/SwiftGodotMacroLibrary/MacroExport.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public struct GodotExport: PeerMacro {
3939
let name = "_mproxy_set_\(varName)"
4040
var body: String = ""
4141

42-
if godotVariants [typeName] == nil {
42+
if typeName == "Variant" {
43+
body = "\(varName) = args [0]"
44+
} else if godotVariants [typeName] == nil {
4345
let optBody = isOptional ? " else { \(varName) = nil }" : ""
4446

4547
// The use of the local function dynamicCast here is such that the compiler

Sources/SwiftGodotMacroLibrary/MacroGodot.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class GodotMacroProcessor {
107107
godotArrayElementTypeName = gArrayCollectionElementTypeName
108108
}
109109

110-
propType = godotTypeToProp (typeName: "Array")
110+
propType = godotTypeToProp (typeName: "GArray")
111111
className = "Array[\(godotArrayElementTypeName)]"
112112
hintStr = godotArrayElementTypeName
113113
} else {
@@ -375,7 +375,7 @@ class GodotMacroProcessor {
375375
ctor.append (
376376
"""
377377
let \(pinfo) = PropInfo (
378-
propertyType: \(godotTypeToProp(typeName: "Array")),
378+
propertyType: \(godotTypeToProp(typeName: "GArray")),
379379
propertyName: "\(varNameWithPrefix.camelCaseToSnakeCase())",
380380
className: StringName("\(godotArrayTypeName)"),
381381
hint: .arrayType,

Sources/SwiftGodotMacroLibrary/MacroSharedApi.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func getIdentifier (_ typeSyntax: TypeSyntax?) -> (typeName: String, generics: [
3131
return (typeName: identifier.name.text, generics: genericTypeNames, isOptional: opt)
3232
} else if let array = typeSyntax.as(ArrayTypeSyntax.self),
3333
let elementTypeName = array.element.as(IdentifierTypeSyntax.self)?.name.text {
34-
return (typeName: "Array", generics: [elementTypeName], isOptional: opt)
34+
return (typeName: "GArray", generics: [elementTypeName], isOptional: opt)
3535
}
3636
return nil
3737
}
@@ -146,7 +146,7 @@ func getTypeName (_ parameter: FunctionParameterSyntax) -> String? {
146146
parameter.isObjectCollection,
147147
parameter.isVariantCollection
148148
].allSatisfy ({ $0 == false }) else {
149-
return "Array"
149+
return "GArray"
150150
}
151151
guard let typeName = parameter.type.as (IdentifierTypeSyntax.self)?.name.text else {
152152
return nil
@@ -164,7 +164,7 @@ var godotVariants = [
164164
"Double": ".float",
165165
"Bool": ".bool",
166166
"AABB": ".aabb",
167-
"Array": ".array",
167+
"GArray": ".array",
168168
"Basis": ".basis",
169169
"Callable": ".callable",
170170
"Color": ".color",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// This file is solely here to ensure that we can compile the resulting macros,
3+
// which is not covered by the macro output generation.
4+
//
5+
//
6+
// Created by Miguel de Icaza on 2/27/24.
7+
//
8+
9+
import Foundation
10+
import SwiftGodot
11+
12+
@Godot
13+
class Demo: Object {
14+
@Export var demo: GArray = GArray()
15+
}
16+
17+
@Godot
18+
class Demo2: Object {
19+
@Export var demo: Variant = Variant()
20+
}
21+

Tests/SwiftGodotMacrosTests/MacroGodotExportCollectionTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,62 @@ func _mproxy_set_greetings(args: [Variant]) -> Variant? {
211211
)
212212
}
213213

214+
func testExportGArray() {
215+
assertMacroExpansion(
216+
"""
217+
@Godot
218+
class SomeNode: Node {
219+
@Export var someArray: GArray = GArray()
220+
}
221+
""",
222+
expandedSource:
223+
"""
224+
225+
class SomeNode: Node {
226+
var someArray: GArray = GArray()
227+
228+
func _mproxy_set_someArray (args: [Variant]) -> Variant? {
229+
guard let arg = args.first else {
230+
return nil
231+
}
232+
if let value = GArray (arg) {
233+
self.someArray = value
234+
} else {
235+
GD.printErr ("Unable to set `someArray` value: ", arg)
236+
}
237+
return nil
238+
}
239+
240+
func _mproxy_get_someArray (args: [Variant]) -> Variant? {
241+
return Variant (someArray)
242+
}
243+
244+
override open class var classInitializer: Void {
245+
let _ = super.classInitializer
246+
return _initializeClass
247+
}
248+
249+
private static var _initializeClass: Void = {
250+
let className = StringName("SomeNode")
251+
assert(ClassDB.classExists(class: className))
252+
let classInfo = ClassInfo<SomeNode> (name: className)
253+
let _psomeArray = PropInfo (
254+
propertyType: .array,
255+
propertyName: "someArray",
256+
className: className,
257+
hint: .none,
258+
hintStr: "",
259+
usage: .default)
260+
classInfo.registerMethod (name: "_mproxy_get_someArray", flags: .default, returnValue: _psomeArray, arguments: [], function: SomeNode._mproxy_get_someArray)
261+
classInfo.registerMethod (name: "_mproxy_set_someArray", flags: .default, returnValue: nil, arguments: [_psomeArray], function: SomeNode._mproxy_set_someArray)
262+
classInfo.registerProperty (_psomeArray, getter: "_mproxy_get_someArray", setter: "_mproxy_set_someArray")
263+
} ()
264+
}
265+
""",
266+
macros: testMacros
267+
)
268+
}
269+
214270
func testExportArrayIntGodotMacro() {
215271
assertMacroExpansion(
216272
"""

0 commit comments

Comments
 (0)