67 NSAutoreleasePool* pool =
nil;
68 NSMutableArray* result =
nil;
72 pool = [[NSAutoreleasePool alloc] init];
74 result = [NSMutableArray arrayWithCapacity:[script count]];
76 foreach (statement, script) {
81 if ([statement isKindOfClass:[NSDictionary
class]]) {
83 }
else if ([statement isKindOfClass:[NSString
class]]) {
86 OOLog(
@"script.syntax.statement.invalidType",
@"***** SCRIPT ERROR: in %@, statement is of invalid type - expected string or dictionary, got %@.",
StringFromStack(stack), [statement
class]);
90 if (statement !=
nil) {
91 [result addObject:statement];
98 return [result autorelease];
158 NSArray* tokens =
nil;
159 NSUInteger i, tokenCount;
161 NSString* selectorString =
nil;
162 NSString* sanitizedSelectorString =
nil;
163 NSString* comparatorString =
nil;
165 NSMutableArray* rhs =
nil;
166 NSString* rhsItem =
nil;
167 NSString* rhsSelector =
nil;
168 NSArray* sanitizedRHSItem =
nil;
169 NSString* stringSegment =
nil;
172 tokenCount = [tokens count];
174 if (tokenCount < 1) {
175 OOLog(
@"script.debug.syntax.scriptCondition.noneSpecified",
@"***** SCRIPT ERROR: in %@, empty script condition.",
StringFromStack(stack));
180 selectorString = [tokens oo_stringAtIndex:0];
183 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), condition, selectorString);
188 if (tokenCount > 1) {
189 comparatorString = [tokens oo_stringAtIndex:1];
190 if ([comparatorString isEqualToString:
@"equal"])
192 else if ([comparatorString isEqualToString:
@"notequal"])
194 else if ([comparatorString isEqualToString:
@"lessthan"])
196 else if ([comparatorString isEqualToString:
@"greaterthan"])
198 else if ([comparatorString isEqualToString:
@"morethan"])
200 else if ([comparatorString isEqualToString:
@"oneof"])
202 else if ([comparatorString isEqualToString:
@"undefined"])
205 OOLog(
@"script.debug.syntax.badComparison",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), unknown comparison operator \"%@\", will return NO.",
StringFromStack(stack), condition, comparatorString);
214 OOLog(
@"script.debug.syntax.noOperator",
@"----- WARNING: SCRIPT in %@ -- No operator in expression \"%@\
", will always evaluate as false.",
StringFromStack(stack), condition);
220 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
@"undefined",
@"numbers");
222 }
else if (opType ==
OP_BOOL) {
223 switch (comparatorValue) {
230 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
OOComparisonTypeToString(comparatorValue),
@"booleans");
241 if (tokenCount > 2) {
242 rhs = [NSMutableArray arrayWithCapacity:tokenCount - 2];
243 for (i = 2; i < tokenCount; i++) {
244 rhsItem = [tokens oo_stringAtIndex:i];
246 if (rhsSelector !=
nil) {
248 if (stringSegment !=
nil) {
250 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
251 [rhs addObject:sanitizedRHSItem];
255 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], rhsSelector, nil];
256 [rhs addObject:sanitizedRHSItem];
259 if (stringSegment ==
nil)
260 stringSegment = rhsItem;
262 stringSegment = [NSString stringWithFormat:@"%@ %@", stringSegment, rhsItem];
266 if (stringSegment !=
nil) {
267 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
268 [rhs addObject:sanitizedRHSItem];
271 rhs = [NSMutableArray array];
274 NSString* rawString =
nil;
275#if INCLUDE_RAW_STRING
276 rawString = condition;
278 rawString =
@"<condition>";
281 return [NSArray arrayWithObjects:
282 [NSNumber numberWithUnsignedInt:opType],
284 sanitizedSelectorString,
285 [NSNumber numberWithUnsignedInt:comparatorValue],
292 NSArray* conditions =
nil;
293 NSArray* doActions =
nil;
294 NSArray* elseActions =
nil;
296 conditions = [statement oo_arrayForKey:@"conditions"];
297 if (conditions ==
nil) {
298 OOLog(
@"script.syntax.noConditions",
@"***** SCRIPT ERROR: in %@, conditions array contains no \"conditions\
" entry, ignoring.",
StringFromStack(stack));
305 if (conditions ==
nil) {
311 doActions = [statement oo_arrayForKey:@"do"];
312 if (doActions !=
nil) {
313 subStack.
key =
@"do";
317 elseActions = [statement oo_arrayForKey:@"else"];
318 if (elseActions !=
nil) {
319 subStack.
key =
@"else";
324 if ([doActions
count] == 0 && [elseActions
count] == 0) {
328 if (doActions ==
nil)
329 doActions = [NSArray array];
330 if (elseActions ==
nil)
331 elseActions = [NSArray array];
333 return [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], conditions, doActions, elseActions, nil];
338 NSMutableArray* tokens =
nil;
339 NSUInteger tokenCount;
340 NSString* rawSelectorString =
nil;
341 NSString* selectorString =
nil;
342 NSString* argument =
nil;
345 tokenCount = [tokens count];
349 rawSelectorString = [tokens objectAtIndex:0];
351 if (selectorString ==
nil) {
352 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), statement, rawSelectorString);
356 if ([selectorString isEqualToString:
@"doNothing"]) {
360 if ([selectorString hasSuffix:
@":"]) {
362 if (tokenCount == 2) {
363 argument = [tokens objectAtIndex:1];
365 [tokens removeObjectAtIndex:0];
366 argument = [tokens componentsJoinedByString:@" "];
369 argument = [argument stringByReplacingOccurrencesOfString:@"[credits_number]" withString:@"[_oo_legacy_credits_number]"];
372 return [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], selectorString, argument, nil];
377 assert(outSanitizedSelector != NULL);
379 *outSanitizedSelector = selectorString;
382 if ([selectorString hasPrefix:
@"mission_"])
384 if ([selectorString hasPrefix:
@"local_"])
389 if (*outSanitizedSelector ==
nil) {
394 if ([selectorString hasSuffix:
@"_string"])
396 if ([selectorString hasSuffix:
@"_number"])
398 if ([selectorString hasSuffix:
@"_bool"])
402 OOLog(
@"script.sanitize.unclassifiedSelector",
@"***** ERROR: Whitelisted query method \"%@\
" has no type suffix, treating as invalid.", selectorString);
408 static NSSet* whitelist =
nil;
409 static NSDictionary* aliases =
nil;
410 NSString* aliasedSelector =
nil;
412 if (whitelist ==
nil) {
417 aliasedSelector = [aliases oo_stringForKey:selectorString];
418 if (aliasedSelector !=
nil)
419 selectorString = aliasedSelector;
421 if (![whitelist containsObject:selectorString])
422 selectorString =
nil;
424 return selectorString;
429 static NSSet* whitelist =
nil;
430 static NSSet* whitelistWithAI =
nil;
431 static NSDictionary* aliases =
nil;
432 static NSDictionary* aliasesWithAI =
nil;
433 NSString* aliasedSelector =
nil;
435 if (whitelist ==
nil) {
436 NSArray* actionMethods =
nil;
437 NSArray* aiMethods =
nil;
438 NSArray* aiAndActionMethods =
nil;
444 if (actionMethods ==
nil)
445 actionMethods = [NSArray array];
446 if (aiMethods ==
nil)
447 aiMethods = [NSArray array];
449 if (aiAndActionMethods !=
nil)
450 actionMethods = [actionMethods arrayByAddingObjectsFromArray:aiAndActionMethods];
452 whitelist = [[NSSet alloc] initWithArray:actionMethods];
453 whitelistWithAI = [[NSSet alloc] initWithArray:[aiMethods arrayByAddingObjectsFromArray:actionMethods]];
458 if (aliasesWithAI !=
nil) {
459 aliasesWithAI = [[aliasesWithAI dictionaryByAddingEntriesFromDictionary:aliases] copy];
461 aliasesWithAI = [aliases copy];
465 aliasedSelector = [(allowAIMethods ? aliasesWithAI : aliases) oo_stringForKey:selectorString];
466 if (aliasedSelector !=
nil)
467 selectorString = aliasedSelector;
469 if (![(allowAIMethods ? whitelistWithAI : whitelist) containsObject:selectorString])
470 selectorString =
nil;
472 return selectorString;