Oolite
Loading...
Searching...
No Matches
OOSystemDescriptionManager.m
Go to the documentation of this file.
1/*
2
3OOPlanetDescriptionManager.h
4
5Singleton class responsible for planet description data.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
29#import "OOStringParsing.h"
30#import "OOTypes.h"
31#import "PlayerEntity.h"
32#import "ResourceManager.h"
33#import "Universe.h"
34
35// character sequence which can't be in property name, system key or layer
36// and is highly unlikely to be in a manifest identifier
37static NSString* const kOOScriptedChangeJoiner = @"~|~";
38
39// likely maximum number of planetinfo properties to be applied to a system
40// just for efficiency - no harm in exceeding it
41#define OO_LIKELY_PROPERTIES_PER_SYSTEM 50
42
43@interface OOSystemDescriptionManager (OOPrivate)
44- (void)setProperties:(NSDictionary*)properties inDescription:(OOSystemDescriptionEntry*)desc;
45- (NSDictionary*)calculatePropertiesForSystemKey:(NSString*)key;
46- (void)updateCacheEntry:(NSUInteger)i;
47- (void)updateCacheEntry:(NSUInteger)i forProperty:(NSString*)property;
48- (id)getProperty:(NSString*)property forSystemKey:(NSString*)key withUniversal:(BOOL)universal;
49/* some planetinfo properties have two ways to specify
50 * need to get the one with higher layer (if they're both at the same layer,
51 * go with property1) */
52- (id)getProperty:(NSString*)property1 orProperty:(NSString*)property2 forSystemKey:(NSString*)key withUniversal:(BOOL)universal;
53
54- (void)saveScriptedChangeToProperty:(NSString*)property forSystemKey:(NSString*)key andLayer:(OOSystemLayer)layer toValue:(id)value fromManifest:(NSString*)manifest;
55
56@end
57
58static NSString* kOOSystemLayerProperty = @"layer";
59
60@implementation OOSystemDescriptionManager
61
62- (id)init
63{
64 self = [super init];
65 if (self != nil) {
66 universalProperties = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
68 // assume specific interstellar settings are rare
69 systemDescriptions = [[NSMutableDictionary alloc] initWithCapacity:OO_SYSTEM_CACHE_LENGTH + 20];
70 for (NSUInteger i = 0; i < OO_SYSTEM_CACHE_LENGTH; i++) {
71 propertyCache[i] = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
72 // hub count of 24 is considerably higher than occurs in
73 // standard planetinfo
74 neighbourCache[i] = [[NSMutableArray alloc] initWithCapacity:24];
75 }
76 propertiesInUse = [[NSMutableSet alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
77 scriptedChanges = [[NSMutableDictionary alloc] initWithCapacity:64];
78 }
79 return self;
80}
81
82- (void)dealloc
83{
87 for (NSUInteger i = 0; i < OO_SYSTEM_CACHE_LENGTH; i++) {
90 }
93 [super dealloc];
94}
95
97{
98 NSUInteger i, j, k, jIndex, kIndex;
99 // firstly, cache all coordinates
100 for (i = 0; i < OO_SYSTEM_CACHE_LENGTH; i++) {
101 coordinatesCache[i] = PointFromString([propertyCache[i] oo_stringForKey:@"coordinates"]);
102 }
103 // now for each system find its neighbours
104 for (i = 0; i < OO_GALAXIES_AVAILABLE; i++) {
105 // one galaxy at a time
106 for (j = 0; j < OO_SYSTEMS_PER_GALAXY; j++) {
107 jIndex = j + (i * OO_SYSTEMS_PER_GALAXY);
108 for (k = j + 1; k < OO_SYSTEMS_PER_GALAXY; k++) {
109 kIndex = k + (i * OO_SYSTEMS_PER_GALAXY);
111 // arrays are of system number only
112 [neighbourCache[jIndex] addObject:[NSNumber numberWithUnsignedInteger:k]];
113 [neighbourCache[kIndex] addObject:[NSNumber numberWithUnsignedInteger:j]];
114 }
115 }
116 }
117 }
118}
119
120- (void)setUniversalProperties:(NSDictionary*)properties
121{
122 [universalProperties addEntriesFromDictionary:properties];
123 [propertiesInUse addObjectsFromArray:[properties allKeys]];
124 for (NSUInteger i = 0; i < OO_SYSTEM_CACHE_LENGTH; i++) {
125 [self updateCacheEntry:i];
126 }
127}
128
129- (void)setInterstellarProperties:(NSDictionary*)properties
130{
131 [self setProperties:properties inDescription:interstellarSpace];
132}
133
134- (void)setProperties:(NSDictionary*)properties forSystemKey:(NSString*)key
135{
136 OOSystemDescriptionEntry* desc = [systemDescriptions objectForKey:key];
137 if (desc == nil) {
138 // create it
139 desc = [[[OOSystemDescriptionEntry alloc] init] autorelease];
140 [systemDescriptions setObject:desc forKey:key];
141 }
142 [self setProperties:properties inDescription:desc];
143 [propertiesInUse addObjectsFromArray:[properties allKeys]];
144
145 NSArray* tokens = ScanTokensFromString(key);
146 if ([tokens count] == 2 && [tokens oo_unsignedIntegerAtIndex:0] < OO_GALAXIES_AVAILABLE && [tokens oo_unsignedIntegerAtIndex:1] < OO_SYSTEMS_PER_GALAXY) {
147 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
148 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
149 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
150 if (index >= OO_SYSTEM_CACHE_LENGTH) {
151 OOLog(@"system.description.error", @"'%@' is an invalid system key. This is an internal error. Please report it.", key);
152 } else {
153 [self updateCacheEntry:index];
154 }
155 }
156}
157
158- (void)setProperty:(NSString*)property forSystemKey:(NSString*)key andLayer:(OOSystemLayer)layer toValue:(id)value fromManifest:(NSString*)manifest
159{
160 OOSystemDescriptionEntry* desc = [systemDescriptions objectForKey:key];
161 if (desc == nil) {
162 // create it
163 desc = [[[OOSystemDescriptionEntry alloc] init] autorelease];
164 [systemDescriptions setObject:desc forKey:key];
165 }
166 [desc setProperty:property forLayer:layer toValue:value];
167 [propertiesInUse addObject:property];
168
169 NSArray* tokens = ScanTokensFromString(key);
170 if ([tokens count] == 2 && [tokens oo_unsignedIntegerAtIndex:0] < OO_GALAXIES_AVAILABLE && [tokens oo_unsignedIntegerAtIndex:1] < OO_SYSTEMS_PER_GALAXY) {
171 [self saveScriptedChangeToProperty:property forSystemKey:key andLayer:layer toValue:value fromManifest:manifest];
172
173 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
174 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
175 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
176 if (index >= OO_SYSTEM_CACHE_LENGTH) {
177 OOLog(@"system.description.error", @"'%@' is an invalid system key. This is an internal error. Please report it.", key);
178 } else {
179 [self updateCacheEntry:index forProperty:property];
180 }
181 }
182 // for interstellar updates, save but don't update cache
183 else if ([tokens count] == 4 && [tokens oo_unsignedIntegerAtIndex:1] < OO_GALAXIES_AVAILABLE && [tokens oo_unsignedIntegerAtIndex:2] < OO_SYSTEMS_PER_GALAXY && [tokens oo_unsignedIntegerAtIndex:3] < OO_SYSTEMS_PER_GALAXY) {
184 [self saveScriptedChangeToProperty:property forSystemKey:key andLayer:layer toValue:value fromManifest:manifest];
185 }
186}
187
188- (void)saveScriptedChangeToProperty:(NSString*)property forSystemKey:(NSString*)key andLayer:(OOSystemLayer)layer toValue:(id)value fromManifest:(NSString*)manifest
189{
190 // if OXP doesn't have a manifest, cancel saving the change
191 if (manifest == nil) {
192 return;
193 }
194 // OOLog(@"saving change",@"%@ %@ %@ %d",manifest,key,property,layer);
195 NSArray* overrideKey = [NSArray arrayWithObjects:manifest, key, property, [[NSNumber numberWithInt:layer] stringValue], nil];
196 // Obj-C copes with NSArray keys to dictionaries fine, but the
197 // plist format doesn't, so they can't be saved.
198 NSString* overrideKeyStr = [overrideKey componentsJoinedByString:kOOScriptedChangeJoiner];
199 if (value != nil) {
200 [scriptedChanges setObject:value forKey:overrideKeyStr];
201 } else {
202 [scriptedChanges removeObjectForKey:overrideKeyStr];
203 }
204}
205
206- (void)importScriptedChanges:(NSDictionary*)scripted
207{
208 NSArray* key = nil;
209 NSString* keyStr = nil;
210 NSString* manifest = nil;
211 foreachkey(keyStr, scripted)
212 {
213 key = [keyStr componentsSeparatedByString:kOOScriptedChangeJoiner];
214 if ([key count] == 4) {
215 manifest = [key oo_stringAtIndex:0];
216 if ([ResourceManager manifestForIdentifier:manifest] != nil) {
217 // OOLog(@"importing",@"%@ -> %@",keyStr,[scripted objectForKey:keyStr]);
218 [self setProperty:[key oo_stringAtIndex:2]
219 forSystemKey:[key oo_stringAtIndex:1]
220 andLayer:[key oo_intAtIndex:3]
221 toValue:[scripted objectForKey:keyStr]
222 fromManifest:manifest];
223 // and doing this set stores it into the manager's copy
224 // of scripted changes
225 // this means in theory we could import more than one
226 }
227 // else OXP not installed, do not load
228 } else {
229 OOLog(@"systemManager.import", @"Key '%@' has unexpected format - skipping", keyStr);
230 }
231 }
232}
233
234// import of the old local_planetinfo_overrides dictionary
235- (void)importLegacyScriptedChanges:(NSDictionary*)scripted
236{
237 NSString* systemKey = nil;
238 NSString* propertyKey = nil;
239 NSString* defaultManifest = @"org.oolite.oolite";
240
241 foreachkey(systemKey, scripted)
242 {
243 NSDictionary* legacyChanges = [scripted oo_dictionaryForKey:systemKey];
244 if ([legacyChanges objectForKey:@"sun_gone_nova"] != nil) {
245 // then this is a change to import even if we don't know
246 // if the OXP is still installed
247 foreachkey(propertyKey, legacyChanges)
248 {
249 [self setProperty:propertyKey
250 forSystemKey:systemKey
251 andLayer:OO_LAYER_OXP_DYNAMIC
252 toValue:[legacyChanges objectForKey:propertyKey]
253 fromManifest:defaultManifest];
254 }
255 /* Fix for older savegames not having a larger sun radius
256 * property set from the Nova mission. */
257 id sr = [self getProperty:@"sun_radius" forSystemKey:systemKey];
258 float sr_num = [sr floatValue];
259 if (sr_num < 600000) {
260 // fix sun radius values
261 [self setProperty:@"sun_radius"
262 forSystemKey:systemKey
263 andLayer:OO_LAYER_OXP_DYNAMIC
264 toValue:[NSNumber numberWithFloat:sr_num + 600000.0f]
265 fromManifest:defaultManifest];
266 }
267 }
268 }
269}
270
271- (NSDictionary*)exportScriptedChanges
272{
273 return [[scriptedChanges copy] autorelease];
274}
275
277{
278 OOSystemID s = [UNIVERSE currentSystemID];
279 if (s >= 0) {
280 NSUInteger index = ([PLAYER galaxyNumber] * OO_SYSTEMS_PER_GALAXY) + s;
281 if (index >= OO_SYSTEM_CACHE_LENGTH) {
282 OOLog(@"system.description.error", @"'%zu' is an invalid system index for the current system. This is an internal error. Please report it.", index);
283 return [NSDictionary dictionary];
284 }
285 return propertyCache[index];
286 } else {
287 OOLog(@"system.description.error", @"%@", @"getPropertiesForCurrentSystem called while player in interstellar space. This is an internal error. Please report it.");
288 // this shouldn't be called for interstellar space
289 return [NSDictionary dictionary];
290 }
291}
292
293- (NSDictionary*)getPropertiesForSystemKey:(NSString*)key
294{
295 NSArray* tokens = ScanTokensFromString(key);
296 if ([tokens count] == 2 && [tokens oo_unsignedIntegerAtIndex:0] < OO_GALAXIES_AVAILABLE && [tokens oo_unsignedIntegerAtIndex:1] < OO_SYSTEMS_PER_GALAXY) {
297 OOGalaxyID g = [tokens oo_unsignedIntegerAtIndex:0];
298 OOSystemID s = [tokens oo_unsignedIntegerAtIndex:1];
299 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
300 if (index >= OO_SYSTEM_CACHE_LENGTH) {
301 OOLog(@"system.description.error", @"'%@' is an invalid system key. This is an internal error. Please report it.", key);
302 return [NSDictionary dictionary];
303 }
304 return propertyCache[index];
305 }
306 // interstellar spaces aren't cached
307 return [self calculatePropertiesForSystemKey:key];
308}
309
310- (NSDictionary*)getPropertiesForSystem:(OOSystemID)s inGalaxy:(OOGalaxyID)g
311{
312 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
313 if (index >= OO_SYSTEM_CACHE_LENGTH) {
314 OOLog(@"system.description.error", @"'%u, %u' is an invalid system. This is an internal error. Please report it.", g, s);
315 return [NSDictionary dictionary];
316 }
317 return propertyCache[index];
318}
319
320- (id)getProperty:(NSString*)property forSystemKey:(NSString*)key
321{
322 return [self getProperty:property forSystemKey:key withUniversal:YES];
323}
324
325- (id)getProperty:(NSString*)property forSystem:(OOSystemID)s inGalaxy:(OOGalaxyID)g
326{
327 if (s < 0) {
328 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
329 return nil;
330 }
331 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
332 if (index >= OO_SYSTEM_CACHE_LENGTH) {
333 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
334 return nil;
335 }
336 return [propertyCache[index] objectForKey:property];
337}
338
339- (id)getProperty:(NSString*)property forSystemKey:(NSString*)key withUniversal:(BOOL)universal
340{
342 if (EXPECT_NOT([key isEqualToString:@"interstellar"])) {
343 desc = interstellarSpace;
344 } else {
345 desc = [systemDescriptions objectForKey:key];
346 }
347 if (desc == nil) {
348 return nil;
349 }
350 id result = nil;
351 result = [desc getProperty:property forLayer:OO_LAYER_OXP_PRIORITY];
352 if (result == nil) {
353 result = [desc getProperty:property forLayer:OO_LAYER_OXP_DYNAMIC];
354 }
355 if (result == nil) {
356 result = [desc getProperty:property forLayer:OO_LAYER_OXP_STATIC];
357 }
358 if (result == nil && universal) {
359 result = [universalProperties objectForKey:property];
360 }
361 if (result == nil) {
362 result = [desc getProperty:property forLayer:OO_LAYER_CORE];
363 }
364 return result;
365}
366
367- (id)getProperty:(NSString*)property1 orProperty:(NSString*)property2 forSystemKey:(NSString*)key withUniversal:(BOOL)universal
368{
369 OOSystemDescriptionEntry* desc = [systemDescriptions objectForKey:key];
370 if (desc == nil) {
371 return nil;
372 }
373 id result = nil;
374 result = [desc getProperty:property1 forLayer:OO_LAYER_OXP_PRIORITY];
375 if (result == nil) {
376 result = [desc getProperty:property2 forLayer:OO_LAYER_OXP_PRIORITY];
377 }
378 if (result == nil) {
379 result = [desc getProperty:property1 forLayer:OO_LAYER_OXP_DYNAMIC];
380 }
381 if (result == nil) {
382 result = [desc getProperty:property2 forLayer:OO_LAYER_OXP_DYNAMIC];
383 }
384 if (result == nil) {
385 result = [desc getProperty:property1 forLayer:OO_LAYER_OXP_STATIC];
386 }
387 if (result == nil) {
388 result = [desc getProperty:property2 forLayer:OO_LAYER_OXP_STATIC];
389 }
390 if (universal) {
391 if (result == nil) {
392 result = [universalProperties objectForKey:property1];
393 }
394 if (result == nil) {
395 result = [universalProperties objectForKey:property2];
396 }
397 }
398 if (result == nil) {
399 result = [desc getProperty:property1 forLayer:OO_LAYER_CORE];
400 }
401 if (result == nil) {
402 result = [desc getProperty:property2 forLayer:OO_LAYER_CORE];
403 }
404 return result;
405}
406
407- (void)setProperties:(NSDictionary*)properties inDescription:(OOSystemDescriptionEntry*)desc
408{
409 OOSystemLayer layer = [properties oo_unsignedIntForKey:kOOSystemLayerProperty defaultValue:OO_LAYER_OXP_STATIC];
410 if (layer > OO_LAYER_OXP_PRIORITY) {
411 OOLog(@"system.description.error", @"Layer %u is not a valid layer number in system information.", layer);
412 layer = OO_LAYER_OXP_PRIORITY;
413 }
414 NSString* key = nil;
415 foreachkey(key, properties)
416 {
417 if (![key isEqualToString:kOOSystemLayerProperty]) {
418 [propertiesInUse addObject:key];
419 [desc setProperty:key forLayer:layer toValue:[properties objectForKey:key]];
420 }
421 }
422}
423
424- (NSDictionary*)calculatePropertiesForSystemKey:(NSString*)key
425{
426 NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
427 NSString* property = nil;
428 id val = nil;
429 BOOL interstellar = [key hasPrefix:@"interstellar:"];
430 foreach (property, propertiesInUse) {
431 // don't use universal properties on interstellar specific regions
432 val = [self getProperty:property forSystemKey:key withUniversal:!interstellar];
433
434 if (val != nil) {
435 [dict setObject:val forKey:property];
436 } else if (interstellar) {
437 // interstellar is always overridden by specific regions
438 // universal properties for interstellar get picked up here
439 val = [self getProperty:property forSystemKey:@"interstellar"];
440 if (val != nil) {
441 [dict setObject:val forKey:property];
442 }
443 }
444 }
445 return dict;
446}
447
448- (void)updateCacheEntry:(NSUInteger)i
449{
450 NSAssert(i < OO_SYSTEM_CACHE_LENGTH, @"Invalid cache entry number");
451 NSString* key = [NSString stringWithFormat:@"%zu %zu", i / OO_SYSTEMS_PER_GALAXY, i % OO_SYSTEMS_PER_GALAXY];
452 NSDictionary* current = [self calculatePropertiesForSystemKey:key];
453
454 [propertyCache[i] removeAllObjects];
455 [propertyCache[i] addEntriesFromDictionary:current];
456}
457
458- (void)updateCacheEntry:(NSUInteger)i forProperty:(NSString*)property
459{
460 NSAssert(i < OO_SYSTEM_CACHE_LENGTH, @"Invalid cache entry number");
461 NSString* key = [NSString stringWithFormat:@"%zu %zu", i / OO_SYSTEMS_PER_GALAXY, i % OO_SYSTEMS_PER_GALAXY];
462 id current = [self getProperty:property forSystemKey:key];
463 if (current == nil) {
464 [propertyCache[i] removeObjectForKey:property];
465 } else {
466 [propertyCache[i] setObject:current forKey:property];
467 }
468}
469
470- (NSPoint)getCoordinatesForSystem:(OOSystemID)s inGalaxy:(OOGalaxyID)g
471{
472 if (s < 0) {
473 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
474 return (NSPoint) { 0, 0 };
475 }
476 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
477 if (index >= OO_SYSTEM_CACHE_LENGTH) {
478 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
479 return (NSPoint) { 0, 0 };
480 }
481 return coordinatesCache[index];
482}
483
484- (NSArray*)getNeighbourIDsForSystem:(OOSystemID)s inGalaxy:(OOGalaxyID)g
485{
486 if (s < 0) {
487 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
488 return nil;
489 }
490 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
491 if (index >= OO_SYSTEM_CACHE_LENGTH) {
492 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
493 return nil;
494 }
495
496 return neighbourCache[index];
497}
498
500{
501 if ([UNIVERSE currentSystemID] < 0) {
502 return kNilRandomSeed;
503 } else {
504 OOSystemID s = [UNIVERSE currentSystemID];
505 NSUInteger index = ([PLAYER galaxyNumber] * OO_SYSTEMS_PER_GALAXY) + s;
506 if (index >= OO_SYSTEM_CACHE_LENGTH) {
507 OOLog(@"system.description.error", @"'%zu' is an invalid system index for the current system. This is an internal error. Please report it.", index);
508 return kNilRandomSeed;
509 }
510 return RandomSeedFromString([propertyCache[index] oo_stringForKey:@"random_seed"]);
511 }
512}
513
514- (Random_Seed)getRandomSeedForSystem:(OOSystemID)s inGalaxy:(OOGalaxyID)g
515{
516 if (s < 0) {
517 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
518 return kNilRandomSeed;
519 }
520 NSUInteger index = (g * OO_SYSTEMS_PER_GALAXY) + s;
521 if (index >= OO_SYSTEM_CACHE_LENGTH) {
522 OOLog(@"system.description.error", @"'%d %d' is an invalid system key. This is an internal error. Please report it.", g, s);
523 return kNilRandomSeed;
524 }
525 return RandomSeedFromString([propertyCache[index] oo_stringForKey:@"random_seed"]);
526}
527
528@end
529
530@interface OOSystemDescriptionEntry (OOPrivate)
531- (id)validateProperty:(NSString*)property withValue:(id)value;
532@end
533
534@implementation OOSystemDescriptionEntry
535
536- (id)init
537{
538 self = [super init];
539 if (self != nil) {
540 for (NSUInteger i = 0; i < OO_SYSTEM_LAYERS; i++) {
541 layers[i] = [[NSMutableDictionary alloc] initWithCapacity:OO_LIKELY_PROPERTIES_PER_SYSTEM];
542 }
543 }
544 return self;
545}
546
547- (void)dealloc
548{
549 for (NSUInteger i = 0; i < OO_SYSTEM_LAYERS; i++) {
550 DESTROY(layers[i]);
551 }
552 [super dealloc];
553}
554
555- (void)setProperty:(NSString*)property forLayer:(OOSystemLayer)layer toValue:(id)value
556{
557 if (value == nil) {
558 [layers[layer] removeObjectForKey:property];
559 } else {
560 // validate type of object for certain properties
561 value = [self validateProperty:property withValue:value];
562 // if it's nil now, validation failed and could not be recovered
563 // so don't actually set anything
564 if (value != nil) {
565 [layers[layer] setObject:value forKey:property];
566 }
567 }
568}
569
570- (id)getProperty:(NSString*)property forLayer:(OOSystemLayer)layer
571{
572 return [layers[layer] objectForKey:property];
573}
574
575/* Mostly the rest of the game gets a system dictionary from
576 * [UNIVERSE currentSystemData] or similar, which means that it uses
577 * safe methods like oo_stringForKey: - a few things use a direct call
578 * to getProperty for various reasons, so need some type validation
579 * here instead. */
580- (id)validateProperty:(NSString*)property withValue:(id)value
581{
582 if ([property isEqualToString:@"coordinates"]) {
583 // must be a string with two numbers in it
584 // TODO: convert two element arrays
585 if (![value isKindOfClass:[NSString class]]) {
586 OOLog(@"system.description.error", @"'%@' is not a valid format for coordinates", value);
587 return nil;
588 }
589 NSArray* tokens = ScanTokensFromString((NSString*)value);
590 if ([tokens count] != 2) {
591 OOLog(@"system.description.error", @"'%@' is not a valid format for coordinates (must have exactly two numbers)", value);
592 return nil;
593 }
594 } else if ([property isEqualToString:@"radius"] || [property isEqualToString:@"government"]) {
595 // read in a context which expects a string, but it's a string representation of a number
596 if (![value isKindOfClass:[NSString class]]) {
597 if ([value isKindOfClass:[NSNumber class]]) {
598 return [value stringValue];
599 } else {
600 OOLog(@"system.description.error", @"'%@' is not a valid value for '%@' (string required)", value, property);
601 return nil;
602 }
603 }
604 } else if ([property isEqualToString:@"inhabitant"] || [property isEqualToString:@"inhabitants"] || [property isEqualToString:@"name"]) {
605 // read in a context which expects a string
606 if (![value isKindOfClass:[NSString class]]) {
607 OOLog(@"system.description.error", @"'%@' is not a valid value for '%@' (string required)", value, property);
608 return nil;
609 }
610 }
611
612 return value;
613}
614
615@end
#define DESTROY(x)
#define foreachkey(VAR, DICT)
#define EXPECT_NOT(x)
#define OOLog(class, format,...)
Definition OOLogging.h:84
unsigned count
return nil
float y
float x
NSMutableArray * ScanTokensFromString(NSString *values)
NSPoint PointFromString(NSString *xyString)
Random_Seed RandomSeedFromString(NSString *abcdefString)
#define OO_GALAXIES_AVAILABLE
#define OO_SYSTEM_CACHE_LENGTH
#define OO_SYSTEM_LAYERS
#define OO_SYSTEMS_PER_GALAXY
static NSString * kOOSystemLayerProperty
static NSString *const kOOScriptedChangeJoiner
int16_t OOSystemID
Definition OOTypes.h:183
uint8_t OOGalaxyID
Definition OOTypes.h:182
#define MAX_JUMP_RANGE
Definition ShipEntity.h:109
#define UNIVERSE
Definition Universe.h:827
id getProperty:forLayer:(NSString *property,[forLayer] OOSystemLayer layer)
id validateProperty:withValue:(NSString *property, [withValue] id value)
void setProperty:forLayer:toValue:(NSString *property,[forLayer] OOSystemLayer layer,[toValue] id value)
NSMutableDictionary * layers[OO_SYSTEM_LAYERS]
void updateCacheEntry:forProperty:(NSUInteger i, [forProperty] NSString *property)
NSMutableDictionary * propertyCache[OO_SYSTEM_CACHE_LENGTH]
NSMutableArray * neighbourCache[OO_SYSTEM_CACHE_LENGTH]
void setProperty:forSystemKey:andLayer:toValue:fromManifest:(NSString *property,[forSystemKey] NSString *key,[andLayer] OOSystemLayer layer,[toValue] id value,[fromManifest] NSString *manifest)
NSDictionary * calculatePropertiesForSystemKey:(NSString *key)
NSMutableDictionary * universalProperties
OOSystemDescriptionEntry * interstellarSpace
void saveScriptedChangeToProperty:forSystemKey:andLayer:toValue:fromManifest:(NSString *property, [forSystemKey] NSString *key, [andLayer] OOSystemLayer layer, [toValue] id value, [fromManifest] NSString *manifest)
NSPoint coordinatesCache[OO_SYSTEM_CACHE_LENGTH]
id getProperty:forSystemKey:withUniversal:(NSString *property, [forSystemKey] NSString *key, [withUniversal] BOOL universal)
id getProperty:forSystemKey:(NSString *property,[forSystemKey] NSString *key)
void setProperties:inDescription:(NSDictionary *properties, [inDescription] OOSystemDescriptionEntry *desc)
const Random_Seed kNilRandomSeed
OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC