Start on OXZ manager.

Doesn't work at all...
This commit is contained in:
cim 2014-01-05 12:19:49 +00:00
parent 012af4837c
commit 36178204f3
4 changed files with 304 additions and 0 deletions

View File

@ -272,6 +272,7 @@ OOLITE_RSRC_MGMT_FILES = \
OOCache.m \ OOCache.m \
OOCacheManager.m \ OOCacheManager.m \
OOConvertSystemDescriptions.m \ OOConvertSystemDescriptions.m \
OOOXZManager.m \
OOPListParsing.m \ OOPListParsing.m \
ResourceManager.m \ ResourceManager.m \
TextureStore.m TextureStore.m

View File

@ -41,6 +41,7 @@ MA 02110-1301, USA.
#import "OOOpenALController.h" #import "OOOpenALController.h"
#import "OODebugSupport.h" #import "OODebugSupport.h"
#import "legacy_random.h" #import "legacy_random.h"
#import "OOOXZManager.h"
#if OOLITE_MAC_OS_X #if OOLITE_MAC_OS_X
#import "JAPersistentFileReference.h" #import "JAPersistentFileReference.h"
@ -239,6 +240,9 @@ static GameController *sSharedController = nil;
} }
} }
// initialise OXZ manager
[OOOXZManager sharedManager];
// moved here to try to avoid initialising this before having an Open GL context // moved here to try to avoid initialising this before having an Open GL context
//[self logProgress:DESC(@"Initialising universe")]; // DESC expansions only possible after Universe init //[self logProgress:DESC(@"Initialising universe")]; // DESC expansions only possible after Universe init
[[Universe alloc] initWithGameView:gameView]; [[Universe alloc] initWithGameView:gameView];
@ -319,6 +323,9 @@ static GameController *sSharedController = nil;
[gameView pollControls]; [gameView pollControls];
[self doPerformGameTick]; [self doPerformGameTick];
// TEMP: just for testing! Should only do this on user request
[[OOOXZManager sharedManager] updateManifests];
[pool release]; [pool release];
} }
@ -374,6 +381,7 @@ static GameController *sSharedController = nil;
#if OOLITE_MAC_OS_X #if OOLITE_MAC_OS_X
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
#endif #endif
} }
} }

66
src/Core/OOOXZManager.h Normal file
View File

@ -0,0 +1,66 @@
/*
OOOXZManager.h
Responsible for installing and uninstalling OXZs
Oolite
Copyright (C) 2004-2013 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import "OOCocoa.h"
#import "OOOpenGL.h"
#import "NSFileManagerOOExtensions.h"
typedef enum {
OXZ_DOWNLOAD_NONE = 0,
OXZ_DOWNLOAD_STARTED = 1,
OXZ_DOWNLOAD_RECEIVING = 2,
OXZ_DOWNLOAD_COMPLETE = 10,
OXZ_DOWNLOAD_ERROR = 99
} OXZDownloadStatus;
#if 0
// TODO: this should check for Mac OS 10.7 or higher, and possibly later GNUStep
@interface OOOXZManager : NSObject <NSURLDownloadDelegate>
#else
@interface OOOXZManager : NSObject
#endif
{
@private
NSArray *_oxzList;
BOOL _updatingManifests;
NSURLDownload *_currentDownload;
OXZDownloadStatus _downloadStatus;
NSUInteger _downloadProgress;
NSUInteger _downloadExpected;
}
+ (OOOXZManager *) sharedManager;
- (BOOL) updateManifests;
- (BOOL) cancelUpdateManifests;
@end

229
src/Core/OOOXZManager.m Normal file
View File

@ -0,0 +1,229 @@
/*
OOOXZManager.m
Responsible for installing and uninstalling OXZs
Oolite
Copyright (C) 2004-2013 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import "OOOXZManager.h"
#import "OOPListParsing.h"
#import "ResourceManager.h"
/* The URL for the manifest.plist array. This one is extremely
* temporary, of course */
static NSString * const kOOOXZDataURL = @"http://compsoc.dur.ac.uk/~cim/oolite/dev/manifests.plist";
/* The filename to store the downloaded manifest.plist array */
static NSString * const kOOOXZManifestCache = @"manifests.plist";
/* The filename to temporarily store the downloaded manifest.plist array */
static NSString * const kOOOXZManifestTmp = @"manifests.plist.new";
static NSString * const kOOOXZErrorLog = @"oxz.manager.error";
static NSString * const kOOOXZDebugLog = @"oxz.manager.debug";
static OOOXZManager *sSingleton = nil;
@interface OOOXZManager (OOPrivate)
- (NSString *) installPath;
- (NSString *) manifestPath;
- (NSString *) manifestDownloadPath;
- (void) setCurrentDownload:(NSURLDownload *)download;
/* Delegates for URL downloader */
- (void) downloadDidBegin:(NSURLDownload *)download;
- (void) download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response;
- (void) download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length;
- (void) downloadDidFinish:(NSURLDownload *)download;
- (void) download:(NSURLDownload *)download didFailWithError:(NSError *)error;
@end
@implementation OOOXZManager
+ (OOOXZManager *)sharedManager
{
// NOTE: assumes single-threaded first access.
if (sSingleton == nil) sSingleton = [[self alloc] init];
return sSingleton;
}
- (id) init
{
self = [super init];
if (self != nil)
{
_updatingManifests = NO;
_downloadStatus = OXZ_DOWNLOAD_NONE;
// if the file has not been downloaded, this will be nil
_oxzList = OOArrayFromFile([self manifestPath]);
}
return self;
}
- (void)dealloc
{
if (sSingleton == self) sSingleton = nil;
[self setCurrentDownload:nil];
DESTROY(_oxzList);
[super dealloc];
}
/* As currently implemented in ResourceManager the lowest-priority
* root path is supposed to be in the user's home directory
* (Mac/Linux) or next to the Oolite install (Windows). This is the
* safest place to write to. */
- (NSString *) installPath
{
return [[ResourceManager rootPaths] lastObject];
}
- (NSString *) manifestPath
{
return [[self installPath] stringByAppendingPathComponent:kOOOXZManifestCache];
}
/* Download mechanism could destroy a correct file if it failed
* half-way and was downloaded on top of the old one. So this loads it
* off to the side a bit */
- (NSString *) manifestDownloadPath
{
return [[self installPath] stringByAppendingPathComponent:kOOOXZManifestTmp];
}
- (void) setCurrentDownload:(NSURLDownload *)download
{
if (_currentDownload != nil)
{
[_currentDownload cancel]; // releases via delegate
}
_currentDownload = [download retain];
}
- (BOOL) updateManifests
{
if (_downloadStatus != OXZ_DOWNLOAD_NONE || _updatingManifests)
{
return NO;
}
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kOOOXZDataURL]];
NSURLDownload *download = [[NSURLDownload alloc] initWithRequest:request delegate:self];
if (download)
{
[download setDestination:[self manifestDownloadPath] allowOverwrite:YES];
/* Delegates don't yet work. Suspect a problem with the run loop. */
_updatingManifests = YES;
_downloadProgress = 0;
_downloadExpected = 0;
[self setCurrentDownload:download]; // retains it
[download release];
OOLog(kOOOXZDebugLog,@"Manifest update request received, using %@ and downloading to %@",[request URL],[self manifestDownloadPath]);
return YES;
}
else
{
OOLog(kOOOXZErrorLog,@"Unable to start downloading manifests file at %@",[request URL]);
return NO;
}
}
- (BOOL) cancelUpdateManifests
{
if (!_updatingManifests || _downloadStatus == OXZ_DOWNLOAD_NONE)
{
return NO;
}
OOLog(kOOOXZDebugLog,@"Trying to cancel manifests file update");
if (_currentDownload != nil)
{
[_currentDownload cancel];
}
else if (_downloadStatus == OXZ_DOWNLOAD_COMPLETE)
{
// then we should clean up the temp file - TODO!
}
_updatingManifests = NO;
_downloadStatus = OXZ_DOWNLOAD_NONE;
return YES;
}
- (void) downloadDidBegin:(NSURLDownload *)download
{
_downloadStatus = OXZ_DOWNLOAD_STARTED;
OOLog(kOOOXZDebugLog,@"Download started");
}
- (void) download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{
_downloadStatus = OXZ_DOWNLOAD_RECEIVING;
OOLog(kOOOXZDebugLog,@"Download receiving");
_downloadExpected = [response expectedContentLength];
_downloadProgress = 0;
}
- (void) download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
{
OOLog(kOOOXZDebugLog,@"Downloaded %d bytes",length);
_downloadProgress += length;
}
- (void) downloadDidFinish:(NSURLDownload *)download
{
_downloadStatus = OXZ_DOWNLOAD_COMPLETE;
OOLog(kOOOXZDebugLog,@"Download complete");
DESTROY(_currentDownload);
}
- (void) download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
_downloadStatus = OXZ_DOWNLOAD_ERROR;
OOLog(kOOOXZErrorLog,@"Error downloading '%@': %@",[[download request] URL],[error localizedDescription]);
DESTROY(_currentDownload);
}
@end