Skip to content

Commit

Permalink
Maintain relative resource paths in PDX (#103)
Browse files Browse the repository at this point in the history
* Rename Core dir to PlaydateKit

With relative resource paths, this will make loading PlaydateKit resources require the PlaydateKit prefix which is less likely to collide with users own directory names.

* Use relative resource paths in PDX root
  • Loading branch information
STREGA authored Nov 21, 2024
1 parent a04bbdb commit 473a271
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 15 deletions.
52 changes: 40 additions & 12 deletions Plugins/PDCPlugin/PDCPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ struct ModuleBuildRequest {

let productSource = context.package.sourceModules.first!
let productSwiftFiles = productSource.sourceFiles(withSuffix: "swift").map(\.path.string)
let productResources = productSource.sourceFiles
.filter { $0.type == .unknown }
.map(\.path)

let playdateKitResources = playdateKitSource.sourceFiles(withSuffix: "png")
.filter { $0.type == .unknown }
.map(\.path)

let playdateKit = ModuleBuildRequest(name: "playdatekit", type: .playdateKit, relativePath: modulesPath, sourcefiles: playdateKitSwiftFiles)
let product = ModuleBuildRequest(name: productName, type: .product, relativePath: context.pluginWorkDirectory, sourcefiles: productSwiftFiles)
Expand Down Expand Up @@ -227,12 +220,47 @@ struct ModuleBuildRequest {
atPath: sourcePath.string,
withIntermediateDirectories: true
)
print("copying resources")
for resource in productResources + playdateKitResources {
print(resource.string)

print("copying resources...")
// Create list or resources including a relative path
var resourcePaths: [(path: String, relativePath: String)] = []

// Scan package and dependencies for resources
for package in context.package.dependencies.map(\.package) + [context.package] {
for module in package.sourceModules {
let moduleResources = module.sourceFiles.filter { $0.type == .unknown }.map(\.path)
for resource in moduleResources {
let relativePrefix = module.directory.string + "/Resources/"
// Only copy resource from the Package's "Resources" directory
guard resource.string.hasPrefix(relativePrefix) else {continue}
let relativePath = resource.string.replacingOccurrences(of: relativePrefix, with: "")
resourcePaths.append((resource.string, relativePath))
}
}
}

// Copy resources
for resource in resourcePaths {
let dest = sourcePath.appending([resource.relativePath])
let destDirectory = dest.removingLastComponent()

var isDirectory: ObjCBool = false
if !FileManager.default.fileExists(atPath: destDirectory.string, isDirectory: &isDirectory) {
let relativeDestDirectory = Path(resource.relativePath).removingLastComponent()
print("creating directory \(relativeDestDirectory.string)/")
try FileManager.default.createDirectory(atPath: destDirectory.string, withIntermediateDirectories: true)
}

// If the resource is pdxinfo, always place it in the pdx root
var destination = dest.string
if resource.path.hasSuffix("pdxinfo") {
destination = sourcePath.appending(["pdxinfo"]).string
}

print("copying \(resource.relativePath)")
try FileManager.default.copyItem(
atPath: resource.string,
toPath: sourcePath.appending([resource.lastComponent]).string
atPath: resource.path,
toPath: destination
)
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/PlaydateKit/Core/UI/UI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public enum UI {

crankIndicatorY = 210 / Int(scale.rawValue)

let imagePath = "crank-notice-bubble-\(scale.rawValue)x.png"
let imagePath = "PlaydateKit/UI/images/crank/crank-notice-bubble-\(scale.rawValue)x.png"
bubbleImage = try Graphics.Bitmap(path: imagePath)

if let bubbleImage {
Expand All @@ -163,12 +163,12 @@ public enum UI {
textOffset = 76 / Int(scale.rawValue)
}

crankFrames = try Graphics.BitmapTable(path: "crank-frames-\(scale.rawValue)x.png")
crankFrames = try Graphics.BitmapTable(path: "PlaydateKit/UI/images/crank/crank-frames-\(scale.rawValue)x.png")
frameCount = (crankFrames?.imageCount ?? 0) * 3

switch scale {
case .oneTimes, .twoTimes:
textFrameImage = try Graphics.Bitmap(path: "crank-notice-text-\(scale.rawValue)x.png")
textFrameImage = try Graphics.Bitmap(path: "PlaydateKit/UI/images/crank/crank-notice-text-\(scale.rawValue)x.png")
textFrameCount = 14
frameCount += textFrameCount
default:
Expand Down

0 comments on commit 473a271

Please sign in to comment.