73 NSAutoreleasePool *pool =
nil;
74 NSMutableArray *result =
nil;
75 NSEnumerator *statementEnum =
nil;
79 pool = [[NSAutoreleasePool alloc] init];
81 result = [NSMutableArray arrayWithCapacity:[script count]];
83 for (statementEnum = [script objectEnumerator]; (statement = [statementEnum nextObject]); )
90 if ([statement isKindOfClass:[NSDictionary
class]])
94 else if ([statement isKindOfClass:[NSString
class]])
100 OOLog(
@"script.syntax.statement.invalidType",
@"***** SCRIPT ERROR: in %@, statement is of invalid type - expected string or dictionary, got %@.",
StringFromStack(stack), [statement
class]);
104 if (statement !=
nil)
106 [result addObject:statement];
113 return [result autorelease];
128 NSEnumerator *conditionEnum =
nil;
129 NSString *condition =
nil;
130 NSMutableArray *result =
nil;
131 NSArray *tokens =
nil;
133 NSUInteger
index = 0;
137 result = [NSMutableArray arrayWithCapacity:[conditions count]];
139 for (conditionEnum = [conditions objectEnumerator]; (condition = [conditionEnum nextObject]); )
146 if (![condition isKindOfClass:[NSString
class]])
148 OOLog(
@"script.syntax.condition.notString",
@"***** SCRIPT ERROR: in %@, bad condition - expected string, got %@; ignoring.",
StringFromStack(stack), [condition
class]);
156 [result addObject:tokens];
165 if (OK)
return result;
179 NSArray *tokens =
nil;
180 NSUInteger i, tokenCount;
182 NSString *selectorString =
nil;
183 NSString *sanitizedSelectorString =
nil;
184 NSString *comparatorString =
nil;
186 NSMutableArray *rhs =
nil;
187 NSString *rhsItem =
nil;
188 NSString *rhsSelector =
nil;
189 NSArray *sanitizedRHSItem =
nil;
190 NSString *stringSegment =
nil;
193 tokenCount = [tokens count];
197 OOLog(
@"script.debug.syntax.scriptCondition.noneSpecified",
@"***** SCRIPT ERROR: in %@, empty script condition.",
StringFromStack(stack));
202 selectorString = [tokens oo_stringAtIndex:0];
206 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), condition, selectorString);
213 comparatorString = [tokens oo_stringAtIndex:1];
214 if ([comparatorString isEqualToString:
@"equal"]) comparatorValue =
COMPARISON_EQUAL;
215 else if ([comparatorString isEqualToString:
@"notequal"]) comparatorValue =
COMPARISON_NOTEQUAL;
216 else if ([comparatorString isEqualToString:
@"lessthan"]) comparatorValue =
COMPARISON_LESSTHAN;
219 else if ([comparatorString isEqualToString:
@"oneof"]) comparatorValue =
COMPARISON_ONEOF;
220 else if ([comparatorString isEqualToString:
@"undefined"]) comparatorValue =
COMPARISON_UNDEFINED;
223 OOLog(
@"script.debug.syntax.badComparison",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), unknown comparison operator \"%@\", will return NO.",
StringFromStack(stack), condition, comparatorString);
234 OOLog(
@"script.debug.syntax.noOperator",
@"----- WARNING: SCRIPT in %@ -- No operator in expression \"%@\
", will always evaluate as false.",
StringFromStack(stack), condition);
241 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
@"undefined",
@"numbers");
246 switch (comparatorValue)
254 OOLog(
@"script.debug.syntax.invalidOperator",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), comparison operator \"%@\" is not valid for %@.",
StringFromStack(stack), condition,
OOComparisonTypeToString(comparatorValue),
@"booleans");
268 rhs = [NSMutableArray arrayWithCapacity:tokenCount - 2];
269 for (i = 2; i < tokenCount; i++)
271 rhsItem = [tokens oo_stringAtIndex:i];
273 if (rhsSelector !=
nil)
276 if (stringSegment !=
nil)
279 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
280 [rhs addObject:sanitizedRHSItem];
284 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], rhsSelector, nil];
285 [rhs addObject:sanitizedRHSItem];
290 if (stringSegment ==
nil) stringSegment = rhsItem;
291 else stringSegment = [NSString stringWithFormat:@"%@ %@", stringSegment, rhsItem];
295 if (stringSegment !=
nil)
297 sanitizedRHSItem = [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], stringSegment, nil];
298 [rhs addObject:sanitizedRHSItem];
303 rhs = [NSMutableArray array];
306 NSString *rawString =
nil;
307#if INCLUDE_RAW_STRING
308 rawString = condition;
310 rawString =
@"<condition>";
313 return [NSArray arrayWithObjects:
314 [NSNumber numberWithUnsignedInt:opType],
316 sanitizedSelectorString,
317 [NSNumber numberWithUnsignedInt:comparatorValue],
325 NSArray *conditions =
nil;
326 NSArray *doActions =
nil;
327 NSArray *elseActions =
nil;
329 conditions = [statement oo_arrayForKey:@"conditions"];
330 if (conditions ==
nil)
332 OOLog(
@"script.syntax.noConditions",
@"***** SCRIPT ERROR: in %@, conditions array contains no \"conditions\
" entry, ignoring.",
StringFromStack(stack));
339 if (conditions ==
nil)
346 if (doActions !=
nil)
348 subStack.
key =
@"do";
352 elseActions = [statement oo_arrayForKey:@"else"];
353 if (elseActions !=
nil)
355 subStack.
key =
@"else";
360 if ([doActions
count] == 0 && [elseActions
count] == 0)
365 if (doActions ==
nil) doActions = [NSArray array];
366 if (elseActions ==
nil) elseActions = [NSArray array];
368 return [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], conditions, doActions, elseActions, nil];
374 NSMutableArray *tokens =
nil;
375 NSUInteger tokenCount;
376 NSString *rawSelectorString =
nil;
377 NSString *selectorString =
nil;
378 NSString *argument =
nil;
381 tokenCount = [tokens count];
382 if (tokenCount == 0)
return nil;
384 rawSelectorString = [tokens objectAtIndex:0];
386 if (selectorString ==
nil)
388 OOLog(
@"script.unpermittedMethod",
@"***** SCRIPT ERROR: in %@ (\"%@\
"), method \"%@\" not allowed.",
StringFromStack(stack), statement, rawSelectorString);
392 if ([selectorString isEqualToString:
@"doNothing"])
397 if ([selectorString hasSuffix:
@":"])
402 argument = [tokens objectAtIndex:1];
406 [tokens removeObjectAtIndex:0];
407 argument = [tokens componentsJoinedByString:@" "];
410 argument = [argument stringByReplacingOccurrencesOfString:@"[credits_number]" withString:@"[_oo_legacy_credits_number]"];
413 return [NSArray arrayWithObjects:[NSNumber numberWithBool:NO], selectorString, argument, nil];
419 assert(outSanitizedSelector != NULL);
421 *outSanitizedSelector = selectorString;
424 if ([selectorString hasPrefix:
@"mission_"])
return OP_MISSION_VAR;
425 if ([selectorString hasPrefix:
@"local_"])
return OP_LOCAL_VAR;
429 if (*outSanitizedSelector ==
nil)
435 if ([selectorString hasSuffix:
@"_string"])
return OP_STRING;
436 if ([selectorString hasSuffix:
@"_number"])
return OP_NUMBER;
437 if ([selectorString hasSuffix:
@"_bool"])
return OP_BOOL;
440 OOLog(
@"script.sanitize.unclassifiedSelector",
@"***** ERROR: Whitelisted query method \"%@\
" has no type suffix, treating as invalid.", selectorString);
447 static NSSet *whitelist =
nil;
448 static NSDictionary *aliases =
nil;
449 NSString *aliasedSelector =
nil;
451 if (whitelist ==
nil)
457 aliasedSelector = [aliases oo_stringForKey:selectorString];
458 if (aliasedSelector !=
nil) selectorString = aliasedSelector;
460 if (![whitelist containsObject:selectorString]) selectorString =
nil;
462 return selectorString;
468 static NSSet *whitelist =
nil;
469 static NSSet *whitelistWithAI =
nil;
470 static NSDictionary *aliases =
nil;
471 static NSDictionary *aliasesWithAI =
nil;
472 NSString *aliasedSelector =
nil;
474 if (whitelist ==
nil)
476 NSArray *actionMethods =
nil;
477 NSArray *aiMethods =
nil;
478 NSArray *aiAndActionMethods =
nil;
484 if (actionMethods ==
nil) actionMethods = [NSArray array];
485 if (aiMethods ==
nil) aiMethods = [NSArray array];
487 if (aiAndActionMethods !=
nil) actionMethods = [actionMethods arrayByAddingObjectsFromArray:aiAndActionMethods];
489 whitelist = [[NSSet alloc] initWithArray:actionMethods];
490 whitelistWithAI = [[NSSet alloc] initWithArray:[aiMethods arrayByAddingObjectsFromArray:actionMethods]];
495 if (aliasesWithAI !=
nil)
497 aliasesWithAI = [[aliasesWithAI dictionaryByAddingEntriesFromDictionary:aliases] copy];
501 aliasesWithAI = [aliases copy];
505 aliasedSelector = [(allowAIMethods ? aliasesWithAI : aliases) oo_stringForKey:selectorString];
506 if (aliasedSelector !=
nil) selectorString = aliasedSelector;
508 if (![(allowAIMethods ? whitelistWithAI : whitelist) containsObject:selectorString]) selectorString =
nil;
510 return selectorString;