Added rudimentary support for saving and loading the Inventory

This commit is contained in:
Nick Loose 2010-11-21 15:23:59 +01:00
parent bae935de1b
commit 000eafedd6
6 changed files with 436 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <Foundation/Foundation.h>
//
// 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

View File

@ -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

View File

@ -393,6 +393,42 @@
<string key="NSTitle">Tools</string>
<object class="NSMutableArray" key="NSMenuItems">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSMenuItem" id="816451080">
<reference key="NSMenu" ref="500439511"/>
<string key="NSTitle">Save Inventory</string>
<string key="NSKeyEquiv">S</string>
<int key="NSKeyEquivModMask">1048576</int>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="35465992"/>
<reference key="NSMixedImage" ref="502551668"/>
</object>
<object class="NSMenuItem" id="241177871">
<reference key="NSMenu" ref="500439511"/>
<string key="NSTitle">Load Inventory</string>
<string key="NSKeyEquiv"/>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="35465992"/>
<reference key="NSMixedImage" ref="502551668"/>
</object>
<object class="NSMenuItem" id="946595032">
<reference key="NSMenu" ref="500439511"/>
<string key="NSTitle">Clear Inventory</string>
<string key="NSKeyEquiv">C</string>
<int key="NSKeyEquivModMask">1048576</int>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="35465992"/>
<reference key="NSMixedImage" ref="502551668"/>
</object>
<object class="NSMenuItem" id="864243634">
<reference key="NSMenu" ref="500439511"/>
<bool key="NSIsDisabled">YES</bool>
<bool key="NSIsSeparator">YES</bool>
<string key="NSTitle"/>
<string key="NSKeyEquiv"/>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="35465992"/>
<reference key="NSMixedImage" ref="502551668"/>
</object>
<object class="NSMenuItem" id="195507455">
<reference key="NSMenu" ref="500439511"/>
<string key="NSTitle">Next Sunrise</string>
@ -413,8 +449,8 @@
</object>
<object class="NSMenuItem" id="431631770">
<reference key="NSMenu" ref="500439511"/>
<string key="NSTitle">Next Sunset</string>
<string key="NSKeyEquiv">S</string>
<string key="NSTitle">Next Nightfall</string>
<string key="NSKeyEquiv">F</string>
<int key="NSKeyEquivModMask">1048576</int>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="35465992"/>
@ -501,6 +537,7 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSMenuItem" id="105068016">
<reference key="NSMenu" ref="992780483"/>
<bool key="NSIsDisabled">YES</bool>
<string key="NSTitle">InsideJob Help</string>
<string key="NSKeyEquiv">?</string>
<int key="NSKeyEquivModMask">1048576</int>
@ -1451,6 +1488,30 @@
</object>
<int key="connectionID">712</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">saveInventoryItems:</string>
<reference key="source" ref="760161335"/>
<reference key="destination" ref="816451080"/>
</object>
<int key="connectionID">719</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">emptyInventory:</string>
<reference key="source" ref="760161335"/>
<reference key="destination" ref="946595032"/>
</object>
<int key="connectionID">720</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">loadInventoryItems:</string>
<reference key="source" ref="760161335"/>
<reference key="destination" ref="241177871"/>
</object>
<int key="connectionID">722</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@ -2048,6 +2109,10 @@
<reference ref="431631770"/>
<reference ref="19459331"/>
<reference ref="234384199"/>
<reference ref="946595032"/>
<reference ref="864243634"/>
<reference ref="816451080"/>
<reference ref="241177871"/>
</object>
<reference key="parent" ref="1003822312"/>
</object>
@ -2071,6 +2136,26 @@
<reference key="object" ref="234384199"/>
<reference key="parent" ref="500439511"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">713</int>
<reference key="object" ref="864243634"/>
<reference key="parent" ref="500439511"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">714</int>
<reference key="object" ref="946595032"/>
<reference key="parent" ref="500439511"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">717</int>
<reference key="object" ref="816451080"/>
<reference key="parent" ref="500439511"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">721</int>
<reference key="object" ref="241177871"/>
<reference key="parent" ref="500439511"/>
</object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@ -2218,6 +2303,10 @@
<string>706.IBPluginDependency</string>
<string>709.IBPluginDependency</string>
<string>711.IBPluginDependency</string>
<string>713.IBPluginDependency</string>
<string>714.IBPluginDependency</string>
<string>717.IBPluginDependency</string>
<string>721.IBPluginDependency</string>
<string>75.IBPluginDependency</string>
<string>75.ImportedFromIB2</string>
<string>81.IBEditorWindowLastContentRect</string>
@ -2294,15 +2383,15 @@
<integer value="1"/>
<string>{74, 862}</string>
<string>{{6, 978}, {478, 20}}</string>
<string>{{1099, 52}, {585, 396}}</string>
<string>{{855, 52}, {585, 396}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{1099, 52}, {585, 396}}</string>
<string>{{855, 52}, {585, 396}}</string>
<integer value="1"/>
<string>{{33, 99}, {480, 360}}</string>
<string>{3.40282e+38, 3.40282e+38}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{656, 813}, {169, 23}}</string>
<string>{{712, 813}, {169, 23}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@ -2389,7 +2478,11 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{585, 753}, {189, 83}}</string>
<string>{{585, 683}, {195, 153}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@ -2423,7 +2516,7 @@
</object>
</object>
<nil key="sourceID"/>
<int key="maxID">712</int>
<int key="maxID">722</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@ -2454,8 +2547,11 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>emptyInventory:</string>
<string>loadInventoryItems:</string>
<string>makeSearchFieldFirstResponder:</string>
<string>menuSelectWorld:</string>
<string>saveInventoryItems:</string>
<string>setNextDay:</string>
<string>setNextMidnight:</string>
<string>setNextNight:</string>
@ -2473,14 +2569,20 @@
<string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>emptyInventory:</string>
<string>loadInventoryItems:</string>
<string>makeSearchFieldFirstResponder:</string>
<string>menuSelectWorld:</string>
<string>saveInventoryItems:</string>
<string>setNextDay:</string>
<string>setNextMidnight:</string>
<string>setNextNight:</string>
@ -2490,6 +2592,14 @@
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBActionInfo">
<string key="name">emptyInventory:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">loadInventoryItems:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">makeSearchFieldFirstResponder:</string>
<string key="candidateClassName">id</string>
@ -2498,6 +2608,10 @@
<string key="name">menuSelectWorld:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">saveInventoryItems:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">setNextDay:</string>
<string key="candidateClassName">id</string>

View File

@ -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 = "<group>"; };
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 = "<group>"; };
BA3329A7129889860079447B /* NSFileManager+DirectoryLocations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFileManager+DirectoryLocations.h"; sourceTree = "<group>"; };
BA3329A8129889860079447B /* NSFileManager+DirectoryLocations.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+DirectoryLocations.m"; sourceTree = "<group>"; };
/* 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 = "<group>";
@ -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;
};