Skip to content

Commit

Permalink
Add new common module (#726)
Browse files Browse the repository at this point in the history
* Create common module used by godot-library and common-tools
  • Loading branch information
CedNaru authored Oct 14, 2024
1 parent f59b1c6 commit 4151313
Show file tree
Hide file tree
Showing 806 changed files with 13,941 additions and 13,923 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/deploy_jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ jobs:
with:
cache-read-only: ${{ github.ref != 'refs/heads/master' }}

- name: Publish common
shell: sh
run: |
modules/kotlin_jvm/kt/gradlew -p modules/kotlin_jvm/kt/ :common:publish
- name: Publish tools-common
shell: sh
run: |
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/src/main/kotlin/godot/tests/Invocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import godot.core.variantArrayOf
import godot.extensions.getNodeAs
import godot.registration.Range
import godot.tests.subpackage.OtherScript
import godot.util.RealT
import godot.common.util.RealT
import org.joda.time.DateTime

enum class TestEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import godot.Node
import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.core.Vector3
import godot.util.RealT
import godot.common.util.RealT

@RegisterClass
class Vector3Test : Node() {
Expand All @@ -17,4 +17,4 @@ class Vector3Test : Node() {
vector3[index] = realT
return vector3
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package godot.codegen.exceptions

import godot.codegen.models.enriched.EnrichedMethod
import godot.codegen.models.enriched.EnrichedSignal
import godot.tools.common.constants.Constraints
import godot.common.constants.Constraints

class TooManyMethodArgument(method: EnrichedMethod) :
Exception("${method.name} has ${method.arguments.size} arguments but the maximum number is ${Constraints.MAX_FUNCTION_ARG_COUNT}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import godot.codegen.services.impl.EnumService
import godot.codegen.services.impl.GenerationService
import godot.codegen.services.impl.LambdaCallableGenerationService
import godot.codegen.services.impl.SignalGenerationService
import godot.tools.common.constants.Constraints
import godot.common.constants.Constraints
import godot.tools.common.constants.GENERATED_COMMENT
import godot.tools.common.constants.godotApiPackage
import java.io.File
Expand Down Expand Up @@ -89,9 +89,9 @@ fun File.generateApiFrom(jsonSource: File) {
}

LambdaCallableGenerationService().generate(Constraints.MAX_FUNCTION_ARG_COUNT).writeTo(this)
SignalGenerationService().generate(Constraints.MAX_FUNCTION_ARG_COUNT).writeTo(this)
SignalGenerationService().generate(Constraints.MAX_SIGNAL_ARG_COUNT).writeTo(this)
}

fun File.generateCoroutine() {
AwaitGenerationService.generate(Constraints.MAX_FUNCTION_ARG_COUNT).writeTo(this)
AwaitGenerationService.generate(Constraints.MAX_SIGNAL_ARG_COUNT).writeTo(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import godot.codegen.traits.NullableTrait
import godot.codegen.traits.WithDefaultValueTrait
import godot.codegen.workarounds.sanitizeApiType
import godot.tools.common.constants.GodotTypes
import godot.tools.common.extensions.convertToCamelCase
import godot.tools.common.extensions.escapeKotlinReservedNames
import godot.common.extensions.convertToCamelCase
import godot.common.extensions.escapeKotlinReservedNames

class EnrichedArgument(val internal: Argument, canBeNull: Boolean) : CastableTrait, NullableTrait, WithDefaultValueTrait {
val name = internal.name.convertToCamelCase().escapeKotlinReservedNames()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import godot.codegen.models.Class
import godot.codegen.models.custom.AdditionalImport
import godot.codegen.traits.IDocumented
import godot.codegen.traits.TypedTrait
import godot.tools.common.extensions.escapeUnderscore
import godot.common.extensions.escapeUnderscore
import java.util.*

class EnrichedClass(val internal: Class) : TypedTrait, IDocumented {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import godot.codegen.models.Method
import godot.codegen.traits.CallableTrait
import godot.codegen.traits.IDocumented
import godot.codegen.workarounds.sanitizeApiType
import godot.tools.common.constants.Constraints
import godot.common.constants.Constraints
import godot.tools.common.constants.GodotTypes
import godot.tools.common.extensions.convertToCamelCase
import godot.common.extensions.convertToCamelCase

class EnrichedMethod(val internal: Method) : CallableTrait, IDocumented {
override val arguments = internal.arguments?.toEnriched() ?: listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import godot.codegen.traits.IDocumented
import godot.codegen.traits.NullableTrait
import godot.codegen.workarounds.sanitizeApiType
import godot.tools.common.constants.GodotTypes
import godot.tools.common.extensions.convertToCamelCase
import godot.common.extensions.convertToCamelCase

class EnrichedProperty(val internal: Property) : CastableTrait, NullableTrait, IDocumented {
val name = internal.name.convertToCamelCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import godot.codegen.exceptions.TooManySignalArgument
import godot.codegen.models.Signal
import godot.codegen.traits.IDocumented
import godot.codegen.traits.TypedTrait
import godot.tools.common.constants.Constraints
import godot.tools.common.extensions.convertToCamelCase
import godot.tools.common.extensions.escapeKotlinReservedNames
import godot.common.constants.Constraints
import godot.common.extensions.convertToCamelCase
import godot.common.extensions.escapeKotlinReservedNames

class EnrichedSignal(val internal: Signal) : TypedTrait, IDocumented {
val name = internal.name.convertToCamelCase()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package godot.codegen.models.enriched

import godot.tools.common.extensions.escapeUnderscore
import godot.common.extensions.escapeUnderscore
import godot.codegen.workarounds.sanitizeApiType
import godot.codegen.models.Singleton

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.UNIT
import com.squareup.kotlinpoet.asClassName
import godot.codegen.constants.VOID_PTR
import godot.codegen.constants.jvmReservedMethods
import godot.codegen.exceptions.ClassGenerationException
import godot.codegen.extensions.applyJvmNameIfNecessary
Expand Down Expand Up @@ -57,6 +56,7 @@ import godot.tools.common.constants.TRANSFER_CONTEXT
import godot.tools.common.constants.TYPE_MANAGER
import godot.tools.common.constants.VARIANT_CASTER_ANY
import godot.tools.common.constants.VARIANT_PARSER_LONG
import godot.tools.common.constants.VOID_PTR
import godot.tools.common.constants.godotApiPackage
import godot.tools.common.constants.godotCorePackage
import java.util.*
Expand Down Expand Up @@ -853,7 +853,7 @@ class GenerationService(
val rawPtr = if (isStatic) {
"0" //nullpointer
} else {
"rawPtr"
"ptr"
}

addStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package godot.codegen.traits
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import godot.tools.common.extensions.convertToCamelCase
import godot.common.extensions.convertToCamelCase

private const val constantTitle = "constant"

Expand Down Expand Up @@ -111,4 +111,4 @@ fun FunSpec.Builder.addKdoc(documented: IDocumented): FunSpec.Builder {
} else {
this.addKdoc(documentation)
}
}
}
32 changes: 32 additions & 0 deletions kt/common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import versioninfo.fullGodotKotlinJvmVersion

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.grgit)
id("com.utopia-rise.godot-publish")
id("com.utopia-rise.versioninfo")
}

// the version is not inherited from the root build.gradle.kts as this here is a separate gradle project. Hence, we set it
// through the godot-dependencies plugin
version = fullGodotKotlinJvmVersion
group = "com.utopia-rise"

kotlin {
jvmToolchain(11)
}

publishing {
publications {
@Suppress("UNUSED_VARIABLE")
val godotCommonGenerator by creating(MavenPublication::class) {
pom {
name.set(project.name)
description.set("Godot common module other libraries.")
}
artifactId = project.name
description = "Godot common module other libraries."
from(components.getByName("java"))
}
}
}
30 changes: 30 additions & 0 deletions kt/common/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

pluginManagement {
includeBuild("../build-logic")
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
google()
}
}

dependencyResolutionManagement {
@Suppress("UnstableApiUsage")
repositories {
mavenLocal()
mavenCentral()
google()
}

versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

plugins {
// to automatically download the toolchain jdk if missing
id("org.gradle.toolchains.foojay-resolver-convention") version "0.6.0"
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package godot.tools.common.constants
package godot.common.constants

// when changed; also update constraints.h!
// Since Godot 4, an unlimited amount of parameters is supported. Limits should be increased when appropriate.
object Constraints {
const val MAX_CONSTRUCTOR_ARG_COUNT = 8
const val MAX_FUNCTION_ARG_COUNT = 16
const val MAX_SIGNAL_ARG_COUNT = 8
const val MAX_SIGNAL_ARG_COUNT = 16
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package godot.tools.common.extensions
package godot.common.extensions

import java.util.*

Expand Down
12 changes: 12 additions & 0 deletions kt/common/src/main/kotlin/godot/common/interop/NativePointer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package godot.common.interop

typealias VoidPtr = Long
const val nullptr: VoidPtr = 0L

interface ValuePointer {
val ptr: VoidPtr
}

interface IdentityPointer: ValuePointer {
val objectID: ObjectID
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package godot.core
package godot.common.interop

val nullObjectID = ObjectID(-1L)

@JvmInline
value class ObjectID(val id: Long) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package godot.util
package godot.common.util

// A basic LRU cache.
internal class LRUCache<K, V>(private val capacity: Int) : LinkedHashMap<K, V>(capacity, 0.75f, true) {
class LRUCache<K, V>(private val capacity: Int) : LinkedHashMap<K, V>(capacity, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<K, V>?): Boolean {
return size > capacity
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package godot.util
package godot.common.util

@Suppress("UNCHECKED_CAST")
internal class IndexedIterator<T>(
class IndexedIterator<T>(
private var size: () -> Int,
private val getter: (Int) -> T,
private val eraser: (Int) -> Unit
Expand All @@ -23,7 +23,7 @@ internal class IndexedIterator<T>(
}
}

internal class Entry<K, V>(
class Entry<K, V>(
override val key: K,
private val getter: (K) -> V,
private val setter: (K, V) -> Unit
Expand All @@ -42,7 +42,7 @@ internal class Entry<K, V>(
}

@Suppress("UNCHECKED_CAST")
internal class MapIterator<K, V>(
class MapIterator<K, V>(
private val keyIterator: MutableIterator<K>,
private val getter: (K) -> V,
private val setter: (K, V) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:Suppress("NOTHING_TO_INLINE", "FloatingPointLiteralPrecision")

package godot.util
package godot.common.util

import kotlin.math.abs
import kotlin.math.floor
Expand Down Expand Up @@ -63,19 +63,19 @@ fun RealT.fposmod(b: RealT): RealT {
return value
}

internal fun snapped(value: NaturalT, step: NaturalT) = if (step != 0) {
fun snapped(value: NaturalT, step: NaturalT) = if (step != 0) {
(floor(value.toRealT() / step.toRealT() + 0.5) * step).toNaturalT()
} else {
value
}

internal fun snapped(value: RealT, step: RealT) = if (step != 0.0) {
fun snapped(value: RealT, step: RealT) = if (step != 0.0) {
floor(value / step + 0.5) * step
} else {
value
}

internal fun bezierDerivative(start: RealT, control1: RealT, control2: RealT, end: RealT, t: RealT): RealT {
fun bezierDerivative(start: RealT, control1: RealT, control2: RealT, end: RealT, t: RealT): RealT {
/* Formula from Wikipedia article on Bezier curves. */
val omt = 1.0 - t
val omt2 = omt * omt
Expand All @@ -84,7 +84,7 @@ internal fun bezierDerivative(start: RealT, control1: RealT, control2: RealT, en
return (control1 - start) * 3.0f * omt2 + (control2 - control1) * 6.0f * omt * t + (end - control2) * 3.0f * t2
}

internal fun bezierInterpolate(start: RealT, control1: RealT, control2: RealT, end: RealT, t: RealT): RealT {
fun bezierInterpolate(start: RealT, control1: RealT, control2: RealT, end: RealT, t: RealT): RealT {
/* Formula from Wikipedia article on Bezier curves. */
val omt: Double = 1.0 - t
val omt2 = omt * omt
Expand All @@ -95,7 +95,7 @@ internal fun bezierInterpolate(start: RealT, control1: RealT, control2: RealT, e
return start * omt3 + control1 * omt2 * t * 3.0 + control2 * omt * t2 * 3.0 + end * t3
}

internal fun cubicInterpolate(
fun cubicInterpolate(
from: Double,
to: Double,
pre: Double,
Expand All @@ -110,7 +110,7 @@ internal fun cubicInterpolate(
inline fun lerp(from: Float, to: Float, weight: Float) = from + (to - from) * weight
inline fun lerp(from: Double, to: Double, weight: Double) = from + (to - from) * weight

internal fun lerpAngle(from: RealT, to: RealT, weight: RealT): RealT {
fun lerpAngle(from: RealT, to: RealT, weight: RealT): RealT {
var difference = (to - from).rem(TAU)
difference = (2 * difference).rem(TAU) - difference
return from + difference * weight;
Expand Down
3 changes: 3 additions & 0 deletions kt/common/src/main/kotlin/godot/common/util/Nullable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package godot.common.util

inline fun <reified T> isNullable(): Boolean = null is T
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package godot.util
package godot.common.util

import kotlin.reflect.KProperty

internal class ThreadLocalDelegate<T>(val provider: () -> T) {
class ThreadLocalDelegate<T>(val provider: () -> T) {
private val threadLocal = ThreadLocal.withInitial { provider() }
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = threadLocal.get()
}

internal fun <T> threadLocal(provider: () -> T) = ThreadLocalDelegate(provider)
fun <T> threadLocal(provider: () -> T) = ThreadLocalDelegate(provider)
Loading

0 comments on commit 4151313

Please sign in to comment.