Oolite
Loading...
Searching...
No Matches
OOConvertSystemDescriptions.m
Go to the documentation of this file.
1/*
2
3OOConvertSystemDescriptions.m
4Oolite
5
6
7Copyright (C) 2008 Jens Ayton
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26
27*/
28
29#import "Universe.h"
30
31#if OO_LOCALIZATION_TOOLS
32
36#import "ResourceManager.h"
37
38static NSMutableDictionary* InitKeyToIndexDict(NSDictionary* dict, NSMutableSet** outUsedIndices);
39static NSString* IndexToKey(NSUInteger index, NSDictionary* indicesToKeys, BOOL useFallback);
40static NSArray* ConvertIndicesToKeys(NSArray* entry, NSDictionary* indicesToKeys);
41static NSNumber* KeyToIndex(NSString* key, NSMutableDictionary* ioKeysToIndices, NSMutableSet* ioUsedIndicies, NSUInteger* ioSlotCache);
42static NSArray* ConvertKeysToIndices(NSArray* entry, NSMutableDictionary* ioKeysToIndices, NSMutableSet* ioUsedIndicies, NSUInteger* ioSlotCache);
43static NSUInteger HighestIndex(NSMutableDictionary* sparseArray); // Actually returns highest index + 1, which is fine.
44
46{
47 NSDictionary* sysDescDict = nil;
48 NSArray* sysDescArray = nil;
49 NSDictionary* keyMap = nil;
50 NSData* data = nil;
51 NSString* error = nil;
52
53 sysDescDict = [ResourceManager dictionaryFromFilesNamed:@"sysdesc.plist"
54 inFolder:@"Config"
55 andMerge:NO];
56 if (sysDescDict == nil) {
57 OOLog(@"sysdesc.compile.failed.fileNotFound", @"%@", @"Could not load a dictionary from sysdesc.plist, ignoring --compile-sysdesc option.");
58 return;
59 }
60
61 keyMap = [ResourceManager dictionaryFromFilesNamed:@"sysdesc_key_table.plist"
62 inFolder:@"Config"
63 andMerge:NO];
64 // keyMap is optional, so no nil check
65
66 sysDescArray = OOConvertSystemDescriptionsToArrayFormat(sysDescDict, keyMap);
67 if (sysDescArray == nil) {
68 OOLog(@"sysdesc.compile.failed.conversion", @"%@", @"Could not convert sysdesc.plist to descriptions.plist format for some reason.");
69 return;
70 }
71
72 sysDescDict = [NSDictionary dictionaryWithObject:sysDescArray forKey:@"system_description"];
73
74 if (asXML) {
75 data = [NSPropertyListSerialization dataFromPropertyList:sysDescDict
76 format:NSPropertyListXMLFormat_v1_0
77 errorDescription:&error];
78 } else {
79 data = [sysDescDict oldSchoolPListFormatWithErrorDescription:&error];
80 }
81
82 if (data == nil) {
83 OOLog(@"sysdesc.compile.failed.XML", @"Could not convert translated sysdesc.plist to property list: %@.", error);
84 return;
85 }
86
87 if ([ResourceManager writeDiagnosticData:data toFileNamed:@"sysdesc-compiled.plist"]) {
88 OOLog(@"sysdesc.compile.success", @"%@", @"Wrote translated sysdesc.plist to sysdesc-compiled.plist.");
89 } else {
90 OOLog(@"sysdesc.compile.failed.writeFailure", @"%@", @"Could not write translated sysdesc.plist to sysdesc-compiled.plist.");
91 }
92}
93
95{
96 NSArray* sysDescArray = nil;
97 NSDictionary* sysDescDict = nil;
98 NSDictionary* keyMap = nil;
99 NSData* data = nil;
100 NSString* error = nil;
101
102 sysDescArray = [[UNIVERSE descriptions] oo_arrayForKey:@"system_description"];
103
104 keyMap = [ResourceManager dictionaryFromFilesNamed:@"sysdesc_key_table.plist"
105 inFolder:@"Config"
106 andMerge:NO];
107 // keyMap is optional, so no nil check
108
109 sysDescDict = OOConvertSystemDescriptionsToDictionaryFormat(sysDescArray, keyMap);
110 if (sysDescArray == nil) {
111 OOLog(@"sysdesc.export.failed.conversion", @"%@", @"Could not convert system_description do sysdesc.plist format for some reason.");
112 return;
113 }
114
115 if (asXML) {
116 data = [NSPropertyListSerialization dataFromPropertyList:sysDescDict
117 format:NSPropertyListXMLFormat_v1_0
118 errorDescription:&error];
119 } else {
120 data = [sysDescDict oldSchoolPListFormatWithErrorDescription:&error];
121 }
122
123 if (data == nil) {
124 OOLog(@"sysdesc.export.failed.XML", @"Could not convert translated system_description to XML property list: %@.", error);
125 return;
126 }
127
128 if ([ResourceManager writeDiagnosticData:data toFileNamed:@"sysdesc.plist"]) {
129 OOLog(@"sysdesc.export.success", @"%@", @"Wrote translated system_description to sysdesc.plist.");
130 } else {
131 OOLog(@"sysdesc.export.failed.writeFailure", @"%@", @"Could not write translated system_description to sysdesc.plist.");
132 }
133}
134
135NSArray* OOConvertSystemDescriptionsToArrayFormat(NSDictionary* descriptionsInDictionaryFormat, NSDictionary* indicesToKeys)
136{
137 NSMutableDictionary* result = nil;
138 NSAutoreleasePool* pool = nil;
139 NSString* key = nil;
140 NSArray* entry = nil;
141 NSMutableDictionary* keysToIndices = nil;
142 NSMutableSet* usedIndices = nil;
143 NSUInteger slotCache = 0;
144 NSNumber* index = nil;
145 NSUInteger i, count;
146 NSMutableArray* realResult = nil;
147
148 pool = [[NSAutoreleasePool alloc] init];
149
150 // Use a dictionary as a sparse array.
151 result = [NSMutableDictionary dictionaryWithCapacity:[descriptionsInDictionaryFormat count]];
152
153 keysToIndices = InitKeyToIndexDict(indicesToKeys, &usedIndices);
154
155 foreachkey(key, descriptionsInDictionaryFormat)
156 {
157 entry = ConvertKeysToIndices([descriptionsInDictionaryFormat objectForKey:key], keysToIndices, usedIndices, &slotCache);
158 index = KeyToIndex(key, keysToIndices, usedIndices, &slotCache);
159
160 [result setObject:entry forKey:index];
161 }
162
163 count = HighestIndex(result);
164 realResult = [NSMutableArray arrayWithCapacity:count];
165 for (i = 0; i < count; i++) {
166 entry = [result objectForKey:[NSNumber numberWithUnsignedInteger:i]];
167 if (entry == nil)
168 entry = [NSArray array];
169 [realResult addObject:entry];
170 }
171
172 [realResult retain];
173 [pool release];
174 return [realResult autorelease];
175}
176
177NSDictionary* OOConvertSystemDescriptionsToDictionaryFormat(NSArray* descriptionsInArrayFormat, NSDictionary* indicesToKeys)
178{
179 NSMutableDictionary* result = nil;
180 NSAutoreleasePool* pool = nil;
181 NSArray* entry = nil;
182 NSString* key = nil;
183 NSUInteger i = 0;
184
185 result = [NSMutableDictionary dictionaryWithCapacity:[descriptionsInArrayFormat count]];
186 pool = [[NSAutoreleasePool alloc] init];
187
188 foreach (entry, descriptionsInArrayFormat) {
189 entry = ConvertIndicesToKeys(entry, indicesToKeys);
190 key = IndexToKey(i, indicesToKeys, YES);
191 ++i;
192
193 [result setObject:entry forKey:key];
194 }
195
196 [pool release];
197 return result;
198}
199
200NSString* OOStringifySystemDescriptionLine(NSString* line, NSDictionary* indicesToKeys, BOOL useFallback)
201{
202 NSUInteger p1, p2;
203 NSRange searchRange;
204 NSString *before = nil, *after = nil, *middle = nil;
205 NSString* key = nil;
206
207 searchRange.location = 0;
208 searchRange.length = [line length];
209
210 while ([line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location != NSNotFound) {
211 p1 = [line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location;
212 p2 = [line rangeOfString:@"]" options:NSLiteralSearch range:searchRange].location + 1;
213
214 before = [line substringWithRange:NSMakeRange(0, p1)];
215 after = [line substringWithRange:NSMakeRange(p2, [line length] - p2)];
216 middle = [line substringWithRange:NSMakeRange(p1 + 1, p2 - p1 - 2)];
217
218 if ([[middle stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] isEqual:@""] && ![middle isEqual:@""]) {
219 // Found [] around integers only
220 key = IndexToKey([middle intValue], indicesToKeys, useFallback);
221 if (key != nil) {
222 line = [NSString stringWithFormat:@"%@[#%@]%@", before, key, after];
223 }
224 }
225
226 searchRange.length -= p2 - searchRange.location;
227 searchRange.location = [line length] - searchRange.length;
228 }
229 return line;
230}
231
232static NSMutableDictionary* InitKeyToIndexDict(NSDictionary* dict, NSMutableSet** outUsedIndices)
233{
234 NSString* key = nil;
235 NSNumber* number = nil;
236 NSMutableDictionary* result = nil;
237 NSMutableSet* used = nil;
238
239 assert(outUsedIndices != NULL);
240
241 result = [NSMutableDictionary dictionaryWithCapacity:[dict count]];
242 used = [NSMutableSet setWithCapacity:[dict count]];
243
244 foreachkey(key, dict)
245 {
246 // Convert keys of dict to array indices
247 number = [NSNumber numberWithInt:[key intValue]];
248 [result setObject:number forKey:[dict objectForKey:key]];
249 [used addObject:number];
250 }
251
252 *outUsedIndices = used;
253 return result;
254}
255
256static NSString* IndexToKey(NSUInteger index, NSDictionary* indicesToKeys, BOOL useFallback)
257{
258 NSString* result = [indicesToKeys objectForKey:[NSString stringWithFormat:@"%zu", index]];
259 if (result == nil && useFallback)
260 result = [NSString stringWithFormat:@"block_%zu", index];
261
262 return result;
263}
264
265static NSArray* ConvertIndicesToKeys(NSArray* entry, NSDictionary* indicesToKeys)
266{
267 NSString* line = nil;
268 NSMutableArray* result = nil;
269
270 result = [NSMutableArray arrayWithCapacity:[entry count]];
271
272 foreach (line, entry) {
273 [result addObject:OOStringifySystemDescriptionLine(line, indicesToKeys, YES)];
274 }
275
276 return result;
277}
278
279static NSNumber* KeyToIndex(NSString* key, NSMutableDictionary* ioKeysToIndices, NSMutableSet* ioUsedIndicies, NSUInteger* ioSlotCache)
280{
281 NSNumber* result = nil;
282
283 assert(ioSlotCache != NULL);
284
285 result = [ioKeysToIndices objectForKey:key];
286 if (result == nil) {
287 // Search for free index
288 do {
289 result = [NSNumber numberWithUnsignedInteger:(*ioSlotCache)++];
290 } while ([ioUsedIndicies containsObject:result]);
291
292 [ioKeysToIndices setObject:result forKey:key];
293 [ioUsedIndicies addObject:result];
294 OOLog(@"sysdesc.compile.unknownKey", @"Assigning key \"%@\" to index %@.", key, result);
295 }
296
297 return result;
298}
299
300static NSArray* ConvertKeysToIndices(NSArray* entry, NSMutableDictionary* ioKeysToIndices, NSMutableSet* ioUsedIndicies, NSUInteger* ioSlotCache)
301{
302 NSString* line = nil;
303 NSUInteger p1, p2;
304 NSRange searchRange;
305 NSMutableArray* result = nil;
306 NSString *before = nil, *after = nil, *middle = nil;
307
308 result = [NSMutableArray arrayWithCapacity:[entry count]];
309
310 foreach (line, entry) {
311 searchRange.location = 0;
312 searchRange.length = [line length];
313
314 while ([line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location != NSNotFound) {
315 p1 = [line rangeOfString:@"[" options:NSLiteralSearch range:searchRange].location;
316 p2 = [line rangeOfString:@"]" options:NSLiteralSearch range:searchRange].location + 1;
317
318 before = [line substringWithRange:NSMakeRange(0, p1)];
319 after = [line substringWithRange:NSMakeRange(p2, [line length] - p2)];
320 middle = [line substringWithRange:NSMakeRange(p1 + 1, p2 - p1 - 2)];
321
322 if ([middle length] > 1 && [middle hasPrefix:@"#"]) {
323 // Found [] around key
324 line = [NSString stringWithFormat:@"%@[%@]%@", before, KeyToIndex([middle substringFromIndex:1], ioKeysToIndices, ioUsedIndicies, ioSlotCache), after];
325 }
326
327 searchRange.length -= p2 - searchRange.location;
328 searchRange.location = [line length] - searchRange.length;
329 }
330
331 [result addObject:line];
332 }
333
334 return result;
335}
336
337static NSUInteger HighestIndex(NSMutableDictionary* sparseArray)
338{
339 NSNumber* key = nil;
340 NSUInteger curr, highest = 0;
341
342 foreachkey(key, sparseArray)
343 {
344 curr = [key intValue];
345 if (highest < curr)
346 highest = curr;
347 }
348
349 return highest;
350}
351
352#endif
static NSMutableDictionary * InitKeyToIndexDict(NSDictionary *dict, NSMutableSet **outUsedIndices)
static NSArray * ConvertKeysToIndices(NSArray *entry, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
static NSUInteger HighestIndex(NSMutableDictionary *sparseArray)
void CompileSystemDescriptions(BOOL asXML)
void ExportSystemDescriptions(BOOL asXML)
static NSNumber * KeyToIndex(NSString *key, NSMutableDictionary *ioKeysToIndices, NSMutableSet *ioUsedIndicies, NSUInteger *ioSlotCache)
static NSArray * ConvertIndicesToKeys(NSArray *entry, NSDictionary *indicesToKeys)
NSDictionary * OOConvertSystemDescriptionsToDictionaryFormat(NSArray *descriptionsInArrayFormat, NSDictionary *indicesToKeys)
NSString * OOStringifySystemDescriptionLine(NSString *line, NSDictionary *indicesToKeys, BOOL useFallback)
NSArray * OOConvertSystemDescriptionsToArrayFormat(NSDictionary *descriptionsInDictionaryFormat, NSDictionary *indicesToKeys)
static NSString * IndexToKey(NSUInteger index, NSDictionary *indicesToKeys, BOOL useFallback)
#define foreachkey(VAR, DICT)
#define OOLog(class, format,...)
Definition OOLogging.h:84
unsigned count
return nil
NSDictionary * dictionaryFromFilesNamed:inFolder:andMerge:(NSString *fileName,[inFolder] NSString *folderName,[andMerge] BOOL mergeFiles)