Skip to content

Commit

Permalink
Merge pull request #1574 from slembcke/develop
Browse files Browse the repository at this point in the history
Publishing fixes.
  • Loading branch information
rkachowski committed Apr 21, 2015
2 parents 02eedd1 + 15fd1ee commit 3c4db04
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 54 deletions.
4 changes: 2 additions & 2 deletions SpriteBuilder/SpriteBuilder Tests/CCBPublisher_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ - (void)testSpriteSheets
// The resolutions tests may be a bit too much here, but there are no
// Tupac tests at the moment
[self assertFileExists:@"Published-iOS/sheet-2x.plist"];
[self assertPNGAtPath:@"Published-iOS/sheet-2x.png" hasWidth:32 hasHeight:16];
[self assertPNGAtPath:@"Published-iOS/sheet-2x.png" hasWidth:16 hasHeight:16];
[self assertFileExists:@"Published-iOS/sheet-4x.plist"];
[self assertPNGAtPath:@"Published-iOS/sheet-4x.png" hasWidth:32 hasHeight:32];
[self assertPNGAtPath:@"Published-iOS/sheet-4x.png" hasWidth:32 hasHeight:16];
[self assertFileExists:@"Published-iOS/sheet-1x.plist"];
[self assertPNGAtPath:@"Published-iOS/sheet-1x.png" hasWidth:16 hasHeight:8];

Expand Down
3 changes: 1 addition & 2 deletions SpriteBuilder/ccBuilder/CCBDirectoryPublisher.m
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,12 @@ - (PublishSpriteSheetOperation *)createSpriteSheetOperation:(NSString *)publishD
statusProgress:_publishingTaskStatusProgress];

NSString *spriteSheetCacheDir = [_projectSettings.tempSpriteSheetCacheDirectory stringByAppendingPathComponent:subPath];
NSString *resolutionCacheDir = [spriteSheetCacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"resources-%@", resolution]];

operation.publishDirectory = publishDirectory;
operation.publishedPNGFiles = _publishedPNGFiles;
operation.srcSpriteSheetDate = srcSpriteSheetDate;
operation.resolution = resolution;
operation.srcDirs = @[resolutionCacheDir, spriteSheetCacheDir];
operation.srcDir = spriteSheetCacheDir;
operation.spriteSheetFile = spriteSheetFile;
operation.subPath = subPath;
operation.osType = _osType;
Expand Down
5 changes: 5 additions & 0 deletions SpriteBuilder/ccBuilder/PublishGeneratedFilesOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ - (void)generateFileLookup
{
[_warnings addWarningWithDescription:@"Could not write fileLookup.plist."];
}

if (![_fileLookup TEMPwriteMetadataToFileAtomically:[_outputDir stringByAppendingPathComponent:@"metadata.plist"]])
{
[_warnings addWarningWithDescription:@"Could not write metadata.plist."];
}
}

- (NSString *)description
Expand Down
2 changes: 2 additions & 0 deletions SpriteBuilder/ccBuilder/PublishImageOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ - (void)publishImage
dither:_dither
compress:_compress
isSpriteSheet:_isSpriteSheet
isAutoScaled:NO
outputFilename:&dstPathConverted
error:&error])
{
Expand Down Expand Up @@ -183,6 +184,7 @@ - (void)publishImage
dither:_dither
compress:_compress
isSpriteSheet:_isSpriteSheet
isAutoScaled:NO
outputFilename:&dstPathConverted
error:&error])
{
Expand Down
1 change: 1 addition & 0 deletions SpriteBuilder/ccBuilder/PublishRenamedFilesLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- (void)addRenamingRuleFrom:(NSString *)src to:(NSString *)dst;

- (BOOL)writeToFileAtomically:(NSString *)filePath;
- (BOOL)TEMPwriteMetadataToFileAtomically:(NSString *)filePath;

- (void)addIntermediateLookupPath:(NSString *)filePath;

Expand Down
14 changes: 14 additions & 0 deletions SpriteBuilder/ccBuilder/PublishRenamedFilesLookup.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ - (BOOL)writeToFileAtomically:(NSString *)filePath
return [plist writeToFile:filePath atomically:YES];
}

- (BOOL)TEMPwriteMetadataToFileAtomically:(NSString *)filePath
{
NSMutableDictionary *intermediateLookups = [self loadAndMergeIntermediateLookups];
[_lookup addEntriesFromDictionary:intermediateLookups];

NSMutableDictionary *data = [NSMutableDictionary dictionary];
for(NSString *alias in _lookup){
data[alias] = @{@"filename": _lookup[alias]};
}

NSDictionary *metadata = @{@"data": data};
return [metadata writeToFile:filePath atomically:YES];
}

- (NSMutableDictionary *)loadAndMergeIntermediateLookups
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
Expand Down
2 changes: 1 addition & 1 deletion SpriteBuilder/ccBuilder/PublishSpriteSheetOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@property (nonatomic, copy) NSString *spriteSheetFile;
@property (nonatomic) CCBPublisherOSType osType;
@property (nonatomic, copy) NSString *subPath;
@property (nonatomic, strong) NSArray *srcDirs;
@property (nonatomic, copy) NSString *srcDir;
@property (nonatomic, copy) NSNumber *resolution;
@property (nonatomic, copy) NSDate *srcSpriteSheetDate;
@property (nonatomic, copy) NSString *publishDirectory;
Expand Down
9 changes: 5 additions & 4 deletions SpriteBuilder/ccBuilder/PublishSpriteSheetOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (void)assertProperties
{
NSAssert(_spriteSheetFile != nil, @"spriteSheetFile should not be nil");
NSAssert(_subPath != nil, @"subPath should not be nil");
NSAssert(_srcDirs != nil, @"srcDirs should not be nil");
NSAssert(_srcDir != nil, @"srcDir should not be nil");
NSAssert(_resolution != nil, @"resolution should not be nil");
NSAssert(_srcSpriteSheetDate != nil, @"srcSpriteSheetDate should not be nil");
NSAssert(_publishDirectory != nil, @"publishDirectory should not be nil");
Expand All @@ -73,7 +73,8 @@ - (void)publishSpriteSheet

[self configurePacker];

NSArray *createdFiles = [_packer createTextureAtlasFromDirectoryPaths:_srcDirs];
NSString *suffix = [NSString stringWithFormat:@"-%dx", self.resolution.intValue];
NSArray *createdFiles = [_packer createTextureAtlasFromDirectoryPath:_srcDir withSuffix:suffix];

[self addCreatedPNGFilesToCreatedFilesSet:createdFiles];

Expand Down Expand Up @@ -190,8 +191,8 @@ - (void)cancel

- (NSString *)description
{
return [NSString stringWithFormat:@"file: %@, res: %@, osType: %i, filefull: %@, srcdirs: %@, publishDirectory: %@, date: %@",
[_spriteSheetFile lastPathComponent], _resolution, _osType, _spriteSheetFile, _srcDirs, _publishDirectory, _srcSpriteSheetDate];
return [NSString stringWithFormat:@"file: %@, res: %@, osType: %i, filefull: %@, srcdir: %@, publishDirectory: %@, date: %@",
[_spriteSheetFile lastPathComponent], _resolution, _osType, _spriteSheetFile, _srcDir, _publishDirectory, _srcSpriteSheetDate];
}


Expand Down
1 change: 1 addition & 0 deletions SpriteBuilder/libs/FCFormatConverter/FCFormatConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef enum {
dither:(BOOL)dither
compress:(BOOL)compress
isSpriteSheet:(BOOL)isSpriteSheet
isAutoScaled:(BOOL)isAutoScaled
outputFilename:(NSString**)outputFilename
error:(NSError**)error;

Expand Down
16 changes: 13 additions & 3 deletions SpriteBuilder/libs/FCFormatConverter/FCFormatConverter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ -(BOOL)convertImageAtPath:(NSString*)srcPath
dither:(BOOL)dither
compress:(BOOL)compress
isSpriteSheet:(BOOL)isSpriteSheet
isAutoScaled:(BOOL)isAutoScaled
outputFilename:(NSString**)outputFilename
error:(NSError**)error;
{
Expand Down Expand Up @@ -191,7 +192,16 @@ -(BOOL)convertImageAtPath:(NSString*)srcPath
hasError = YES;
}


if(isAutoScaled && !hasError && !pvrtexture::GenerateMIPMaps(*pvrTexture, pvrtexture::eResizeLinear)){
if (error)
{
NSString * errorMessage = [NSString stringWithFormat:@"Failure to generate mipmaps: %@", srcPath];
NSDictionary * userInfo __attribute__((unused)) =@{NSLocalizedDescriptionKey:errorMessage};
*error = [NSError errorWithDomain:kErrorDomain code:EPERM userInfo:userInfo];
}
hasError = YES;
}

if(!hasError && !Transcode(*pvrTexture, pixelType, variableType, ePVRTCSpacelRGB, pvrtexture::ePVRTCBest, dither))
{
if (error)
Expand Down Expand Up @@ -235,7 +245,7 @@ -(BOOL)convertImageAtPath:(NSString*)srcPath
// Create compressed file (ccz)
self.zipTask = [[NSTask alloc] init];
[_zipTask setCurrentDirectoryPath:dstDir];
[_zipTask setLaunchPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ccz"]];
[_zipTask setLaunchPath:@"/usr/bin/gzip"];
NSMutableArray* args = [NSMutableArray arrayWithObjects:dstPath, nil];
[_zipTask setArguments:args];
[_zipTask launch];
Expand All @@ -246,7 +256,7 @@ -(BOOL)convertImageAtPath:(NSString*)srcPath
[[NSFileManager defaultManager] removeItemAtPath:dstPath error:NULL];

// Update name of texture file
dstPath = [dstPath stringByAppendingPathExtension:@"ccz"];
dstPath = [dstPath stringByAppendingPathExtension:@"gz"];
}

if ([fm fileExistsAtPath:dstPath])
Expand Down
5 changes: 3 additions & 2 deletions SpriteBuilder/libs/Tupac/Tupac.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@property(nonatomic,copy) NSString* directoryPrefix;
@property(nonatomic,assign) int maxTextureSize;
@property(nonatomic,assign) int padding;
@property(nonatomic,assign) int divisor;
@property(nonatomic,assign) BOOL dither;
@property(nonatomic,assign) BOOL compress;
@property(nonatomic,assign) BOOL trim;
Expand All @@ -50,8 +51,8 @@

+ (NSRect) trimmedRectForImage:(CGImageRef)image;

- (NSArray *)createTextureAtlasFromDirectoryPaths:(NSArray *)dirs;
- (NSArray *)createTextureAtlas;
- (NSArray *)createTextureAtlasFromDirectoryPath:(NSString *)dir withSuffix:(NSString *)suffix;
- (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix;

- (void)cancel;

Expand Down
78 changes: 39 additions & 39 deletions SpriteBuilder/libs/Tupac/Tupac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ - (id)init
self.outputFormat = TupacOutputFormatCocos2D;
self.maxTextureSize = 2048;
self.padding = 1;
self.divisor = 1;
self.trim = YES;
}
return self;
Expand Down Expand Up @@ -200,7 +201,24 @@ + (NSRect) trimmedRectForImage:(CGImageRef)image
return NSMakeRect(x, y, wTrimmed, hTrimmed);
}

- (NSArray *)createTextureAtlas
static NSString *
TrimSuffix(NSString *filename, NSString *suffix)
{
NSString *basename = [filename stringByDeletingPathExtension];
if([basename hasSuffix:suffix]) basename = [basename substringToIndex:basename.length - suffix.length];
NSString *ext = [filename pathExtension];

return [basename stringByAppendingPathExtension:ext];
}

// Pad a size and round the result up to a multiple of the divisor
static int
PadSize(int size, int padding, int divisor)
{
return (size + padding + divisor - 1)/divisor*divisor;
}

- (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix
{
// Reset the error message
if (errorMessage)
Expand Down Expand Up @@ -312,7 +330,7 @@ - (NSArray *)createTextureAtlas
BOOL packingError = NO;
while (!packingError && !allFitted)
{
MaxRectsBinPack bin(outW, outH);
MaxRectsBinPack bin(outW - self.padding, outH - self.padding);

std::vector<TPRectSize> inRects;

Expand All @@ -322,8 +340,8 @@ - (NSArray *)createTextureAtlas
NSRect trimRect = [imageInfo[@"trimRect"] rectValue];

inRects.push_back(TPRectSize());
inRects[numImages].width = (int) (trimRect.size.width + self.padding * 2);
inRects[numImages].height = (int) (trimRect.size.height + self.padding * 2);
inRects[numImages].width = PadSize(trimRect.size.width, self.padding, self.divisor);
inRects[numImages].height = PadSize(trimRect.size.height, self.padding, self.divisor);
inRects[numImages].idx = numImages;

numImages++;
Expand Down Expand Up @@ -471,6 +489,7 @@ - (NSArray *)createTextureAtlas
dither:dither_
compress:compress_
isSpriteSheet:YES
isAutoScaled:NO
outputFilename:&textureFileName
error:&error])
{
Expand Down Expand Up @@ -503,7 +522,7 @@ - (NSArray *)createTextureAtlas
{
// Get info about the image
NSString* filename = self.filenames[(NSUInteger) outRects[index].idx];
NSString* exportFilename = [filename lastPathComponent];
NSString* exportFilename = TrimSuffix([filename lastPathComponent], suffix);
if (directoryPrefix_) exportFilename = [directoryPrefix_ stringByAppendingPathComponent:exportFilename];
NSDictionary* imageInfo = imageInfos[(NSUInteger) outRects[index].idx];

Expand Down Expand Up @@ -539,7 +558,7 @@ - (NSArray *)createTextureAtlas
@"sourceSize" : NSStringFromSize(NSMakeSize(wSrc, hSrc))};
}

metadata[@"textureFileName"] = textureFileName;
metadata[@"textureFileName"] = TrimSuffix(textureFileName, suffix);
metadata[@"format"] = @2;
metadata[@"size"] = NSStringFromSize(NSMakeSize(outW, outH));

Expand Down Expand Up @@ -573,52 +592,33 @@ - (void)generatePreviewImage:(NSString *)pngFilename
}
}

- (NSArray *) createTextureAtlasFromDirectoryPaths:(NSArray *)dirs
- (NSArray *) createTextureAtlasFromDirectoryPath:(NSString *)dir withSuffix:(NSString *)suffix;
{
NSFileManager* fm = [NSFileManager defaultManager];

// Build a list of all file names from all directories
NSMutableSet* allFiles = [NSMutableSet set];
// Add all the absolute file names to an array from the correct directories
NSMutableArray* absoluteFilepaths = [NSMutableArray array];

for (NSString* dir in dirs)
{
NSArray* files = [fm contentsOfDirectoryAtPath:dir error:NULL];

if (cancelled_)
{
return nil;
}
NSArray* files = [fm contentsOfDirectoryAtPath:dir error:NULL];

for (NSString* file in files)
{
NSString *lower = [[file pathExtension] lowercaseString];
if ([lower isEqualToString:@"png"] || [lower isEqualToString:@"psd"])
{
[allFiles addObject:[file lastPathComponent]];
}
}
if (cancelled_)
{
return nil;
}

// Add all the absolute file names to an array from the correct directories
NSMutableArray* absoluteFilepaths = [NSMutableArray array];
for (NSString* file in allFiles)

for (NSString* file in files)
{
for (NSString* dir in dirs)
NSString *ext = [[file pathExtension] lowercaseString];
NSString *basename = [file stringByDeletingPathExtension];
if ([basename hasSuffix:suffix] && ([ext isEqualToString:@"png"] || [ext isEqualToString:@"psd"]))
{
NSString* absFilepath = [dir stringByAppendingPathComponent:file];

if ([fm fileExistsAtPath:absFilepath])
{
[absoluteFilepaths addObject:absFilepath];
//foundFile = YES;
break;
}
[absoluteFilepaths addObject:[dir stringByAppendingPathComponent:file]];
}
}

// Generate the sprite sheet
self.filenames = absoluteFilepaths;
return [self createTextureAtlas];
return [self createTextureAtlasTrimSuffix:suffix];
}

- (void)cancel
Expand Down
2 changes: 1 addition & 1 deletion SpriteBuilder/libs/cocos2d-iphone

0 comments on commit 3c4db04

Please sign in to comment.