29#import "OOJavaScriptEngine.h"
33#define PROFILE_WRITES 0
37#define CACHE_PLIST_FORMAT NSPropertyListBinaryFormat_v1_0
39#define CACHE_PLIST_FORMAT NSPropertyListGNUstepBinaryFormat
75@interface OOCacheManager (Private)
84- (BOOL)writeDict:(NSDictionary*)inDict;
86- (void)buildCachesFromDictionary:(NSDictionary*)inDict;
89- (BOOL)directoryExists:(NSString*)inPath create:(BOOL)inCreate;
93@interface OOCacheManager (PlatformSpecific)
95- (NSString*)cachePathCreatingIfNecessary:(BOOL)inCreate;
105- (id)initWithCacheContents:(NSDictionary*)cacheContents;
131 return [
NSString stringWithFormat:@"<%@ %p>{dirty=%s}", [
self class],
self, [
self dirty] ? "yes" : "no"];
144- (id)objectForKey:(NSString*)inKey inCache:(NSString*)inCacheKey
146 NSMutableDictionary* cache =
nil;
149 NSParameterAssert(inKey !=
nil && inCacheKey !=
nil);
151 cache = [_caches objectForKey:inCacheKey];
153 result = [
cache objectForKey:inKey];
166- (void)setObject:(
id)inObject forKey:(NSString*)inKey inCache:(NSString*)inCacheKey
168 NSMutableDictionary* cache =
nil;
170 NSParameterAssert(inObject !=
nil && inKey !=
nil && inCacheKey !=
nil);
175 cache = [_caches objectForKey:inCacheKey];
182 [_caches setObject:cache forKey:inCacheKey];
185 [
cache setObject:inObject forKey:inKey];
190- (void)removeObjectForKey:(NSString*)inKey inCache:(NSString*)inCacheKey
192 NSMutableDictionary* cache =
nil;
194 NSParameterAssert(inKey !=
nil && inCacheKey !=
nil);
196 cache = [_caches objectForKey:inCacheKey];
198 if (
nil != [cache objectForKey:inKey]) {
199 [
cache removeObjectForKey:inKey];
210- (void)clearCache:(NSString*)inCacheKey
212 NSParameterAssert(inCacheKey !=
nil);
214 if (
nil != [
_caches objectForKey:inCacheKey]) {
215 [_caches removeObjectForKey:inCacheKey];
251- (void)setAllowCacheWrites:(BOOL)flag
256- (NSString*)cacheDirectoryPathCreatingIfNecessary:(BOOL)create
267 if (![
self directoryExists:cachePath create:create])
272 [[
NSFileManager defaultManager] removeFileAtPath:[
cachePath stringByAppendingPathComponent:@"Oolite-cache.plist"] handler:nil];
275 cachePath = [
cachePath stringByAppendingPathComponent:@"org.aegidian.oolite"];
276 if (![
self directoryExists:cachePath create:create])
283@implementation OOCacheManager (Private)
287 NSDictionary* cache =
nil;
288 NSString* cacheVersion =
nil;
289 NSString* ooliteVersion =
nil;
290 NSData* endianTag =
nil;
291 NSNumber* formatVersion =
nil;
293 uint64_t endianTagValue = 0;
295 ooliteVersion = [[[
NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
305 cacheVersion = [
cache objectForKey:kCacheKeyVersion];
306 if (![cacheVersion isEqual:ooliteVersion]) {
307 OOLog(
kOOLogDataCacheRebuild,
@"Data cache version (%@) does not match Oolite version (%@), rebuilding cache.", cacheVersion, ooliteVersion);
311 formatVersion = [
cache objectForKey:kCacheKeyFormatVersion];
318 endianTag = [
cache objectForKey:kCacheKeyEndianTag];
319 if (![endianTag isKindOfClass:[NSData
class]] || [endianTag length] !=
sizeof endianTagValue) {
323 endianTagValue = *(
const uint64_t*)[endianTag bytes];
350 NSMutableDictionary* newCache =
nil;
351 NSString* ooliteVersion =
nil;
352 NSData* endianTag =
nil;
353 NSNumber* formatVersion =
nil;
354 NSDictionary* pListRep =
nil;
359 if (_scheduledWrite !=
nil)
367 OOLog(
@"dataCache.willWrite",
@"%@",
@"Scheduling data cache write.");
369 OOLog(
@"dataCache.willWrite",
@"%@",
@"About to write cache.");
372 ooliteVersion = [[[
NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
373 endianTag = [
NSData dataWithBytes:&endianTagValue length:sizeof endianTagValue];
374 formatVersion = [
NSNumber numberWithUnsignedInt:kFormatVersionValue];
377 if (ooliteVersion ==
nil || endianTag ==
nil || formatVersion ==
nil || pListRep ==
nil) {
378 OOLog(
@"dataCache.cantWrite",
@"%@",
@"Failed to write data cache -- prerequisites not fulfilled. This is an internal error, please report it.");
383 [
newCache setObject:ooliteVersion forKey:kCacheKeyVersion];
384 [
newCache setObject:formatVersion forKey:kCacheKeyFormatVersion];
385 [
newCache setObject:endianTag forKey:kCacheKeyEndianTag];
386 [
newCache setObject:pListRep forKey:kCacheKeyCaches];
388#if PROFILE_WRITES && !WRITE_ASYNC
393 NSDictionary* cacheData = newCache;
398 OOLog(
@"dataCache.profile",
@"Time to prepare cache data: %g seconds.", endT);
405 OOLog(
@"dataCache.profile",
@"Time to prepare cache data: %g seconds.", prepareT);
408 if ([
self writeDict:newCache]) {
435 NSString* path =
nil;
437 NSString* errorString =
nil;
445 data = [
NSData dataWithContentsOfFile:path];
450 mutabilityOption:NSPropertyListImmutable
452 errorDescription:&errorString];
454 @catch (NSException* exception) {
459 if (errorString !=
nil) {
460 OOLog(
@"dataCache.badData",
@"Could not read data cache: %@", errorString);
461#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
466 if (![contents isKindOfClass:[NSDictionary
class]])
472- (BOOL)writeDict:(NSDictionary*)inDict
474 NSString* path =
nil;
476 NSString* errorDesc =
nil;
488#if OOLITE_RELEASE_PLIST_ERROR_STRINGS
499 BOOL result = [
plist writeToFile:path atomically:NO];
504 OOLog(
@"dataCache.profile",
@"Time to serialize cache: %g seconds. Time to write data: %g seconds.", serializeT, writeT);
513- (void)buildCachesFromDictionary:(NSDictionary*)inDict
517 NSMutableDictionary* cache =
nil;
527 value = [
inDict oo_dictionaryForKey:key];
531 [
_caches setObject:cache forKey:key];
539 return [OODeepCopy(_caches) autorelease];
542- (BOOL)directoryExists:(NSString*)inPath create:(BOOL)inCreate
544 BOOL exists, directory;
547 exists = [
fmgr fileExistsAtPath:inPath isDirectory:&directory];
549 if (exists && !directory) {
556 if (![fmgr oo_createDirectoryAtPath:inPath attributes:
nil]) {
567- (NSString*)cachePathCreatingIfNecessary:(BOOL)create
570 return [
cachePath stringByAppendingPathComponent:@"Data Cache.plist"];
575- (NSString*)cachePathCreatingIfNecessary:(BOOL)create
577 NSString* cachePath = [
self cacheDirectoryPathCreatingIfNecessary:create];
578 return [cachePath stringByAppendingPathComponent:@"Oolite-cache.plist"];
585@implementation OOCacheManager (Singleton)
594+ (id)allocWithZone:(NSZone*)inZone
603- (id)copyWithZone:(NSZone*)inZone
632- (id)initWithCacheContents:(NSDictionary*)cacheContents
634 if ((
self = [super init])) {
static NSString *const kCacheKeyEndianTag
static NSString *const kOOLogDataCacheWriteSuccess
static NSString *const kOOLogDataCacheBuildPathError
static NSString *const kOOLogDataCacheRetrieveSuccess
static NSString *const kOOLogDataCacheClearSuccess
static NSString *const kOOLogDataCacheWriteFailed
static NSString *const kOOLogDataCacheRetrieveFailed
static NSString *const kOOLogDataCacheSetFailed
static NSString *const kCacheKeyCaches
static NSString *const kOOLogDataCacheRebuild
static NSString *const kOOLogDataCacheSerializationError
static NSString *const kCacheKeyVersion
static NSString *const kOOLogDataCacheRemoveSuccess
static NSString *const kOOLogDataCacheNotFound
static NSString *const kCacheKeyFormatVersion
static NSString *const kOOLogDataCacheSetSuccess
static NSString *const kOOLogDataCacheFound
#define foreachkey(VAR, DICT)
#define OODebugLog(class, format,...)
#define OOLogOutdentIf(class)
#define OOLog(class, format,...)
#define OOLogIndentIf(class)
static OONullTexture * sSingleton
NSDictionary * loadDict()
NSDictionary * dictionaryOfCaches()
NSDictionary * _cacheContents
BOOL addTask:priority:(id< OOAsyncWorkTask > task,[priority] OOAsyncWorkPriority priority)
void waitForTaskToComplete:(id< OOAsyncWorkTask > task)
OOAsyncWorkManager * sharedAsyncWorkManager()
NSMutableDictionary * _caches
void finishOngoingFlush()
void buildCachesFromDictionary:(NSDictionary *inDict)
NSString * cacheDirectoryPathCreatingIfNecessary:(BOOL create)
NSString * cachePathCreatingIfNecessary:(BOOL inCreate)
OOCacheManager * sharedCache()
NSDictionary * loadDict()
NSDictionary * dictionaryOfCaches()