From ddf957fcf7b4d00156afef737a8b2a6d85b6a205 Mon Sep 17 00:00:00 2001 From: rogerman Date: Thu, 28 Nov 2013 08:51:43 +0000 Subject: [PATCH] Cocoa Port: - Load cheat item icons higher up in the front-end code. - Remove CocoaDSCheatManager's and CocoaDSCheatSearch's dependence on CocoaDSCore. Just use basic mutex pointers instead. --- desmume/src/cocoa/cocoa_cheat.h | 30 +-- desmume/src/cocoa/cocoa_cheat.mm | 202 +++++++++++++----- .../userinterface/EmuControllerDelegate.mm | 4 +- .../userinterface/cheatWindowDelegate.mm | 7 + 4 files changed, 173 insertions(+), 70 deletions(-) diff --git a/desmume/src/cocoa/cocoa_cheat.h b/desmume/src/cocoa/cocoa_cheat.h index 49064dfea..8f83cf256 100644 --- a/desmume/src/cocoa/cocoa_cheat.h +++ b/desmume/src/cocoa/cocoa_cheat.h @@ -20,8 +20,6 @@ #include "../cheatSystem.h" #undef BOOL -@class CocoaDSCore; - /******************************************************************************************** CocoaDSCheatItem - OBJECTIVE-C CLASS @@ -74,6 +72,13 @@ - (void) setDataWithDictionary:(NSDictionary *)dataDict; - (NSDictionary *) dataDictionary; ++ (void) setIconInternalCheat:(NSImage *)iconImage; ++ (NSImage *) iconInternalCheat; ++ (void) setIconActionReplay:(NSImage *)iconImage; ++ (NSImage *) iconActionReplay; ++ (void) setIconCodeBreaker:(NSImage *)iconImage; ++ (NSImage *) iconCodeBreaker; + @end /******************************************************************************************** @@ -89,7 +94,9 @@ CHEATS *listData; NSMutableArray *list; - CocoaDSCore *cdsCore; + pthread_mutex_t *mutexCoreExecute; + BOOL isUsingDummyMutex; + NSUInteger untitledCount; NSString *dbTitle; NSString *dbDate; @@ -97,15 +104,14 @@ @property (readonly) CHEATS *listData; @property (readonly) NSMutableArray *list; -@property (retain) CocoaDSCore *cdsCore; +@property (assign) pthread_mutex_t *mutexCoreExecute; @property (assign) NSUInteger untitledCount; @property (copy) NSString *dbTitle; @property (copy) NSString *dbDate; -- (id) initWithCore:(CocoaDSCore *)core; -- (id) initWithCore:(CocoaDSCore *)core fileURL:(NSURL *)fileURL; -- (id) initWithCore:(CocoaDSCore *)core listData:(CHEATS *)cheatList; -- (id) initWithCore:(CocoaDSCore *)core fileURL:(NSURL *)fileURL listData:(CHEATS *)cheatList; +- (id) initWithFileURL:(NSURL *)fileURL; +- (id) initWithListData:(CHEATS *)cheatList; +- (id) initWithFileURL:(NSURL *)fileURL listData:(CHEATS *)cheatList; - (BOOL) add:(CocoaDSCheatItem *)cheatItem; - (void) remove:(CocoaDSCheatItem *)cheatItem; @@ -129,17 +135,17 @@ CHEATSEARCH *listData; NSMutableArray *addressList; - CocoaDSCore *cdsCore; + pthread_mutex_t *mutexCoreExecute; + BOOL isUsingDummyMutex; + NSUInteger searchCount; } @property (readonly) CHEATSEARCH *listData; @property (readonly) NSMutableArray *addressList; -@property (retain) CocoaDSCore *cdsCore; +@property (assign) pthread_mutex_t *mutexCoreExecute; @property (readonly) NSUInteger searchCount; -- (id) initWithCore:(CocoaDSCore *)core; - - (NSUInteger) runExactValueSearch:(NSInteger)value byteSize:(UInt8)byteSize signType:(NSInteger)signType; - (void) runExactValueSearchOnThread:(id)object; - (NSUInteger) runComparativeSearch:(NSInteger)typeID byteSize:(UInt8)byteSize signType:(NSInteger)signType; diff --git a/desmume/src/cocoa/cocoa_cheat.mm b/desmume/src/cocoa/cocoa_cheat.mm index 3327f5f54..f467caec1 100644 --- a/desmume/src/cocoa/cocoa_cheat.mm +++ b/desmume/src/cocoa/cocoa_cheat.mm @@ -17,7 +17,6 @@ */ #import "cocoa_cheat.h" -#import "cocoa_core.h" #import "cocoa_globals.h" #import "cocoa_util.h" @@ -87,13 +86,6 @@ static NSImage *iconCodeBreaker = nil; data = cheatData; } - if (iconInternalCheat == nil || iconActionReplay == nil || iconCodeBreaker == nil) - { - iconInternalCheat = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AppIcon_DeSmuME" ofType:@"icns"]]; - iconActionReplay = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Icon_ActionReplay_128x128" ofType:@"png"]]; - iconCodeBreaker = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Icon_CodeBreaker_128x128" ofType:@"png"]]; - } - pthread_mutex_init(&mutexData, NULL); willAdd = NO; @@ -132,9 +124,7 @@ static NSImage *iconCodeBreaker = nil; } pthread_mutex_lock(&mutexData); - data = cheatData; - pthread_mutex_unlock(&mutexData); [self update]; @@ -633,6 +623,36 @@ static NSImage *iconCodeBreaker = nil; nil]; } ++ (void) setIconInternalCheat:(NSImage *)iconImage +{ + iconInternalCheat = iconImage; +} + ++ (NSImage *) iconInternalCheat +{ + return iconInternalCheat; +} + ++ (void) setIconActionReplay:(NSImage *)iconImage +{ + iconActionReplay = iconImage; +} + ++ (NSImage *) iconActionReplay +{ + return iconActionReplay; +} + ++ (void) setIconCodeBreaker:(NSImage *)iconImage +{ + iconCodeBreaker = iconImage; +} + ++ (NSImage *) iconCodeBreaker +{ + return iconCodeBreaker; +} + @end @@ -640,32 +660,27 @@ static NSImage *iconCodeBreaker = nil; @synthesize listData; @synthesize list; -@synthesize cdsCore; +@dynamic mutexCoreExecute; @synthesize untitledCount; @synthesize dbTitle; @synthesize dbDate; - (id)init { - return [self initWithCore:nil fileURL:nil listData:nil]; + return [self initWithFileURL:nil listData:nil]; } -- (id) initWithCore:(CocoaDSCore *)core +- (id) initWithFileURL:(NSURL *)fileURL { - return [self initWithCore:core fileURL:nil listData:nil]; + return [self initWithFileURL:fileURL listData:nil]; } -- (id) initWithCore:(CocoaDSCore *)core fileURL:(NSURL *)fileURL +- (id) initWithListData:(CHEATS *)cheatList { - return [self initWithCore:core fileURL:fileURL listData:nil]; + return [self initWithFileURL:nil listData:cheatList]; } -- (id) initWithCore:(CocoaDSCore *)core listData:(CHEATS *)cheatList -{ - return [self initWithCore:core fileURL:nil listData:cheatList]; -} - -- (id) initWithCore:(CocoaDSCore *)core fileURL:(NSURL *)fileURL listData:(CHEATS *)cheatList +- (id) initWithFileURL:(NSURL *)fileURL listData:(CHEATS *)cheatList { self = [super init]; if(self == nil) @@ -705,7 +720,10 @@ static NSImage *iconCodeBreaker = nil; } } - cdsCore = [core retain]; + mutexCoreExecute = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(mutexCoreExecute, NULL); + isUsingDummyMutex = YES; + untitledCount = 0; dbTitle = nil; dbDate = nil; @@ -717,13 +735,50 @@ static NSImage *iconCodeBreaker = nil; { self.dbTitle = nil; self.dbDate = nil; - self.cdsCore = nil; [list release]; delete (CHEATS *)self.listData; + if (isUsingDummyMutex) + { + pthread_mutex_destroy(mutexCoreExecute); + free(mutexCoreExecute); + mutexCoreExecute = NULL; + } + [super dealloc]; } +- (void) setMutexCoreExecute:(pthread_mutex_t *)theMutex +{ + if (theMutex == NULL && isUsingDummyMutex) + { + return; + } + else if (theMutex == NULL && !isUsingDummyMutex) + { + mutexCoreExecute = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(mutexCoreExecute, NULL); + isUsingDummyMutex = YES; + return; + } + else if (theMutex != NULL && isUsingDummyMutex) + { + pthread_mutex_destroy(mutexCoreExecute); + free(mutexCoreExecute); + isUsingDummyMutex = NO; + mutexCoreExecute = theMutex; + } + else if (theMutex != NULL && !isUsingDummyMutex) + { + mutexCoreExecute = theMutex; + } +} + +- (pthread_mutex_t *) mutexCoreExecute +{ + return mutexCoreExecute; +} + - (BOOL) add:(CocoaDSCheatItem *)cheatItem { BOOL result = NO; @@ -737,7 +792,7 @@ static NSImage *iconCodeBreaker = nil; // to check if the list got reallocated. CHEATS_LIST *cheatListData = self.listData->getListPtr(); - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); switch (cheatItem.cheatType) { @@ -769,7 +824,7 @@ static NSImage *iconCodeBreaker = nil; break; } - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); if (![self.list containsObject:cheatItem]) { @@ -809,9 +864,9 @@ static NSImage *iconCodeBreaker = nil; return; } - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); self.listData->remove(selectionIndex); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); // Removing an item from the raw cheat list data shifts all higher elements // by one, so we need to do the same. @@ -840,7 +895,7 @@ static NSImage *iconCodeBreaker = nil; return result; } - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); switch (cheatItem.cheatType) { @@ -872,7 +927,7 @@ static NSImage *iconCodeBreaker = nil; break; } - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); [cheatItem update]; @@ -881,18 +936,18 @@ static NSImage *iconCodeBreaker = nil; - (BOOL) save { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); BOOL result = self.listData->save(); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); return result; } - (NSUInteger) activeCount { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); NSUInteger activeCheatsCount = self.listData->getActiveCount(); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); return activeCheatsCount; } @@ -944,9 +999,9 @@ static NSImage *iconCodeBreaker = nil; return; } - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); [CocoaDSCheatManager applyInternalCheatWithItem:cheatItem]; - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); } + (void) setMasterCheatList:(CocoaDSCheatManager *)cheatListManager @@ -1071,15 +1126,10 @@ static NSImage *iconCodeBreaker = nil; @synthesize listData; @synthesize addressList; -@synthesize cdsCore; +@dynamic mutexCoreExecute; @synthesize searchCount; - (id)init -{ - return [self initWithCore:nil]; -} - -- (id) initWithCore:(CocoaDSCore *)core { self = [super init]; if(self == nil) @@ -1094,9 +1144,12 @@ static NSImage *iconCodeBreaker = nil; return nil; } + mutexCoreExecute = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(mutexCoreExecute, NULL); + isUsingDummyMutex = YES; + listData = newListData; addressList = nil; - cdsCore = [core retain]; searchCount = 0; return self; @@ -1104,17 +1157,54 @@ static NSImage *iconCodeBreaker = nil; - (void)dealloc { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); self.listData->close(); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); [addressList release]; - self.cdsCore = nil; delete (CHEATSEARCH *)self.listData; + if (isUsingDummyMutex) + { + pthread_mutex_destroy(mutexCoreExecute); + free(mutexCoreExecute); + mutexCoreExecute = NULL; + } + [super dealloc]; } +- (void) setMutexCoreExecute:(pthread_mutex_t *)theMutex +{ + if (theMutex == NULL && isUsingDummyMutex) + { + return; + } + else if (theMutex == NULL && !isUsingDummyMutex) + { + mutexCoreExecute = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(mutexCoreExecute, NULL); + isUsingDummyMutex = YES; + return; + } + else if (theMutex != NULL && isUsingDummyMutex) + { + pthread_mutex_destroy(mutexCoreExecute); + free(mutexCoreExecute); + isUsingDummyMutex = NO; + mutexCoreExecute = theMutex; + } + else if (theMutex != NULL && !isUsingDummyMutex) + { + mutexCoreExecute = theMutex; + } +} + +- (pthread_mutex_t *) mutexCoreExecute +{ + return mutexCoreExecute; +} + - (NSUInteger) runExactValueSearch:(NSInteger)value byteSize:(UInt8)byteSize signType:(NSInteger)signType { NSUInteger itemCount = 0; @@ -1124,17 +1214,17 @@ static NSImage *iconCodeBreaker = nil; { byteSize--; - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); listExists = (NSUInteger)self.listData->start((u8)CHEATSEARCH_SEARCHSTYLE_EXACT_VALUE, (u8)byteSize, (u8)signType); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); } if (listExists) { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); itemCount = (NSUInteger)self.listData->search((u32)value); NSMutableArray *newAddressList = [[CocoaDSCheatSearch addressListWithListObject:self.listData maxItems:100] retain]; - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); [addressList release]; addressList = newAddressList; @@ -1161,18 +1251,18 @@ static NSImage *iconCodeBreaker = nil; { byteSize--; - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); listExists = (NSUInteger)self.listData->start((u8)CHEATSEARCH_SEARCHSTYLE_COMPARATIVE, (u8)byteSize, (u8)signType); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); addressList = nil; } else { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); itemCount = (NSUInteger)self.listData->search((u8)typeID); NSMutableArray *newAddressList = [[CocoaDSCheatSearch addressListWithListObject:self.listData maxItems:100] retain]; - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); [addressList release]; addressList = newAddressList; @@ -1196,9 +1286,9 @@ static NSImage *iconCodeBreaker = nil; - (void) reset { - pthread_mutex_lock(self.cdsCore.mutexCoreExecute); + pthread_mutex_lock([self mutexCoreExecute]); self.listData->close(); - pthread_mutex_unlock(self.cdsCore.mutexCoreExecute); + pthread_mutex_unlock([self mutexCoreExecute]); searchCount = 0; [addressList release]; diff --git a/desmume/src/cocoa/userinterface/EmuControllerDelegate.mm b/desmume/src/cocoa/userinterface/EmuControllerDelegate.mm index a0324d0ff..ec7bf932d 100644 --- a/desmume/src/cocoa/userinterface/EmuControllerDelegate.mm +++ b/desmume/src/cocoa/userinterface/EmuControllerDelegate.mm @@ -1454,7 +1454,7 @@ // If the ROM has an associated cheat file, load it now. NSString *cheatsPath = [[CocoaDSFile fileURLFromRomURL:[theRom fileURL] toKind:@"Cheat"] path]; CocoaDSCore *cdsCore = (CocoaDSCore *)[cdsCoreController content]; - CocoaDSCheatManager *newCheatList = [[[CocoaDSCheatManager alloc] initWithCore:cdsCore fileURL:[NSURL fileURLWithPath:cheatsPath]] autorelease]; + CocoaDSCheatManager *newCheatList = [[[CocoaDSCheatManager alloc] initWithFileURL:[NSURL fileURLWithPath:cheatsPath]] autorelease]; if (newCheatList != nil) { NSMutableDictionary *cheatWindowBindings = (NSMutableDictionary *)[cheatWindowController content]; @@ -1518,7 +1518,7 @@ } [cheatWindowDelegate setCdsCheats:newCheatList]; - [[cheatWindowDelegate cdsCheatSearch] setCdsCore:cdsCore]; + [[cheatWindowDelegate cdsCheatSearch] setMutexCoreExecute:[cdsCore mutexCoreExecute]]; [cheatWindowDelegate setCheatSearchViewByStyle:CHEATSEARCH_SEARCHSTYLE_EXACT_VALUE]; } diff --git a/desmume/src/cocoa/userinterface/cheatWindowDelegate.mm b/desmume/src/cocoa/userinterface/cheatWindowDelegate.mm index fbf4d3c94..14bd9433b 100644 --- a/desmume/src/cocoa/userinterface/cheatWindowDelegate.mm +++ b/desmume/src/cocoa/userinterface/cheatWindowDelegate.mm @@ -94,6 +94,13 @@ [bindings setValue:[NSNumber numberWithInteger:CHEATSEARCH_UNSIGNED] forKey:@"cheatSearchSignType"]; [bindings setValue:@"Search not started." forKey:@"cheatSearchAddressCount"]; + if ([CocoaDSCheatItem iconInternalCheat] == nil || [CocoaDSCheatItem iconActionReplay] == nil || [CocoaDSCheatItem iconCodeBreaker] == nil) + { + [CocoaDSCheatItem setIconInternalCheat:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"AppIcon_DeSmuME" ofType:@"icns"]]]; + [CocoaDSCheatItem setIconActionReplay:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Icon_ActionReplay_128x128" ofType:@"png"]]]; + [CocoaDSCheatItem setIconCodeBreaker:[[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Icon_CodeBreaker_128x128" ofType:@"png"]]]; + } + return self; }