Skip to content

Commit

Permalink
Add NSError errors in place of NSException
Browse files Browse the repository at this point in the history
  • Loading branch information
avivian committed Jun 24, 2021
1 parent f7c3e11 commit 0daa4fe
Show file tree
Hide file tree
Showing 22 changed files with 557 additions and 437 deletions.
3 changes: 1 addition & 2 deletions Source/Renderer/Rive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#import "RivePrivateHeaders.h"
#import "RiveRenderer.hpp"

@implementation RiveException
@end
NSString *const RiveErrorDomain = @"rive.app.ios.runtime";

/*
* RiveRenderer
Expand Down
36 changes: 20 additions & 16 deletions Source/Renderer/RiveArtboard.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,40 @@ - (NSInteger)animationCount {
}

// Returns the first animation in the artboard, or null if it has none
- (RiveLinearAnimation *)firstAnimation {
- (RiveLinearAnimation *)firstAnimation:(NSError**) error {
rive::LinearAnimation *animation = _artboard->firstAnimation();
if (animation == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoAnimations" reason:@"No Animations found." userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoAnimations userInfo:@{NSLocalizedDescriptionKey: @"No Animations found.", @"name": @"NoAnimations"}];
return nil;
}
else {
return [[RiveLinearAnimation alloc] initWithAnimation:animation];
}

}

- (RiveLinearAnimation *)animationFromIndex:(NSInteger)index {
- (RiveLinearAnimation *)animationFromIndex:(NSInteger)index error:(NSError**) error {
if (index < 0 || index >= [self animationCount]) {
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation found at index %ld.", (long)index] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoAnimationFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No Animation found at index %ld.", (long)index], @"name": @"NoAnimationFound"}];
return nil;
}
return [[RiveLinearAnimation alloc] initWithAnimation: _artboard->animation(index)];
}

- (RiveLinearAnimation *)animationFromName:(NSString *)name {
- (RiveLinearAnimation *)animationFromName:(NSString *)name error:(NSError**) error {
std::string stdName = std::string([name UTF8String]);
rive::LinearAnimation *animation = _artboard->animation(stdName);
if (animation == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoAnimationFound" reason:[NSString stringWithFormat: @"No Animation found with name %@.", name] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoAnimationFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No Animation found with name %@.", name], @"name": @"NoAnimationFound"}];
return nil;
}
return [[RiveLinearAnimation alloc] initWithAnimation: animation];
}

- (NSArray *)animationNames{
NSMutableArray *animationNames = [NSMutableArray array];

for (NSUInteger i=0; i<[self animationCount]; i++){
[animationNames addObject:[[self animationFromIndex: i] name]];
[animationNames addObject:[[self animationFromIndex:i error:nil] name]];
}
return animationNames;
}
Expand All @@ -69,39 +71,41 @@ - (NSInteger)stateMachineCount {
return _artboard->stateMachineCount();
}

- (RiveStateMachine *)firstStateMachine {
- (RiveStateMachine *)firstStateMachine:(NSError**)error {
rive::StateMachine *stateMachine = _artboard->firstStateMachine();
if (stateMachine == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoStateMachines" reason:@"No State Machines found." userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoStateMachines userInfo:@{NSLocalizedDescriptionKey: @"No State Machines found.", @"name": @"NoStateMachines"}];
return nil;
}
else {
return [[RiveStateMachine alloc] initWithStateMachine:stateMachine];
}
}

// Returns a state machine at the given index, or null if the index is invalid
- (RiveStateMachine *)stateMachineFromIndex:(NSInteger)index {
- (RiveStateMachine *)stateMachineFromIndex:(NSInteger)index error:(NSError**)error {
if (index < 0 || index >= [self stateMachineCount]) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineFound" reason:[NSString stringWithFormat: @"No State Machine found at index %ld.", (long)index] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoStateMachineFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No State Machine found at index %ld.", (long)index], @"name": @"NoStateMachineFound"}];
return nil;
}
return [[RiveStateMachine alloc] initWithStateMachine: _artboard->stateMachine(index)];
}

// Returns a state machine with the given name, or null if none exists
- (RiveStateMachine *)stateMachineFromName:(NSString *)name {
- (RiveStateMachine *)stateMachineFromName:(NSString *)name error:(NSError**)error {
std::string stdName = std::string([name UTF8String]);
rive::StateMachine *machine = _artboard->stateMachine(stdName);
if (machine == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineFound" reason:[NSString stringWithFormat: @"No State Machine found with name %@.", name] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoStateMachineFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No State Machine found with name %@.", name], @"name": @"NoStateMachineFound"}];
return nil;
}
return [[RiveStateMachine alloc] initWithStateMachine: machine];
}

- (NSArray *)stateMachineNames{
NSMutableArray *stateMachineNames = [NSMutableArray array];

for (NSUInteger i=0; i<[self stateMachineCount]; i++){
[stateMachineNames addObject:[[self stateMachineFromIndex: i] name]];
[stateMachineNames addObject:[[self stateMachineFromIndex:i error:nil] name]];
}
return stateMachineNames;
}
Expand Down
63 changes: 37 additions & 26 deletions Source/Renderer/RiveFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ @implementation RiveFile {
+ (uint)majorVersion { return UInt8(rive::File::majorVersion); }
+ (uint)minorVersion { return UInt8(rive::File::minorVersion); }

- (nullable instancetype)initWithByteArray:(NSArray *)array {
- (nullable instancetype)initWithByteArray:(NSArray *)array error:(NSError**)error {
if (self = [super init]) {
UInt8* bytes;
@try {
Expand All @@ -41,7 +41,10 @@ - (nullable instancetype)initWithByteArray:(NSArray *)array {
bytes[index] = number.unsignedIntValue;
}];
rive::BinaryReader reader = [self getReader:bytes byteLength:array.count];
[self import:reader];
BOOL ok = [self import:reader error:error];
if (!ok) {
return nil;
}
self.isLoaded = true;
}
@finally {
Expand All @@ -53,10 +56,13 @@ - (nullable instancetype)initWithByteArray:(NSArray *)array {
return nil;
}

- (nullable instancetype)initWithBytes:(UInt8 *)bytes byteLength:(UInt64)length {
- (nullable instancetype)initWithBytes:(UInt8 *)bytes byteLength:(UInt64)length error:(NSError**)error {
if (self = [super init]) {
rive::BinaryReader reader = [self getReader:bytes byteLength:length];
[self import:reader];
BOOL ok = [self import:reader error:error];
if (!ok) {
return nil;
}
self.isLoaded = true;
return self;
}
Expand All @@ -66,20 +72,19 @@ - (nullable instancetype)initWithBytes:(UInt8 *)bytes byteLength:(UInt64)length
/*
* Creates a RiveFile from a binary resource
*/
- (nullable instancetype)initWithResource:(NSString *)resourceName withExtension:(NSString *)extension {
- (nullable instancetype)initWithResource:(NSString *)resourceName withExtension:(NSString *)extension error:(NSError**)error {
NSString *filepath = [[NSBundle mainBundle] pathForResource:resourceName ofType:extension];
NSURL *fileUrl = [NSURL fileURLWithPath:filepath];
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
UInt8 *bytePtr = (UInt8 *)[fileData bytes];

return [self initWithBytes:bytePtr byteLength:fileData.length];
return [self initWithBytes:bytePtr byteLength:fileData.length error:error];
}

/*
* Creates a RiveFile from a binary resource, and assumes the resource extension is '.riv'
*/
- (nullable instancetype)initWithResource:(NSString *)resourceName {
return [self initWithResource:resourceName withExtension:@"riv"];
- (nullable instancetype)initWithResource:(NSString *)resourceName error:(NSError**)error {
return [self initWithResource:resourceName withExtension:@"riv" error:error];
}

/*
Expand All @@ -100,12 +105,15 @@ - (nullable instancetype)initWithHttpUrl:(NSString *)url withDelegate:(id<RiveFi
NSData *data = [NSData dataWithContentsOfURL: location];
UInt8 *bytes = (UInt8 *)[data bytes];
rive::BinaryReader reader = [self getReader:bytes byteLength:[data length]];
[self import:reader];
// TODO: Do something with this error the proper way with delegates.
NSError* error = nil;
[self import:reader error:&error];
self.isLoaded = true;
dispatch_async(dispatch_get_main_queue(), ^{
if ([[NSThread currentThread] isMainThread]) {
if ([self.delegate respondsToSelector:@selector(riveFileDidLoad:)]) {
[self.delegate riveFileDidLoad:self];
NSError * error = nil;
[self.delegate riveFileDidLoad:self error:&error];
}
}
});
Expand All @@ -122,51 +130,55 @@ - (nullable instancetype)initWithHttpUrl:(NSString *)url withDelegate:(id<RiveFi
return nil;
}

- (void) import:(rive::BinaryReader)reader {
- (BOOL) import:(rive::BinaryReader)reader error:(NSError**)error {
rive::ImportResult result = rive::File::import(reader, &riveFile);
if (result == rive::ImportResult::success) {
return;
return true;
}
else if(result == rive::ImportResult::unsupportedVersion){
@throw [[RiveException alloc] initWithName:@"UnsupportedVersion" reason:@"Unsupported Rive File Version." userInfo:nil];

*error = [NSError errorWithDomain:RiveErrorDomain code:RiveUnsupportedVersion userInfo:@{NSLocalizedDescriptionKey: @"Unsupported Rive File Version", @"name": @"UnsupportedVersion"}];
return false;
}
else if(result == rive::ImportResult::malformed){
@throw [[RiveException alloc] initWithName:@"Malformed" reason:@"Malformed Rive File." userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveMalformedFile userInfo:@{NSLocalizedDescriptionKey: @"Malformed Rive File.", @"name": @"Malformed"}];
return false;
}
else {
@throw [[RiveException alloc] initWithName:@"Unknown" reason:@"Unknown error loading file." userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveUnknownError userInfo:@{NSLocalizedDescriptionKey: @"Unknown error loading file.", @"name": @"Unknown"}];
return false;
}
}

- (RiveArtboard *)artboard {
- (RiveArtboard *)artboard:(NSError**)error {
rive::Artboard *artboard = riveFile->artboard();
if (artboard == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoArtboardsFound" reason: @"No Artboards Found." userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoArtboardsFound userInfo:@{NSLocalizedDescriptionKey: @"No Artboards Found.", @"name": @"NoArtboardsFound"}];
return nil;
}
else {
return [[RiveArtboard alloc] initWithArtboard: artboard];
}

}

- (NSInteger)artboardCount {
return riveFile->artboardCount();
}

- (RiveArtboard *)artboardFromIndex:(NSInteger)index {
- (RiveArtboard *)artboardFromIndex:(NSInteger)index error:(NSError**)error {
if (index >= [self artboardCount]) {
@throw [[RiveException alloc] initWithName:@"NoArtboardFound" reason:[NSString stringWithFormat: @"No Artboard Found at index %ld.", (long)index] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoArtboardFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No Artboard Found at index %ld.", (long)index], @"name": @"NoArtboardFound"}];
return nil;
}
return [[RiveArtboard alloc]
initWithArtboard: reinterpret_cast<rive::Artboard *>(riveFile->artboard(index))];
}

- (RiveArtboard *)artboardFromName:(NSString *)name {
- (RiveArtboard *)artboardFromName:(NSString *)name error:(NSError**)error {
std::string stdName = std::string([name UTF8String]);
rive::Artboard *artboard = riveFile->artboard(stdName);
if (artboard == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoArtboardFound" reason:[NSString stringWithFormat: @"No Artboard Found with name %@.", name] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoArtboardFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No Artboard Found with name %@.", name], @"name": @"NoArtboardFound"}];
return nil;
} else {
return [[RiveArtboard alloc] initWithArtboard: artboard];
}
Expand All @@ -176,8 +188,7 @@ - (NSArray *)artboardNames {
NSMutableArray *artboardNames = [NSMutableArray array];

for (NSUInteger i=0; i<[self artboardCount]; i++) {
NSString* name = [[self artboardFromIndex: i] name];
[artboardNames addObject:name];
[artboardNames addObject:[[self artboardFromIndex: i error:nil] name]];
}
return artboardNames;
}
Expand Down
23 changes: 12 additions & 11 deletions Source/Renderer/RiveStateMachine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (RiveStateMachineInstance *)instance {
return [[RiveStateMachineInstance alloc] initWithStateMachine: stateMachine];
}

- (RiveStateMachineInput *)_convertInput:(const rive::StateMachineInput *)input{
- (RiveStateMachineInput *)_convertInput:(const rive::StateMachineInput *)input error:(NSError**)error {
if (input->is<rive::StateMachineBool>()){
return [[RiveStateMachineBoolInput alloc] initWithStateMachineInput: input];
}
Expand All @@ -58,35 +58,36 @@ - (RiveStateMachineInput *)_convertInput:(const rive::StateMachineInput *)input{
return [[RiveStateMachineTriggerInput alloc] initWithStateMachineInput: input];
}
else {
@throw [[RiveException alloc] initWithName:@"UnkownInput" reason: @"Unknown State Machine Input" userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveUnknownStateMachineInput userInfo:@{NSLocalizedDescriptionKey: @"Unknown State Machine Input", @"name": @"UnknownStateMachineInput"}];
return nil;
}
}

// Creates a new instance of this state machine
- (RiveStateMachineInput *)inputFromIndex:(NSInteger)index {
- (RiveStateMachineInput *)inputFromIndex:(NSInteger)index error:(NSError**)error {
if (index >= [self inputCount]) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No Input found at index %ld.", (long)index] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoStateMachineInputFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No Input found at index %ld.", (long)index], @"name": @"NoStateMachineInputFound"}];
return nil;
}
return [self _convertInput: stateMachine->input(index) ];
return [self _convertInput: stateMachine->input(index) error:error];
}

// Creates a new instance of this state machine
- (RiveStateMachineInput *)inputFromName:(NSString*)name {

- (RiveStateMachineInput *)inputFromName:(NSString*)name error:(NSError**)error {
std::string stdName = std::string([name UTF8String]);
const rive::StateMachineInput *stateMachineInput = stateMachine->input(stdName);
if (stateMachineInput == nullptr) {
@throw [[RiveException alloc] initWithName:@"NoStateMachineInputFound" reason:[NSString stringWithFormat: @"No State Machine Input found with name %@.", name] userInfo:nil];
*error = [NSError errorWithDomain:RiveErrorDomain code:RiveNoStateMachineInputFound userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"No State Machine Input found with name %@.", name], @"name": @"NoStateMachineInputFound"}];
return nil;
} else {
return [self _convertInput: stateMachineInput];
return [self _convertInput: stateMachineInput error:error];
}
}

- (NSArray *)inputNames{
NSMutableArray *inputNames = [NSMutableArray array];

for (NSUInteger i=0; i<[self inputCount]; i++){
[inputNames addObject:[[self inputFromIndex: i] name]];
[inputNames addObject:[[self inputFromIndex:i error:nil] name]];
}
return inputNames;
}
Expand Down
Loading

0 comments on commit 0daa4fe

Please sign in to comment.