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