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 cd95cd68f8
commit ffa46ae2d0
3 changed files with 109 additions and 84 deletions

View file

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

View file

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

View file

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