OpenMPT: Update exception handling

Make exception handling more robust and thorough. Never
know what may happen, make sure to handle most cases.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2025-01-30 01:02:32 -08:00
parent fd3e9e2b24
commit 8eddb7bf40
3 changed files with 109 additions and 84 deletions

View file

@ -37,20 +37,20 @@
id<CogSource> source = [audioSourceClass audioSourceForURL:url];
if(![source open:url])
return 0;
return @[];
if(![source seekable])
return 0;
return @[];
[source seek:0 whence:SEEK_END];
long size = [source tell];
[source seek:0 whence:SEEK_SET];
try {
std::vector<char> data(static_cast<std::size_t>(size));
[source read:data.data() amount:size];
try {
std::map<std::string, std::string> ctls;
openmpt::module *mod = new openmpt::module(data, std::clog, ctls);
@ -66,8 +66,9 @@
}
return tracks;
} catch(std::exception & /*e*/) {
return 0;
} catch(std::exception &e) {
ALog(@"Exception caught while processing with OpenMPT: %s", e.what());
return @[];
}
}

View file

@ -13,6 +13,7 @@
#import "PlaylistController.h"
static void g_push_archive_extensions(std::vector<std::string> &list) {
try {
static std::string archive_extensions[] = {
"mdz", "mdr", "s3z", "xmz", "itz", "mptmz"
};
@ -20,6 +21,9 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
if(list.empty() || std::find(list.begin(), list.end(), archive_extensions[i]) == list.end())
list.push_back(archive_extensions[i]);
}
} catch (std::exception &e) {
ALog(@"Exception caught pushing archive extensions for OpenMPT: %s", e.what());
}
}
@implementation OMPTDecoder
@ -46,6 +50,7 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
sampleRate = 192000.0;
}
try {
std::vector<char> data(static_cast<std::size_t>(size));
[source read:data.data() amount:size];
@ -70,8 +75,6 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
interp = 4;
else if([resampling isEqualToString:@"sinc"])
interp = 8;
try {
std::map<std::string, std::string> ctls;
ctls["seek.sync_samples"] = "1";
mod = new openmpt::module(data, std::clog, ctls);
@ -86,7 +89,8 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
mod->set_render_param(openmpt::module::RENDER_INTERPOLATIONFILTER_LENGTH, interp);
mod->set_render_param(openmpt::module::RENDER_VOLUMERAMPING_STRENGTH, -1);
mod->ctl_set_boolean("render.resampler.emulate_amiga", true);
} catch(std::exception & /*e*/) {
} catch(std::exception &e) {
ALog(@"Exception caught opening module with OpenMPT: %s", e.what());
return NO;
}
@ -113,6 +117,7 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
}
- (AudioChunk *)readAudio {
try {
mod->set_repeat_count(IsRepeatOneSet() ? -1 : 0);
int frames = 1024;
@ -140,16 +145,29 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
[chunk assignSamples:buffer frameCount:total];
return chunk;
} catch (std::exception &e) {
ALog(@"Exception caught while playing with OpenMPT: %s", e.what());
return nil;
}
}
- (long)seek:(long)frame {
try {
mod->set_position_seconds(frame / sampleRate);
} catch (std::exception &e) {
ALog(@"Exception caught while seeking with OpenMPT: %s", e.what());
return -1;
}
return frame;
}
- (void)cleanUp {
try {
delete mod;
} catch (std::exception &e) {
ALog(@"Exception caught while deleting instance of OpenMPT: %s", e.what());
}
mod = NULL;
}
@ -170,6 +188,7 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
}
+ (NSArray *)fileTypes {
try {
std::vector<std::string> extensions = openmpt::get_supported_extensions();
g_push_archive_extensions(extensions);
NSMutableArray *array = [NSMutableArray array];
@ -179,6 +198,10 @@ static void g_push_archive_extensions(std::vector<std::string> &list) {
}
return [NSArray arrayWithArray:array];
} catch (std::exception &e) {
ALog(@"Exception caught while enumerating OpenMPT file extensions: %s", e.what());
return @[];
}
}
+ (NSArray *)mimeTypes {

View file

@ -41,15 +41,16 @@ static void setDictionary(NSMutableDictionary *dict, NSString *tag, NSString *va
id<CogSource> source = [audioSourceClass audioSourceForURL:url];
if(![source open:url])
return 0;
return @{};
if(![source seekable])
return 0;
return @{};
[source seek:0 whence:SEEK_END];
long size = [source tell];
[source seek:0 whence:SEEK_SET];
try {
std::vector<char> data(static_cast<std::size_t>(size));
[source read:data.data() amount:size];
@ -60,7 +61,6 @@ static void setDictionary(NSMutableDictionary *dict, NSString *tag, NSString *va
else
track_num = [[url fragment] intValue];
try {
std::map<std::string, std::string> ctls;
openmpt::module *mod = new openmpt::module(data, std::clog, ctls);
@ -85,8 +85,9 @@ static void setDictionary(NSMutableDictionary *dict, NSString *tag, NSString *va
delete mod;
return dict;
} catch(std::exception & /*e*/) {
return 0;
} catch(std::exception &e) {
ALog(@"Exception caught while reading metadata with OpenMPT: %s", e.what());
return @{};
}
}