Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C++ for WinSDKExtras #8

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion windows/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
),
],
Expand Down
8 changes: 4 additions & 4 deletions windows/Sources/FabricSandbox/JniEntrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Jni
import WinSDK

@_cdecl("Java_net_fabricmc_sandbox_Main_nativeEntrypoint")
public func entrypoint(env: UnsafeMutablePointer<JNIEnv?>!, clazz: jclass!) {
public func entrypoint(env: UnsafeMutablePointer<JNIEnv>, 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)")
}
}
8 changes: 4 additions & 4 deletions windows/Sources/FabricSandbox/String+Jni.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Jni

extension String {
// Construct a String from a jstring
init(_ env: UnsafeMutablePointer<JNIEnv?>!, jstring: jstring!) {
let jni = env.pointee!.pointee
let chars = jni.GetStringChars(env, jstring, nil)!
defer { jni.ReleaseStringChars(env, jstring, chars) }
init(_ env: UnsafeMutablePointer<JNIEnv>, jstring: jstring!) {
var jni = env.pointee
let chars = jni.GetStringChars(jstring, nil)!
defer { jni.ReleaseStringChars(jstring, chars) }
self = String(decodingCString: chars, as: UTF16.self)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "WinSdkExtras.h"
#include "WinSDKExtras.h"

#include <userenv.h>
#include <VersionHelpers.h>
Expand Down Expand Up @@ -65,7 +65,7 @@ DWORD _SECURITY_MAX_SID_SIZE() {
}

LPWCH _CASTSID(PSID pSid) {
return (LPWCH)pSid;
return static_cast<LPWCH>(pSid);
}

LPPROC_THREAD_ATTRIBUTE_LIST allocateAttributeList(size_t size) {
Expand Down
4 changes: 4 additions & 0 deletions windows/Sources/WinSDKExtras/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module WinSDKExtras {
header "WinSDKExtras.h"
export *
}
Loading