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
37@interface OOAIStateMachineVerifierStage (Private)
38
39- (void) validateAI:(NSString *)aiName;
40
41@end
42
43
45
46- (void) dealloc
47{
48 [_whitelist release];
49 [_usedAIs release];
50
51 [super dealloc];
52}
53
54
55- (NSString *) name
56{
57 return kStageName;
58}
59
60
61- (BOOL) shouldRun
62{
63 return [_usedAIs count] != 0;
64}
65
66
67- (void) run
68{
69 NSArray *aiNames = nil;
70 NSString *aiName = nil;
71 NSMutableSet *whitelist = nil;
72
73 // Build whitelist. Note that we merge in aliases since the distinction doesn't matter when just validating.
74 whitelist = [[NSMutableSet alloc] init];
75 [whitelist addObjectsFromArray:[[ResourceManager whitelistDictionary] oo_arrayForKey:@"ai_methods"]];
76 [whitelist addObjectsFromArray:[[ResourceManager whitelistDictionary] oo_arrayForKey:@"ai_and_action_methods"]];
77 [whitelist addObjectsFromArray:[[[ResourceManager whitelistDictionary] oo_dictionaryForKey:@"ai_method_aliases"] allKeys]];
78 _whitelist = [whitelist copy];
79 [whitelist release];
80
81 aiNames = [[_usedAIs allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
82 foreach (aiName, aiNames)
83 {
84 [self validateAI:aiName];
85 }
86
87 [_whitelist release];
88 _whitelist = nil;
89}
90
91
92+ (NSString *) nameForReverseDependencyForVerifier:(OOOXPVerifier *)verifier
93{
94 return kStageName;
95}
96
97
98- (void) stateMachineNamed:(NSString *)name usedByShip:(NSString *)shipName
99{
100 OOFileScannerVerifierStage *fileScanner = nil;
101
102 if (name == nil) return;
103 if ([_usedAIs containsObject:name]) return;
104 if (_usedAIs == nil) _usedAIs = [[NSMutableSet alloc] init];
105 [_usedAIs addObject:name];
106
107 fileScanner = [[self verifier] fileScannerStage];
108 if (![fileScanner fileExists:name
109 inFolder:@"AIs"
110 referencedFrom:[NSString stringWithFormat:@"shipdata.plist entry \"%@\"", shipName]
111 checkBuiltIn:YES])
112 {
113 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]);
114 }
115}
116
117@end
118
119
120@implementation OOAIStateMachineVerifierStage (Private)
121
122- (void) validateAI:(NSString *)aiName
123{
124 NSString *path = nil;
125 NSDictionary *aiStateMachine = nil;
126 NSString *stateKey = nil;
127 NSDictionary *stateHandlers = nil;
128 NSString *handlerKey = nil;
129 NSArray *handlerActions = nil;
130 NSString *action = nil;
131 NSRange spaceRange;
132 NSString *selector = nil;
133 NSMutableSet *badSelectors = nil;
134 NSString *badSelectorDesc = nil;
135 NSUInteger index = 0;
136
137 OOLog(@"verifyOXP.verbose.validateAI", @"- Validating AI \"%@\".", aiName);
138 OOLogIndentIf(@"verifyOXP.verbose.validateAI");
139
140 // Attempt to load AI.
141 path = [[[self verifier] fileScannerStage] pathForFile:aiName inFolder:@"AIs" referencedFrom:@"AI list" checkBuiltIn:NO];
142 if (path == nil) return;
143
144 badSelectors = [NSMutableSet set];
145
146 aiStateMachine = OODictionaryFromFile(path);
147 if (aiStateMachine == nil)
148 {
149 OOLog(@"verifyOXP.validateAI.failed.notDictPlist", @"***** ERROR: could not interpret \"%@\" as a dictionary.", path);
150 return;
151 }
152
153 // Validate each state.
154 foreachkey (stateKey, aiStateMachine)
155 {
156 stateHandlers = [aiStateMachine objectForKey:stateKey];
157 if (![stateHandlers isKindOfClass:[NSDictionary class]])
158 {
159 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.state", @"***** ERROR: state \"%@\" in AI \"%@\" is not a dictionary.", stateKey, aiName);
160 continue;
161 }
162
163 // Verify handlers for this state.
164 foreachkey (handlerKey, stateHandlers)
165 {
166 handlerActions = [stateHandlers objectForKey:handlerKey];
167 if (![handlerActions isKindOfClass:[NSArray class]])
168 {
169 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.handler", @"***** ERROR: handler \"%@\" for state \"%@\" in AI \"%@\" is not an array, ignoring.", handlerKey, stateKey, aiName);
170 continue;
171 }
172
173 // Verify commands for this handler.
174 index = 0;
175 foreach (action, handlerActions)
176 {
177 index++;
178 if (![action isKindOfClass:[NSString class]])
179 {
180 OOLog(@"verifyOXP.validateAI.failed.invalidFormat.action", @"***** ERROR: action %llu in handler \"%@\" for state \"%@\" in AI \"%@\" is not a string, ignoring.", index - 1, handlerKey, stateKey, aiName);
181 continue;
182 }
183
184 // Trim spaces from beginning and end.
185 action = [action stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
186
187 // Cut off parameters.
188 spaceRange = [action rangeOfString:@" "];
189 if (spaceRange.location == NSNotFound) selector = action;
190 else selector = [action substringToIndex:spaceRange.location];
191
192 // Check against whitelist.
193 if (![_whitelist containsObject:selector])
194 {
195 [badSelectors addObject:selector];
196 }
197 }
198 }
199 }
200
201 if ([badSelectors count] != 0)
202 {
203 badSelectorDesc = [[[badSelectors allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] componentsJoinedByString:@", "];
204 OOLog(@"verifyOXP.validateAI.failed.badSelector", @"***** ERROR: the AI \"%@\" uses %llu unpermitted method%s: %@", aiName, [badSelectors count], ([badSelectors count] == 1) ? "" : "s", badSelectorDesc);
205 }
206
207 OOLogOutdentIf(@"verifyOXP.verbose.validateAI");
208}
209
210@end
211
212#endif
static NSString *const kStageName
#define foreachkey(VAR, DICT)
Definition OOCocoa.h:353
#define OOLogOutdentIf(class)
Definition OOLogging.h:102
#define OOLog(class, format,...)
Definition OOLogging.h:88
#define OOLogIndentIf(class)
Definition OOLogging.h:101
NSDictionary * OODictionaryFromFile(NSString *path)
unsigned count
return nil
NSDictionary * whitelistDictionary()