Refactors cheat usage for non-throwing EmulatorCore.deactivateCheat(_:)

This commit is contained in:
Riley Testut 2016-05-28 02:07:57 -05:00
parent fecb1396d2
commit b2bf15a79c
4 changed files with 30 additions and 46 deletions

@ -1 +1 @@
Subproject commit a20b36f96e52bc236d243cd8b1b55fc5e1bbf76c Subproject commit d8532bd184a9a1e87b48ad5ad9f4a6d24d81783f

@ -1 +1 @@
Subproject commit cac97f68eea3eb85471a16b468ae8da6fa1038e6 Subproject commit 58dce561dd80baf954f487fc8fa15aae998559a6

View File

@ -347,9 +347,9 @@ extension EmulationViewController: CheatsViewControllerDelegate
try self.emulatorCore.activateCheat(cheat) try self.emulatorCore.activateCheat(cheat)
} }
func cheatsViewController(cheatsViewController: CheatsViewController, didDeactivateCheat cheat: Cheat) throws func cheatsViewController(cheatsViewController: CheatsViewController, didDeactivateCheat cheat: Cheat)
{ {
try self.emulatorCore.deactivateCheat(cheat) self.emulatorCore.deactivateCheat(cheat)
} }
private func updateCheats() private func updateCheats()
@ -370,31 +370,26 @@ extension EmulationViewController: CheatsViewControllerDelegate
let cheats = Cheat.instancesWithPredicate(predicate, inManagedObjectContext: backgroundContext, type: Cheat.self) let cheats = Cheat.instancesWithPredicate(predicate, inManagedObjectContext: backgroundContext, type: Cheat.self)
for cheat in cheats for cheat in cheats
{ {
do if cheat.enabled
{ {
if cheat.enabled do
{ {
try self.emulatorCore.activateCheat(cheat) try self.emulatorCore.activateCheat(cheat)
} }
else catch EmulatorCore.CheatError.invalid
{ {
try self.emulatorCore.deactivateCheat(cheat) print("Invalid cheat:", cheat.name, cheat.code)
} }
catch let error as NSError
{
print("Unknown Cheat Error:", error, cheat.name, cheat.code)
}
} }
catch EmulatorCore.CheatError.invalid else
{ {
print("Invalid cheat:", cheat.name, cheat.code) self.emulatorCore.deactivateCheat(cheat)
} }
catch EmulatorCore.CheatError.doesNotExist
{
// Ignore this error, because we could be deactivating a cheat that hasn't yet been activated
print("Cheat does not exist:", cheat.name, cheat.code)
}
catch let error as NSError
{
print("Unknown Cheat Error:", error, cheat.name, cheat.code)
}
} }
if running if running

View File

@ -17,7 +17,7 @@ protocol CheatsViewControllerDelegate: class
{ {
func cheatsViewControllerActiveEmulatorCore(saveStatesViewController: CheatsViewController) -> EmulatorCore func cheatsViewControllerActiveEmulatorCore(saveStatesViewController: CheatsViewController) -> EmulatorCore
func cheatsViewController(cheatsViewController: CheatsViewController, didActivateCheat cheat: Cheat) throws func cheatsViewController(cheatsViewController: CheatsViewController, didActivateCheat cheat: Cheat) throws
func cheatsViewController(cheatsViewController: CheatsViewController, didDeactivateCheat cheat: Cheat) throws func cheatsViewController(cheatsViewController: CheatsViewController, didDeactivateCheat cheat: Cheat)
} }
class CheatsViewController: UITableViewController class CheatsViewController: UITableViewController
@ -121,7 +121,7 @@ private extension CheatsViewController
func deleteCheat(cheat: Cheat) func deleteCheat(cheat: Cheat)
{ {
let _ = try? self.delegate.cheatsViewController(self, didDeactivateCheat: cheat) self.delegate.cheatsViewController(self, didDeactivateCheat: cheat)
let backgroundContext = DatabaseManager.sharedManager.backgroundManagedObjectContext() let backgroundContext = DatabaseManager.sharedManager.backgroundManagedObjectContext()
backgroundContext.performBlock { backgroundContext.performBlock {
@ -191,30 +191,27 @@ extension CheatsViewController
let temporaryCheat = backgroundContext.objectWithID(cheat.objectID) as! Cheat let temporaryCheat = backgroundContext.objectWithID(cheat.objectID) as! Cheat
temporaryCheat.enabled = !temporaryCheat.enabled temporaryCheat.enabled = !temporaryCheat.enabled
do if temporaryCheat.enabled
{ {
if temporaryCheat.enabled do
{ {
try self.delegate.cheatsViewController(self, didActivateCheat: temporaryCheat) try self.delegate.cheatsViewController(self, didActivateCheat: temporaryCheat)
} }
else catch EmulatorCore.CheatError.invalid
{ {
try self.delegate.cheatsViewController(self, didDeactivateCheat: temporaryCheat) print("Invalid cheat:", cheat.name, cheat.code)
}
catch let error as NSError
{
print("Unknown Cheat Error:", error, cheat.name, cheat.code)
} }
} }
catch EmulatorCore.CheatError.invalid else
{ {
print("Invalid cheat:", cheat.name, cheat.code) self.delegate.cheatsViewController(self, didDeactivateCheat: temporaryCheat)
}
catch EmulatorCore.CheatError.doesNotExist
{
print("Cheat does not exist:", cheat.name, cheat.code)
}
catch let error as NSError
{
print("Unknown Cheat Error:", error, cheat.name, cheat.code)
} }
backgroundContext.saveWithErrorLogging() backgroundContext.saveWithErrorLogging()
} }
@ -281,22 +278,14 @@ extension CheatsViewController: EditCheatViewControllerDelegate
guard previousCheat.code != code else { return } guard previousCheat.code != code else { return }
do self.delegate.cheatsViewController(self, didDeactivateCheat: previousCheat)
{
try self.delegate.cheatsViewController(self, didDeactivateCheat: previousCheat)
}
catch let error as NSError
{
print(error)
}
}) })
} }
} }
func editCheatViewController(editCheatViewController: EditCheatViewController, deactivateCheat cheat: Cheat) func editCheatViewController(editCheatViewController: EditCheatViewController, deactivateCheat cheat: Cheat)
{ {
let _ = try? self.delegate.cheatsViewController(self, didDeactivateCheat: cheat) self.delegate.cheatsViewController(self, didDeactivateCheat: cheat)
} }
} }