basic poc app
This commit is contained in:
1 parent
4a2d58f189
commit
59670cb841
6 files changed
+315
-28
No files matched your search
@@ -1,24 +0,0 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// Pocket Poster
|
||||
//
|
||||
// Created by lemin on 5/31/25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
var body: some View {
|
||||
VStack {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundStyle(.tint)
|
||||
Text("Hello, world!")
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
//
|
||||
// PosterBoardManager.swift
|
||||
// Pocket Poster
|
||||
//
|
||||
// Created by lemin on 5/31/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ZIPFoundation
|
||||
|
||||
class PosterBoardManager {
|
||||
private static func unzipFile(at url: URL) throws -> URL {
|
||||
let fileName = url.deletingPathExtension().lastPathComponent
|
||||
let fileData = try Data(contentsOf: url)
|
||||
let fileManager = FileManager()
|
||||
|
||||
// Write the file to the Documents Directory
|
||||
let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
||||
let path = docDir[0]
|
||||
let url = path.appending(path: fileName)
|
||||
|
||||
// Remove All files in this directory
|
||||
let existingFiles = try FileManager.default.contentsOfDirectory(at: path, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
|
||||
for fileUrl in existingFiles
|
||||
{
|
||||
try FileManager.default.removeItem(at: fileUrl)
|
||||
}
|
||||
|
||||
// Save our Zip file
|
||||
try fileData.write(to: url, options: [.atomic])
|
||||
|
||||
// Unzip the Zipped Up File
|
||||
var destinationURL = path
|
||||
if FileManager.default.fileExists(atPath: url.path())
|
||||
{
|
||||
destinationURL.append(path: "directory")
|
||||
try fileManager.unzipItem(at: url, to: destinationURL)
|
||||
}
|
||||
|
||||
return destinationURL
|
||||
}
|
||||
|
||||
static func getDescriptorsFromTendie(_ url: URL) throws -> URL? {
|
||||
let unzippedDir = try unzipFile(at: url)
|
||||
|
||||
for dir in try FileManager.default.contentsOfDirectory(at: unzippedDir, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) {
|
||||
let fileName = dir.lastPathComponent
|
||||
if fileName.lowercased() == "descriptor" || fileName.lowercased() == "descriptors" {
|
||||
return dir
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
static func randomizeWallpaperId(url: URL) throws {
|
||||
let randomizedID = Int.random(in: 9999...99999)
|
||||
var files = [URL]()
|
||||
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
|
||||
for case let fileURL as URL in enumerator
|
||||
{
|
||||
do
|
||||
{
|
||||
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
|
||||
if fileAttributes.isRegularFile!
|
||||
{
|
||||
files.append(fileURL)
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
print(error, fileURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setPlistValue(file: String, key: String, value: Any, recursive: Bool = true) {
|
||||
// thanks gpt
|
||||
guard let plistData = FileManager.default.contents(atPath: file),
|
||||
var plist = try? PropertyListSerialization.propertyList(from: plistData, options: [], format: nil) as? [String: Any] else {
|
||||
return
|
||||
}
|
||||
|
||||
plist[key] = value
|
||||
|
||||
guard let updatedData = try? PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) else {
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
try updatedData.write(to: URL(fileURLWithPath: file))
|
||||
} catch {
|
||||
print("Failed to write updated plist: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
for file in files {
|
||||
switch file.lastPathComponent {
|
||||
case "com.apple.posterkit.provider.descriptor.identifier":
|
||||
try String(randomizedID).data(using: .utf8)?.write(to: file)
|
||||
|
||||
case "com.apple.posterkit.provider.contents.userInfo":
|
||||
setPlistValue(file: file.path(), key: "wallpaperRepresentingIdentifier", value: randomizedID)
|
||||
|
||||
case "Wallpaper.plist":
|
||||
setPlistValue(file: file.path(), key: "identifier", value: randomizedID, recursive: false)
|
||||
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static func applyTendie(_ url: URL, appHash: String) throws {
|
||||
// scope the resource
|
||||
let accessing = url.startAccessingSecurityScopedResource()
|
||||
defer {
|
||||
if accessing {
|
||||
url.stopAccessingSecurityScopedResource()
|
||||
}
|
||||
}
|
||||
|
||||
guard let descriptors = try getDescriptorsFromTendie(url) else { return } // TODO: Add error handling
|
||||
// create the folder
|
||||
for descr in try FileManager.default.contentsOfDirectory(at: descriptors, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) {
|
||||
if descr.lastPathComponent != "__MACOSX" {
|
||||
try randomizeWallpaperId(url: descr)
|
||||
let newURL = SymHandler.getDocumentsDirectory().appendingPathComponent(UUID().uuidString, conformingTo: .directory)
|
||||
try FileManager.default.moveItem(at: descr, to: newURL)
|
||||
|
||||
let _ = try SymHandler.createDescriptorsSymlink(appHash: appHash)
|
||||
do {
|
||||
try FileManager.default.trashItem(at: newURL, resultingItemURL: nil)
|
||||
} catch {
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
SymHandler.cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
try? FileManager.default.removeItem(at: descriptors)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// SymHandler.swift
|
||||
// Pocket Poster
|
||||
//
|
||||
// Created by lemin on 5/31/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class SymHandler {
|
||||
// MARK: URL Getter Operations
|
||||
static func getDocumentsDirectory() -> URL {
|
||||
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
||||
let documentsDirectory = paths[0]
|
||||
return documentsDirectory
|
||||
}
|
||||
|
||||
private static func getSymlinkURL() -> URL {
|
||||
return getDocumentsDirectory().appendingPathComponent(".Trash", conformingTo: .symbolicLink)
|
||||
}
|
||||
|
||||
// MARK: Symlink Creation
|
||||
static func createSymlink(to path: String) throws -> URL {
|
||||
// returns the url of the symlink
|
||||
let symURL = getSymlinkURL()
|
||||
cleanup()
|
||||
|
||||
// create the symlink to the hashed app folder
|
||||
try FileManager.default.createSymbolicLink(at: symURL, withDestinationURL: URL(fileURLWithPath: path, isDirectory: true))
|
||||
|
||||
return symURL
|
||||
}
|
||||
|
||||
static func createAppSymlink(for appHash: String) throws -> URL {
|
||||
return try createSymlink(to: "/var/mobile/Containers/Data/Application/\(appHash)")
|
||||
}
|
||||
|
||||
static func createDescriptorsSymlink(appHash: String) throws -> URL {
|
||||
// create a symlink directly to the descriptors
|
||||
return try createAppSymlink(for: "\(appHash)/Library/Application Support/PRBPosterExtensionDataStore/61/Extensions/com.apple.WallpaperKit.CollectionsPoster/descriptors")
|
||||
}
|
||||
|
||||
static func cleanup() {
|
||||
// remove the symlink if it exists
|
||||
let symURL = getSymlinkURL()
|
||||
// remove existing symlink
|
||||
try? FileManager.default.removeItem(at: symURL)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// ContentView.swift
|
||||
// Pocket Poster
|
||||
//
|
||||
// Created by lemin on 5/31/25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
struct ContentView: View {
|
||||
// Prefs
|
||||
@AppStorage("pbHash") var pbHash: String = ""
|
||||
|
||||
@State var showTendiesImporter: Bool = false
|
||||
@State var selectedTendie: URL? = nil
|
||||
|
||||
@State var showErrorAlert = false
|
||||
@State var lastError: String?
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
TextField("App Hash", text: $pbHash)
|
||||
|
||||
Button(action: {
|
||||
showTendiesImporter.toggle()
|
||||
}) {
|
||||
Text("Select Tendie")
|
||||
}
|
||||
|
||||
if let selectedTendie = selectedTendie {
|
||||
Text("Selected Tendie: \(selectedTendie)")
|
||||
if pbHash != "" {
|
||||
Button(action: {
|
||||
do {
|
||||
try PosterBoardManager.applyTendie(selectedTendie, appHash: pbHash)
|
||||
lastError = "Success. Delete in files app."
|
||||
showErrorAlert.toggle()
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
showErrorAlert.toggle()
|
||||
}
|
||||
}) {
|
||||
Text("Apply")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.fileImporter(isPresented: $showTendiesImporter, allowedContentTypes: [UTType(filenameExtension: "tendies", conformingTo: .data)!], onCompletion: { result in
|
||||
switch result {
|
||||
case .success(let url):
|
||||
selectedTendie = url
|
||||
case .failure(let error):
|
||||
lastError = error.localizedDescription
|
||||
showErrorAlert.toggle()
|
||||
}
|
||||
})
|
||||
.alert("Error", isPresented: $showErrorAlert) {
|
||||
Button("OK") {}
|
||||
} message: {
|
||||
Text(lastError ?? "???")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
}
|
||||
Reference in new issue
Block a user