Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
GetMetadataForFile.m File Reference
import <CoreFoundation/CoreFoundation.h>
import <CoreServices/CoreServices.h>
import <Foundation/Foundation.h>
import <stdarg.h>
import "NSScannerOOExtensions.h"
import "OOCollectionExtractors.h"
import "NSDataOOExtensions.h"
+ Include dependency graph for GetMetadataForFile.m:

Go to the source code of this file.

Macros

#define kTitle   (NSString *)kMDItemTitle
 
#define kAuthors   (NSString *)kMDItemAuthors
 
#define kVersion   (NSString *)kMDItemVersion
 
#define kCopyright   (NSString *)kMDItemCopyright
 
#define kIdentifier   (NSString *)kMDItemIdentifier
 
#define kDescription   (NSString *)kMDItemDescription
 
#define kURL   (NSString *)kMDItemURL
 
#define kTextContent   (NSString *)kMDItemTextContent
 
#define kShipIDs   @"org_aegidian_oolite_shipids"
 
#define kShipClassNames   @"org_aegidian_oolite_shipclassnames"
 
#define kShipRoles   @"org_aegidian_oolite_shiproles"
 
#define kShipModels   @"org_aegidian_oolite_shipmodels"
 
#define kCombatRating   @"org_aegidian_oolite_combatrating"
 
#define kSystemName   @"org_aegidian_oolite_systemname"
 
#define kMinOoliteVersion   @"org_aegidian_oolite_minversion"
 
#define kMaxOoliteVersion   @"org_aegidian_oolite_maxversion"
 

Functions

static bool GetMetadataForSaveFile (void *thisInterface, NSMutableDictionary *attributes, NSString *pathToFile)
 
static bool GetMetadataForExpansionPack (void *thisInterface, NSMutableDictionary *attributes, NSString *pathToFile)
 
static id GetBundlePropertyList (NSString *inPListName)
 
static NSDictionary * ConfigDictionary (NSString *basePath, NSString *name)
 
static NSDictionary * MergeShipData (NSDictionary *shipData, NSDictionary *shipDataOverrides)
 
static NSDictionary * MergeShipDataEntry (NSDictionary *baseDict, NSDictionary *overrideDict)
 
static NSDictionary * OOParseRolesFromString (NSString *string)
 
static NSMutableArray * ScanTokensFromString (NSString *values)
 
BOOL GetMetadataForFile (void *thisInterface, NSMutableDictionary *attributes, CFStringRef contentTypeUTI, NSString *pathToFile)
 
void OOLogWithFunctionFileAndLine (NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat,...)
 

Variables

NSString *const kOOLogFileNotFound = @""
 

Macro Definition Documentation

◆ kAuthors

#define kAuthors   (NSString *)kMDItemAuthors

Definition at line 35 of file GetMetadataForFile.m.

◆ kCombatRating

#define kCombatRating   @"org_aegidian_oolite_combatrating"

Definition at line 46 of file GetMetadataForFile.m.

◆ kCopyright

#define kCopyright   (NSString *)kMDItemCopyright

Definition at line 37 of file GetMetadataForFile.m.

◆ kDescription

#define kDescription   (NSString *)kMDItemDescription

Definition at line 39 of file GetMetadataForFile.m.

◆ kIdentifier

#define kIdentifier   (NSString *)kMDItemIdentifier

Definition at line 38 of file GetMetadataForFile.m.

◆ kMaxOoliteVersion

#define kMaxOoliteVersion   @"org_aegidian_oolite_maxversion"

Definition at line 49 of file GetMetadataForFile.m.

◆ kMinOoliteVersion

#define kMinOoliteVersion   @"org_aegidian_oolite_minversion"

Definition at line 48 of file GetMetadataForFile.m.

◆ kShipClassNames

#define kShipClassNames   @"org_aegidian_oolite_shipclassnames"

Definition at line 43 of file GetMetadataForFile.m.

◆ kShipIDs

#define kShipIDs   @"org_aegidian_oolite_shipids"

Definition at line 42 of file GetMetadataForFile.m.

◆ kShipModels

#define kShipModels   @"org_aegidian_oolite_shipmodels"

Definition at line 45 of file GetMetadataForFile.m.

◆ kShipRoles

#define kShipRoles   @"org_aegidian_oolite_shiproles"

Definition at line 44 of file GetMetadataForFile.m.

◆ kSystemName

#define kSystemName   @"org_aegidian_oolite_systemname"

Definition at line 47 of file GetMetadataForFile.m.

◆ kTextContent

#define kTextContent   (NSString *)kMDItemTextContent

Definition at line 41 of file GetMetadataForFile.m.

◆ kTitle

#define kTitle   (NSString *)kMDItemTitle

Definition at line 34 of file GetMetadataForFile.m.

◆ kURL

#define kURL   (NSString *)kMDItemURL

Definition at line 40 of file GetMetadataForFile.m.

◆ kVersion

#define kVersion   (NSString *)kMDItemVersion

Definition at line 36 of file GetMetadataForFile.m.

Function Documentation

◆ ConfigDictionary()

static NSDictionary * ConfigDictionary ( NSString * basePath,
NSString * name )
static

Definition at line 235 of file GetMetadataForFile.m.

236{
237 NSString *path = [[basePath stringByAppendingPathComponent:@"Config"] stringByAppendingPathComponent:name];
238 NSData *data = [NSData oo_dataWithOXZFile:path];
239 if (data == nil)
240 {
241 path = [basePath stringByAppendingPathComponent:name];
242 data = [NSData oo_dataWithOXZFile:path];
243 }
244 if (data != nil)
245 {
246 id plist = [NSPropertyListSerialization propertyListWithData:data
247 options:0
248 format:NULL
249 error:NULL];
250 if ([plist isKindOfClass:NSDictionary.class]) return plist;
251 }
252
253 return nil;
254}
return nil

References nil.

Referenced by GetMetadataForExpansionPack().

+ Here is the caller graph for this function:

◆ GetBundlePropertyList()

static id GetBundlePropertyList ( NSString * inPListName)
static

Definition at line 226 of file GetMetadataForFile.m.

227{
228 NSBundle *bundle = [NSBundle bundleWithIdentifier:@"org.aegidian.oolite.md-importer"];
229 NSString *path = [bundle pathForResource:inPListName ofType:@"plist"];
230 NSData *data = [NSData dataWithContentsOfFile:path];
231 return [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
232}

Referenced by GetMetadataForSaveFile().

+ Here is the caller graph for this function:

◆ GetMetadataForExpansionPack()

static bool GetMetadataForExpansionPack ( void * thisInterface,
NSMutableDictionary * attributes,
NSString * pathToFile )
static

Definition at line 135 of file GetMetadataForFile.m.

136{
137 NSDictionary *manifest = ConfigDictionary(pathToFile, @"manifest.plist");
138 if (manifest != nil)
139 {
140 NSString *title = [manifest oo_stringForKey:@"title"];
141 if (title != nil) attributes[kTitle] = title;
142
143 NSString *identifier = [manifest oo_stringForKey:@"identifier"];
144 if (identifier != nil) attributes[kIdentifier] = identifier;
145
146 NSString *version = [manifest oo_stringForKey:@"version"];
147 if (version != nil) attributes[kVersion] = version;
148
149 // Allow a string or array for author
150 id author = manifest[@"author"];
151 if ([author isKindOfClass:NSString.class]) author = @[author];
152 if ([author isKindOfClass:NSArray.class]) attributes[kAuthors] = author;
153
154 NSString *description = [manifest oo_stringForKey:@"description"];
155 if (description != nil) attributes[kDescription] = description;
156
157 NSString *copyright = [manifest oo_stringForKey:@"copyright"];
158 if (copyright == nil) copyright = [manifest oo_stringForKey:@"license"];
159 if (copyright != nil) attributes[kCopyright] = copyright;
160
161 NSString *url = [manifest oo_stringForKey:@"download_url"];
162 if (url == nil) url = [manifest oo_stringForKey:@"information_url"];
163 if (url != nil) attributes[kURL] = url;
164
165 NSString *minVersion = [manifest oo_stringForKey:@"required_oolite_version"];
166 if (minVersion != nil) attributes[kMinOoliteVersion] = minVersion;
167 }
168 else
169 {
170 // No manifest, look for requires.plist
171
172 NSDictionary *requires = ConfigDictionary(pathToFile, @"requires.plist");
173 if (requires != nil)
174 {
175 NSString *minVersion = [requires objectForKey:@"version"];
176 if (minVersion != nil) attributes[kMinOoliteVersion] = minVersion;
177
178 NSString *maxVersion = [requires objectForKey:@"max_version"];
179 if (maxVersion != nil) attributes[kMaxOoliteVersion] = maxVersion;
180 }
181
182 // Not "officially" supported, but exists in some OXPs.
183 NSDictionary *infoPList = ConfigDictionary(pathToFile, @"Info.plist");
184 NSString *version = [infoPList oo_stringForKey:@"CFBundleVersion"];
185 if (version != nil) attributes[kVersion] = version;
186 }
187
188 NSDictionary *shipData = ConfigDictionary(pathToFile, @"shipdata.plist");
189 NSDictionary *shipDataOverrides = ConfigDictionary(pathToFile, @"shipdata-overrides.plist");
190 shipData = MergeShipData(shipData, shipDataOverrides);
191
192 if (shipData.count != 0)
193 {
194 NSMutableSet *shipNames = [NSMutableSet setWithCapacity:shipData.count];
195 NSMutableSet *shipModels = [NSMutableSet setWithCapacity:shipData.count];
196 NSMutableSet *shipRoles = [NSMutableSet new];
197
198 attributes[kShipIDs] = shipData.allKeys;
199
200 for (NSDictionary *ship in shipData)
201 {
202 if (![ship isKindOfClass:NSDictionary.class]) continue;
203
204 NSString *name = [ship oo_stringForKey:@"name"];
205 if (name != nil) [shipNames addObject:name];
206
207 NSString *model = [ship oo_stringForKey:@"model"];
208 if (model != nil) [shipModels addObject:model];
209
210 NSString *role = [ship oo_stringForKey:@"roles"];
211 if (role != nil) [shipRoles addObjectsFromArray:OOParseRolesFromString(role).allKeys];
212 }
213
214 attributes[kShipClassNames] = shipNames.allObjects;
215 attributes[kShipModels] = shipModels.allObjects;
216 attributes[kShipRoles] = shipRoles.allObjects;
217 }
218
219 return YES;
220}
static NSDictionary * ConfigDictionary(NSString *basePath, NSString *name)
static NSDictionary * MergeShipData(NSDictionary *shipData, NSDictionary *shipDataOverrides)

References ConfigDictionary(), MergeShipData(), and nil.

Referenced by GetMetadataForFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetMetadataForFile()

BOOL GetMetadataForFile ( void * thisInterface,
NSMutableDictionary * attributes,
CFStringRef contentTypeUTI,
NSString * pathToFile )

Definition at line 68 of file GetMetadataForFile.m.

72{
73 @autoreleasepool
74 {
75 @try
76 {
77 if (UTTypeConformsTo(contentTypeUTI, CFSTR("org.aegidian.oolite.save")))
78 {
79 return GetMetadataForSaveFile(thisInterface, attributes, pathToFile);
80 }
81 if (UTTypeConformsTo(contentTypeUTI, CFSTR("org.aegidian.oolite.expansion")))
82 {
83 return GetMetadataForExpansionPack(thisInterface, attributes, pathToFile);
84 }
85 }
86 @catch (id any)
87 {
88 return false;
89 }
90 }
91}
static bool GetMetadataForSaveFile(void *thisInterface, NSMutableDictionary *attributes, NSString *pathToFile)
static bool GetMetadataForExpansionPack(void *thisInterface, NSMutableDictionary *attributes, NSString *pathToFile)

References GetMetadataForExpansionPack(), and GetMetadataForSaveFile().

+ Here is the call graph for this function:

◆ GetMetadataForSaveFile()

static bool GetMetadataForSaveFile ( void * thisInterface,
NSMutableDictionary * attributes,
NSString * pathToFile )
static

Definition at line 93 of file GetMetadataForFile.m.

94{
95 NSDictionary *content = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
96 if (content == nil) return false;
97
98 NSString *playerName = [content oo_stringForKey:@"player_name"];
99 if (playerName != nil) attributes[kTitle] = playerName;
100
101 NSString *shipDesc = [content oo_stringForKey:@"ship_desc"];
102 if (shipDesc != nil) attributes[kShipIDs] = shipDesc;
103
104 NSString *shipName = [content oo_stringForKey:@"ship_name"];
105 if (shipName != nil) attributes[kShipClassNames] = shipName;
106
107 NSString *currentSystemName = [content oo_stringForKey:@"current_system_name"];
108 if (currentSystemName != nil) attributes[kSystemName] = currentSystemName;
109
110 NSArray *commLog = [content oo_arrayForKey:@"comm_log"];
111 if (commLog.count != 0) attributes[kTextContent] = [commLog componentsJoinedByString:@"\n"];
112
113 NSInteger killCount = [content oo_integerForKey:@"ship_kills"];
114 if (killCount > 0)
115 {
116 NSArray *ratings = GetBundlePropertyList(@"Values")[@"ratings"];
117 if (ratings != nil)
118 {
119 int rating = 0;
120 const int kRequiredKills[8] = { 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0200, 0x0A00, 0x1900 };
121
122 while (rating < 8 && kRequiredKills[rating] <= killCount)
123 {
124 rating ++;
125 }
126
127 attributes[kCombatRating] = ratings[rating];
128 }
129 }
130
131 return true;
132}
static id GetBundlePropertyList(NSString *inPListName)

References GetBundlePropertyList(), and nil.

Referenced by GetMetadataForFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MergeShipData()

static NSDictionary * MergeShipData ( NSDictionary * shipData,
NSDictionary * shipDataOverrides )
static

Definition at line 257 of file GetMetadataForFile.m.

258{
259 NSDictionary *baseDict = nil;
260 NSDictionary *overrideDict = nil;
261
262 if (shipDataOverrides == nil) return shipData;
263 if (shipData == nil) return shipDataOverrides;
264
265 NSMutableDictionary *mutableShipData = [NSMutableDictionary dictionaryWithDictionary:shipData];
266 for (NSString *key in shipDataOverrides)
267 {
268 baseDict = [shipData oo_dictionaryForKey:key];
269 overrideDict = [shipDataOverrides oo_dictionaryForKey:key];
270 mutableShipData[key] = MergeShipDataEntry(baseDict, overrideDict);
271 }
272
273 return mutableShipData;
274}
static NSDictionary * MergeShipDataEntry(NSDictionary *baseDict, NSDictionary *overrideDict)

References MergeShipDataEntry(), and nil.

Referenced by GetMetadataForExpansionPack().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MergeShipDataEntry()

static NSDictionary * MergeShipDataEntry ( NSDictionary * baseDict,
NSDictionary * overrideDict )
static

Definition at line 277 of file GetMetadataForFile.m.

278{
279 if (baseDict == nil) return overrideDict;
280
281 NSMutableDictionary *mutableEntry = [NSMutableDictionary dictionaryWithDictionary:baseDict];
282 [mutableEntry addEntriesFromDictionary:overrideDict];
283
284 return mutableEntry;
285}

References nil.

Referenced by MergeShipData().

+ Here is the caller graph for this function:

◆ OOLogWithFunctionFileAndLine()

void OOLogWithFunctionFileAndLine ( NSString * inMessageClass,
const char * inFunction,
const char * inFile,
unsigned long inLine,
NSString * inFormat,
... )

Definition at line 365 of file GetMetadataForFile.m.

366{
367
368}

◆ OOParseRolesFromString()

static NSDictionary * OOParseRolesFromString ( NSString * string)
static

Definition at line 290 of file GetMetadataForFile.m.

291{
292 NSMutableDictionary *result = nil;
293 NSArray *tokens = nil;
294 NSUInteger i, count;
295 NSString *role = nil;
296 float probability;
297 NSScanner *scanner = nil;
298
299 // Split string at spaces, sanity checks, set-up.
300 if (string == nil) return nil;
301
302 tokens = ScanTokensFromString(string);
303 count = [tokens count];
304 if (count == 0) return nil;
305
306 result = [NSMutableDictionary dictionaryWithCapacity:count];
307
308 // Scan tokens, looking for probabilities.
309 for (i = 0; i != count; ++i)
310 {
311 role = [tokens objectAtIndex:i];
312
313 probability = 1.0f;
314 if ([role rangeOfString:@"("].location != NSNotFound)
315 {
316 scanner = [[NSScanner alloc] initWithString:role];
317 [scanner scanUpToString:@"(" intoString:&role];
318 [scanner scanString:@"(" intoString:NULL];
319 if (![scanner scanFloat:&probability]) probability = 1.0f;
320 // Ignore rest of string
321 }
322
323 // shipKey roles start with [ so other roles can't
324 if (0 <= probability && ![role hasPrefix:@"["])
325 {
326 [result setObject:[NSNumber numberWithFloat:probability] forKey:role];
327 }
328 }
329
330 if ([result count] == 0) result = nil;
331 return result;
332}
static NSMutableArray * ScanTokensFromString(NSString *values)
unsigned count

◆ ScanTokensFromString()

static NSMutableArray * ScanTokensFromString ( NSString * values)
static

Definition at line 335 of file GetMetadataForFile.m.

336{
337 NSMutableArray *result = nil;
338 NSScanner *scanner = nil;
339 NSString *token = nil;
340 static NSCharacterSet *space_set = nil;
341
342 if (values == nil) return [NSMutableArray array];
343 if (space_set == nil) space_set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
344
345 result = [NSMutableArray array];
346 scanner = [NSScanner scannerWithString:values];
347
348 while (![scanner isAtEnd])
349 {
350 [scanner ooliteScanCharactersFromSet:space_set intoString:NULL];
351 if ([scanner ooliteScanUpToCharactersFromSet:space_set intoString:&token])
352 {
353 [result addObject:token];
354 }
355 }
356
357 return result;
358}

Referenced by OOParseRolesFromString().

+ Here is the caller graph for this function:

Variable Documentation

◆ kOOLogFileNotFound

NSString* const kOOLogFileNotFound = @""

Definition at line 371 of file GetMetadataForFile.m.

Referenced by CopyDataFromFile(), and GetShaderSource().