Line data Source code
1 0 : /* 2 : 3 : OOTextureVerifierStage.m 4 : 5 : 6 : Oolite 7 : Copyright (C) 2004-2013 Giles C Williams and contributors 8 : 9 : This program is free software; you can redistribute it and/or 10 : modify it under the terms of the GNU General Public License 11 : as published by the Free Software Foundation; either version 2 12 : of the License, or (at your option) any later version. 13 : 14 : This program is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU General Public License for more details. 18 : 19 : You should have received a copy of the GNU General Public License 20 : along with this program; if not, write to the Free Software 21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 22 : MA 02110-1301, USA. 23 : 24 : */ 25 : 26 : #import "OOTextureVerifierStage.h" 27 : 28 : #if OO_OXP_VERIFIER_ENABLED 29 : 30 : #import "OOTextureLoader.h" 31 : #import "OOFileScannerVerifierStage.h" 32 : #import "OOMaths.h" 33 : 34 0 : static NSString * const kStageName = @"Testing textures and images"; 35 : 36 : 37 : @interface OOTextureVerifierStage (OOPrivate) 38 : 39 0 : - (void)checkTextureNamed:(NSString *)name inFolder:(NSString *)folder; 40 : 41 : @end 42 : 43 : 44 : @implementation OOTextureVerifierStage 45 : 46 0 : - (id)init 47 : { 48 : self = [super init]; 49 : if (self != nil) 50 : { 51 : _usedTextures = [[NSMutableSet alloc] init]; 52 : } 53 : return self; 54 : } 55 : 56 : 57 0 : - (void)dealloc 58 : { 59 : [_usedTextures release]; 60 : 61 : [super dealloc]; 62 : } 63 : 64 : 65 : + (NSString *)nameForReverseDependencyForVerifier:(OOOXPVerifier *)verifier 66 : { 67 : return kStageName; 68 : } 69 : 70 : 71 0 : - (NSString *)name 72 : { 73 : return kStageName; 74 : } 75 : 76 : 77 0 : - (BOOL)shouldRun 78 : { 79 : return [_usedTextures count] != 0 || [[[self verifier] fileScannerStage] filesInFolder:@"Images"] != nil; 80 : } 81 : 82 : 83 0 : - (void)run 84 : { 85 : NSEnumerator *nameEnum = nil; 86 : NSString *name = nil; 87 : NSAutoreleasePool *pool = nil; 88 : 89 : for (nameEnum = [_usedTextures objectEnumerator]; (name = [nameEnum nextObject]); ) 90 : { 91 : pool = [[NSAutoreleasePool alloc] init]; 92 : [self checkTextureNamed:name inFolder:@"Textures"]; 93 : [pool release]; 94 : } 95 : [_usedTextures release]; 96 : _usedTextures = nil; 97 : 98 : // All "images" are considered used, since we don't have a reasonable way to look for images referenced in JavaScript scripts. 99 : nameEnum = [[[[self verifier] fileScannerStage] filesInFolder:@"Images"] objectEnumerator]; 100 : while ((name = [nameEnum nextObject])) 101 : { 102 : [self checkTextureNamed:name inFolder:@"Images"]; 103 : } 104 : } 105 : 106 : 107 : - (void) textureNamed:(NSString *)name usedInContext:(NSString *)context 108 : { 109 : OOFileScannerVerifierStage *fileScanner = nil; 110 : 111 : if (name == nil) return; 112 : if ([_usedTextures containsObject:name]) return; 113 : [_usedTextures addObject:name]; 114 : 115 : fileScanner = [[self verifier] fileScannerStage]; 116 : if (![fileScanner fileExists:name 117 : inFolder:@"Textures" 118 : referencedFrom:context 119 : checkBuiltIn:YES]) 120 : { 121 : OOLog(@"verifyOXP.texture.notFound", @"----- WARNING: texture \"%@\" referenced in %@ could not be found in %@ or in Oolite.", name, context, [[self verifier] oxpDisplayName]); 122 : } 123 : } 124 : 125 : @end 126 : 127 : 128 : @implementation OOTextureVerifierStage (OOPrivate) 129 : 130 : - (void)checkTextureNamed:(NSString *)name inFolder:(NSString *)folder 131 : { 132 : OOTextureLoader *loader = nil; 133 : NSString *path = nil; 134 : OOFileScannerVerifierStage *fileScanner = nil; 135 : NSString *displayName = nil; 136 : OOPixMapDimension rWidth, rHeight; 137 : BOOL success; 138 : OOPixMap pixmap; 139 : OOTextureDataFormat format; 140 : 141 : fileScanner = [[self verifier] fileScannerStage]; 142 : path = [fileScanner pathForFile:name 143 : inFolder:folder 144 : referencedFrom:nil 145 : checkBuiltIn:NO]; 146 : 147 : if (path == nil) return; 148 : 149 : loader = [OOTextureLoader loaderWithPath:path 150 : options:kOOTextureMinFilterNearest | 151 : kOOTextureMinFilterNearest | 152 : kOOTextureNoShrink | 153 : kOOTextureNoFNFMessage | 154 : kOOTextureNeverScale]; 155 : 156 : displayName = [fileScanner displayNameForFile:name andFolder:folder]; 157 : if (loader == nil) 158 : { 159 : OOLog(@"verifyOXP.texture.failed", @"***** ERROR: image %@ could not be read.", displayName); 160 : } 161 : else 162 : { 163 : success = [loader getResult:&pixmap format:&format originalWidth:NULL originalHeight:NULL]; 164 : 165 : if (success) 166 : { 167 : rWidth = OORoundUpToPowerOf2_PixMap((2 * pixmap.width) / 3); 168 : rHeight = OORoundUpToPowerOf2_PixMap((2 * pixmap.height) / 3); 169 : if (pixmap.width != rWidth || pixmap.height != rHeight) 170 : { 171 : OOLog(@"verifyOXP.texture.notPOT", @"----- WARNING: image %@ has non-power-of-two dimensions; it will have to be rescaled (from %ux%u pixels to %ux%u pixels) at runtime.", displayName, pixmap.width, pixmap.height, rWidth, rHeight); 172 : } 173 : else 174 : { 175 : OOLog(@"verifyOXP.verbose.texture.OK", @"- %@ (%ux%u px) OK.", displayName, pixmap.width, pixmap.height); 176 : } 177 : 178 : OOFreePixMap(&pixmap); 179 : } 180 : else 181 : { 182 : OOLog(@"verifyOXP.texture.failed", @"***** ERROR: texture loader failed to load %@.", displayName); 183 : } 184 : } 185 : } 186 : 187 : @end 188 : 189 : 190 : @implementation OOTextureHandlingStage 191 : 192 0 : - (NSSet *)dependents 193 : { 194 : NSMutableSet *result = [[super dependents] mutableCopy]; 195 : [result addObject:[OOTextureVerifierStage nameForReverseDependencyForVerifier:[self verifier]]]; 196 : return [result autorelease]; 197 : } 198 : 199 : @end 200 : 201 : 202 : @implementation OOOXPVerifier(OOTextureVerifierStage) 203 : 204 : - (OOTextureVerifierStage *)textureVerifierStage 205 : { 206 : return [self stageWithName:kStageName]; 207 : } 208 : 209 : @end 210 : 211 : #endif