Skip to content

Commit

Permalink
Fix threading issues, create scene based app contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
furby-tm committed Aug 9, 2024
1 parent 2a88c4a commit 6c2c886
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 164 deletions.
3 changes: 2 additions & 1 deletion Kraken.usda
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#usda 1.0
(
doc = "Kraken v1.0.9 | 05-05-2024 16:14:40"
doc = "Kraken v1.1.0 | 08-09-2024 09:20:10"
)

def Xform "Geometry"
Expand Down Expand Up @@ -35,3 +35,4 @@ def "Materials"
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public extension Editor.Code.Language
static let usd: Editor.Code.Language = .init(
id: .usd,
tsName: "usd",
extensions: ["usda"],
extensions: ["usd", "usda", "usdc", "usdz"],
lineCommentString: "#",
rangeCommentStrings: ("", ""),
documentationCommentStrings: [.pair(("\"", "\""))]
Expand Down
26 changes: 14 additions & 12 deletions Sources/Kraken/IO/Hydra/KIO.StageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public extension Kraken.IO
tmpDir.appending(component: "Untitled.usda")
}

public func getRandomTmpURL() -> URL
{
let uuid = UUID().uuidString.suffix(5)
let rdmDir = tmpDir.appending(component: uuid)

return rdmDir.appending(component: "Untitled.usda")
}

public func getStartupURL() -> URL
{
tmpDir.appending(component: "Startup.usda")
}

public func makeTmp() -> Kraken.IO.USD
{
Kraken.IO.USD(fileURL: getTmpURL())
Expand All @@ -127,19 +140,8 @@ public extension Kraken.IO
* - stageData: The stage data to save to the file.
* - file: The usd filepath to save the data to.
* - stage: The stage to save and reload. */
public func save(contentsOfFile stageData: String, atPath file: String, stage: inout UsdStageRefPtr)
public func reloadAndSave(stage: inout UsdStageRefPtr)
{
let fileURL = URL(fileURLWithPath: file)

do
{
try stageData.write(to: fileURL, atomically: true, encoding: .utf8)
}
catch
{
print("Error writing to file: \(fileURL.absoluteString)")
}

stage.reload()
save(&stage)
}
Expand Down
118 changes: 65 additions & 53 deletions Sources/Kraken/IO/Universal/KIO.USD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,20 @@

import CodeLanguages
import CosmoEditor
import PixarUSD
import SwiftUI
import UniformTypeIdentifiers
import PixarUSD

public extension Kraken.IO
{
@Observable
final class USD: ReferenceFileDocument
{
var text: String
var fileURL: URL
var stage: UsdStageRefPtr
var context: Kraken.IO.USD.Context

public init(text: String = "", fileURL: URL = Kraken.IO.Stage.manager.getTmpURL())
public init(fileURL: URL = Kraken.IO.Stage.manager.getRandomTmpURL())
{
self.fileURL = fileURL
self.text = text

if FileManager.default.fileExists(atPath: fileURL.path)
{
self.stage = Usd.Stage.open(fileURL.path)
}
else
{
self.stage = Usd.Stage.createNew(fileURL.path)
}

Kraken.IO.Stage.manager.save(&stage)

var contents = ""
self.stage.exportToString(&contents, addSourceFileComment: false)
self.text = contents
context = Kraken.IO.USD.Context(fileURL: fileURL)
}

public static var readableContentTypes: [UTType]
Expand All @@ -83,51 +65,73 @@ public extension Kraken.IO

public init(configuration: ReadConfiguration) throws
{
let url = Kraken.IO.Stage.manager.getTmpURL()
self.stage = Usd.Stage.open(url.path)
self.fileURL = url
self.text = ""
context = Kraken.IO.USD.Context()

guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else
{
var contents: String = ""
self.stage.exportToString(&contents, addSourceFileComment: false)
Kraken.IO.Stage.manager.save(
contentsOfFile: contents,
atPath: url.path,
stage: &stage
)

self.text = contents
Kraken.IO.Stage.manager.reloadAndSave(stage: &context.stage)

var contents = ""
context.stage.exportToString(&contents, addSourceFileComment: false)
context.usda = contents
return
}
context.usda = string
}

public func fileWrapper(snapshot _: Kraken.IO.USD.Context, configuration _: WriteConfiguration) throws -> FileWrapper
{
context.stage.exportToString(&context.usda, addSourceFileComment: false)

return .init(regularFileWithContents: context.usda.data(using: .utf8)!)
}

Kraken.IO.Stage.manager.save(
contentsOfFile: string,
atPath: url.path,
stage: &stage
)
public func snapshot(contentType _: UTType) throws -> Kraken.IO.USD.Context
{
context
}
}
}

var contents: String = ""
self.stage.exportToString(&contents, addSourceFileComment: false)
public extension Kraken.IO.USD
{
@Observable
class Context: Identifiable
{
public var usda: String
public var fileURL: URL
public var stage: UsdStageRefPtr
public var stageCache: UsdStageCache

self.text = contents
public var id: Int
{
stageCache.GetId(stage).ToLongInt()
}

public func fileWrapper(snapshot: UsdStageRefPtr, configuration: WriteConfiguration) throws -> FileWrapper
public init(fileURL: URL = Kraken.IO.Stage.manager.getRandomTmpURL())
{
var usda = ""
stage.exportToString(&usda, addSourceFileComment: false)
text = usda
self.fileURL = fileURL
usda = ""
stage = Usd.Stage.createNew(fileURL.path)

stageCache = UsdStageCache()
UsdStageCacheContext.bind(cache: &stageCache)

stage = Usd.Stage.open(fileURL.path)

return .init(regularFileWithContents: usda.data(using: .utf8)!)
Kraken.IO.Stage.manager.save(&stage)
stage.exportToString(&usda, addSourceFileComment: false)
}

public func snapshot(contentType: UTType) throws -> UsdStageRefPtr
public func open(fileURL: URL)
{
return self.stage
self.fileURL = fileURL
stage = Usd.Stage.open(fileURL.path)

Kraken.IO.Stage.manager.save(&stage)
stage.exportToString(&self.usda, addSourceFileComment: false)
}
}
}
Expand Down Expand Up @@ -155,12 +159,20 @@ public extension ReferenceFileDocumentConfiguration<Kraken.IO.USD>
}
}

extension Kraken.IO.USD: Hashable
{
public func hash(into hasher: inout Hasher)
{
hasher.combine(context.fileURL)
}
}

extension Kraken.IO.USD: Equatable
{
public static func == (lhs: Kraken.IO.USD, rhs: Kraken.IO.USD) -> Bool
{
return
lhs.fileURL == rhs.fileURL &&
lhs.text == rhs.text
lhs.context.fileURL == rhs.context.fileURL &&
lhs.context.id == rhs.context.id &&
lhs.context.usda == rhs.context.usda
}
}
9 changes: 5 additions & 4 deletions Sources/Kraken/KR/KR.Kraken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct Kraken: SwiftUI.App
/* --- xxx --- */

/** The kraken universal scene description context. */
@State private var C = Kraken.IO.USD()
@State private var C = Kraken.IO.USD(fileURL: Kraken.IO.Stage.manager.getStartupURL())

/* --- xxx --- */

Expand All @@ -69,7 +69,7 @@ public struct Kraken: SwiftUI.App

public init()
{
Kraken.IO.Stage.manager.save(&C.stage)
Kraken.IO.Stage.manager.save(&C.context.stage)

Msg.logger.info("\(Kraken.versionInfo())")
Msg.logger.info("Kraken launched.")
Expand All @@ -80,7 +80,8 @@ public struct Kraken: SwiftUI.App
public var body: some SwiftUI.Scene
{
Kraken.UI.MicaWindow(title: "Kraken", id: "kraken")
{
{ context in

if showSplash
{
Kraken.UI.SplashScreen(
Expand All @@ -96,7 +97,7 @@ public struct Kraken: SwiftUI.App
{
createMainMenu()

Kraken.UI.UniversalContext()
Kraken.UI.UniversalContext(context: context)
.environment(C)
}
}
Expand Down
Loading

0 comments on commit 6c2c886

Please sign in to comment.