Skip to content

Commit

Permalink
Added more resources. Show GitHub's rate limit error when it happens.
Browse files Browse the repository at this point in the history
  • Loading branch information
diegotl committed Oct 24, 2023
1 parent f0daf19 commit 4860f33
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 12 deletions.
22 changes: 18 additions & 4 deletions Empusa/EmpusaModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,24 @@ final class EmpusaModel: ObservableObject {
loadResourcesVersions()

if !result.failedResources.isEmpty {
alertData = .init(
title: "Alert",
message: "Failed to install \(result.failedResourceNames):\n\n\(result.failedResources.last!.error.localizedDescription)"
)
let rateLimitExceeded = result.failedResources.contains { failedResource in
if case ClientError.rateLimitExceeded = failedResource.error {
return true
}
return false
}

if rateLimitExceeded {
alertData = .init(
title: "Alert",
message: "You've exceeded GitHub's rate limit. Connect to a VPN or wait few hours and try again. The resources that failed to install are: \(result.failedResourceNames)."
)
} else {
alertData = .init(
title: "Alert",
message: "Failed to install \(result.failedResourceNames):\n\n\(result.failedResources.last!.error.localizedDescription)"
)
}
} else {
alertData = .init(
title: "Success",
Expand Down
2 changes: 1 addition & 1 deletion Empusa/UI Components/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct MainView: View {
HStack {
Spacer()

Button("Run") {
Button("Download and install") {
model.execute()
}
.disabled(!model.canStartProcess)
Expand Down
28 changes: 23 additions & 5 deletions Packages/EmpusaKit/Sources/EmpusaKit/Client/Client.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Foundation
import Combine

public enum ClientError: Error {
case rateLimitExceeded
}

protocol ClientProtocol {
func request<T: Decodable>(
url: URL
Expand All @@ -19,11 +23,25 @@ final class Client: NSObject, ClientProtocol {
.shared
.data(for: request)

return try JSONDecoder()
.decode(
T.self,
from: data
)
do {
let decodedResponse = try JSONDecoder()
.decode(
T.self,
from: data
)
return decodedResponse
} catch {
if let decodedErrorResponse = try? JSONDecoder()
.decode(
GitHubErrorResponse.self,
from: data
),
decodedErrorResponse.message.contains("API rate limit exceeded") {
throw ClientError.rateLimitExceeded
}

throw error
}
}

func downloadFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct GitHubErrorResponse: Decodable {
let message: String
}
48 changes: 47 additions & 1 deletion Packages/EmpusaKit/Sources/EmpusaKit/Models/SwitchResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ public enum SwitchResource: String, Codable, CaseIterable {
case nxShell
case goldleaf
case awooInstaller
case tinwooInstaller
case missionControl
case nxGallery
case nxActivityLog
case nxOvlloader
case teslaMenu
case ovlSysmodules
case sysClk

public var displayName: String {
switch self {
Expand All @@ -88,9 +93,14 @@ public enum SwitchResource: String, Codable, CaseIterable {
case .nxShell: "NX-Shell"
case .goldleaf: "Goldleaf"
case .awooInstaller: "Awoo Installer"
case .tinwooInstaller: "TinWoo Installer"
case .missionControl: "MissionControl"
case .nxGallery: "NXGallery"
case .nxActivityLog: "NX Activity Log"
case .nxOvlloader: "nx-ovlloader"
case .teslaMenu: "Tesla-Menu"
case .ovlSysmodules: "ovlSysmodules"
case .sysClk: "sys-clk"
}
}

Expand Down Expand Up @@ -188,6 +198,12 @@ public enum SwitchResource: String, Codable, CaseIterable {
repo: "Awoo-Installer",
assetPrefix: "Awoo-Installer.zip"
)
case .tinwooInstaller:
.github(
owner: "hax4dazy",
repo: "TinWoo",
assetPrefix: "TinWoo-Installer.zip"
)
case .missionControl:
.github(
owner: "ndeadly",
Expand All @@ -206,6 +222,30 @@ public enum SwitchResource: String, Codable, CaseIterable {
repo: "NX-Activity-Log",
assetPrefix: "NX-Activity-Log.nro"
)
case .nxOvlloader:
.github(
owner: "WerWolv",
repo: "nx-ovlloader",
assetPrefix: "nx-ovlloader.zip"
)
case .teslaMenu:
.github(
owner: "WerWolv",
repo: "Tesla-Menu",
assetPrefix: "ovlmenu.zip"
)
case .ovlSysmodules:
.github(
owner: "WerWolv",
repo: "ovl-sysmodules",
assetPrefix: "ovlSysmodules.ovl"
)
case .sysClk:
.github(
owner: "retronx-team",
repo: "sys-clk",
assetPrefix: "sys-clk-"
)
}
}

Expand All @@ -227,9 +267,14 @@ public enum SwitchResource: String, Codable, CaseIterable {
case .nxShell: "NX-Shell.nro"
case .goldleaf: "Goldleaf.nro"
case .awooInstaller: "Awoo-Installer.zip"
case .tinwooInstaller: "TinWoo-Installer.zip"
case .missionControl: "MissionControl.zip"
case .nxGallery: "NXGallery.zip"
case .nxActivityLog: "NX-Activity-Log.nro"
case .nxOvlloader: "nx-ovlloader.zip"
case .teslaMenu: "ovlmenu.zip"
case .ovlSysmodules: "ovlSysmodules.ovl"
case .sysClk: "sys-clk.zip"
}
}

Expand All @@ -246,7 +291,8 @@ public enum SwitchResource: String, Codable, CaseIterable {
switch self {
case .hekateIPL, .emummc, .lockpickRCM, .hbAppStore,
.fusee, .jksv, .ftpd, .nxThemesInstaller,
.nxShell, .goldleaf, .nxActivityLog:
.nxShell, .goldleaf, .nxActivityLog,
.ovlSysmodules:
false
default:
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ extension SwitchResource {
progressSubject: progressSubject
)

case .atmosphere, .sigpatches, .tinfoil, .awooInstaller, .missionControl, .nxGallery:
case .atmosphere, .sigpatches, .tinfoil,
.awooInstaller, .tinwooInstaller, .missionControl,
.nxGallery, .nxOvlloader, .teslaMenu:
fileManager.merge(
from: location,
to: destination,
Expand All @@ -217,6 +219,32 @@ extension SwitchResource {
to: destination.appending(path: "switch"),
progressSubject: progressSubject
)

case .ovlSysmodules:
fileManager.moveFile(
at: location.appending(path: "ovlSysmodules.ovl"),
to: destination.appending(path: "switch").appending(path: ".overlays"),
progressSubject: progressSubject
)

case .sysClk:
fileManager.merge(
from: location.appending(path: "atmosphere"),
to: destination.appending(path: "atmosphere"),
progressSubject: progressSubject
)

fileManager.merge(
from: location.appending(path: "switch"),
to: destination.appending(path: "switch"),
progressSubject: progressSubject
)

fileManager.merge(
from: location.appending(path: "config"),
to: destination.appending(path: "config"),
progressSubject: progressSubject
)
}
}
}

0 comments on commit 4860f33

Please sign in to comment.