66// Copyright © 2021 Rive. All rights reserved.
77//
88
9-
109#import < Rive.h>
1110#import < RivePrivateHeaders.h>
1211
@@ -32,23 +31,6 @@ @implementation RiveFile {
3231+ (uint)majorVersion { return UInt8 (rive::File::majorVersion); }
3332+ (uint)minorVersion { return UInt8 (rive::File::minorVersion); }
3433
35- - (void ) import : (rive::BinaryReader)reader {
36- rive::ImportResult result = rive::File::import (reader, &riveFile);
37- if (result == rive::ImportResult::success) {
38- return ;
39- }
40- else if (result == rive::ImportResult::unsupportedVersion){
41- @throw [[RiveException alloc ] initWithName: @" UnsupportedVersion" reason: @" Unsupported Rive File Version." userInfo: nil ];
42-
43- }
44- else if (result == rive::ImportResult::malformed){
45- @throw [[RiveException alloc ] initWithName: @" Malformed" reason: @" Malformed Rive File." userInfo: nil ];
46- }
47- else {
48- @throw [[RiveException alloc ] initWithName: @" Unknown" reason: @" Unknown error loading file." userInfo: nil ];
49- }
50- }
51-
5234- (nullable instancetype )initWithByteArray : (NSArray *)array {
5335 if (self = [super init ]) {
5436 UInt8* bytes;
@@ -60,6 +42,7 @@ - (nullable instancetype)initWithByteArray:(NSArray *)array {
6042 }];
6143 rive::BinaryReader reader = [self getReader: bytes byteLength: array.count];
6244 [self import: reader];
45+ self.isLoaded = true ;
6346 }
6447 @finally {
6548 free (bytes);
@@ -74,11 +57,88 @@ - (nullable instancetype)initWithBytes:(UInt8 *)bytes byteLength:(UInt64)length
7457 if (self = [super init ]) {
7558 rive::BinaryReader reader = [self getReader: bytes byteLength: length];
7659 [self import: reader];
60+ self.isLoaded = true ;
61+ return self;
62+ }
63+ return nil ;
64+ }
65+
66+ /*
67+ * Creates a RiveFile from a binary resource
68+ */
69+ - (nullable instancetype )initWithResource : (NSString *)resourceName withExtension : (NSString *)extension {
70+ NSString *filepath = [[NSBundle mainBundle ] pathForResource: resourceName ofType: extension];
71+ NSURL *fileUrl = [NSURL fileURLWithPath: filepath];
72+ NSData *fileData = [NSData dataWithContentsOfURL: fileUrl];
73+ UInt8 *bytePtr = (UInt8 *)[fileData bytes ];
74+
75+ return [[RiveFile alloc ] initWithBytes: bytePtr byteLength: fileData.length];
76+ }
77+
78+ /*
79+ * Creates a RiveFile from a binary resource, and assumes the resource extension is '.riv'
80+ */
81+ - (nullable instancetype )initWithResource : (NSString *)resourceName {
82+ return [[RiveFile alloc ] initWithResource: resourceName withExtension: @" riv" ];
83+ }
84+
85+ /*
86+ * Creates a RiveFile from an HTTP url
87+ */
88+ - (nullable instancetype )initWithHttpUrl : (NSString *)url withDelegate : (id <RiveFileDelegate>)delegate {
89+ self.isLoaded = false ;
90+ if (self = [super init ]) {
91+ self.delegate = delegate;
92+ // Set up the http download task
93+ NSURL *URL = [NSURL URLWithString: url];
94+ NSURLSession *session = [NSURLSession sessionWithConfiguration:
95+ [NSURLSessionConfiguration defaultSessionConfiguration ]];
96+ NSURLSessionTask *task = [session downloadTaskWithURL: URL
97+ completionHandler: ^(NSURL *location, NSURLResponse *response, NSError *error) {
98+ if (!error) {
99+ // Load the data into the reader
100+ NSData *data = [NSData dataWithContentsOfURL: location];
101+ UInt8 *bytes = (UInt8 *)[data bytes ];
102+ rive::BinaryReader reader = [self getReader: bytes byteLength: [data length ]];
103+ [self import: reader];
104+ self.isLoaded = true ;
105+ dispatch_async (dispatch_get_main_queue (), ^{
106+ if ([[NSThread currentThread ] isMainThread ]) {
107+ if ([self .delegate respondsToSelector: @selector (riveFileDidLoad: )]) {
108+ [self .delegate riveFileDidLoad: self ];
109+ }
110+ }
111+ });
112+ }
113+ }];
114+
115+ // Kick off the http download
116+ [task resume ];
117+
118+ // Return the as yet uninitialized RiveFile
77119 return self;
78120 }
121+
79122 return nil ;
80123}
81124
125+ - (void ) import : (rive::BinaryReader)reader {
126+ rive::ImportResult result = rive::File::import (reader, &riveFile);
127+ if (result == rive::ImportResult::success) {
128+ return ;
129+ }
130+ else if (result == rive::ImportResult::unsupportedVersion){
131+ @throw [[RiveException alloc ] initWithName: @" UnsupportedVersion" reason: @" Unsupported Rive File Version." userInfo: nil ];
132+
133+ }
134+ else if (result == rive::ImportResult::malformed){
135+ @throw [[RiveException alloc ] initWithName: @" Malformed" reason: @" Malformed Rive File." userInfo: nil ];
136+ }
137+ else {
138+ @throw [[RiveException alloc ] initWithName: @" Unknown" reason: @" Unknown error loading file." userInfo: nil ];
139+ }
140+ }
141+
82142- (RiveArtboard *)artboard {
83143 rive::Artboard *artboard = riveFile->artboard ();
84144 if (artboard == nullptr ) {
0 commit comments