Implement hash proc for Godot types

This commit is contained in:
Ruslan Mustakov 2018-06-04 11:23:23 +07:00
commit 76de026b4f
15 changed files with 139 additions and 38 deletions

View file

@ -1,5 +1,8 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import vector3
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
@ -9,6 +12,9 @@ proc initAABB*(pos, size: Vector3): AABB {.inline.} =
proc `$`*(self: AABB): string {.inline.} = proc `$`*(self: AABB): string {.inline.} =
$getGDNativeAPI().aabbAsString(self) $getGDNativeAPI().aabbAsString(self)
proc hash*(self: AABB): Hash {.inline.} =
!$(self.position.hash() !& self.size.hash())
proc area*(self: AABB): float32 {.inline.} = proc area*(self: AABB): float32 {.inline.} =
getGDNativeAPI().aabbGetArea(self) getGDNativeAPI().aabbGetArea(self)

View file

@ -1,6 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import math import math, hashes
import godotbase, vector3, quats import godotbase, vector3, quats
import godotcoretypes import godotcoretypes
@ -201,6 +202,9 @@ proc `$`*(self: Basis): string {.inline.} =
result.add(", ") result.add(", ")
result.add($self[i][j]) result.add($self[i][j])
proc hash*(self: Basis): Hash {.inline.} =
!$(self.elements[0].hash() !& self.elements[1].hash() !& self.elements[2].hash())
template cofac(row1, col1, row2, col2: int): float32 = template cofac(row1, col1, row2, col2: int): float32 =
self.elements[row1][col1] * self.elements[row2][col2] - self.elements[row1][col1] * self.elements[row2][col2] -
self.elements[row1][col2] * self.elements[row2][col1] self.elements[row1][col2] * self.elements[row2][col1]

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
proc initColor*(r, g, b: float32; a: float32 = 1.0'f32): Color {.inline.} = proc initColor*(r, g, b: float32; a: float32 = 1.0'f32): Color {.inline.} =
@ -33,6 +35,9 @@ proc `$`*(self: Color): string {.inline.} =
result.add(", ") result.add(", ")
result.add($self.a) result.add($self.a)
proc hash*(self: Color): Hash {.inline.} =
!$(self.r.hash() !& self.g.hash() !& self.b.hash() !& self.a.hash())
proc toHex(val: float32, target: var string, targetIdx: int) = proc toHex(val: float32, target: var string, targetIdx: int) =
let val = clamp(int(val * 255), 0, 255) let val = clamp(int(val * 255), 0, 255)
let nums = [(val and 0xF0) shr 4, val and 0xF] let nums = [(val and 0xF0) shr 4, val and 0xF]

View file

@ -33,15 +33,15 @@ proc isEmpty*(self: Dictionary): bool {.inline.} =
proc clear*(self: Dictionary) {.inline.} = proc clear*(self: Dictionary) {.inline.} =
self.godotDictionary.clear() self.godotDictionary.clear()
proc hash*(self: Dictionary): Hash {.inline.} =
hash(self.godotDictionary.godotHash())
import variants, arrays import variants, arrays
proc newVariant*(dict: Dictionary): Variant {.inline.} = proc newVariant*(dict: Dictionary): Variant {.inline.} =
new(result, variantFinalizer) new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], dict.godotDictionary) initGodotVariant(result.godotVariant[], dict.godotDictionary)
proc asDictionary*(self: Variant): Dictionary {.inline.} =
newDictionary(self.godotVariant[].asGodotDictionary)
proc contains*(self: Dictionary; key: Variant): bool {.inline.}= proc contains*(self: Dictionary; key: Variant): bool {.inline.}=
self.godotDictionary.contains(key.godotVariant[]) self.godotDictionary.contains(key.godotVariant[])
@ -51,9 +51,6 @@ proc contains*(self: Dictionary; keys: Array): bool {.inline.} =
proc del*(self: Dictionary; key: Variant) {.inline.} = proc del*(self: Dictionary; key: Variant) {.inline.} =
self.godotDictionary.del(key.godotVariant[]) self.godotDictionary.del(key.godotVariant[])
proc hash*(self: Dictionary): Hash {.inline.} =
hash(self.godotDictionary.godotHash())
proc keys*(self: Dictionary): Array {.inline.} = proc keys*(self: Dictionary): Array {.inline.} =
newArray(self.godotDictionary.keys()) newArray(self.godotDictionary.keys())

View file

@ -1,5 +1,7 @@
# Copyright 2018 Xored Software, Inc. # Copyright 2018 Xored Software, Inc.
import hashes
import internal.godotinternaltypes, internal.godotnodepaths, import internal.godotinternaltypes, internal.godotnodepaths,
internal.godotstrings internal.godotstrings
@ -32,6 +34,9 @@ proc `$`*(self: NodePath): string {.inline.} =
result = $s result = $s
s.deinit() s.deinit()
proc hash*(self: NodePath): Hash {.inline.} =
($self).hash()
proc isAbsolute*(self: NodePath): bool {.inline.} = proc isAbsolute*(self: NodePath): bool {.inline.} =
self.path.isAbsolute() self.path.isAbsolute()

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import vector3 import vector3
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
@ -20,6 +22,9 @@ proc initPlane*(normal: Vector3; d: float32): Plane {.inline.} =
proc `$`*(self: Plane): string {.inline.} = proc `$`*(self: Plane): string {.inline.} =
$getGDNativeAPI().planeAsString(self) $getGDNativeAPI().planeAsString(self)
proc hash*(self: Plane): Hash {.inline, noinit.} =
!$(self.normal.hash() !& self.d.hash())
proc normalized*(self: Plane): Plane {.inline.} = proc normalized*(self: Plane): Plane {.inline.} =
getGDNativeAPI().planeNormalized(self) getGDNativeAPI().planeNormalized(self)

View file

@ -1,8 +1,11 @@
# Copyright 2018 Xored Software, Inc. # Copyright 2018 Xored Software, Inc.
import hashes
import godotcoretypes import godotcoretypes
import internal.godotinternaltypes, internal.godotpoolarrays, import internal.godotinternaltypes, internal.godotpoolarrays,
internal.godotstrings internal.godotstrings
import vector2, vector3, colors
template definePoolArrayBase(T, GodotT, DataT, fieldName, newProcName, template definePoolArrayBase(T, GodotT, DataT, fieldName, newProcName,
initProcName) = initProcName) =
@ -83,6 +86,11 @@ template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
for pair in arr.fieldName.mpairs: for pair in arr.fieldName.mpairs:
yield (pair[0].int, pair[1]) yield (pair[0].int, pair[1])
proc hash*(arr: T): Hash =
for item in arr.items():
result = result !& item.hash()
result = !$result
definePoolArrayBase(PoolByteArray, GodotPoolByteArray, uint8, definePoolArrayBase(PoolByteArray, GodotPoolByteArray, uint8,
godotPoolByteArray, newPoolByteArray, godotPoolByteArray, newPoolByteArray,
initGodotPoolByteArray) initGodotPoolByteArray)
@ -156,3 +164,8 @@ iterator items*(arr: PoolStringArray): string =
iterator pairs*(arr: PoolStringArray): tuple[key: int, val: string] = iterator pairs*(arr: PoolStringArray): tuple[key: int, val: string] =
for i in 0..<arr.len: for i in 0..<arr.len:
yield (i, arr[i]) yield (i, arr[i])
proc hash*(arr: PoolStringArray): Hash =
for s in arr:
result = result !& s.hash()
result = !$result

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
@ -17,6 +19,9 @@ proc initQuat*(axis: Vector3; angle: float32): Quat {.inline.} =
proc `$`*(self: Quat): string {.inline.} = proc `$`*(self: Quat): string {.inline.} =
$getGDNativeAPI().quatAsString(self) $getGDNativeAPI().quatAsString(self)
proc hash*(self: Quat): Hash {.inline.} =
!$(self.x.hash() !& self.y.hash() !& self.z.hash() !& self.w.hash())
proc length*(self: Quat): float32 {.inline.} = proc length*(self: Quat): float32 {.inline.} =
getGDNativeAPI().quatLength(self) getGDNativeAPI().quatLength(self)

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import vector2 import vector2
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
@ -15,9 +17,12 @@ proc initRect2*(pos, size: Vector2): Rect2 {.inline, noinit.} =
proc initRect2*(x, y, sizeX, sizeY: float32): Rect2 {.inline, noinit.} = proc initRect2*(x, y, sizeX, sizeY: float32): Rect2 {.inline, noinit.} =
Rect2(position: vec2(x, y), size: vec2(sizeX, sizeY)) Rect2(position: vec2(x, y), size: vec2(sizeX, sizeY))
proc `$`*(self: Rect2): string {.inline.} = proc `$`*(self: Rect2): string {.inline, noinit.} =
$getGDNativeAPI().rect2AsString(self) $getGDNativeAPI().rect2AsString(self)
proc hash*(self: Rect2): Hash {.inline, noinit.} =
!$(self.position.hash() !& self.size.hash())
proc area*(self: Rect2): float32 {.inline, noinit.} = proc area*(self: Rect2): float32 {.inline, noinit.} =
self.size.x * self.size.y self.size.x * self.size.y

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import godotcoretypes, internal.godotinternaltypes, gdnativeapi import godotcoretypes, internal.godotinternaltypes, gdnativeapi
proc initRID*(): RID {.inline.} = proc initRID*(): RID {.inline.} =
@ -14,6 +16,9 @@ proc id*(self: RID): uint32 {.inline.} =
proc `$`*(self: RID): string {.inline.} = proc `$`*(self: RID): string {.inline.} =
$self.id $self.id
proc hash*(self: RID): Hash {.inline.} =
self.id.hash()
proc `==`*(a, b: RID): bool {.inline.} = proc `==`*(a, b: RID): bool {.inline.} =
getGDNativeAPI().ridOperatorEqual(a, b) getGDNativeAPI().ridOperatorEqual(a, b)

View file

@ -1,5 +1,7 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes, vector2
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
@ -15,6 +17,9 @@ proc initTransform2D*(xAxis, yAxis, origin: Vector2): Transform2D {.inline.} =
proc `$`*(self: Transform2D): string {.inline.} = proc `$`*(self: Transform2D): string {.inline.} =
$getGDNativeAPI().transform2DAsString(self) $getGDNativeAPI().transform2DAsString(self)
proc hash*(self: Transform2D): Hash {.inline, noinit.} =
!$(self.elements[0].hash() !& self.elements[1].hash() !& self.elements[2].hash())
proc inverse*(self: Transform2D): Transform2D {.inline.} = proc inverse*(self: Transform2D): Transform2D {.inline.} =
getGDNativeAPI().transform2DInverse(self) getGDNativeAPI().transform2DInverse(self)

View file

@ -1,8 +1,10 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import hashes
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
import basis import basis, vector3
proc initTransform*(): Transform {.inline.} = proc initTransform*(): Transform {.inline.} =
result.basis = initBasis() result.basis = initBasis()
@ -21,6 +23,9 @@ proc initTransform*(basis: Basis, origin: Vector3): Transform {.inline.} =
proc `$`*(self: Transform): string {.inline.} = proc `$`*(self: Transform): string {.inline.} =
$getGDNativeAPI().transformAsString(self) $getGDNativeAPI().transformAsString(self)
proc hash*(self: Transform): Hash {.inline.} =
!$(self.basis.hash() !& self.origin.hash())
proc inverse*(self: Transform): Transform {.inline.} = proc inverse*(self: Transform): Transform {.inline.} =
getGDNativeAPI().transformInverse(self) getGDNativeAPI().transformInverse(self)

View file

@ -1,4 +1,4 @@
import tables import tables, hashes
import godotcoretypes import godotcoretypes
import internal.godotinternaltypes, internal.godotvariants, import internal.godotinternaltypes, internal.godotvariants,
@ -140,7 +140,7 @@ proc newVariant*(obj: ptr GodotObject): Variant {.inline.} =
new(result, variantFinalizer) new(result, variantFinalizer)
initGodotVariant(result.godotVariant, obj) initGodotVariant(result.godotVariant, obj)
import arrays, poolarrays import arrays, poolarrays, dictionaries
proc newVariant*(arr: Array): Variant {.inline.} = proc newVariant*(arr: Array): Variant {.inline.} =
new(result, variantFinalizer) new(result, variantFinalizer)
@ -177,81 +177,116 @@ proc newVariant*(pca: PoolColorArray): Variant {.inline.} =
proc asBool*(self: Variant): bool {.inline.} = proc asBool*(self: Variant): bool {.inline.} =
self.godotVariant.asBool() self.godotVariant.asBool()
proc asUInt*(self: Variant): uint64 = proc asUInt*(self: Variant): uint64 {.inline.} =
self.godotVariant.asUInt() self.godotVariant.asUInt()
proc asInt*(self: Variant): int64 = proc asInt*(self: Variant): int64 {.inline.} =
self.godotVariant.asInt() self.godotVariant.asInt()
proc asReal*(self: Variant): cdouble = proc asReal*(self: Variant): cdouble {.inline.} =
self.godotVariant.asReal() self.godotVariant.asReal()
proc asString*(self: Variant): string = proc asString*(self: Variant): string {.inline.} =
$self $self
proc asVector2*(self: Variant): Vector2 = proc asVector2*(self: Variant): Vector2 {.inline.} =
self.godotVariant.asVector2() self.godotVariant.asVector2()
proc asRect2*(self: Variant): Rect2 = proc asRect2*(self: Variant): Rect2 {.inline.} =
self.godotVariant.asRect2() self.godotVariant.asRect2()
proc asVector3*(self: Variant): Vector3 = proc asVector3*(self: Variant): Vector3 {.inline.} =
self.godotVariant.asVector3() self.godotVariant.asVector3()
proc asTransform2D*(self: Variant): Transform2D = proc asTransform2D*(self: Variant): Transform2D {.inline.} =
self.godotVariant.asTransform2D() self.godotVariant.asTransform2D()
proc asPlane*(self: Variant): Plane = proc asPlane*(self: Variant): Plane {.inline.} =
self.godotVariant.asPlane() self.godotVariant.asPlane()
proc asQuat*(self: Variant): Quat = proc asQuat*(self: Variant): Quat {.inline.} =
self.godotVariant.asQuat() self.godotVariant.asQuat()
proc asAABB*(self: Variant): AABB = proc asAABB*(self: Variant): AABB {.inline.} =
self.godotVariant.asAABB() self.godotVariant.asAABB()
proc asBasis*(self: Variant): Basis = proc asBasis*(self: Variant): Basis {.inline.} =
self.godotVariant.asBasis() self.godotVariant.asBasis()
proc asTransform*(self: Variant): Transform = proc asTransform*(self: Variant): Transform {.inline.} =
self.godotVariant.asTransform() self.godotVariant.asTransform()
proc asColor*(self: Variant): Color = proc asColor*(self: Variant): Color {.inline.} =
self.godotVariant.asColor() self.godotVariant.asColor()
proc asNodePath*(self: Variant): NodePath = proc asNodePath*(self: Variant): NodePath {.inline.} =
result = newNodePath(self.godotVariant.asNodePath()) result = newNodePath(self.godotVariant.asNodePath())
proc asRID*(self: Variant): RID = proc asRID*(self: Variant): RID {.inline.} =
self.godotVariant.asRID() self.godotVariant.asRID()
proc asGodotObject*(self: Variant): ptr GodotObject = proc asGodotObject*(self: Variant): ptr GodotObject {.inline.} =
self.godotVariant.asGodotObject() self.godotVariant.asGodotObject()
proc asArray*(self: Variant): Array = proc asDictionary*(self: Variant): Dictionary {.inline.} =
newDictionary(self.godotVariant.asGodotDictionary())
proc asArray*(self: Variant): Array {.inline.} =
newArray(self.godotVariant.asGodotArray()) newArray(self.godotVariant.asGodotArray())
proc asPoolByteArray*(self: Variant): PoolByteArray = proc asPoolByteArray*(self: Variant): PoolByteArray {.inline.} =
newPoolByteArray(self.godotVariant.asGodotPoolByteArray()) newPoolByteArray(self.godotVariant.asGodotPoolByteArray())
proc asPoolIntArray*(self: Variant): PoolIntArray = proc asPoolIntArray*(self: Variant): PoolIntArray {.inline.} =
newPoolIntArray(self.godotVariant.asGodotPoolIntArray()) newPoolIntArray(self.godotVariant.asGodotPoolIntArray())
proc asPoolRealArray*(self: Variant): PoolRealArray = proc asPoolRealArray*(self: Variant): PoolRealArray {.inline.} =
newPoolRealArray(self.godotVariant.asGodotPoolRealArray()) newPoolRealArray(self.godotVariant.asGodotPoolRealArray())
proc asPoolStringArray*(self: Variant): PoolStringArray = proc asPoolStringArray*(self: Variant): PoolStringArray {.inline.} =
newPoolStringArray(self.godotVariant.asGodotPoolStringArray()) newPoolStringArray(self.godotVariant.asGodotPoolStringArray())
proc asPoolVector2Array*(self: Variant): PoolVector2Array = proc asPoolVector2Array*(self: Variant): PoolVector2Array {.inline.} =
newPoolVector2Array(self.godotVariant.asGodotPoolVector2Array()) newPoolVector2Array(self.godotVariant.asGodotPoolVector2Array())
proc asPoolVector3Array*(self: Variant): PoolVector3Array = proc asPoolVector3Array*(self: Variant): PoolVector3Array {.inline.} =
newPoolVector3Array(self.godotVariant.asGodotPoolVector3Array()) newPoolVector3Array(self.godotVariant.asGodotPoolVector3Array())
proc asPoolColorArray*(self: Variant): PoolColorArray = proc asPoolColorArray*(self: Variant): PoolColorArray {.inline.} =
newPoolColorArray(self.godotVariant.asGodotPoolColorArray()) newPoolColorArray(self.godotVariant.asGodotPoolColorArray())
proc hash*(self: Variant): Hash =
proc objectHash(obj: ptr GodotObject): Hash =
if not obj.isNil: cast[int](obj).hash() else: 0.hash()
result = case self.getType():
of VariantType.Nil: Hash(0)
of VariantType.Bool: self.asBool().hash()
of VariantType.Int: self.asInt().hash()
of VariantType.Real: self.asReal().hash()
of VariantType.String: self.asString().hash()
of VariantType.Vector2: self.asVector2().hash()
of VariantType.Rect2: self.asRect2().hash()
of VariantType.Vector3: self.asVector3().hash()
of VariantType.Transform2D: self.asTransform2D().hash()
of VariantType.Plane: self.asPlane().hash()
of VariantType.Quat: self.asQuat().hash()
of VariantType.AABB: self.asAABB().hash()
of VariantType.Basis: self.asBasis().hash()
of VariantType.Transform: self.asTransform().hash()
of VariantType.Color: self.asColor().hash()
of VariantType.NodePath: self.asNodePath().hash()
of VariantType.RID: self.asRID().hash()
of VariantType.Object: self.asGodotObject().objectHash()
of VariantType.Dictionary: self.asDictionary().hash()
of VariantType.Array: self.asArray().hash()
of VariantType.PoolByteArray: self.asPoolByteArray().hash()
of VariantType.PoolIntArray: self.asPoolIntArray().hash()
of VariantType.PoolRealArray: self.asPoolRealArray().hash()
of VariantType.PoolStringArray: self.asPoolStringArray().hash()
of VariantType.PoolVector2Array: self.asPoolVector2Array().hash()
of VariantType.PoolVector3Array: self.asPoolVector3Array().hash()
of VariantType.PoolColorArray: self.asPoolColorArray().hash()
proc hasMethod*(self: Variant; meth: string): bool = proc hasMethod*(self: Variant; meth: string): bool =
var s = meth.toGodotString() var s = meth.toGodotString()
result = self.godotVariant.hasMethod(s) result = self.godotVariant.hasMethod(s)

View file

@ -1,6 +1,6 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import math, godotbase import math, godotbase, hashes
import internal.godotinternaltypes, internal.godotstrings import internal.godotinternaltypes, internal.godotstrings
import godotcoretypes, gdnativeapi import godotcoretypes, gdnativeapi
@ -16,6 +16,9 @@ proc vec2*(x, y: float32): Vector2 {.inline, noinit.} =
proc `$`*(self: Vector2): string {.inline, noinit.} = proc `$`*(self: Vector2): string {.inline, noinit.} =
$getGDNativeAPI().vector2AsString(self) $getGDNativeAPI().vector2AsString(self)
proc hash*(self: Vector2): Hash {.inline, noinit.} =
!$(self.x.hash() !& self.y.hash())
proc `+`*(self, other: Vector2): Vector2 {.inline, noinit.} = proc `+`*(self, other: Vector2): Vector2 {.inline, noinit.} =
Vector2(x: self.x + other.x, y: self.y + other.y) Vector2(x: self.x + other.x, y: self.y + other.y)

View file

@ -1,6 +1,6 @@
# Copyright (c) 2018 Xored Software, Inc. # Copyright (c) 2018 Xored Software, Inc.
import math import math, hashes
import godotbase, godotcoretypes import godotbase, godotcoretypes
{.push stackTrace: off.} {.push stackTrace: off.}
@ -21,6 +21,9 @@ proc `$`*(self: Vector3): string {.inline.} =
result.add($self.z) result.add($self.z)
result.add(')') result.add(')')
proc hash*(self: Vector3): Hash {.inline, noinit.} =
!$(self.x.hash() !& self.y.hash() !& self.z.hash())
proc `+`*(a, b: Vector3): Vector3 {.inline.} = proc `+`*(a, b: Vector3): Vector3 {.inline.} =
result.x = a.x + b.x result.x = a.x + b.x
result.y = a.y + b.y result.y = a.y + b.y