From e37239abc01047e75629b2c829da992398cbf6d6 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sat, 25 May 2024 10:31:50 +0100 Subject: [PATCH] Use C++ for WinSDKExtras --- windows/Package.swift | 9 ++++++++- windows/Sources/FabricSandbox/JniEntrypoint.swift | 8 ++++---- windows/Sources/FabricSandbox/String+Jni.swift | 8 ++++---- .../WinSDKExtras/{WinSdkExtras.c => WinSDKExtras.cpp} | 4 ++-- windows/Sources/WinSDKExtras/include/module.modulemap | 4 ++++ 5 files changed, 22 insertions(+), 11 deletions(-) rename windows/Sources/WinSDKExtras/{WinSdkExtras.c => WinSDKExtras.cpp} (97%) create mode 100644 windows/Sources/WinSDKExtras/include/module.modulemap diff --git a/windows/Package.swift b/windows/Package.swift index bdd9a7b..083c8ed 100644 --- a/windows/Package.swift +++ b/windows/Package.swift @@ -32,12 +32,14 @@ let package = Package( ), // A C library containing additional Windows SDK functions that arent included in the default WinSDK module .target( - name: "WinSDKExtras" + name: "WinSDKExtras", + swiftSettings: [.interoperabilityMode(.Cxx)] ), // A collection of utility functions for the Windows platform .target( name: "WindowsUtils", dependencies: [ .target(name: "WinSDKExtras") ], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), // Code shared between the sandbox and the runtime @@ -48,18 +50,21 @@ let package = Package( .executableTarget( name: "SandboxTest", dependencies: [ .target(name: "WindowsUtils") ], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), // The generic sandbox library .target( name: "Sandbox", dependencies: [ .target(name: "WinSDKExtras"), .target(name: "WindowsUtils"), .target(name: "Shared")], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), // The Minecraft/Fabric specific parts of the sandbox .target( name: "FabricSandbox", dependencies: [ .target(name: "Jni"), .target(name: "WinSDKExtras"), .target(name: "WindowsUtils"), .target(name: "Sandbox"), .product(name: "Logging", package: "swift-log")], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), // The swift code that is used in the sandboxed process, invoked via the hook @@ -79,11 +84,13 @@ let package = Package( .executableTarget( name: "Packager", dependencies: [.target(name: "WinSDKExtras"), .target(name: "WindowsUtils"), .target(name: "Sandbox"), .product(name: "Logging", package: "swift-log")], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), .testTarget( name: "FabricSandboxTests", dependencies: [ .target(name: "FabricSandbox"), .target(name: "WindowsUtils"), .product(name: "Testing", package: "swift-testing")], + swiftSettings: [.interoperabilityMode(.Cxx)], linkerSettings: linkerSettings ), ], diff --git a/windows/Sources/FabricSandbox/JniEntrypoint.swift b/windows/Sources/FabricSandbox/JniEntrypoint.swift index 6daf48f..01ce8d2 100644 --- a/windows/Sources/FabricSandbox/JniEntrypoint.swift +++ b/windows/Sources/FabricSandbox/JniEntrypoint.swift @@ -2,12 +2,12 @@ import Jni import WinSDK @_cdecl("Java_net_fabricmc_sandbox_Main_nativeEntrypoint") -public func entrypoint(env: UnsafeMutablePointer!, clazz: jclass!) { +public func entrypoint(env: UnsafeMutablePointer, clazz: jclass!) { do { try FabricSandbox().run() } catch { - let jni = env.pointee!.pointee - let runtimeException = jni.FindClass(env, "java/lang/RuntimeException")! - let _ = jni.ThrowNew(env, runtimeException, "\(error)") + var jni = env.pointee + let runtimeException = jni.FindClass("java/lang/RuntimeException")! + let _ = jni.ThrowNew(runtimeException, "\(error)") } } diff --git a/windows/Sources/FabricSandbox/String+Jni.swift b/windows/Sources/FabricSandbox/String+Jni.swift index 9abda5b..e3553ea 100644 --- a/windows/Sources/FabricSandbox/String+Jni.swift +++ b/windows/Sources/FabricSandbox/String+Jni.swift @@ -2,10 +2,10 @@ import Jni extension String { // Construct a String from a jstring - init(_ env: UnsafeMutablePointer!, jstring: jstring!) { - let jni = env.pointee!.pointee - let chars = jni.GetStringChars(env, jstring, nil)! - defer { jni.ReleaseStringChars(env, jstring, chars) } + init(_ env: UnsafeMutablePointer, jstring: jstring!) { + var jni = env.pointee + let chars = jni.GetStringChars(jstring, nil)! + defer { jni.ReleaseStringChars(jstring, chars) } self = String(decodingCString: chars, as: UTF16.self) } } diff --git a/windows/Sources/WinSDKExtras/WinSdkExtras.c b/windows/Sources/WinSDKExtras/WinSDKExtras.cpp similarity index 97% rename from windows/Sources/WinSDKExtras/WinSdkExtras.c rename to windows/Sources/WinSDKExtras/WinSDKExtras.cpp index 181b356..dc9c683 100644 --- a/windows/Sources/WinSDKExtras/WinSdkExtras.c +++ b/windows/Sources/WinSDKExtras/WinSDKExtras.cpp @@ -1,4 +1,4 @@ -#include "WinSdkExtras.h" +#include "WinSDKExtras.h" #include #include @@ -65,7 +65,7 @@ DWORD _SECURITY_MAX_SID_SIZE() { } LPWCH _CASTSID(PSID pSid) { - return (LPWCH)pSid; + return static_cast(pSid); } LPPROC_THREAD_ATTRIBUTE_LIST allocateAttributeList(size_t size) { diff --git a/windows/Sources/WinSDKExtras/include/module.modulemap b/windows/Sources/WinSDKExtras/include/module.modulemap new file mode 100644 index 0000000..d94ce19 --- /dev/null +++ b/windows/Sources/WinSDKExtras/include/module.modulemap @@ -0,0 +1,4 @@ +module WinSDKExtras { + header "WinSDKExtras.h" + export * +} \ No newline at end of file