73 NSAutoreleasePool *pool =
nil;
74 NSMutableArray *result =
nil;
78 pool = [[NSAutoreleasePool alloc] init];
80 result = [NSMutableArray arrayWithCapacity:[script count]];
82 foreach (statement, script)
89 if ([statement isKindOfClass:[NSDictionary class]])
93 else if ([statement isKindOfClass:[NSString class]])
99 OOLog(
@"script.syntax.statement.invalidType",
@"***** SCRIPT ERROR: in %@, statement is of invalid type - expected string or dictionary, got %@.",
StringFromStack(stack), [statement
class]);
103 if (statement !=
nil)
105 [result addObject:statement];
112 return [result autorelease];
177 NSArray *tokens =
nil;
178 NSUInteger i, tokenCount;
180 NSString *selectorString =
nil;
181 NSString *sanitizedSelectorString =
nil;
182 NSString *comparatorString =
nil;
184 NSMutableArray *rhs =
nil;
185 NSString *rhsItem =
nil;
186 NSString *rhsSelector =
nil;
187 NSArray *sanitizedRHSItem =
nil;
188 NSString *stringSegment =
nil;
191 tokenCount = [tokens count];
195 OOLog(
@"script.debug.syntax.scriptCondition.noneSpecified",
@"***** SCRIPT ERROR: in %@, empty script condition.",
StringFromStack(stack));
200 selectorString = [tokens oo_stringAtIndex:0];
204 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), condition, selectorString);
211 comparatorString = [tokens oo_stringAtIndex:1];
212 if ([comparatorString isEqualToString:
@"equal"]) comparatorValue =
COMPARISON_EQUAL;
213 else if ([comparatorString isEqualToString:
@"notequal"]) comparatorValue =
COMPARISON_NOTEQUAL;
214 else if ([comparatorString isEqualToString:
@"lessthan"]) comparatorValue =
COMPARISON_LESSTHAN;
217 else if ([comparatorString isEqualToString:
@"oneof"]) comparatorValue =
COMPARISON_ONEOF;
218 else if ([comparatorString isEqualToString:
@"undefined"]) comparatorValue =
COMPARISON_UNDEFINED;
221 OOLog(
@"script.debug.syntax.badComparison",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), unknown comparison operator \"%@\", will return NO.",
StringFromStack(stack), condition, comparatorString);
232 OOLog(
@"script.debug.syntax.noOperator",
@"----- WARNING: SCRIPT in %@ -- No operator in expression \"%@\
", will always evaluate as false.",
StringFromStack(stack), condition);
239 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
@"undefined",
@"numbers");
244 switch (comparatorValue)
252 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
OOComparisonTypeToString(comparatorValue),
@"booleans");
266 rhs = [NSMutableArray arrayWithCapacity:tokenCount - 2];
267 for (i = 2; i < tokenCount; i++)
269 rhsItem = [tokens oo_stringAtIndex:i];
271 if (rhsSelector !=
nil)
274 if (stringSegment !=
nil)
277 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
278 [rhs addObject:sanitizedRHSItem];
282 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], rhsSelector, nil];
283 [rhs addObject:sanitizedRHSItem];
288 if (stringSegment ==
nil) stringSegment = rhsItem;
289 else stringSegment = [NSString stringWithFormat:@"%@ %@", stringSegment, rhsItem];
293 if (stringSegment !=
nil)
295 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
296 [rhs addObject:sanitizedRHSItem];
301 rhs = [NSMutableArray array];
304 NSString *rawString =
nil;
305#if INCLUDE_RAW_STRING
306 rawString = condition;
308 rawString =
@"<condition>";
311 return [NSArray arrayWithObjects:
312 [NSNumber numberWithUnsignedInt:opType],
314 sanitizedSelectorString,
315 [NSNumber numberWithUnsignedInt:comparatorValue],
323 NSArray *conditions =
nil;
324 NSArray *doActions =
nil;
325 NSArray *elseActions =
nil;
327 conditions = [statement oo_arrayForKey:@"conditions"];
328 if (conditions ==
nil)
330 OOLog(
@"script.syntax.noConditions",
@"***** SCRIPT ERROR: in %@, conditions array contains no \"conditions\
" entry, ignoring.",
StringFromStack(stack));
337 if (conditions ==
nil)
344 if (doActions !=
nil)
346 subStack.
key =
@"do";
350 elseActions = [statement oo_arrayForKey:@"else"];
351 if (elseActions !=
nil)
353 subStack.
key =
@"else";
358 if ([doActions
count] == 0 && [elseActions
count] == 0)
363 if (doActions ==
nil) doActions = [NSArray array];
364 if (elseActions ==
nil) elseActions = [NSArray array];
366 return [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], conditions, doActions, elseActions, nil];
372 NSMutableArray *tokens =
nil;
373 NSUInteger tokenCount;
374 NSString *rawSelectorString =
nil;
375 NSString *selectorString =
nil;
376 NSString *argument =
nil;
379 tokenCount = [tokens count];
380 if (tokenCount == 0)
return nil;
382 rawSelectorString = [tokens objectAtIndex:0];
384 if (selectorString ==
nil)
386 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), statement, rawSelectorString);
390 if ([selectorString isEqualToString:
@"doNothing"])
395 if ([selectorString hasSuffix:
@":"])
400 argument = [tokens objectAtIndex:1];
404 [tokens removeObjectAtIndex:0];
405 argument = [tokens componentsJoinedByString:@" "];
408 argument = [argument stringByReplacingOccurrencesOfString:@"[credits_number]" withString:@"[_oo_legacy_credits_number]"];
411 return [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], selectorString, argument, nil];
417 assert(outSanitizedSelector != NULL);
419 *outSanitizedSelector = selectorString;
422 if ([selectorString hasPrefix:
@"mission_"])
return OP_MISSION_VAR;
423 if ([selectorString hasPrefix:
@"local_"])
return OP_LOCAL_VAR;
427 if (*outSanitizedSelector ==
nil)
433 if ([selectorString hasSuffix:
@"_string"])
return OP_STRING;
434 if ([selectorString hasSuffix:
@"_number"])
return OP_NUMBER;
435 if ([selectorString hasSuffix:
@"_bool"])
return OP_BOOL;
438 OOLog(
@"script.sanitize.unclassifiedSelector",
@"***** ERROR: Whitelisted query method \"%@\
" has no type suffix, treating as invalid.", selectorString);
445 static NSSet *whitelist =
nil;
446 static NSDictionary *aliases =
nil;
447 NSString *aliasedSelector =
nil;
449 if (whitelist ==
nil)
455 aliasedSelector = [aliases oo_stringForKey:selectorString];
456 if (aliasedSelector !=
nil) selectorString = aliasedSelector;
458 if (![whitelist containsObject:selectorString]) selectorString =
nil;
460 return selectorString;
466 static NSSet *whitelist =
nil;
467 static NSSet *whitelistWithAI =
nil;
468 static NSDictionary *aliases =
nil;
469 static NSDictionary *aliasesWithAI =
nil;
470 NSString *aliasedSelector =
nil;
472 if (whitelist ==
nil)
474 NSArray *actionMethods =
nil;
475 NSArray *aiMethods =
nil;
476 NSArray *aiAndActionMethods =
nil;
482 if (actionMethods ==
nil) actionMethods = [NSArray array];
483 if (aiMethods ==
nil) aiMethods = [NSArray array];
485 if (aiAndActionMethods !=
nil) actionMethods = [actionMethods arrayByAddingObjectsFromArray:aiAndActionMethods];
487 whitelist = [[NSSet alloc] initWithArray:actionMethods];
488 whitelistWithAI = [[NSSet alloc] initWithArray:[aiMethods arrayByAddingObjectsFromArray:actionMethods]];
493 if (aliasesWithAI !=
nil)
495 aliasesWithAI = [[aliasesWithAI dictionaryByAddingEntriesFromDictionary:aliases] copy];
499 aliasesWithAI = [aliases copy];
503 aliasedSelector = [(allowAIMethods ? aliasesWithAI : aliases) oo_stringForKey:selectorString];
504 if (aliasedSelector !=
nil) selectorString = aliasedSelector;
506 if (![(allowAIMethods ? whitelistWithAI : whitelist) containsObject:selectorString]) selectorString =
nil;
508 return selectorString;