diff --git a/Classes/IJInventoryWindowController.h b/Classes/IJInventoryWindowController.h index 5103c85..05fae22 100644 --- a/Classes/IJInventoryWindowController.h +++ b/Classes/IJInventoryWindowController.h @@ -67,4 +67,9 @@ - (IBAction)setNextNoon:(id)sender; - (IBAction)setNextMidnight:(id)sender; +- (IBAction)emptyInventory:(id)sender; +- (IBAction)saveInventoryItems:(id)sender; +- (IBAction)loadInventoryItems:(id)sender; + + @end diff --git a/Classes/IJInventoryWindowController.m b/Classes/IJInventoryWindowController.m index c6cc036..93502f5 100644 --- a/Classes/IJInventoryWindowController.m +++ b/Classes/IJInventoryWindowController.m @@ -12,6 +12,7 @@ #import "IJInventoryView.h" #import "IJItemPropertiesViewController.h" #import "MAAttachedWindow.h" +#import "NSFileManager+DirectoryLocations.h" @interface IJInventoryWindowController () - (void)saveWorld; @@ -360,6 +361,103 @@ [self calcTimePoints:number]; } +- (void)clearInventory{ + + [armorInventory removeAllObjects]; + [quickInventory removeAllObjects]; + [normalInventory removeAllObjects]; + + [inventoryView setItems:normalInventory]; + [quickView setItems:quickInventory]; + [armorView setItems:armorInventory]; + + [self markDirty]; +} + +- (void)saveInventory +{ + + NSString *path = [[NSFileManager defaultManager] applicationSupportDirectory]; + NSLog(@"%@",path); + NSString *file = @"Inventory.plist"; + + + NSString *InventoryPath = [path stringByAppendingPathComponent:file]; + + NSLog(@"%@",InventoryPath); + + + NSMutableArray *newInventory = [NSMutableArray array]; + + for (NSArray *items in [NSArray arrayWithObjects:armorInventory, quickInventory, normalInventory, nil]) + { + for (IJInventoryItem *item in items) + { + if (item.count > 0 && item.itemId > 0) + [newInventory addObject:item]; + } + } + + [NSKeyedArchiver archiveRootObject: newInventory toFile:InventoryPath]; +} + +-(void)loadInventory +{ + NSString *path = [[NSFileManager defaultManager] applicationSupportDirectory]; + NSString *file = @"Inventory.plist"; + NSString *InventoryPath = [path stringByAppendingPathComponent:file]; + + + [self clearInventory]; + NSArray *newInventory = [NSKeyedUnarchiver unarchiveObjectWithFile:InventoryPath]; + + for (int i = 0; i < IJInventorySlotQuickLast + 1 - IJInventorySlotQuickFirst; i++) + [quickInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotQuickFirst + i]]; + + for (int i = 0; i < IJInventorySlotNormalLast + 1 - IJInventorySlotNormalFirst; i++) + [normalInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotNormalFirst + i]]; + + for (int i = 0; i < IJInventorySlotArmorLast + 1 - IJInventorySlotArmorFirst; i++) + [armorInventory addObject:[IJInventoryItem emptyItemWithSlot:IJInventorySlotArmorFirst + i]]; + + for (IJInventoryItem *item in newInventory) + { + if (IJInventorySlotQuickFirst <= item.slot && item.slot <= IJInventorySlotQuickLast) + { + [quickInventory replaceObjectAtIndex:item.slot - IJInventorySlotQuickFirst withObject:item]; + } + else if (IJInventorySlotNormalFirst <= item.slot && item.slot <= IJInventorySlotNormalLast) + { + [normalInventory replaceObjectAtIndex:item.slot - IJInventorySlotNormalFirst withObject:item]; + } + else if (IJInventorySlotArmorFirst <= item.slot && item.slot <= IJInventorySlotArmorLast) + { + [armorInventory replaceObjectAtIndex:item.slot - IJInventorySlotArmorFirst withObject:item]; + } + } + + [inventoryView setItems:normalInventory]; + [quickView setItems:quickInventory]; + [armorView setItems:armorInventory]; + +} + +- (IBAction)emptyInventory:(id)sender +{ + [self clearInventory]; +} + +- (IBAction)saveInventoryItems:(id)sender +{ + [self saveInventory]; +} + +- (IBAction)loadInventoryItems:(id)sender +{ + [self loadInventory]; +} + + #pragma mark - #pragma mark IJInventoryViewDelegate diff --git a/Classes/NSFileManager+DirectoryLocations.h b/Classes/NSFileManager+DirectoryLocations.h new file mode 100644 index 0000000..3b9c178 --- /dev/null +++ b/Classes/NSFileManager+DirectoryLocations.h @@ -0,0 +1,27 @@ +// +// NSFileManager+DirectoryLocations.h +// +// Created by Matt Gallagher on 06 May 2010 +// +// Permission is given to use this source code file, free of charge, in any +// project, commercial or otherwise, entirely at your risk, with the condition +// that any redistribution (in part or whole) of source code must retain +// this copyright and permission notice. Attribution in compiled projects is +// appreciated but not required. +// + +#import + +// +// DirectoryLocations is a set of global methods for finding the fixed location +// directoriess. +// +@interface NSFileManager (DirectoryLocations) + +- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory + inDomain:(NSSearchPathDomainMask)domainMask + appendPathComponent:(NSString *)appendComponent + error:(NSError **)errorOut; +- (NSString *)applicationSupportDirectory; + +@end diff --git a/Classes/NSFileManager+DirectoryLocations.m b/Classes/NSFileManager+DirectoryLocations.m new file mode 100644 index 0000000..d8313e3 --- /dev/null +++ b/Classes/NSFileManager+DirectoryLocations.m @@ -0,0 +1,179 @@ +// +// NSFileManager+DirectoryLocations.m +// +// Created by Matt Gallagher on 06 May 2010 +// +// Permission is given to use this source code file, free of charge, in any +// project, commercial or otherwise, entirely at your risk, with the condition +// that any redistribution (in part or whole) of source code must retain +// this copyright and permission notice. Attribution in compiled projects is +// appreciated but not required. +// + +#import "NSFileManager+DirectoryLocations.h" + +enum +{ + DirectoryLocationErrorNoPathFound, + DirectoryLocationErrorFileExistsAtLocation +}; + +NSString * const DirectoryLocationDomain = @"DirectoryLocationDomain"; + +@implementation NSFileManager (DirectoryLocations) + +// +// findOrCreateDirectory:inDomain:appendPathComponent:error: +// +// Method to tie together the steps of: +// 1) Locate a standard directory by search path and domain mask +// 2) Select the first path in the results +// 3) Append a subdirectory to that path +// 4) Create the directory and intermediate directories if needed +// 5) Handle errors by emitting a proper NSError object +// +// Parameters: +// searchPathDirectory - the search path passed to NSSearchPathForDirectoriesInDomains +// domainMask - the domain mask passed to NSSearchPathForDirectoriesInDomains +// appendComponent - the subdirectory appended +// errorOut - any error from file operations +// +// returns the path to the directory (if path found and exists), nil otherwise +// +- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory + inDomain:(NSSearchPathDomainMask)domainMask + appendPathComponent:(NSString *)appendComponent + error:(NSError **)errorOut +{ + // + // Search for the path + // + NSArray* paths = NSSearchPathForDirectoriesInDomains( + searchPathDirectory, + domainMask, + YES); + if ([paths count] == 0) + { + if (errorOut) + { + NSDictionary *userInfo = + [NSDictionary dictionaryWithObjectsAndKeys: + NSLocalizedStringFromTable( + @"No path found for directory in domain.", + @"Errors", + nil), + NSLocalizedDescriptionKey, + [NSNumber numberWithInteger:searchPathDirectory], + @"NSSearchPathDirectory", + [NSNumber numberWithInteger:domainMask], + @"NSSearchPathDomainMask", + nil]; + *errorOut = + [NSError + errorWithDomain:DirectoryLocationDomain + code:DirectoryLocationErrorNoPathFound + userInfo:userInfo]; + } + return nil; + } + + // + // Normally only need the first path returned + // + NSString *resolvedPath = [paths objectAtIndex:0]; + + // + // Append the extra path component + // + if (appendComponent) + { + resolvedPath = [resolvedPath + stringByAppendingPathComponent:appendComponent]; + } + + // + // Check if the path exists + // + BOOL exists; + BOOL isDirectory; + exists = [self + fileExistsAtPath:resolvedPath + isDirectory:&isDirectory]; + if (!exists || !isDirectory) + { + if (exists) + { + if (errorOut) + { + NSDictionary *userInfo = + [NSDictionary dictionaryWithObjectsAndKeys: + NSLocalizedStringFromTable( + @"File exists at requested directory location.", + @"Errors", + nil), + NSLocalizedDescriptionKey, + [NSNumber numberWithInteger:searchPathDirectory], + @"NSSearchPathDirectory", + [NSNumber numberWithInteger:domainMask], + @"NSSearchPathDomainMask", + nil]; + *errorOut = + [NSError + errorWithDomain:DirectoryLocationDomain + code:DirectoryLocationErrorFileExistsAtLocation + userInfo:userInfo]; + } + return nil; + } + + // + // Create the path if it doesn't exist + // + NSError *error = nil; + BOOL success = [self + createDirectoryAtPath:resolvedPath + withIntermediateDirectories:YES + attributes:nil + error:&error]; + if (!success) + { + if (errorOut) + { + *errorOut = error; + } + return nil; + } + } + + if (errorOut) + { + *errorOut = nil; + } + return resolvedPath; +} + +// +// applicationSupportDirectory +// +// Returns the path to the applicationSupportDirectory (creating it if it doesn't +// exist). +// +- (NSString *)applicationSupportDirectory +{ + NSString *executableName = + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; + NSError *error; + NSString *result = + [self + findOrCreateDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appendPathComponent:executableName + error:&error]; + if (!result) + { + NSLog(@"Unable to find or create application support directory:\n%@", error); + } + return result; +} + +@end diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index 80dac17..c32fdc3 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -393,6 +393,42 @@ Tools YES + + + Save Inventory + S + 1048576 + 2147483647 + + + + + + Load Inventory + + 2147483647 + + + + + + Clear Inventory + C + 1048576 + 2147483647 + + + + + + YES + YES + + + 2147483647 + + + Next Sunrise @@ -413,8 +449,8 @@ - Next Sunset - S + Next Nightfall + F 1048576 2147483647 @@ -501,6 +537,7 @@ YES + YES InsideJob Help ? 1048576 @@ -1451,6 +1488,30 @@ 712 + + + saveInventoryItems: + + + + 719 + + + + emptyInventory: + + + + 720 + + + + loadInventoryItems: + + + + 722 + @@ -2048,6 +2109,10 @@ + + + + @@ -2071,6 +2136,26 @@ + + 713 + + + + + 714 + + + + + 717 + + + + + 721 + + + @@ -2218,6 +2303,10 @@ 706.IBPluginDependency 709.IBPluginDependency 711.IBPluginDependency + 713.IBPluginDependency + 714.IBPluginDependency + 717.IBPluginDependency + 721.IBPluginDependency 75.IBPluginDependency 75.ImportedFromIB2 81.IBEditorWindowLastContentRect @@ -2294,15 +2383,15 @@ {74, 862} {{6, 978}, {478, 20}} - {{1099, 52}, {585, 396}} + {{855, 52}, {585, 396}} com.apple.InterfaceBuilder.CocoaPlugin - {{1099, 52}, {585, 396}} + {{855, 52}, {585, 396}} {{33, 99}, {480, 360}} {3.40282e+38, 3.40282e+38} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{656, 813}, {169, 23}} + {{712, 813}, {169, 23}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2389,7 +2478,11 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{585, 753}, {189, 83}} + {{585, 683}, {195, 153}} + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2423,7 +2516,7 @@ - 712 + 722 @@ -2454,8 +2547,11 @@ YES YES + emptyInventory: + loadInventoryItems: makeSearchFieldFirstResponder: menuSelectWorld: + saveInventoryItems: setNextDay: setNextMidnight: setNextNight: @@ -2473,14 +2569,20 @@ id id id + id + id + id YES YES + emptyInventory: + loadInventoryItems: makeSearchFieldFirstResponder: menuSelectWorld: + saveInventoryItems: setNextDay: setNextMidnight: setNextNight: @@ -2490,6 +2592,14 @@ YES + + emptyInventory: + id + + + loadInventoryItems: + id + makeSearchFieldFirstResponder: id @@ -2498,6 +2608,10 @@ menuSelectWorld: id + + saveInventoryItems: + id + setNextDay: id diff --git a/InsideJob.xcodeproj/project.pbxproj b/InsideJob.xcodeproj/project.pbxproj index df3f747..2896816 100644 --- a/InsideJob.xcodeproj/project.pbxproj +++ b/InsideJob.xcodeproj/project.pbxproj @@ -29,6 +29,7 @@ 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; BA24598B1297428900F8B9C2 /* blockNotFound.png in Resources */ = {isa = PBXBuildFile; fileRef = BA24598A1297428900F8B9C2 /* blockNotFound.png */; }; + BA3329A9129889860079447B /* NSFileManager+DirectoryLocations.m in Sources */ = {isa = PBXBuildFile; fileRef = BA3329A8129889860079447B /* NSFileManager+DirectoryLocations.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -70,6 +71,8 @@ 8D1107310486CEB800E47090 /* InsideJob-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "InsideJob-Info.plist"; sourceTree = ""; }; 8D1107320486CEB800E47090 /* Inside Job.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Inside Job.app"; sourceTree = BUILT_PRODUCTS_DIR; }; BA24598A1297428900F8B9C2 /* blockNotFound.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = blockNotFound.png; sourceTree = ""; }; + BA3329A7129889860079447B /* NSFileManager+DirectoryLocations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFileManager+DirectoryLocations.h"; sourceTree = ""; }; + BA3329A8129889860079447B /* NSFileManager+DirectoryLocations.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+DirectoryLocations.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -191,6 +194,8 @@ 668B255B125D5BCA0060BF71 /* NSData+CocoaDevAdditions.m */, 66BC04F612619072005A23F4 /* NSColor+Additions.h */, 66BC04F712619072005A23F4 /* NSColor+Additions.m */, + BA3329A7129889860079447B /* NSFileManager+DirectoryLocations.h */, + BA3329A8129889860079447B /* NSFileManager+DirectoryLocations.m */, ); name = Categories; sourceTree = ""; @@ -309,6 +314,7 @@ 66BC033B1260CC59005A23F4 /* MAAttachedWindow.m in Sources */, 66BC03621260D095005A23F4 /* IJItemPropertiesViewController.m in Sources */, 66BC04F812619072005A23F4 /* NSColor+Additions.m in Sources */, + BA3329A9129889860079447B /* NSFileManager+DirectoryLocations.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };