Fix a crash when the rating index is outside the bounds of the ratings codes. This happens when a users changes the ratings settings in the preferences.
This commit is contained in:
1 parent
acbc6a3f93
commit
0fdad0eebf
2 files changed
+43
-22
No files matched your search
+27
-17
@@ -1324,51 +1324,61 @@ static const genreType_t genreType_strings[] = {
|
|||||||
/* Rewrite extended metadata using the generic iTMF api */
|
/* Rewrite extended metadata using the generic iTMF api */
|
||||||
|
|
||||||
if ([tagsDict valueForKey:@"Rating"]) {
|
if ([tagsDict valueForKey:@"Rating"]) {
|
||||||
MP4ItmfItemList* list = MP4ItmfGetItemsByMeaning(fileHandle, "com.apple.iTunes", "iTunEXTC");
|
MP4ItmfItemList *list = MP4ItmfGetItemsByMeaning(fileHandle, "com.apple.iTunes", "iTunEXTC");
|
||||||
if (list) {
|
if (list) {
|
||||||
uint32_t i;
|
for (uint32_t i = 0; i < list->size; i++) {
|
||||||
for (i = 0; i < list->size; i++) {
|
MP4ItmfItem *item = &list->elements[i];
|
||||||
MP4ItmfItem* item = &list->elements[i];
|
|
||||||
MP4ItmfRemoveItem(fileHandle, item);
|
MP4ItmfRemoveItem(fileHandle, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MP4ItmfItemListFree(list);
|
MP4ItmfItemListFree(list);
|
||||||
|
|
||||||
MP4ItmfItem* newItem = MP4ItmfItemAlloc( "----", 1 );
|
MP4ItmfItem *newItem = MP4ItmfItemAlloc("----", 1);
|
||||||
newItem->mean = strdup( "com.apple.iTunes" );
|
newItem->mean = strdup("com.apple.iTunes");
|
||||||
newItem->name = strdup( "iTunEXTC" );
|
newItem->name = strdup("iTunEXTC");
|
||||||
|
|
||||||
MP4ItmfData* data = &newItem->dataList.elements[0];
|
MP4ItmfData *data = &newItem->dataList.elements[0];
|
||||||
|
|
||||||
|
NSString *ratingString = ratingiTunesCode;
|
||||||
|
NSArray *iTunesCodes = [[MP42Ratings defaultManager] iTunesCodes];
|
||||||
|
|
||||||
|
// This whole thing is extremely convoluted and wrong in some cases.
|
||||||
if (![[tagsDict valueForKey:@"Rating"] isKindOfClass:[NSNumber class]] ||
|
if (![[tagsDict valueForKey:@"Rating"] isKindOfClass:[NSNumber class]] ||
|
||||||
[[tagsDict valueForKey:@"Rating"] unsignedIntegerValue] == [[MP42Ratings defaultManager] unknownIndex]) {
|
[[tagsDict valueForKey:@"Rating"] unsignedIntegerValue] == [[MP42Ratings defaultManager] unknownIndex]) {
|
||||||
if (!ratingiTunesCode) {
|
if (!ratingString) {
|
||||||
ratingiTunesCode = [[[[MP42Ratings defaultManager] iTunesCodes] objectAtIndex:[[MP42Ratings defaultManager] unknownIndex]] retain];
|
ratingString = [iTunesCodes objectAtIndex:[[MP42Ratings defaultManager] unknownIndex]];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ratingiTunesCode = [[[[MP42Ratings defaultManager] iTunesCodes] objectAtIndex:[[tagsDict valueForKey:@"Rating"] unsignedIntegerValue]] retain];
|
NSUInteger index = [[tagsDict valueForKey:@"Rating"] unsignedIntegerValue];
|
||||||
|
if (iTunesCodes.count > index) {
|
||||||
|
ratingString = [iTunesCodes objectAtIndex:[[tagsDict valueForKey:@"Rating"] unsignedIntegerValue]];
|
||||||
}
|
}
|
||||||
NSString *ratingString = ratingiTunesCode;
|
}
|
||||||
|
|
||||||
if ([[tagsDict valueForKey:@"Rating Annotation"] length] && [ratingString length]) {
|
if ([[tagsDict valueForKey:@"Rating Annotation"] length] && [ratingString length]) {
|
||||||
ratingString = [NSString stringWithFormat:@"%@%@", ratingString, [tagsDict valueForKey:@"Rating Annotation"]];
|
ratingString = [NSString stringWithFormat:@"%@%@", ratingString, [tagsDict valueForKey:@"Rating Annotation"]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ratingString) {
|
||||||
data->typeCode = MP4_ITMF_BT_UTF8;
|
data->typeCode = MP4_ITMF_BT_UTF8;
|
||||||
data->valueSize = strlen([ratingString UTF8String]);
|
data->valueSize = strlen([ratingString UTF8String]);
|
||||||
data->value = (uint8_t*)malloc( data->valueSize );
|
data->value = (uint8_t*)malloc( data->valueSize );
|
||||||
memcpy( data->value, [ratingString UTF8String], data->valueSize );
|
memcpy( data->value, [ratingString UTF8String], data->valueSize );
|
||||||
|
|
||||||
MP4ItmfAddItem(fileHandle, newItem);
|
MP4ItmfAddItem(fileHandle, newItem);
|
||||||
MP4ItmfItemFree(newItem);
|
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
MP4ItmfItemFree(newItem);
|
||||||
|
|
||||||
|
} else {
|
||||||
MP4ItmfItemList* list = MP4ItmfGetItemsByMeaning(fileHandle, "com.apple.iTunes", "iTunEXTC");
|
MP4ItmfItemList* list = MP4ItmfGetItemsByMeaning(fileHandle, "com.apple.iTunes", "iTunEXTC");
|
||||||
if (list) {
|
if (list) {
|
||||||
uint32_t i;
|
for (uint32_t i = 0; i < list->size; i++) {
|
||||||
for (i = 0; i < list->size; i++) {
|
MP4ItmfItem *item = &list->elements[i];
|
||||||
MP4ItmfItem* item = &list->elements[i];
|
|
||||||
MP4ItmfRemoveItem(fileHandle, item);
|
MP4ItmfRemoveItem(fileHandle, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MP4ItmfItemListFree(list);
|
MP4ItmfItemListFree(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-5
@@ -22,9 +22,9 @@
|
|||||||
return sharedRatingsManager;
|
return sharedRatingsManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) init {
|
- (instancetype) init {
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
NSString* ratingsJSON = [[NSBundle bundleForClass:[MP42Ratings class]] pathForResource:@"Ratings" ofType:@"json"];
|
NSString *ratingsJSON = [[NSBundle bundleForClass:[MP42Ratings class]] pathForResource:@"Ratings" ofType:@"json"];
|
||||||
if (!ratingsJSON) {
|
if (!ratingsJSON) {
|
||||||
[self release];
|
[self release];
|
||||||
return nil;
|
return nil;
|
||||||
@@ -32,28 +32,36 @@
|
|||||||
|
|
||||||
JSONDecoder *jsonDecoder = [JSONDecoder decoder];
|
JSONDecoder *jsonDecoder = [JSONDecoder decoder];
|
||||||
ratingsDictionary = [[jsonDecoder objectWithData:[NSData dataWithContentsOfFile:ratingsJSON]] retain];
|
ratingsDictionary = [[jsonDecoder objectWithData:[NSData dataWithContentsOfFile:ratingsJSON]] retain];
|
||||||
|
|
||||||
// construct movie ratings
|
// construct movie ratings
|
||||||
ratings = [[NSMutableArray alloc] init];
|
ratings = [[NSMutableArray alloc] init];
|
||||||
iTunesCodes = [[NSMutableArray alloc] init];
|
iTunesCodes = [[NSMutableArray alloc] init];
|
||||||
|
|
||||||
// if a specific country is picked, include the USA ratings at the end
|
// if a specific country is picked, include the USA ratings at the end
|
||||||
|
NSString *selectedCountry = [[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"];
|
||||||
NSDictionary *usaRatings = nil;
|
NSDictionary *usaRatings = nil;
|
||||||
|
|
||||||
for (NSDictionary *countryRatings in ratingsDictionary) {
|
for (NSDictionary *countryRatings in ratingsDictionary) {
|
||||||
NSString *countryName = [countryRatings valueForKey:@"country"];
|
NSString *countryName = [countryRatings valueForKey:@"country"];
|
||||||
|
|
||||||
if ([countryName isEqualToString:@"USA"]) {
|
if ([countryName isEqualToString:@"USA"]) {
|
||||||
usaRatings = countryRatings;
|
usaRatings = countryRatings;
|
||||||
}
|
}
|
||||||
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"All countries"]) {
|
|
||||||
if (![countryName isEqualToString:@"Unknown"] && ![countryName isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"]]) {
|
if (![selectedCountry isEqualToString:@"All countries"]) {
|
||||||
|
if (![countryName isEqualToString:@"Unknown"] && ![countryName isEqualToString:selectedCountry]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
|
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
|
||||||
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", countryName, [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
|
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", countryName, [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
|
||||||
[iTunesCodes addObject:[NSString stringWithFormat:@"%@|%@|%@|", [rating valueForKey:@"prefix"], [rating valueForKey:@"itunes-code"], [rating valueForKey:@"itunes-value"]]];
|
[iTunesCodes addObject:[NSString stringWithFormat:@"%@|%@|%@|", [rating valueForKey:@"prefix"], [rating valueForKey:@"itunes-code"], [rating valueForKey:@"itunes-value"]]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"All countries"] && ![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"USA"]) {
|
|
||||||
|
if (![selectedCountry isEqualToString:@"All countries"] && ![selectedCountry isEqualToString:@"USA"]) {
|
||||||
if (usaRatings) {
|
if (usaRatings) {
|
||||||
for (NSDictionary *rating in [usaRatings valueForKey:@"ratings"]) {
|
for (NSDictionary *rating in [usaRatings valueForKey:@"ratings"]) {
|
||||||
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", @"USA", [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
|
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", @"USA", [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
|
||||||
@@ -113,9 +121,11 @@
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aRatingString != nil) {
|
if (aRatingString != nil) {
|
||||||
NSLog(@"Unknown rating information: %@", target1);
|
NSLog(@"Unknown rating information: %@", target1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (NSDictionary *countryRatings in ratingsDictionary) {
|
for (NSDictionary *countryRatings in ratingsDictionary) {
|
||||||
if ([[countryRatings valueForKey:@"country"] isEqualToString:aCountry]) {
|
if ([[countryRatings valueForKey:@"country"] isEqualToString:aCountry]) {
|
||||||
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
|
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
|
||||||
@@ -125,6 +135,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [self unknownIndex];
|
return [self unknownIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user