Skip to content

Commit

Permalink
Setup dynamic library loading at runtime.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <[email protected]>
  • Loading branch information
furby-tm committed Sep 7, 2024
1 parent 2f2e471 commit a00c5ca
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Kraken.usda
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#usda 1.0
(
doc = "Kraken v1.1.0 | 09-06-2024 09:23:39"
doc = "Kraken v1.1.0 | 09-06-2024 18:01:09"
)

def Xform "Geometry"
Expand Down
20 changes: 19 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "921dba52a8c494676d11c4dd9440867fd2ee6b3d86ea02c679523d82c4edf548",
"originHash" : "15f7851235948e80635869fd5e8b8fb8b839f1c60452c7a73645f71b257ffebd",
"pins" : [
{
"identity" : "galah",
Expand All @@ -10,6 +10,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "krakenplugs",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/KrakenPlugs.git",
"state" : {
"revision" : "522989cec1fb7f8015694a5c79671fc8ab39e7ac",
"version" : "1.0.0"
}
},
{
"identity" : "mainoffender",
"kind" : "remoteSourceControl",
Expand All @@ -28,6 +37,15 @@
"version" : "1.7.7"
}
},
{
"identity" : "metaverseplugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/wabiverse/MetaversePlugin.git",
"state" : {
"revision" : "c5b20ae3e9ee715914bf7701cb1f75cfac0e638c",
"version" : "1.0.0"
}
},
{
"identity" : "metaversepythonframework",
"kind" : "remoteSourceControl",
Expand Down
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ let package = Package(
// --- 🦄 Package Dependencies. ---
dependencies: [
// .package(url: "https://github.com/stackotter/swift-cross-ui", revision: "f57f7ab"),
.package(url: "https://github.com/wabiverse/MetaversePlugin.git", from: "1.0.0"),
.package(url: "https://github.com/wabiverse/KrakenPlugs.git", from: "1.0.0"),
.package(url: "https://github.com/wabiverse/SwiftUSD.git", from: "24.8.0"),
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", revision: "96ad58b"),
.package(url: "https://github.com/ChimeHQ/TextFormation.git", from: "0.8.2"),
Expand Down Expand Up @@ -71,6 +73,8 @@ let package = Package(
name: "Kraken",
dependencies: [
.product(name: "PixarUSD", package: "SwiftUSD"),
.product(name: "MetaversePlugin", package: "MetaversePlugin"),
.product(name: "KrakenPlug", package: "KrakenPlugs"),
.target(name: "KrakenKit"),
.target(name: "KrakenLib"),
.target(name: "CosmoEditor"),
Expand Down
37 changes: 31 additions & 6 deletions Sources/Kraken/KR/KR.Creator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,39 @@ public enum Creator
/* setup usd plugins & resources. */
Pixar.Bundler.shared.setup(.resources)

/* setup kraken plugins. */
#if os(macOS)
let plugins = "\(Bundle.main.bundlePath)/Contents/Libraries"
#else
let plugins = "\(Bundle.main.bundlePath)/Libraries"
#endif

#if os(Linux)
let plugExt = "so"
#elseif os(Windows)
let plugExt = "dll"
#else
let plugExt = "dylib"
#endif

Msg.logger.info("Kraken plugins path: \(plugins)")
if FileManager.default.fileExists(atPath: plugins)
{
let krakenPlug = "\(plugins)/libKrakenPlug.\(plugExt)"
if FileManager.default.fileExists(atPath: krakenPlug)
{
Kraken.Plugin.info(at: krakenPlug)
}
}

#if canImport(PyBundle) && DEBUG
/*
embed & init python.
TODO: for some reason python
crashes release builds, figure
out why (its likely improper
resource bundle paths).
*/
embed & init python.
TODO: for some reason python
crashes release builds, figure
out why (its likely improper
resource bundle paths).
*/
PyBundler.shared.pyInit()
PyBundler.shared.pyInfo()
#endif /* canImport(PyBundle) */
Expand Down
83 changes: 83 additions & 0 deletions Sources/Kraken/KR/KR.Plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* --------------------------------------------------------------
* :: : K R A K E N : ::
* --------------------------------------------------------------
* @wabistudios :: metaverse :: kraken
*
* This program is free software; you can redistribute it, and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Check out
* the GNU General Public License for more details.
*
* You should have received a copy for this software license, the
* GNU General Public License along with this program; or, if not
* write to the Free Software Foundation, Inc., to the address of
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (C) 2023 Wabi Foundation.
* All Rights Reserved.
* --------------------------------------------------------------
* . x x x . o o o . x x x . : : : . o x o . : : : .
* -------------------------------------------------------------- */

import Foundation
import MetaversePlugin
import PixarUSD

public extension Kraken
{
enum Plugin
{
public typealias InitFunction = @convention(c) () -> UnsafeMutableRawPointer

public static func load(at path: String) -> MetaversePlugin
{
let openRes = dlopen(path, RTLD_NOW | RTLD_LOCAL)
if openRes != nil
{
defer
{
dlclose(openRes)
}

let symbolName = "createPlugin"
let sym = dlsym(openRes, symbolName)

if sym != nil
{
let f: InitFunction = unsafeBitCast(sym, to: InitFunction.self)
let pluginPointer = f()
let builder = Unmanaged<MetaversePluginBuilder>.fromOpaque(pluginPointer).takeRetainedValue()
return builder.build()
}
else
{
fatalError("error loading lib: symbol \(symbolName) not found, path: \(path)")
}
}
else
{
if let err = dlerror()
{
fatalError("error opening lib: \(String(format: "%s", err)), path: \(path)")
}
else
{
fatalError("error opening lib: unknown error, path: \(path)")
}
}
}

public static func info(at path: String)
{
let plugin = Kraken.Plugin.load(at: path)
let name = plugin.name

Msg.logger.info("Loaded plugin { name: \(name), path: \(path) }")
}
}
}
45 changes: 0 additions & 45 deletions sample.galah

This file was deleted.

0 comments on commit a00c5ca

Please sign in to comment.