From d4ab8a5524b767610e4b87787386af7ada801911 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Fri, 12 Jan 2018 23:22:50 +0700 Subject: [PATCH] Update after recent GDNative API changes --- godot.nimble | 2 +- godot/gdnativeapi.nim | 51 +++++++++++++++++++-------- godot/godotapigen.nim | 26 ++++++++------ godot/internal/godotinternaltypes.nim | 3 ++ godot/internal/godotstrings.nim | 20 ++++------- 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/godot.nimble b/godot.nimble index 8415020..dd4f14d 100644 --- a/godot.nimble +++ b/godot.nimble @@ -1,4 +1,4 @@ -version = "0.7.1" +version = "0.7.2" author = "Xored Software, Inc." description = "Godot Engine bindings" license = "MIT" diff --git a/godot/gdnativeapi.nim b/godot/gdnativeapi.nim index c6282ae..3028b96 100644 --- a/godot/gdnativeapi.nim +++ b/godot/gdnativeapi.nim @@ -1265,18 +1265,20 @@ type {.noconv, raises: [], gcsafe, tags: [], locks: 0.} # String API + charStringLength: proc (self: GodotCharString): cint + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + charStringGetData: proc (self: GodotCharString): cstring + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + charStringDestroy: proc (self: var GodotCharString) + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringNew: proc (dest: var GodotString) {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringNewCopy: proc (dest: var GodotString, src: GodotString) {.noconv, raises: [], gcsafe, tags: [], locks: 0.} - stringNewData: proc (dest: var GodotString, contents: cstring, size: cint) - {.noconv, raises: [], gcsafe, tags: [], locks: 0.} - stringNewUTF16Data: pointer - stringGetData: proc (self: GodotString, dest: cstring, size: var cint) - {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringNewWithWideString: pointer stringOperatorIndex: pointer stringOperatorIndexConst: pointer - stringUTF16Str: pointer + stringWideStr: pointer stringOperatorEqual: proc (self, other: GodotString): bool {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringOperatorLess: proc (self, other: GodotString): bool @@ -1285,6 +1287,9 @@ type {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringLength: proc (self: GodotString): cint {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringCasecmpTo: pointer + stringNocasecmpTo: pointer + stringNaturalnocasecmpTo: pointer stringBeginsWith: pointer stringBeginsWithCharArray: pointer stringBigrams: pointer @@ -1376,11 +1381,14 @@ type stringErase: pointer stringAscii: pointer stringAsciiExtended: pointer - stringUtf8: pointer + stringUtf8: proc (self: GodotString): GodotCharString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringParseUtf8: pointer stringParseUtf8WithLen: pointer - stringCharsToUtf8: pointer - stringCharsToUtf8WithLen: pointer + stringCharsToUtf8: proc (str: cstring): GodotString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringCharsToUtf8WithLen: proc (str: cstring, len: cint): GodotString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringHash: pointer stringHash64: pointer stringHashChars: pointer @@ -2541,14 +2549,16 @@ type {.noconv, raises: [], gcsafe, tags: [], locks: 0.} # String API + charStringLength*: proc (self: GodotCharString): cint + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + charStringGetData*: proc (self: GodotCharString): cstring + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + charStringDestroy*: proc (self: var GodotCharString) + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringNew*: proc (dest: var GodotString) {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringNewCopy*: proc (dest: var GodotString, src: GodotString) {.noconv, raises: [], gcsafe, tags: [], locks: 0.} - stringNewData*: proc (dest: var GodotString, contents: cstring, size: cint) - {.noconv, raises: [], gcsafe, tags: [], locks: 0.} - stringGetData*: proc (self: GodotString, dest: cstring, size: var cint) - {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringOperatorEqual*: proc (self, other: GodotString): bool {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringOperatorLess*: proc (self, other: GodotString): bool @@ -2559,6 +2569,12 @@ type {.noconv, raises: [], gcsafe, tags: [], locks: 0.} stringDestroy*: proc (self: var GodotString) {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringUtf8*: proc (self: GodotString): GodotCharString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringCharsToUtf8*: proc (str: cstring): GodotString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} + stringCharsToUtf8WithLen*: proc (str: cstring, len: cint): GodotString + {.noconv, raises: [], gcsafe, tags: [], locks: 0.} # Misc API objectDestroy*: proc (self: ptr GodotObject) @@ -3120,15 +3136,20 @@ proc setGDNativeAPI*(apiStruct: pointer, initOptions: ptr GDNativeInitOptions) = variantDestroy # String API + charStringLength + charStringGetData + charStringDestroy + stringNew stringNewCopy - stringNewData - stringGetData stringOperatorEqual stringOperatorLess stringOperatorPlus stringLength stringDestroy + stringUtf8 + stringCharsToUtf8 + stringCharsToUtf8WithLen # Misc API objectDestroy diff --git a/godot/godotapigen.nim b/godot/godotapigen.nim index f8c06e3..4075229 100644 --- a/godot/godotapigen.nim +++ b/godot/godotapigen.nim @@ -697,10 +697,14 @@ proc makeMethod(types: Table[string, GodotType], tree: PNode, toNimType(types, methodObj["return_type"].str) else: nil - for prop in typ.jsonNode["properties"]: - if prop["getter"].str == methodObj["name"].str or - prop["setter"].str == methodObj["name"].str: - return + if not typ.isSingleton: + # for singletons we don't generate Nim setters/getters, + # but use plain procedures instead. That's because setter syntax is + # supposed to be used on an object (have at least 2 params). + for prop in typ.jsonNode["properties"]: + if prop["getter"].str == methodObj["name"].str or + prop["setter"].str == methodObj["name"].str: + return var args = newSeqOfCap[MethodArg](methodObj["arguments"].len + 1) var origArgs = methodObj["arguments"] @@ -924,16 +928,18 @@ proc genApi*(targetDir: string, apiJsonFile: string) = # first, generate declarations only for ease of # human readability of the file - for property in obj["properties"]: - makeProperty(types, tree, methodBindRegsitry, typ, property, - withImplementation = false) + if not typ.isSingleton: + for property in obj["properties"]: + makeProperty(types, tree, methodBindRegsitry, typ, property, + withImplementation = false) for meth in obj["methods"]: makeMethod(types, tree, methodBindRegsitry, typ, meth, withImplementation = false) - for property in obj["properties"]: - makeProperty(types, tree, methodBindRegsitry, typ, - property, withImplementation = true) + if not typ.isSingleton: + for property in obj["properties"]: + makeProperty(types, tree, methodBindRegsitry, typ, + property, withImplementation = true) for meth in obj["methods"]: makeMethod(types, tree, methodBindRegsitry, typ, meth, withImplementation = true) diff --git a/godot/internal/godotinternaltypes.nim b/godot/internal/godotinternaltypes.nim index 508c39e..2ae38c3 100644 --- a/godot/internal/godotinternaltypes.nim +++ b/godot/internal/godotinternaltypes.nim @@ -32,6 +32,9 @@ type GodotString* {.byref.} = object data: array[sizeof(int), byte] + GodotCharString* {.byref.} = object + data: array[sizeof(int), byte] + VariantType* {.size: sizeof(cint), pure.} = enum Nil, ## atomic types Bool, diff --git a/godot/internal/godotstrings.nim b/godot/internal/godotstrings.nim index 58e4a24..fce9a41 100644 --- a/godot/internal/godotstrings.nim +++ b/godot/internal/godotstrings.nim @@ -8,16 +8,7 @@ proc initGodotString(dest: var GodotString) {.inline, raises: [].} = proc initGodotString(dest: var GodotString; contents: cstring; size: cint) {.inline.} = ## Initializes ``dest`` from UTF-8 ``contents`` - getGDNativeAPI().stringNewData(dest, contents, size) - -proc getData(self: GodotString; dest: cstring; - size: var cint) {.inline.} = - ## Converts ``self`` into UTF-8 encoding, putting the result into ``dest``. - getGDNativeAPI().stringGetData(self, dest, size) - -proc len*(self: GodotString): cint {.inline.} = - ## Returns the length of string in bytes if it is represented as UTF-8. - getData(self, nil, result) + dest = getGDNativeAPI().stringCharsToUtf8WithLen(contents, size) proc `==`*(self, b: GodotString): bool {.inline.} = getGDNativeAPI().stringOperatorEqual(self, b) @@ -33,10 +24,11 @@ proc deinit*(self: var GodotString) {.inline.} = proc `$`*(self: GodotString): string = ## Converts the ``GodotString`` into Nim string - var length = self.len - result = newStringOfCap(length) - getData(self, addr result[0], length) - result.setLen(length) + var charStr = getGDNativeAPI().stringUtf8(self) + let length = getGDNativeAPI().charStringLength(charStr) + result = newString(length) + copyMem(addr result[0], getGDNativeAPI().charStringGetData(charStr), length) + getGDNativeAPI().charStringDestroy(charStr) assert(result[length] == '\0') proc toGodotString*(s: string): GodotString {.inline.} =