From 9361ad31a79062ae2e22f8d42e567c5d2d040771 Mon Sep 17 00:00:00 2001 From: Kenta <106167071+Its-Kenta@users.noreply.github.com> Date: Tue, 19 Sep 2023 19:42:53 +0100 Subject: [PATCH] Add Windows version of resourcePath. Add linker to definition file to fix duplicate symbol on compile --- build.gradle.kts | 2 +- .../kotlin/kaylibkit/kCore/KCore.mingw.kt | 44 ++++++++++++++++++- src/nativeInterop/cinterop/kaylibc.def | 2 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index bf9613d..60cd954 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "com.prism-architect" -version = "1.0.4" +version = "1.0.5" // Codeberg repository properties providers val tokenProvider: Provider = providers.gradleProperty("KaylibKitToken") diff --git a/src/mingwMain/kotlin/kaylibkit/kCore/KCore.mingw.kt b/src/mingwMain/kotlin/kaylibkit/kCore/KCore.mingw.kt index 40373ad..039cf77 100644 --- a/src/mingwMain/kotlin/kaylibkit/kCore/KCore.mingw.kt +++ b/src/mingwMain/kotlin/kaylibkit/kCore/KCore.mingw.kt @@ -1,11 +1,51 @@ package kaylibkit.kCore - import kotlinx.cinterop.ExperimentalForeignApi +import kotlinx.cinterop.addressOf +import kotlinx.cinterop.pin +import kotlinx.cinterop.toKString +import platform.windows.* @OptIn(ExperimentalForeignApi::class) actual fun setTraceLogCallbackInternal(callback: TraceLogCallback) { kaylibc.SetTraceLogCallback(callback) } +/** + * Returns the absolute path to the 'resources' folder located at the same location as your executable. + * + * This property provides the path to the 'resources' folder associated with your executable. + * Ensure that a folder named 'resources' is present in the same directory as your executable. + * + * @return The absolute path to the 'resources' folder, or null if it doesn't exist or cannot be determined. + */ +@OptIn(ExperimentalForeignApi::class) actual inline val resourcePath: String? - get() { return "Not yet implemented" } \ No newline at end of file + get() { + try { + val buffer = ByteArray(MAX_PATH) // Create a buffer to store the path to the executable file. + val pathLength = GetModuleFileNameA(null, buffer.pin().addressOf(0), MAX_PATH.toUInt()) // Retrieves the fully qualified path for the executable + + // Check if the path length is less than or equal to zero, indicating an error. Throw exception if error is found + if (pathLength.toInt() == 0) throw ResourcePathException("Failed to retrieve executable path.") + + val executablePath = buffer.toKString() // Convert the buffer to a Kotlin string to get the full path to the executable. + + // Find the index of the last slash character ('\\') in the executable path, which separates the directory from the executable file name. + val lastBackslashIndex = executablePath.lastIndexOf('\\') + if (lastBackslashIndex == -1) throw ResourcePathException("Invalid executable path format: $executablePath.") + + // Extract the directory portion of the executable path + val executableDir = executablePath.substring(0, lastBackslashIndex + 1) + val resourceDir = executableDir + "resources" // Return the directory path of the executable file along with the "resources" subdirectory. + + // Check if the "resources" directory exists + if (GetFileAttributesW(resourceDir) == INVALID_FILE_ATTRIBUTES) { + throw ResourcePathException("The 'resources' directory does not exist at: $resourceDir, unable to load resources.") + } + + return resourceDir + } catch (e: ResourcePathException) { + println("ResourcePathException: ${e.message}") + return null + } + } \ No newline at end of file diff --git a/src/nativeInterop/cinterop/kaylibc.def b/src/nativeInterop/cinterop/kaylibc.def index 7c44401..315db7e 100644 --- a/src/nativeInterop/cinterop/kaylibc.def +++ b/src/nativeInterop/cinterop/kaylibc.def @@ -6,7 +6,7 @@ libraryPaths.linux = src/nativeInterop/cinterop/lib/linux libraryPaths.mingw = src/nativeInterop/cinterop/lib/mingw compilerOpts = -Isrc/nativeInterop/cinterop/include -linkerOpts.mingw = -lwinmm -lgdi32 -lopengl32 -lkernel32 +linkerOpts.mingw = -lwinmm -lgdi32 -lopengl32 -lkernel32 -Wl,--allow-multiple-definition linkerOpts.osx = -framework CoreVideo -framework IOKit -framework Cocoa -framework GLUT -framework OpenGL