Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOCheckEquipmentPListVerifierStage.m
Go to the documentation of this file.
1/*
2
3OOCheckEquipmentPListVerifierStage.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
27
28#if OO_OXP_VERIFIER_ENABLED
29
31#import "Universe.h"
33
34static NSString * const kStageName = @"Checking equipment.plist";
35
36
37@interface OOCheckEquipmentPListVerifierStage (OOPrivate)
38
39- (void)runCheckWithEquipment:(NSArray *)equipmentPList;
40
41@end
42
43
45
46- (NSString *)name
47{
48 return kStageName;
49}
50
51
52- (BOOL)shouldRun
53{
54 OOFileScannerVerifierStage *fileScanner = nil;
55
56 fileScanner = [[self verifier] fileScannerStage];
57 return [fileScanner fileExists:@"equipment.plist"
58 inFolder:@"Config"
60 checkBuiltIn:NO];
61}
62
63
64- (void)run
65{
66 OOFileScannerVerifierStage *fileScanner = nil;
67 NSArray *equipmentPList = nil;
68
69 fileScanner = [[self verifier] fileScannerStage];
70
71 equipmentPList = [fileScanner plistNamed:@"equipment.plist"
72 inFolder:@"Config"
74 checkBuiltIn:NO];
75
76 if (equipmentPList == nil) return;
77
78 // Check that it's an array
79 if (![equipmentPList isKindOfClass:[NSArray class]])
80 {
81 OOLog(@"verifyOXP.equipmentPList.notArray", @"%@", @"***** ERROR: equipment.plist is not an array.");
82 return;
83 }
84
85
86 [self runCheckWithEquipment:equipmentPList];
87}
88
89@end
90
91
92@implementation OOCheckEquipmentPListVerifierStage (OOPrivate)
93
94- (void)runCheckWithEquipment:(NSArray *)equipmentPList
95{
96 NSEnumerator *entryEnum = nil;
97 NSArray *entry = nil;
98 unsigned entryIndex = 0;
99 NSUInteger elemCount;
100 NSString *name = nil;
101 NSString *entryDesc = nil;
102
103 for (entryEnum = [equipmentPList objectEnumerator]; (entry = [entryEnum nextObject]); )
104 {
105 ++entryIndex;
106
107 // Entries should be arrays.
108 if (![entry isKindOfClass:[NSArray class]])
109 {
110 OOLog(@"verifyOXP.equipmentPList.entryNotArray", @"***** ERROR: equipment.plist entry %u of equipment.plist is not an array.", entryIndex);
111 continue;
112 }
113
114 elemCount = [entry count];
115
116 // Make a name for entry for display purposes.
117 if (EQUIPMENT_KEY_INDEX < elemCount) name = [entry oo_stringAtIndex:EQUIPMENT_KEY_INDEX];
118 else name = nil;
119
120 if (name != nil) entryDesc = [NSString stringWithFormat:@"%u (\"%@\")", entryIndex, name];
121 else entryDesc = [NSString stringWithFormat:@"%u", entryIndex];
122
123 // Check that the entry has an acceptable number of elements.
124 if (elemCount < 5)
125 {
126 OOLog(@"verifyOXP.equipmentPList.badEntrySize", @"***** ERROR: equipment.plist entry %@ has too few elements (%lu, should be 5 or 6).", entryDesc, elemCount);
127 continue;
128 }
129 if (6 < elemCount)
130 {
131 OOLog(@"verifyOXP.equipmentPList.badEntrySize", @"----- WARNING: equipment.plist entry %@ has too many elements (%lu, should be 5 or 6).", entryDesc, elemCount);
132 }
133
134 /* Check element types. The numbers are required to be unsigned
135 integers; the use of a negative default will catch both negative
136 values and unconvertable values.
137 */
138 if ([entry oo_longAtIndex:EQUIPMENT_TECH_LEVEL_INDEX defaultValue:-1] < 0)
139 {
140 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: tech level for entry %@ of equipment.plist is not a positive integer.", entryDesc);
141 }
142 if ([entry oo_longAtIndex:EQUIPMENT_PRICE_INDEX defaultValue:-1] < 0)
143 {
144 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: price for entry %@ of equipment.plist is not a positive integer.", entryDesc);
145 }
146 if ([entry oo_stringAtIndex:EQUIPMENT_SHORT_DESC_INDEX] == nil)
147 {
148 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: short description for entry %@ of equipment.plist is not a string.", entryDesc);
149 }
150 if ([entry oo_stringAtIndex:EQUIPMENT_KEY_INDEX] == nil)
151 {
152 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: key for entry %@ of equipment.plist is not a string.", entryDesc);
153 }
154 if ([entry oo_stringAtIndex:EQUIPMENT_LONG_DESC_INDEX] == nil)
155 {
156 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: long description for entry %@ of equipment.plist is not a string.", entryDesc);
157 }
158
159 if (5 < elemCount)
160 {
161 if ([entry oo_dictionaryAtIndex:EQUIPMENT_EXTRA_INFO_INDEX] == nil)
162 {
163 OOLog(@"verifyOXP.equipmentPList.badElementType", @"***** ERROR: equipment.plist entry %@'s extra information dictionary is not a dictionary.", entryDesc);
164 }
165 // TODO: verify contents of extra info dictionary.
166 }
167 }
168}
169
170@end
171
172#endif
static NSString *const kStageName
static NSString *const kStageName
#define OOLog(class, format,...)
Definition OOLogging.h:88
return nil
@ EQUIPMENT_SHORT_DESC_INDEX
Definition Universe.h:81
@ EQUIPMENT_LONG_DESC_INDEX
Definition Universe.h:83
@ EQUIPMENT_TECH_LEVEL_INDEX
Definition Universe.h:79
@ EQUIPMENT_KEY_INDEX
Definition Universe.h:82
@ EQUIPMENT_PRICE_INDEX
Definition Universe.h:80
@ EQUIPMENT_EXTRA_INFO_INDEX
Definition Universe.h:84
id plistNamed:inFolder:referencedFrom:checkBuiltIn:(NSString *file,[inFolder] NSString *folder,[referencedFrom] NSString *context,[checkBuiltIn] BOOL checkBuiltIn)
BOOL fileExists:inFolder:referencedFrom:checkBuiltIn:(NSString *file,[inFolder] NSString *folder,[referencedFrom] NSString *context,[checkBuiltIn] BOOL checkBuiltIn)