Oolite
Loading...
Searching...
No Matches
OOStringExpander.m File Reference
import "OOStringExpander.h"
import "OOCollectionExtractors.h"
import "OOFoundation.h"
import "OOJavaScriptEngine.h"
import "OOStringParsing.h"
import "PlayerEntity.h"
import "PlayerEntityScriptMethods.h"
import "ResourceManager.h"
import "Universe.h"
Include dependency graph for OOStringExpander.m:

Go to the source code of this file.

Classes

struct  OOStringExpansionContext

Macros

#define WARNINGS   1
#define OO_EXPANDER_RANDOM   (context->useGoodRNG ? (Ranrot() & 0xFF) : gen_rnd_number())
#define SyntaxError(CONTEXT, CLASS, FORMAT, ...)
#define SyntaxWarning(CONTEXT, CLASS, FORMAT, ...)
#define OPERATOR(name)
#define kInvalidGEscapeMessage   @"String escape code %G must be followed by six integers."
#define kInvalidJEscapeMessage   @"String escape code %J must be followed by three integers."

Enumerations

enum  { kStackAllocationLimit = UINT16_MAX , kRecursionLimit = 100 }

Functions

static NSString * GetSystemName (OOStringExpansionContext *context)
static NSString * GetSystemNameIan (OOStringExpansionContext *context)
static NSString * GetRandomNameN (OOStringExpansionContext *context)
static NSString * GetRandomNameR (OOStringExpansionContext *context)
static NSArray * GetSystemDescriptions (OOStringExpansionContext *context)
static void AppendCharacters (NSMutableString **result, const unichar *characters, NSUInteger start, NSUInteger end)
static NSString * NewRandomDigrams (OOStringExpansionContext *context)
static NSString * OldRandomDigrams (void)
static NSString * Expand (OOStringExpansionContext *context, NSString *string, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandKey (OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandDigitKey (OOStringExpansionContext *context, const unichar *characters, NSUInteger keyStart, NSUInteger keyLength, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandStringKey (OOStringExpansionContext *context, NSString *key, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandStringKeyOverride (OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeySpecial (OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyKeyboardBinding (OOStringExpansionContext *context, NSString *key)
static NSMapTable * SpecialSubstitutionSelectors (void)
static NSString * ExpandStringKeyFromDescriptions (OOStringExpansionContext *context, NSString *key, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandStringKeyMissionVariable (OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyLegacyLocalVariable (OOStringExpansionContext *context, NSString *key)
static NSString * ExpandLegacyScriptSelectorKey (OOStringExpansionContext *context, NSString *key)
static SEL LookUpLegacySelector (NSString *key)
static NSString * ExpandPercentEscape (OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static NSString * ExpandSystemNameForGalaxyEscape (OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static NSString * ExpandSystemNameEscape (OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static NSString * ExpandPercentR (OOStringExpansionContext *context, NSString *input)
static void ReportWarningForUnknownKey (OOStringExpansionContext *context, NSString *key)
static NSString * ApplyOperators (NSString *string, NSString *operatorsString)
static NSString * ApplyOneOperator (NSString *string, NSString *op, NSString *param)
static void SyntaxIssue (OOStringExpansionContext *context, const char *function, const char *fileName, NSUInteger line, NSString *logMessageClass, NSString *prefix, NSString *format,...) OO_TAKES_FORMAT_STRING(7
NSString * OOExpandDescriptionString (Random_Seed seed, NSString *string, NSDictionary *overrides, NSDictionary *legacyLocals, NSString *systemName, OOExpandOptions options)
NSString * OOGenerateSystemDescription (Random_Seed seed, NSString *name)
Random_Seed OOStringExpanderDefaultRandomSeed (void)
static NSString * Operator_cr (NSString *string, NSString *param)
static NSString * Operator_dcr (NSString *string, NSString *param)
static NSString * Operator_icr (NSString *string, NSString *param)
static NSString * Operator_idcr (NSString *string, NSString *param)
static NSString * Operator_precision (NSString *string, NSString *param)
static NSString * Operator_multiply (NSString *string, NSString *param)
static NSString * Operator_add (NSString *string, NSString *param)

Macro Definition Documentation

◆ kInvalidGEscapeMessage

#define kInvalidGEscapeMessage   @"String escape code %G must be followed by six integers."

◆ kInvalidJEscapeMessage

#define kInvalidJEscapeMessage   @"String escape code %J must be followed by three integers."

Referenced by ExpandSystemNameEscape().

◆ OO_EXPANDER_RANDOM

#define OO_EXPANDER_RANDOM   (context->useGoodRNG ? (Ranrot() & 0xFF) : gen_rnd_number())

◆ OPERATOR

#define OPERATOR ( name)
Value:
[NSValue valueWithPointer:Operator_##name], @ #name
#define OPERATOR(name)

◆ SyntaxError

#define SyntaxError ( CONTEXT,
CLASS,
FORMAT,
... )
Value:
SyntaxIssue(CONTEXT, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, CLASS, OOLOG_WARNING_PREFIX, FORMAT, ##__VA_ARGS__)
#define OOLOG_WARNING_PREFIX
Definition OOLogging.h:123
static void SyntaxIssue(OOStringExpansionContext *context, const char *function, const char *fileName, NSUInteger line, NSString *logMessageClass, NSString *prefix, NSString *format,...) OO_TAKES_FORMAT_STRING(7

Definition at line 147 of file OOStringExpander.m.

Referenced by ExpandSystemNameEscape(), ExpandSystemNameForGalaxyEscape(), and ReportWarningForUnknownKey().

◆ SyntaxWarning

#define SyntaxWarning ( CONTEXT,
CLASS,
FORMAT,
... )
Value:
SyntaxIssue(CONTEXT, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, CLASS, OOLOG_WARNING_PREFIX, FORMAT, ##__VA_ARGS__)

Definition at line 150 of file OOStringExpander.m.

Referenced by Expand(), ExpandDigitKey(), ExpandKey(), ExpandPercentEscape(), ExpandStringKeyOverride(), and ReportWarningForUnknownKey().

◆ WARNINGS

#define WARNINGS   1

Definition at line 38 of file OOStringExpander.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kStackAllocationLimit 
kRecursionLimit 

Definition at line 45 of file OOStringExpander.m.

45 {
46 /*
47 Total stack limit for strings being parsed (in UTF-16 code elements,
48 i.e. units of 2 bytes), used recursively - for instance, if the root
49 string takes 10,000 characters, any string it recurses into gets
50 kStackAllocationLimit - 10,000. If the limit would be exceeded,
51 the unexpanded string is returned instead.
52
53 The limit is expected to be much higher than necessary for any practical
54 string, and exists only to catch pathological behaviour without crashing.
55 */
56 kStackAllocationLimit = UINT16_MAX,
57
58 /*
59 Recursion limit, for much the same purpose. Without it, we crash about
60 22,000 stack frames deep when trying to expand a = "[a]" on a Mac.
61 */
62 kRecursionLimit = 100
63};
@ kRecursionLimit
@ kStackAllocationLimit

Function Documentation

◆ AppendCharacters()

void AppendCharacters ( NSMutableString ** result,
const unichar * characters,
NSUInteger start,
NSUInteger end )
static

Definition at line 1047 of file OOStringExpander.m.

1048{
1049 NSCParameterAssert(result != NULL && characters != NULL && start <= end);
1050
1051 if (*result == nil) {
1052 // Ensure there is a string. We want this even if the range is empty.
1053 *result = [NSMutableString string];
1054 }
1055
1056 if (start == end)
1057 return;
1058
1059 /* What we want here is a method like -[NSMutableString
1060 appendCharacters:(unichar)characters length:(NSUInteger)length], which
1061 unfortunately doesn't exist. On Mac OS X, CoreFoundation provides an
1062 equivalent. For GNUstep, we have to use a temporary string.
1063
1064 TODO: build the output string in a fixed-size stack buffer instead.
1065 */
1066#if OOLITE_MAC_OS_X
1067 CFStringAppendCharacters((CFMutableStringRef)*result, characters + start, end - start);
1068#else
1069 NSString* temp = [[NSString alloc] initWithCharacters:characters + start length:end - start];
1070 [*result appendString:temp];
1071 [temp release];
1072#endif
1073}
return nil

References nil.

Referenced by Expand().

Here is the caller graph for this function:

◆ ApplyOneOperator()

NSString * ApplyOneOperator ( NSString * string,
NSString * op,
NSString * param )
static

Definition at line 472 of file OOStringExpander.m.

473{
474 static NSDictionary* operators = nil;
475
476 if (operators == nil) {
477#define OPERATOR(name) [NSValue valueWithPointer:Operator_##name], @ #name
478 operators = [[NSDictionary alloc] initWithObjectsAndKeys:
479 OPERATOR(dcr),
480 OPERATOR(cr),
481 OPERATOR(icr),
482 OPERATOR(idcr),
483 OPERATOR(precision),
484 OPERATOR(multiply),
485 OPERATOR(add),
486 nil];
487 }
488
489 NSString* (*operator)(NSString* string, NSString* param) = [[operators objectForKey:op] pointerValue];
490 if (operator!= NULL) {
491 return operator(string, param);
492 }
493
494 OOLogERR(@"strings.expand.invalidOperator", @"Unknown string expansion operator %@", op);
495 return string;
496}
#define OOLogERR(class, format,...)
Definition OOLogging.h:125

References nil, and OOLogERR.

Referenced by ApplyOperators().

Here is the caller graph for this function:

◆ ApplyOperators()

NSString * ApplyOperators ( NSString * string,
NSString * operatorsString )
static

Definition at line 410 of file OOStringExpander.m.

411{
412 NSArray* operators = [operatorsString componentsSeparatedByString:@"|"];
413 NSString* op = nil;
414
415 foreach (op, operators) {
416 NSString* param = nil;
417 NSRange colon = [op rangeOfString:@":"];
418 if (colon.location != NSNotFound) {
419 param = [op substringFromIndex:colon.location + colon.length];
420 op = [op substringToIndex:colon.location];
421 }
422 string = ApplyOneOperator(string, op, param);
423 }
424
425 return string;
426}
static NSString * ApplyOneOperator(NSString *string, NSString *op, NSString *param)

References ApplyOneOperator(), and nil.

Referenced by ExpandKey().

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

◆ Expand()

NSString * Expand ( OOStringExpansionContext * context,
NSString * string,
NSUInteger sizeLimit,
NSUInteger recursionLimit )
static

Definition at line 243 of file OOStringExpander.m.

244{
245 NSCParameterAssert(string != nil && context != NULL && sizeLimit <= kStackAllocationLimit);
246
247 const NSUInteger size = [string length];
248
249 // Avoid stack overflow.
250 if (EXPECT_NOT(size > sizeLimit || recursionLimit == 0))
251 return string;
252 sizeLimit -= size;
253 recursionLimit--;
254
255 // Nothing to expand in an empty string, and the size-1 thing below would be trouble.
256 if (size == 0)
257 return string;
258
259 unichar characters[size];
260 [string getCharacters:characters range:(NSRange) { 0, size }];
261
262 /* Beginning of current range of non-special characters. If we encounter
263 a substitution, we'll be copying from here forward.
264 */
265 NSUInteger copyRangeStart = 0;
266
267 // Mutable string for result if we perform any substitutions.
268 NSMutableString* result = nil;
269
270 /* The iteration limit is size - 1 because every valid substitution is at
271 least 2 characters long. This way, characters[idx + 1] is always valid.
272 */
273 for (NSUInteger idx = 0; idx < size - 1; idx++) {
274 /* Main parsing loop. If, at the end of the loop, replacement != nil,
275 we copy the characters from copyRangeStart to idx into the result,
276 the insert replacement, and skip replaceLength characters forward
277 (minus one, because idx is incremented by the loop.)
278 */
279 NSString* replacement = nil;
280 NSUInteger replaceLength = 0;
281 unichar thisChar = characters[idx];
282
283 if (thisChar == '[') {
284 replacement = ExpandKey(context, characters, size, idx, &replaceLength, sizeLimit, recursionLimit);
285 } else if (thisChar == '%') {
286 replacement = ExpandPercentEscape(context, characters, size, idx, &replaceLength);
287 } else if (thisChar == ']') {
288 SyntaxWarning(context, @"strings.expand.warning.unbalancedClosingBracket", @"%@", @"Unbalanced ] in string.");
289 } else if (thisChar == '\\' && context->convertBackslashN) {
290 if (characters[idx + 1] == 'n') {
291 replaceLength = 2;
292 replacement = @"\n";
293 }
294 } else {
295 // No token start character, so we definitely have no replacement.
296 continue;
297 }
298
299 if (replacement != nil) {
300 /* If replacement string is "\x7F", eat the following character.
301 This is used in system_description for the one empty string
302 in [22].
303 */
304 if ([replacement isEqualToString:@"\x7F"] && replaceLength < size) {
305 replaceLength++;
306 replacement = @"";
307 }
308
309 // Avoid copying if we're replacing the entire input string.
310 if (copyRangeStart == 0 && replaceLength == size) {
311 return replacement;
312 }
313
314 // Write the pending literal segment to result. This also allocates result if needed.
315 AppendCharacters(&result, characters, copyRangeStart, idx);
316
317 [result appendString:replacement];
318
319 // Skip over replaced part and start a new literal segment.
320 idx += replaceLength - 1;
321 copyRangeStart = idx + 1;
322 }
323 }
324
325 if (result != nil) {
326 // Append any trailing literal segment.
327 AppendCharacters(&result, characters, copyRangeStart, size);
328
329 // Don't turn result immutable; doing it once at top level is sufficient.
330 return result;
331 } else {
332 // No substitutions, return original string.
333 return string;
334 }
335}
#define EXPECT_NOT(x)
#define SyntaxWarning(CONTEXT, CLASS, FORMAT,...)
static NSString * ExpandPercentEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static void AppendCharacters(NSMutableString **result, const unichar *characters, NSUInteger start, NSUInteger end)
static NSString * ExpandKey(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength, NSUInteger sizeLimit, NSUInteger recursionLimit)
voidpf void uLong size
Definition ioapi.h:126

References AppendCharacters(), OOStringExpansionContext::convertBackslashN, ExpandKey(), ExpandPercentEscape(), EXPECT_NOT, kStackAllocationLimit, nil, and SyntaxWarning.

Referenced by ExpandDigitKey(), ExpandStringKeyFromDescriptions(), and OOExpandDescriptionString().

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

◆ ExpandDigitKey()

NSString * ExpandDigitKey ( OOStringExpansionContext * context,
const unichar * characters,
NSUInteger keyStart,
NSUInteger keyLength,
NSUInteger sizeLimit,
NSUInteger recursionLimit )
static

Definition at line 508 of file OOStringExpander.m.

509{
510 NSCParameterAssert(context != NULL && characters != NULL);
511
512 NSUInteger keyValue = 0, idx;
513 for (idx = keyStart; idx < (keyStart + keyLength); idx++) {
514 NSCAssert2(isdigit(characters[idx]), @"%s called with non-numeric key [%@].", __FUNCTION__, [NSString stringWithCharacters:characters + keyStart length:keyLength]);
515
516 keyValue = keyValue * 10 + characters[idx] - '0';
517 }
518
519 // Retrieve selected system_description entry.
520 NSArray* sysDescs = GetSystemDescriptions(context);
521 NSArray* entry = [sysDescs oo_arrayAtIndex:keyValue];
522
523 if (EXPECT_NOT(entry == nil)) {
524 if (keyValue >= context->sysDescCount) {
525 SyntaxWarning(context, @"strings.expand.warning.outOfRangeKey", @"Out-of-range system description expansion key [%@] in string.", [NSString stringWithCharacters:characters + keyStart length:keyLength]);
526 } else {
527 // This is out of the scope of whatever triggered it, so shouldn't be a JS warning.
528 OOLogERR(@"strings.expand.invalidData", @"%@", @"descriptions.plist entry system_description must be an array of arrays of strings.");
529 }
530 return nil;
531 }
532
533 // Select a random sub-entry.
534 NSUInteger selection, count = [entry count];
535 NSUInteger rnd = OO_EXPANDER_RANDOM;
536 if (count == 5 && !context->useGoodRNG) {
537 // Time-honoured Elite-compatible way for five items.
538 if (rnd >= 0xCC)
539 selection = 4;
540 else if (rnd >= 0x99)
541 selection = 3;
542 else if (rnd >= 0x66)
543 selection = 2;
544 else if (rnd >= 0x33)
545 selection = 1;
546 else
547 selection = 0;
548 } else {
549 // General way.
550 selection = (rnd * count) / 256;
551 }
552
553 // Look up and recursively expand string.
554 NSString* string = [entry oo_stringAtIndex:selection];
555 return Expand(context, string, sizeLimit, recursionLimit);
556}
unsigned count
static NSArray * GetSystemDescriptions(OOStringExpansionContext *context)
#define OO_EXPANDER_RANDOM
static NSString * Expand(OOStringExpansionContext *context, NSString *string, NSUInteger sizeLimit, NSUInteger recursionLimit)

References count, Expand(), EXPECT_NOT, GetSystemDescriptions(), nil, OO_EXPANDER_RANDOM, OOLogERR, SyntaxWarning, OOStringExpansionContext::sysDescCount, and OOStringExpansionContext::useGoodRNG.

Referenced by ExpandKey().

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

◆ ExpandKey()

NSString * ExpandKey ( OOStringExpansionContext * context,
const unichar * characters,
NSUInteger size,
NSUInteger idx,
NSUInteger * replaceLength,
NSUInteger sizeLimit,
NSUInteger recursionLimit )
static

Definition at line 349 of file OOStringExpander.m.

350{
351 NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
352 NSCParameterAssert(characters[idx] == '[');
353
354 // Find the balancing close bracket.
355 NSUInteger end, balanceCount = 1, firstBar = 0;
356 bool allDigits = true;
357
358 for (end = idx + 1; end < size && balanceCount > 0; end++) {
359 if (characters[end] == ']')
360 balanceCount--;
361 else {
362 if (characters[end] == '[')
363 balanceCount++;
364 else if (characters[end] == '|' && firstBar == 0)
365 firstBar = end;
366 if (!isdigit(characters[end]) && firstBar == 0)
367 allDigits = false;
368 }
369 }
370
371 // Fail if no balancing bracket.
372 if (EXPECT_NOT(balanceCount != 0)) {
373 SyntaxWarning(context, @"strings.expand.warning.unbalancedOpeningBracket", @"%@", @"Unbalanced [ in string.");
374 return nil;
375 }
376
377 NSUInteger totalLength = end - idx;
378 *replaceLength = totalLength;
379 NSUInteger keyStart = idx + 1, keyLength = totalLength - 2;
380 if (firstBar != 0)
381 keyLength = firstBar - idx - 1;
382
383 if (EXPECT_NOT(keyLength == 0)) {
384 SyntaxWarning(context, @"strings.expand.warning.emptyKey", @"%@", @"Invalid expansion code [] string. (To avoid this message, use %%[%%].)");
385 return nil;
386 }
387
388 NSString* expanded = nil;
389 if (allDigits) {
390 expanded = ExpandDigitKey(context, characters, keyStart, keyLength, sizeLimit, recursionLimit);
391 } else {
392 NSString* key = [NSString stringWithCharacters:characters + keyStart length:keyLength];
393 expanded = ExpandStringKey(context, key, sizeLimit, recursionLimit);
394 }
395
396 if (firstBar != 0) {
397 NSString* operators = [NSString stringWithCharacters:characters + firstBar + 1 length:end - firstBar - 2];
398 expanded = ApplyOperators(expanded, operators);
399 }
400
401 return expanded;
402}
static NSString * ExpandDigitKey(OOStringExpansionContext *context, const unichar *characters, NSUInteger keyStart, NSUInteger keyLength, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ApplyOperators(NSString *string, NSString *operatorsString)
static NSString * ExpandStringKey(OOStringExpansionContext *context, NSString *key, NSUInteger sizeLimit, NSUInteger recursionLimit)

References ApplyOperators(), ExpandDigitKey(), ExpandStringKey(), EXPECT_NOT, nil, and SyntaxWarning.

Referenced by Expand().

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

◆ ExpandLegacyScriptSelectorKey()

NSString * ExpandLegacyScriptSelectorKey ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 764 of file OOStringExpander.m.

765{
766 NSCParameterAssert(context != NULL && key != nil);
767
768 SEL selector = LookUpLegacySelector(key);
769
770 if (selector != NULL) {
771 return [[PLAYER performSelector:selector] description];
772 } else {
773 return nil;
774 }
775}
static SEL LookUpLegacySelector(NSString *key)

References LookUpLegacySelector(), and nil.

Referenced by ExpandStringKey().

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

◆ ExpandPercentEscape()

NSString * ExpandPercentEscape ( OOStringExpansionContext * context,
const unichar * characters,
NSUInteger size,
NSUInteger idx,
NSUInteger * replaceLength )
static

Definition at line 864 of file OOStringExpander.m.

865{
866 NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
867 NSCParameterAssert(characters[idx] == '%');
868
869 // All %-escapes except %J and %G are 2 characters.
870 *replaceLength = 2;
871 unichar selector = characters[idx + 1];
872
873 switch (selector) {
874 case 'H':
875 return GetSystemName(context);
876
877 case 'I':
878 return GetSystemNameIan(context);
879
880 case 'N':
881 return GetRandomNameN(context);
882
883 case 'R':
884 // to keep planet description generation consistent with earlier versions
885 // this must be done after all other substitutions in a second pass.
886 context->hasPercentR = true;
887 return @"%R";
888
889 case 'G':
890 return ExpandSystemNameForGalaxyEscape(context, characters, size, idx, replaceLength);
891
892 case 'J':
893 return ExpandSystemNameEscape(context, characters, size, idx, replaceLength);
894
895 case '%':
896 return @"%";
897
898 case '[':
899 return @"[";
900
901 case ']':
902 return @"]";
903
904 /* These are NSString formatting specifiers that occur in
905 descriptions.plist. The '.' is for floating-point (g and f)
906 specifiers that have field widths specified. No unadorned
907 %f or %g is found in vanilla Oolite descriptions.plist.
908
909 Ideally, these would be replaced with the caller formatting
910 the value and passing it as an override - it would be safer
911 and make descriptions.plist clearer - but it would be a big
912 job and uglify the callers without newfangled Objective-C
913 dictionary literals.
914 -- Ahruman 2012-10-05
915 */
916 case '@':
917 case 'd':
918 case '.':
919 return nil;
920
921 default:
922 // Yay, percent signs!
923 SyntaxWarning(context, @"strings.expand.warning.unknownPercentEscape", @"Unknown escape code in string: %%%lc. (To encode a %% sign without this warning, use %%%% - but prefer \"percent\" in prose writing.)", selector);
924
925 return nil;
926 }
927}
static NSString * GetSystemName(OOStringExpansionContext *context)
static NSString * ExpandSystemNameEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static NSString * GetSystemNameIan(OOStringExpansionContext *context)
static NSString * ExpandSystemNameForGalaxyEscape(OOStringExpansionContext *context, const unichar *characters, NSUInteger size, NSUInteger idx, NSUInteger *replaceLength)
static NSString * GetRandomNameN(OOStringExpansionContext *context)

References ExpandSystemNameEscape(), ExpandSystemNameForGalaxyEscape(), GetRandomNameN(), GetSystemName(), GetSystemNameIan(), OOStringExpansionContext::hasPercentR, nil, and SyntaxWarning.

Referenced by Expand().

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

◆ ExpandPercentR()

NSString * ExpandPercentR ( OOStringExpansionContext * context,
NSString * input )
static

Definition at line 934 of file OOStringExpander.m.

935{
936 NSRange containsR = [input rangeOfString:@"%R"];
937 if (containsR.location == NSNotFound) {
938 return input; // no %Rs to replace
939 }
940 NSString* percentR = GetRandomNameR(context);
941 NSMutableString* output = [NSMutableString stringWithString:input];
942
943 /* This loop should be completely unnecessary, but for some reason
944 * replaceOccurrencesOfString sometimes only replaces the first
945 * instance of %R if percentR contains the non-ASCII
946 * digrams-apostrophe character. (I guess
947 * http://lists.gnu.org/archive/html/gnustep-dev/2011-10/msg00048.html
948 * this bug in GNUstep's implementation here, which is in 1.22) So
949 * to cover that case, if there are still %R in the string after
950 * replacement, try again. Affects things like thargoid curses, and
951 * particularly %Rful expansions of [nom]. Probably this can be
952 * tidied up once GNUstep 1.22 is ancient history, but that'll be a
953 * few years yet. - CIM 15/1/2013 */
954
955 do {
956 [output replaceOccurrencesOfString:@"%R" withString:percentR options:NSLiteralSearch range:NSMakeRange(0, [output length])];
957 } while ([output rangeOfString:@"%R"].location != NSNotFound);
958
959 return [NSString stringWithString:output];
960}
static NSString * GetRandomNameR(OOStringExpansionContext *context)

References GetRandomNameR().

Referenced by OOExpandDescriptionString().

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

◆ ExpandStringKey()

NSString * ExpandStringKey ( OOStringExpansionContext * context,
NSString * key,
NSUInteger sizeLimit,
NSUInteger recursionLimit )
static

Definition at line 563 of file OOStringExpander.m.

564{
565 NSCParameterAssert(context != NULL && key != nil);
566
567 // Overrides have top priority.
568 NSString* result = ExpandStringKeyOverride(context, key);
569
570 // Specials override descriptions.plist.
571 if (result == nil)
572 result = ExpandStringKeySpecial(context, key);
573
574 // Now try descriptions.plist.
575 if (result == nil)
576 result = ExpandStringKeyFromDescriptions(context, key, sizeLimit, recursionLimit);
577
578 // For efficiency, descriptions.plist overrides keybindings.
579 // OXPers should therefore avoid oolite_key_ description keys
580 if (result == nil)
581 result = ExpandStringKeyKeyboardBinding(context, key);
582
583 // Try mission variables.
584 if (result == nil)
585 result = ExpandStringKeyMissionVariable(context, key);
586
587 // Try legacy local variables.
588 if (result == nil)
589 result = ExpandStringKeyLegacyLocalVariable(context, key);
590
591 // Try legacy script methods.
592 if (result == nil)
593 ExpandLegacyScriptSelectorKey(context, key);
594
595#if WARNINGS
596 // None of that worked, so moan a bit.
597 if (result == nil)
598 ReportWarningForUnknownKey(context, key);
599#endif
600
601 return result;
602}
static NSString * ExpandStringKeyLegacyLocalVariable(OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyFromDescriptions(OOStringExpansionContext *context, NSString *key, NSUInteger sizeLimit, NSUInteger recursionLimit)
static NSString * ExpandLegacyScriptSelectorKey(OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyMissionVariable(OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyOverride(OOStringExpansionContext *context, NSString *key)
static void ReportWarningForUnknownKey(OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeyKeyboardBinding(OOStringExpansionContext *context, NSString *key)
static NSString * ExpandStringKeySpecial(OOStringExpansionContext *context, NSString *key)

References ExpandLegacyScriptSelectorKey(), ExpandStringKeyFromDescriptions(), ExpandStringKeyKeyboardBinding(), ExpandStringKeyLegacyLocalVariable(), ExpandStringKeyMissionVariable(), ExpandStringKeyOverride(), ExpandStringKeySpecial(), nil, and ReportWarningForUnknownKey().

Referenced by ExpandKey(), and OOExpandDescriptionString().

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

◆ ExpandStringKeyFromDescriptions()

NSString * ExpandStringKeyFromDescriptions ( OOStringExpansionContext * context,
NSString * key,
NSUInteger sizeLimit,
NSUInteger recursionLimit )
static

Definition at line 712 of file OOStringExpander.m.

713{
714 id value = [[UNIVERSE descriptions] objectForKey:key];
715 if (value != nil) {
716 if ([value isKindOfClass:[NSArray class]] && [value count] > 0) {
717 NSUInteger rnd = OO_EXPANDER_RANDOM % [value count];
718 value = [value oo_objectAtIndex:rnd];
719 }
720
721 if (![value isKindOfClass:[NSString class]]) {
722 // This is out of the scope of whatever triggered it, so shouldn't be a JS warning.
723 OOLogERR(@"strings.expand.invalidData", @"String expansion value %@ for [%@] from descriptions.plist is not a string or number.", [value shortDescription], key);
724 return nil;
725 }
726
727 // Expand recursively.
728 return Expand(context, value, sizeLimit, recursionLimit);
729 }
730
731 return nil;
732}

References count, Expand(), nil, OO_EXPANDER_RANDOM, and OOLogERR.

Referenced by ExpandStringKey().

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

◆ ExpandStringKeyKeyboardBinding()

NSString * ExpandStringKeyKeyboardBinding ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 659 of file OOStringExpander.m.

660{
661 NSCParameterAssert(context != NULL && key != nil);
662 if ([key hasPrefix:@"oolite_key_"]) {
663 NSString* binding = [key substringFromIndex:7];
664 return [PLAYER keyBindingDescription2:binding];
665 }
666 return nil;
667}

References nil.

Referenced by ExpandStringKey().

Here is the caller graph for this function:

◆ ExpandStringKeyLegacyLocalVariable()

NSString * ExpandStringKeyLegacyLocalVariable ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 753 of file OOStringExpander.m.

754{
755 return [[context->legacyLocals objectForKey:key] description];
756}

Referenced by ExpandStringKey().

Here is the caller graph for this function:

◆ ExpandStringKeyMissionVariable()

NSString * ExpandStringKeyMissionVariable ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 738 of file OOStringExpander.m.

739{
740 if ([key hasPrefix:@"mission_"]) {
741 return [PLAYER missionVariableForKey:key];
742 }
743
744 return nil;
745}

References nil.

Referenced by ExpandStringKey().

Here is the caller graph for this function:

◆ ExpandStringKeyOverride()

NSString * ExpandStringKeyOverride ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 613 of file OOStringExpander.m.

614{
615 NSCParameterAssert(context != NULL && key != nil);
616
617 id value = [context->overrides objectForKey:key];
618 if (value != nil) {
619#if WARNINGS
620 if (![value isKindOfClass:[NSString class]] && ![value isKindOfClass:[NSNumber class]]) {
621 SyntaxWarning(context, @"strings.expand.warning.invalidOverride", @"String expansion override value %@ for [%@] is not a string or number.", [value shortDescription], key);
622 }
623#endif
624 return [value description];
625 }
626
627 return nil;
628}

References nil, and SyntaxWarning.

Referenced by ExpandStringKey().

Here is the caller graph for this function:

◆ ExpandStringKeySpecial()

NSString * ExpandStringKeySpecial ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 636 of file OOStringExpander.m.

637{
638 NSCParameterAssert(context != NULL && key != nil);
639
640 NSMapTable* specials = SpecialSubstitutionSelectors();
641 SEL selector = NSMapGet(specials, key);
642 if (selector != NULL) {
643 NSCAssert2([PLAYER respondsToSelector:selector], @"Special string expansion selector %@ for [%@] is not implemented.", NSStringFromSelector(selector), key);
644
645 NSString* result = [PLAYER performSelector:selector];
646 if (result != nil) {
647 NSCAssert2([result isKindOfClass:[NSString class]], @"Special string expansion [%@] expanded to %@, but expected a string.", key, [result shortDescription]);
648 return result;
649 }
650 }
651
652 return nil;
653}
static NSMapTable * SpecialSubstitutionSelectors(void)
#define PLAYER

References nil, PLAYER, and SpecialSubstitutionSelectors().

Referenced by ExpandStringKey().

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

◆ ExpandSystemNameEscape()

NSString * ExpandSystemNameEscape ( OOStringExpansionContext * context,
const unichar * characters,
NSUInteger size,
NSUInteger idx,
NSUInteger * replaceLength )
static

Definition at line 1014 of file OOStringExpander.m.

1015{
1016 NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
1017 NSCParameterAssert(characters[idx + 1] == 'J');
1018
1019 // A valid %J escape is always five characters including the three digits.
1020 *replaceLength = 5;
1021
1022#define kInvalidJEscapeMessage @"String escape code %J must be followed by three integers."
1023 if (EXPECT_NOT(size - idx < 5)) {
1024 // Too close to end of string to actually have three characters, let alone three digits.
1025 SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidJEscapeMessage);
1026 return nil;
1027 }
1028
1029 char hundreds = characters[idx + 2];
1030 char tens = characters[idx + 3];
1031 char units = characters[idx + 4];
1032
1033 if (!(isdigit(hundreds) && isdigit(tens) && isdigit(units))) {
1034 SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidJEscapeMessage);
1035 return nil;
1036 }
1037
1038 OOSystemID sysID = (hundreds - '0') * 100 + (tens - '0') * 10 + (units - '0');
1039 if (sysID > kOOMaximumSystemID) {
1040 SyntaxError(context, @"strings.expand.invalidJEscape.range", @"String escape code %%J%3u is out of range (must be less than %u).", sysID, kOOMaximumSystemID + 1);
1041 return nil;
1042 }
1043
1044 return [UNIVERSE getSystemName:sysID];
1045}
#define SyntaxError(CONTEXT, CLASS, FORMAT,...)
#define kInvalidJEscapeMessage
@ kOOMaximumSystemID
Definition OOTypes.h:187
int16_t OOSystemID
Definition OOTypes.h:183

References EXPECT_NOT, kInvalidJEscapeMessage, kOOMaximumSystemID, nil, and SyntaxError.

Referenced by ExpandPercentEscape().

Here is the caller graph for this function:

◆ ExpandSystemNameForGalaxyEscape()

NSString * ExpandSystemNameForGalaxyEscape ( OOStringExpansionContext * context,
const unichar * characters,
NSUInteger size,
NSUInteger idx,
NSUInteger * replaceLength )
static

Definition at line 967 of file OOStringExpander.m.

968{
969 NSCParameterAssert(context != NULL && characters != NULL && replaceLength != NULL);
970 NSCParameterAssert(characters[idx + 1] == 'G');
971
972 // A valid %G escape is always eight characters including the six digits.
973 *replaceLength = 8;
974
975#define kInvalidGEscapeMessage @"String escape code %G must be followed by six integers."
976 if (EXPECT_NOT(size - idx < 8)) {
977 // Too close to end of string to actually have six characters, let alone six digits.
978 SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidGEscapeMessage);
979 return nil;
980 }
981
982 char hundreds = characters[idx + 2];
983 char tens = characters[idx + 3];
984 char units = characters[idx + 4];
985 char galHundreds = characters[idx + 5];
986 char galTens = characters[idx + 6];
987 char galUnits = characters[idx + 7];
988
989 if (!(isdigit(hundreds) && isdigit(tens) && isdigit(units) && isdigit(galHundreds) && isdigit(galTens) && isdigit(galUnits))) {
990 SyntaxError(context, @"strings.expand.invalidJEscape", @"%@", kInvalidGEscapeMessage);
991 return nil;
992 }
993
994 OOSystemID sysID = (hundreds - '0') * 100 + (tens - '0') * 10 + (units - '0');
995 if (sysID > kOOMaximumSystemID) {
996 SyntaxError(context, @"strings.expand.invalidJEscape.range", @"String escape code %%G%3u for system is out of range (must be less than %u).", sysID, kOOMaximumSystemID + 1);
997 return nil;
998 }
999
1000 OOGalaxyID galID = (galHundreds - '0') * 100 + (galTens - '0') * 10 + (galUnits - '0');
1001 if (galID > kOOMaximumGalaxyID) {
1002 SyntaxError(context, @"strings.expand.invalidJEscape.range", @"String escape code %%G%3u for galaxy is out of range (must be less than %u).", galID, kOOMaximumGalaxyID + 1);
1003 return nil;
1004 }
1005
1006 return [UNIVERSE getSystemName:sysID forGalaxy:galID];
1007}
#define kInvalidGEscapeMessage
@ kOOMaximumGalaxyID
Definition OOTypes.h:186
uint8_t OOGalaxyID
Definition OOTypes.h:182

References EXPECT_NOT, kInvalidGEscapeMessage, kOOMaximumGalaxyID, kOOMaximumSystemID, nil, and SyntaxError.

Referenced by ExpandPercentEscape().

Here is the caller graph for this function:

◆ GetRandomNameN()

NSString * GetRandomNameN ( OOStringExpansionContext * context)
static

Definition at line 1096 of file OOStringExpander.m.

1097{
1098 NSCParameterAssert(context != NULL);
1099
1100 if (context->randomNameN == nil) {
1101 context->randomNameN = [NewRandomDigrams(context) retain];
1102 }
1103
1104 return context->randomNameN;
1105}

References nil, and OOStringExpansionContext::randomNameN.

Referenced by ExpandPercentEscape().

Here is the caller graph for this function:

◆ GetRandomNameR()

NSString * GetRandomNameR ( OOStringExpansionContext * context)
static

Definition at line 1107 of file OOStringExpander.m.

1108{
1109 NSCParameterAssert(context != NULL);
1110
1111 if (context->randomNameR == nil) {
1112 context->randomNameR = [OldRandomDigrams() retain];
1113 }
1114
1115 return context->randomNameR;
1116}

References nil, and OOStringExpansionContext::randomNameR.

Referenced by ExpandPercentR().

Here is the caller graph for this function:

◆ GetSystemDescriptions()

NSArray * GetSystemDescriptions ( OOStringExpansionContext * context)
static

Definition at line 1118 of file OOStringExpander.m.

1119{
1120 NSCParameterAssert(context != NULL);
1121
1122 if (context->systemDescriptions == nil) {
1123 context->systemDescriptions = [[[UNIVERSE descriptions] oo_arrayForKey:@"system_description"] retain];
1124 context->sysDescCount = [context->systemDescriptions count];
1125 }
1126
1127 return context->systemDescriptions;
1128}

References nil, OOStringExpansionContext::sysDescCount, and OOStringExpansionContext::systemDescriptions.

Referenced by ExpandDigitKey().

Here is the caller graph for this function:

◆ GetSystemName()

NSString * GetSystemName ( OOStringExpansionContext * context)
static

Definition at line 1075 of file OOStringExpander.m.

1076{
1077 NSCParameterAssert(context != NULL);
1078 if (context->systemName == nil) {
1079 context->systemName = [[UNIVERSE getSystemName:[PLAYER systemID]] retain];
1080 }
1081
1082 return context->systemName;
1083}

References nil, and OOStringExpansionContext::systemName.

Referenced by ExpandPercentEscape().

Here is the caller graph for this function:

◆ GetSystemNameIan()

NSString * GetSystemNameIan ( OOStringExpansionContext * context)
static

Definition at line 1085 of file OOStringExpander.m.

1086{
1087 NSCParameterAssert(context != NULL);
1088
1089 if (context->systemNameWithIan == nil) {
1090 context->systemNameWithIan = [OOExpandWithOptions(context->seed, kOOExpandDisallowPercentI | kOOExpandGoodRNG | kOOExpandKey, @"planetname-possessive") retain];
1091 }
1092
1093 return context->systemNameWithIan;
1094}

References nil, and OOStringExpansionContext::systemNameWithIan.

Referenced by ExpandPercentEscape().

Here is the caller graph for this function:

◆ LookUpLegacySelector()

SEL LookUpLegacySelector ( NSString * key)
static

Definition at line 782 of file OOStringExpander.m.

783{
784 SEL selector = NULL;
785 static NSMapTable* selectorCache = NULL;
786
787 // Try cache lookup.
788 if (selectorCache != NULL) {
789 selector = NSMapGet(selectorCache, key);
790 }
791
792 if (selector == NULL) {
793 static NSDictionary* aliases = nil;
794 static NSSet* whitelist = nil;
795 if (whitelist == nil) {
796 NSDictionary* whitelistDict = [ResourceManager whitelistDictionary];
797 whitelist = [[NSSet alloc] initWithArray:[whitelistDict oo_arrayForKey:@"query_methods"]];
798 aliases = [[whitelistDict oo_dictionaryForKey:@"query_method_aliases"] copy];
799 }
800
801 NSString* selectorName = [aliases oo_stringForKey:key];
802 if (selectorName == nil)
803 selectorName = key;
804
805 if ([whitelist containsObject:selectorName]) {
806 selector = NSSelectorFromString(selectorName);
807
808 /* This is an assertion, not a warning, because whitelist.plist is
809 part of the game and cannot be overriden by OXPs. If there is an
810 invalid selector in the whitelist, it's a game bug.
811 */
812 NSCAssert1([PLAYER respondsToSelector:selector], @"Player does not respond to whitelisted query selector %@.", key);
813 }
814
815 if (selector != NULL) {
816 // Add it to cache.
817 if (selectorCache == NULL) {
818 selectorCache = NSCreateMapTable(NSObjectMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, [whitelist count]);
819 }
820 NSMapInsertKnownAbsent(selectorCache, key, selector);
821 }
822 }
823
824 return selector;
825}
NSDictionary * whitelistDictionary()

References count, nil, PLAYER, and ResourceManager::whitelistDictionary.

Referenced by ExpandLegacyScriptSelectorKey().

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

◆ NewRandomDigrams()

NSString * NewRandomDigrams ( OOStringExpansionContext * context)
static

Definition at line 1152 of file OOStringExpander.m.

1153{
1154 unsigned length = (OO_EXPANDER_RANDOM % 4) + 1;
1155 if ((OO_EXPANDER_RANDOM % 5) < ((length == 1) ? 3 : 1))
1156 ++length; // Make two-letter names rarer and 10-letter names happen sometimes
1157 NSString* digrams = [[UNIVERSE descriptions] objectForKey:@"digrams"];
1158 NSUInteger count = [digrams length] / 2;
1159 NSMutableString* name = [NSMutableString stringWithCapacity:length * 2];
1160
1161 for (unsigned i = 0; i != length; ++i) {
1162 [name appendString:[digrams substringWithRange:NSMakeRange((OO_EXPANDER_RANDOM % count) * 2, 2)]];
1163 }
1164
1165 return [name capitalizedString];
1166}

References count, and OO_EXPANDER_RANDOM.

◆ OldRandomDigrams()

NSString * OldRandomDigrams ( void )
static

Definition at line 1134 of file OOStringExpander.m.

1135{
1136 /* The only point of using %R is for world generation, so there's
1137 * no point in checking the context */
1138 unsigned len = gen_rnd_number() & 3;
1139 NSString* digrams = [[UNIVERSE descriptions] objectForKey:@"digrams"];
1140 NSMutableString* name = [NSMutableString stringWithCapacity:256];
1141
1142 for (unsigned i = 0; i <= len; i++) {
1143 unsigned x = gen_rnd_number() & 0x3e;
1144 [name appendString:[digrams substringWithRange:NSMakeRange(x, 2)]];
1145 }
1146
1147 return [name capitalizedString];
1148}
float x
int gen_rnd_number(void)

References gen_rnd_number(), and x.

Here is the call graph for this function:

◆ OOExpandDescriptionString()

NSString * OOExpandDescriptionString ( Random_Seed seed,
NSString * string,
NSDictionary * overrides,
NSDictionary * legacyLocals,
NSString * systemName,
OOExpandOptions options )

Definition at line 160 of file OOStringExpander.m.

161{
162 if (string == nil)
163 return nil;
164
165 OOStringExpansionContext context = {
166 .seed = seed,
167 .systemName = [systemName retain],
168 .overrides = [overrides retain],
169 .legacyLocals = [legacyLocals retain],
170 .isJavaScript = options & kOOExpandForJavaScript,
171 .convertBackslashN = options & kOOExpandBackslashN,
172 .useGoodRNG = options & kOOExpandGoodRNG
173 };
174
175 // Avoid recursive %I expansion by pre-seeding cache with literal %I.
176 if (options & kOOExpandDisallowPercentI) {
177 context.systemNameWithIan = @"%I";
178 }
179
180 OORandomState savedRandomState;
181 if (options & kOOExpandReseedRNG) {
182 savedRandomState = OOSaveRandomState();
184 }
185
186 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
187 NSString *result = nil, *intermediate = nil;
188 @try {
189 // TODO: profile caching the results. Would need to keep track of whether we've done something nondeterministic (array selection, %R etc).
190 if (options & kOOExpandKey) {
191 intermediate = ExpandStringKey(&context, string, kStackAllocationLimit, kRecursionLimit);
192 } else {
193 intermediate = Expand(&context, string, kStackAllocationLimit, kRecursionLimit);
194 }
195 if (!context.hasPercentR) {
196 result = intermediate;
197 } else {
198 result = ExpandPercentR(&context, intermediate);
199 }
200 }
201 @finally {
202 [context.systemName release];
203 [context.overrides release];
204 [context.legacyLocals release];
205 [context.systemNameWithIan release];
206 [context.randomNameN release];
207 [context.randomNameR release];
208 [context.systemDescriptions release];
209 }
210
211 if (options & kOOExpandReseedRNG) {
212 OORestoreRandomState(savedRandomState);
213 }
214
215 result = [result copy];
216 [pool release];
217 return [result autorelease];
218}
@ kOOExpandForJavaScript
Report warnings through JavaScript runtime system instead of normal logging.
@ kOOExpandDisallowPercentI
Disallow I expansion (used when expanding I itself).
@ kOOExpandKey
Treat string as a key. Expand("foo", kOOExpandKey) == Expand(@"[foo]", kOOExpandNoOptions).
@ kOOExpandBackslashN
Convert literal "\\n"s to line breaks (used for missiontext.plist for historical reasons).
@ kOOExpandGoodRNG
Use RANDROT for selecting from description arrays and for N expansion.
@ kOOExpandReseedRNG
Set "really random" seeds while expanding.
static NSString * ExpandPercentR(OOStringExpansionContext *context, NSString *input)
void OORestoreRandomState(OORandomState state)
OORandomState OOSaveRandomState(void)
void OOSetReallyRandomRANROTAndRndSeeds(void)

References Expand(), ExpandPercentR(), ExpandStringKey(), OOStringExpansionContext::hasPercentR, kOOExpandBackslashN, kOOExpandDisallowPercentI, kOOExpandForJavaScript, kOOExpandGoodRNG, kOOExpandKey, kOOExpandReseedRNG, kRecursionLimit, kStackAllocationLimit, nil, OORestoreRandomState(), OOSaveRandomState(), OOSetReallyRandomRANROTAndRndSeeds(), and OOStringExpansionContext::systemNameWithIan.

Referenced by GlobalExpandDescription(), GlobalExpandDescription(), GlobalExpandMissionText(), GlobalExpandMissionText(), OOGenerateSystemDescription(), PlayerEntity(Scripting)::PerformActionStatment, and ShipEntity::sendExpandedMessage:toShip:.

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

◆ OOGenerateSystemDescription()

NSString * OOGenerateSystemDescription ( Random_Seed seed,
NSString * name )

Definition at line 220 of file OOStringExpander.m.

221{
223 return OOExpandDescriptionString(seed, @"system-description-string", nil, nil, name, kOOExpandKey);
224}
NSString * OOExpandDescriptionString(Random_Seed seed, NSString *string, NSDictionary *overrides, NSDictionary *legacyLocals, NSString *systemName, OOExpandOptions options)
void seed_RNG_only_for_planet_description(Random_Seed s_seed)

References kOOExpandKey, nil, OOExpandDescriptionString(), and seed_RNG_only_for_planet_description().

Here is the call graph for this function:

◆ OOStringExpanderDefaultRandomSeed()

Random_Seed OOStringExpanderDefaultRandomSeed ( void )

Definition at line 226 of file OOStringExpander.m.

227{
228 return [[UNIVERSE systemManager] getRandomSeedForCurrentSystem];
229}

Referenced by PlayerEntity(Scripting)::addMissionText:, PlayerEntity(Scripting)::PerformActionStatment, and ShipEntity::sendExpandedMessage:toShip:.

Here is the caller graph for this function:

◆ Operator_add()

NSString * Operator_add ( NSString * string,
NSString * param )
static

Definition at line 458 of file OOStringExpander.m.

459{
460 return [NSString stringWithFormat:@"%g", [string doubleValue] + [param doubleValue]];
461}

◆ Operator_cr()

NSString * Operator_cr ( NSString * string,
NSString * param )
static

Definition at line 428 of file OOStringExpander.m.

429{
430 return OOCredits([string doubleValue] * 10);
431}
OOINLINE NSString * OOCredits(OOCreditsQuantity tenthsOfCredits)

References OOCredits().

Here is the call graph for this function:

◆ Operator_dcr()

NSString * Operator_dcr ( NSString * string,
NSString * param )
static

Definition at line 433 of file OOStringExpander.m.

434{
435 return OOCredits([string longLongValue]);
436}

References OOCredits().

Here is the call graph for this function:

◆ Operator_icr()

NSString * Operator_icr ( NSString * string,
NSString * param )
static

Definition at line 438 of file OOStringExpander.m.

439{
440 return OOIntCredits([string longLongValue]);
441}
OOINLINE NSString * OOIntCredits(OOCreditsQuantity integerCredits)

References OOIntCredits().

Here is the call graph for this function:

◆ Operator_idcr()

NSString * Operator_idcr ( NSString * string,
NSString * param )
static

Definition at line 443 of file OOStringExpander.m.

444{
445 return OOIntCredits(round([string doubleValue] / 10.0));
446}

References OOIntCredits().

Here is the call graph for this function:

◆ Operator_multiply()

NSString * Operator_multiply ( NSString * string,
NSString * param )
static

Definition at line 453 of file OOStringExpander.m.

454{
455 return [NSString stringWithFormat:@"%g", [string doubleValue] * [param doubleValue]];
456}

◆ Operator_precision()

NSString * Operator_precision ( NSString * string,
NSString * param )
static

Definition at line 448 of file OOStringExpander.m.

449{
450 return [NSString stringWithFormat:@"%.*f", [param intValue], [string doubleValue]];
451}

◆ ReportWarningForUnknownKey()

void ReportWarningForUnknownKey ( OOStringExpansionContext * context,
NSString * key )
static

Definition at line 834 of file OOStringExpander.m.

835{
836 if ([key hasSuffix:@"_string"] || [key hasSuffix:@"_number"] || [key hasSuffix:@"_bool"]) {
837 SyntaxError(context, @"strings.expand.invalidSelector", @"Unpermitted legacy script method [%@] in string.", key);
838 } else {
839 SyntaxWarning(context, @"strings.expand.warning.unknownExpansion", @"Unknown expansion key [%@] in string.", key);
840 }
841}

References SyntaxError, and SyntaxWarning.

Referenced by ExpandStringKey().

Here is the caller graph for this function:

◆ SpecialSubstitutionSelectors()

NSMapTable * SpecialSubstitutionSelectors ( void )
static

Definition at line 674 of file OOStringExpander.m.

675{
676 static NSMapTable* specials = NULL;
677 if (specials != NULL)
678 return specials;
679
680 struct {
681 NSString* key;
682 SEL selector;
683 } selectors[] = {
684 { @"commander_name", @selector(commanderName_string) },
685 { @"commander_shipname", @selector(commanderShip_string) },
686 { @"commander_shipdisplayname", @selector(commanderShipDisplayName_string) },
687 { @"commander_rank", @selector(commanderRank_string) },
688 { @"commander_kills", @selector(commanderKillsAsString) },
689 { @"commander_legal_status", @selector(commanderLegalStatus_string) },
690 { @"commander_bounty", @selector(commanderBountyAsString) },
691 { @"credits_number", @selector(creditsFormattedForSubstitution) },
692 { @"_oo_legacy_credits_number", @selector(creditsFormattedForLegacySubstitution) }
693 };
694 unsigned i, count = sizeof selectors / sizeof *selectors;
695
696 specials = NSCreateMapTable(NSObjectMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, count);
697 for (i = 0; i < count; i++) {
698 NSMapInsertKnownAbsent(specials, selectors[i].key, selectors[i].selector);
699 }
700
701 return specials;
702}

References count.

Referenced by ExpandStringKeySpecial().

Here is the caller graph for this function:

◆ SyntaxIssue()

void SyntaxIssue ( OOStringExpansionContext * context,
const char * function,
const char * fileName,
NSUInteger line,
NSString * logMessageClass,
NSString * prefix,
NSString * format,
... )
static

Definition at line 1168 of file OOStringExpander.m.

1169{
1170 NSCParameterAssert(context != NULL);
1171
1172 va_list args;
1173 va_start(args, format);
1174
1175 if (OOLogWillDisplayMessagesInClass(logMessageClass)) {
1176 if (context->isJavaScript) {
1177 /* NOTE: syntax errors are reported as warnings when called from JS
1178 because we don't want to start throwing exceptions when the old
1179 expander didn't.
1180 */
1181 JSContext* jsc = OOJSAcquireContext();
1182 OOJSReportWarningWithArguments(jsc, format, args);
1184 } else {
1185 format = [prefix stringByAppendingString:format];
1186 OOLogWithFunctionFileAndLineAndArguments(logMessageClass, function, fileName, line, format, args);
1187 }
1188 }
1189
1190 va_end(args);
1191}
void OOJSReportWarningWithArguments(JSContext *context, NSString *format, va_list args)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
void void void OOLogWithFunctionFileAndLineAndArguments(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat, va_list inArguments) OO_TAKES_FORMAT_STRING(5
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136

References OOStringExpansionContext::isJavaScript, OOJSAcquireContext(), OOJSRelinquishContext(), OOJSReportWarningWithArguments(), OOLogWillDisplayMessagesInClass(), and OOLogWithFunctionFileAndLineAndArguments().

Here is the call graph for this function: