Oolite
Loading...
Searching...
No Matches
OOAIStateMachineVerifierStage.m
Go to the documentation of this file.
1/*
2
3OOAIStateMachineVerifierStage.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
28#import "OOPListParsing.h"
29
30#if OO_OXP_VERIFIER_ENABLED
31
32#import "ResourceManager.h"
33
34static NSString* const kStageName = @"Validating AIs";
35
36@interface OOAIStateMachineVerifierStage (Private)
37
38- (void)validateAI:(NSString*)aiName;
39
40@end
41
43
44- (void)dealloc
45{
46 [_whitelist release];
47 [_usedAIs release];
48
49 [super dealloc];
50}
51
52- (NSString*)name
53{
54 return kStageName;
55}
56
57- (BOOL)shouldRun
58{
59 return [_usedAIs count] != 0;
60}
61
62- (void)run
63{
64 NSArray* aiNames = nil;
65 NSString* aiName = nil;
66 NSMutableSet* whitelist = nil;
67
68 // Build whitelist. Note that we merge in aliases since the distinction doesn't matter when just validating.
69 whitelist = [[NSMutableSet alloc] init];
70 [whitelist addObjectsFromArray:[[ResourceManager whitelistDictionary] oo_arrayForKey:@"ai_methods"]];
71 [whitelist addObjectsFromArray:[[ResourceManager whitelistDictionary] oo_arrayForKey:@"ai_and_action_methods"]];
72 [whitelist addObjectsFromArray:[[[ResourceManager whitelistDictionary] oo_dictionaryForKey:@"ai_method_aliases"] allKeys]];
73 _whitelist = [whitelist copy];
74 [whitelist release];
75
76 aiNames = [[_usedAIs allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
77 foreach (aiName, aiNames) {
78 [self validateAI:aiName];
79 }
80
81 [_whitelist release];
83}
84
85+ (NSString*)nameForReverseDependencyForVerifier:(OOOXPVerifier*)verifier
86{
87 return kStageName;
88}
89
90- (void)stateMachineNamed:(NSString*)name usedByShip:(NSString*)shipName
91{
92 OOFileScannerVerifierStage* fileScanner = nil;
93
94 if (name == nil)
95 return;
96 if ([_usedAIs containsObject:name])
97 return;
98 if (_usedAIs == nil)
99 _usedAIs = [[NSMutableSet alloc] init];
100 [_usedAIs addObject:name];
101
102 fileScanner = [[self verifier] fileScannerStage];
103 if (![fileScanner fileExists:name
104 inFolder:@"AIs"
105 referencedFrom:[NSString stringWithFormat:@"shipdata.plist entry \"%@\"", shipName]
106 checkBuiltIn:YES]) {
107 OOLog(@"verifyOXP.validateAI.notFound", @"----- WARNING: AI state machine \"%@\" referenced in shipdata.plist entry \"%@\" could not be found in %@ or in Oolite.", name, shipName, [[self verifier] oxpDisplayName]);
108 }
109}
110
111@end
112
113@implementation OOAIStateMachineVerifierStage (Private)
114
115- (void)validateAI:(NSString*)aiName
116{
117 NSString* path = nil;
118 NSDictionary* aiStateMachine = nil;
119 NSString* stateKey = nil;
120 NSDictionary* stateHandlers = nil;
121 NSString* handlerKey = nil;
122 NSArray* handlerActions = nil;
123 NSString* action = nil;
124 NSRange spaceRange;
125 NSString* selector = nil;
126 NSMutableSet* badSelectors = nil;
127 NSString* badSelectorDesc = nil;
128 NSUInteger index = 0;
129
130 OOLog(@"verifyOXP.verbose.validateAI", @"- Validating AI \"%@\".", aiName);
131 OOLogIndentIf(@"verifyOXP.verbose.validateAI");
132
133 // Attempt to load AI.
134 path = [[[self verifier] fileScannerStage] pathForFile:aiName inFolder:@"AIs" referencedFrom:@"AI list" checkBuiltIn:NO];
135 if (path == nil)
136 return;
137
138 badSelectors = [NSMutableSet set];
139
140 aiStateMachine = OODictionaryFromFile(path);
141 if (aiStateMachine == nil) {
142 OOLog(@"verifyOXP.validateAI.failed.notDictPlist", @"***** ERROR: could not interpret \"%@\" as a dictionary.", path);
143 return;
144 }
145
146 // Validate each state.
147 foreachkey(stateKey, aiStateMachine)
148 {
149 stateHandlers = [aiStateMachine objectForKey:stateKey];
150 if (![stateHandlers isKindOfClass:[NSDictionary class]]) {
151 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.state", @"***** ERROR: state \"%@\" in AI \"%@\" is not a dictionary.", stateKey, aiName);
152 continue;
153 }
154
155 // Verify handlers for this state.
156 foreachkey(handlerKey, stateHandlers)
157 {
158 handlerActions = [stateHandlers objectForKey:handlerKey];
159 if (![handlerActions isKindOfClass:[NSArray class]]) {
160 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.handler", @"***** ERROR: handler \"%@\" for state \"%@\" in AI \"%@\" is not an array, ignoring.", handlerKey, stateKey, aiName);
161 continue;
162 }
163
164 // Verify commands for this handler.
165 index = 0;
166 foreach (action, handlerActions) {
167 index++;
168 if (![action isKindOfClass:[NSString class]]) {
169 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.action", @"***** ERROR: action %zu in handler \"%@\" for state \"%@\" in AI \"%@\" is not a string, ignoring.", index - 1, handlerKey, stateKey, aiName);
170 continue;
171 }
172
173 // Trim spaces from beginning and end.
174 action = [action stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
175
176 // Cut off parameters.
177 spaceRange = [action rangeOfString:@" "];
178 if (spaceRange.location == NSNotFound)
179 selector = action;
180 else
181 selector = [action substringToIndex:spaceRange.location];
182
183 // Check against whitelist.
184 if (![_whitelist containsObject:selector]) {
185 [badSelectors addObject:selector];
186 }
187 }
188 }
189 }
190
191 if ([badSelectors count] != 0) {
192 badSelectorDesc = [[[badSelectors allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] componentsJoinedByString:@", "];
193 OOLog(@"verifyOXP.validateAI.failed.badSelector", @"***** ERROR: the AI \"%@\" uses %zu unpermitted method%s: %@", aiName, [badSelectors count], ([badSelectors count] == 1) ? "" : "s", badSelectorDesc);
194 }
195
196 OOLogOutdentIf(@"verifyOXP.verbose.validateAI");
197}
198
199@end
200
201#endif
static NSString *const kStageName
#define foreachkey(VAR, DICT)
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define OOLogIndentIf(class)
Definition OOLogging.h:107
NSDictionary * OODictionaryFromFile(NSString *path)
unsigned count
return nil
NSString * pathForFile:inFolder:referencedFrom:checkBuiltIn:(NSString *file,[inFolder] NSString *folder,[referencedFrom] NSString *context,[checkBuiltIn] BOOL checkBuiltIn)
OOOXPVerifier * verifier()
OOFileScannerVerifierStage * fileScannerStage()
NSDictionary * whitelistDictionary()