Skip to content

Commit

Permalink
Merge pull request #176 from tishin/fix-struct-members
Browse files Browse the repository at this point in the history
Limit struct stored property generation to members with offsets
  • Loading branch information
migueldeicaza authored Oct 16, 2023
2 parents 20bcd95 + 98d33c0 commit 587eab5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
48 changes: 15 additions & 33 deletions Generator/Generator/BuiltinGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,8 @@ func generateBuiltinCtors (_ p: Printer,

// We need to initialize some variables before we call
if let members {
if bc.name == "Color" {
p ("self.red = 0")
p ("self.green = 0")
p ("self.blue = 0")
p ("self.alpha = 0")
} else if bc.name == "Plane" {
p ("self.plane = Vector3 ()")
p ("self.d = 0")
} else {
for x in members {
p ("self.\(x.name) = \(MemberBuiltinJsonTypeToSwift(x.type)) ()")
}
for x in members {
p ("self.\(x.name) = \(MemberBuiltinJsonTypeToSwift(x.type)) ()")
}
// Another special case: empty constructors in generated structs (those we added fields for)
// we just keep the manual initialization and do not call the constructor
Expand Down Expand Up @@ -598,33 +588,25 @@ func generateBuiltinClasses (values: [JGodotBuiltinClass], outputDir: String?) a
}
}
}
if let members = bc.members {
if bc.name == "Color" {
memberDoc ("red")
p ("public var red: Float")
memberDoc ("green")
p ("public var green: Float")
memberDoc ("blue")
p ("public var blue: Float")
memberDoc ("alpha")
p ("public var alpha: Float")
} else if bc.name == "Plane" {
memberDoc ("plane")
p ("public var plane: Vector3")
memberDoc ("d")
p ("public var d: Float")
} else {
for x in members {
memberDoc (x.name)
p ("public var \(x.name): \(MemberBuiltinJsonTypeToSwift (x.type))")
}
let storedMembers: [JGodotArgument]?
if kind == .isStruct, let memberOffsets = builtinMemberOffsets [bc.name] {
storedMembers = memberOffsets.compactMap({ m in
return bc.members?.first(where: { $0.name == m.member })
})
} else {
storedMembers = bc.members
}
if let members = storedMembers {
for x in members {
memberDoc (x.name)
p ("public var \(x.name): \(MemberBuiltinJsonTypeToSwift (x.type))")
}
}

if let enums = bc.enums {
generateEnums(p, cdef: bc, values: enums, constantDocs: docClass?.constants?.constant, prefix: bc.name + ".")
}
generateBuiltinCtors (p, bc, docClass, bc.constructors, godotTypeName: bc.name, typeName: typeName, typeEnum: typeEnum, members: bc.members)
generateBuiltinCtors (p, bc, docClass, bc.constructors, godotTypeName: bc.name, typeName: typeName, typeEnum: typeEnum, members: storedMembers)

generateBuiltinMethods(p, bc, docClass, bc.methods ?? [], typeName, typeEnum, isStruct: kind == .isStruct)
generateBuiltinOperators (p, bc, docClass, typeName: typeName)
Expand Down
11 changes: 10 additions & 1 deletion Generator/Generator/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ for x in ["Float", "Int", "float", "int", "Int32", "Bool", "bool"] {
isStructMap [x] = true
}

let buildConfiguration: String = "float_64"
var builtinSizes: [String: Int] = [:]
for cs in jsonApi.builtinClassSizes {
if cs.buildConfiguration == "float_64" {
if cs.buildConfiguration == buildConfiguration {
for c in cs.sizes {
builtinSizes [c.name] = c.size
}
}
}
var builtinMemberOffsets: [String: [JGodotMember]] = [:]
for mo in jsonApi.builtinClassMemberOffsets {
if mo.buildConfiguration == buildConfiguration {
for c in mo.classes {
builtinMemberOffsets [c.name.rawValue] = c.members
}
}
}

let generatedBuiltinDir: String? = singleFile ? nil : (outputDir + "/generated-builtin/")
let generatedDir: String? = singleFile ? nil : (outputDir + "/generated/")
Expand Down
37 changes: 37 additions & 0 deletions Sources/SwiftGodot/Extensions/Endable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Endable.swift
//
//
// Created by Mikhail Tishin on 15.10.2023.
//

public protocol Endable {

associatedtype VectorType: AdditiveArithmetic

var position: VectorType { get }
var size: VectorType { get set }

}

public extension Endable {

var end: VectorType {
set {
size = newValue - position
}
get {
return position + size
}
}

}

extension Vector2: AdditiveArithmetic {}
extension Vector2i: AdditiveArithmetic {}
extension Vector3: AdditiveArithmetic {}
extension Vector3i: AdditiveArithmetic {}

extension AABB: Endable {}
extension Rect2: Endable {}
extension Rect2i: Endable {}

0 comments on commit 587eab5

Please sign in to comment.