Skip to content

Instantly share code, notes, and snippets.

@jarrodnorwell
Last active May 15, 2024 15:35
Show Gist options
  • Save jarrodnorwell/21a50cd58145eeb7dd8cab686454b20a to your computer and use it in GitHub Desktop.
Save jarrodnorwell/21a50cd58145eeb7dd8cab686454b20a to your computer and use it in GitHub Desktop.
//
// LibraryManager.swift
// Folium
//
// Created by Jarrod Norwell on 15/5/2024.
//
import Foundation
class LibraryManager {
static let shared = LibraryManager()
var cytrusManager = CytrusManager.shared
var grapeManager = GrapeManager.shared
var kiwiManager = KiwiManager.shared
func library() throws {
func crawler(_ core: Core) throws -> (core: Core, urls: [URL]) {
enum CrawlerError : Error {
case enumeratorFailed
}
let documentsDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
guard let enumerator = FileManager.default.enumerator(at: documentsDirectory.appendingPathComponent(core.rawValue, conformingTo: .folder),
includingPropertiesForKeys: [.isRegularFileKey]) else {
throw CrawlerError.enumeratorFailed
}
return (core, try enumerator.reduce(into: [URL]()) { partialResult, element in
switch element {
case let url as URL:
let attributes = try url.resourceValues(forKeys: [.isRegularFileKey])
if let isRegularFile = attributes.isRegularFile, isRegularFile {
let pathExtension = url.pathExtension.lowercased()
switch core {
case .cytrus:
if ["3ds", "3dsx", "app", "cci", "cxi"].contains(pathExtension) {
partialResult.append(url)
}
case .grape:
if ["gba", "nds"].contains(pathExtension) {
partialResult.append(url)
}
case .kiwi:
if ["nes"].contains(pathExtension) {
partialResult.append(url)
}
}
}
default:
break
}
})
}
// TODO: Add cores to populate additional game information such as the icon, publisher, region, size and title
func games(_ core: Core, _ urls: [URL]) {
urls.forEach { url in
guard let nameWithoutExtension = url.lastPathComponent.components(separatedBy: ".").first else {
return
}
switch core {
case .cytrus:
cytrusManager.cytrus.add(.init(core: core, fileDetails: .init(extension: url.pathExtension.lowercased(), name: url.lastPathComponent,
nameWithoutExtension: nameWithoutExtension,
url: .documentsDirectory), title: ""))
case .grape:
grapeManager.grape.add(.init(core: core, fileDetails: .init(extension: url.pathExtension.lowercased(), name: url.lastPathComponent,
nameWithoutExtension: nameWithoutExtension,
url: .documentsDirectory), title: ""))
case .kiwi:
kiwiManager.kiwi.add(.init(core: core, fileDetails: .init(extension: url.pathExtension.lowercased(), name: url.lastPathComponent,
nameWithoutExtension: nameWithoutExtension,
url: .documentsDirectory), title: ""))
}
}
}
let cytrusCrawler = try crawler(.cytrus)
games(cytrusCrawler.core, cytrusCrawler.urls)
let grapeCrawler = try crawler(.grape)
games(grapeCrawler.core, grapeCrawler.urls)
let kiwiCrawler = try crawler(.kiwi)
games(kiwiCrawler.core, kiwiCrawler.urls)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment