Oolite
Loading...
Searching...
No Matches
OOScript.m
Go to the documentation of this file.
1/*
2
3OOScript.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOScript.h"
26#import "OOJSScript.h"
27#import "OOPListScript.h"
28#import "OOLogging.h"
29#import "Universe.h"
31#import "OOPListParsing.h"
32#import "ResourceManager.h"
33#import "OODebugStandards.h"
34
35
36static NSString * const kOOLogLoadScriptJavaScript = @"script.load.javaScript";
37static NSString * const kOOLogLoadScriptPList = @"script.load.pList";
38static NSString * const kOOLogLoadScriptOK = @"script.load.parseOK";
39static NSString * const kOOLogLoadScriptParseError = @"script.load.parseError";
40static NSString * const kOOLogLoadScriptNone = @"script.load.none";
41
42
43@implementation OOScript
44
45+ (NSArray *)worldScriptsAtPath:(NSString *)path
46{
47 NSFileManager *fmgr = nil;
48 NSString *filePath = nil;
49 NSArray *names = nil;
50 NSArray *result = nil;
51 id script = nil;
52 BOOL foundScript = NO;
53
54 fmgr = [NSFileManager defaultManager];
55
56 // First, look for world-scripts.plist.
57 filePath = [path stringByAppendingPathComponent:@"world-scripts.plist"];
58 if (filePath != nil)
59 {
60 names = OOArrayFromFile(filePath);
61 if (names != nil)
62 {
63 foundScript = YES;
64 result = [self scriptsFromList:names];
65 }
66 }
67
68 // Second, try to load a JavaScript.
69 if (result == nil)
70 {
71 filePath = [path stringByAppendingPathComponent:@"script.js"];
72 if ([fmgr oo_oxzFileExistsAtPath:filePath]) foundScript = YES;
73 else
74 {
75 filePath = [path stringByAppendingPathComponent:@"script.es"];
76 if ([fmgr oo_oxzFileExistsAtPath:filePath]) foundScript = YES;
77 }
78 if (foundScript)
79 {
80 OOLog(kOOLogLoadScriptJavaScript, @"Trying to load JavaScript script %@", filePath);
82
83 script = [OOJSScript scriptWithPath:filePath properties:nil];
84 if (script != nil)
85 {
86 result = [NSArray arrayWithObject:script];
87 OOLog(kOOLogLoadScriptOK, @"Successfully loaded JavaScript script %@", filePath);
88 }
89 else OOLogERR(kOOLogLoadScriptParseError, @"Failed to load JavaScript script %@", filePath);
90
92 }
93 }
94
95 // Third, try to load a plist script.
96 if (result == nil)
97 {
98 filePath = [path stringByAppendingPathComponent:@"script.plist"];
99 if ([fmgr oo_oxzFileExistsAtPath:filePath])
100 {
101 OOStandardsDeprecated([NSString stringWithFormat:@"Legacy script %@ is deprecated",filePath]);
102 if (!OOEnforceStandards())
103 {
104 foundScript = YES;
105 OOLog(kOOLogLoadScriptPList, @"Trying to load property list script %@", filePath);
107
108 result = [OOPListScript scriptsInPListFile:filePath];
109 if (result != nil) OOLog(kOOLogLoadScriptOK, @"Successfully loaded property list script %@", filePath);
110 else OOLogERR(kOOLogLoadScriptParseError, @"Failed to load property list script %@", filePath);
111
113 }
114 }
115 }
116
117 if (result == nil && foundScript)
118 {
119 OOLog(kOOLogLoadScriptNone, @"No script could be loaded from %@", path);
120 }
121
122 return result;
123}
124
125
126+ (NSArray *)scriptsFromFileNamed:(NSString *)fileName
127{
128 NSArray *result = nil;
129 NSString *path = [ResourceManager pathForFileNamed:fileName inFolder:@"Scripts"];
130 if (path != nil)
131 {
132 result = [self scriptsFromFileAtPath:path];
133 }
134
135 if (result == nil)
136 {
137 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
138 }
139
140 return result;
141}
142
143
144+ (NSArray *)scriptsFromList:(NSArray *)fileNames
145{
146 NSString *name = nil;
147 NSMutableArray *result = nil;
148 NSArray *scripts = nil;
149
150 result = [NSMutableArray arrayWithCapacity:[fileNames count]];
151
152 foreach (name, fileNames)
153 {
154 scripts = [self scriptsFromFileNamed:name];
155 if (scripts != nil) [result addObjectsFromArray:scripts];
156 }
157
158 return result;
159}
160
161
162+ (NSArray *)scriptsFromFileAtPath:(NSString *)filePath
163{
164 // oo_oxzFile always returns false for directories
165 if (![[NSFileManager defaultManager] oo_oxzFileExistsAtPath:filePath]) return nil;
166
167 NSString *extension = [[filePath pathExtension] lowercaseString];
168
169 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
170 {
171 NSArray *result = nil;
172 OOScript *script = [OOJSScript scriptWithPath:filePath properties:nil];
173 if (script != nil) result = [NSArray arrayWithObject:script];
174 return result;
175 }
176 else if ([extension isEqualToString:@"plist"])
177 {
178 OOStandardsDeprecated([NSString stringWithFormat:@"Legacy script %@ is deprecated",filePath]);
179 if (OOEnforceStandards())
180 {
181 return nil;
182 }
183 return [OOPListScript scriptsInPListFile:filePath];
184 }
185
186 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", filePath);
187 return nil;
188}
189
190
191+ (id)jsScriptFromFileNamed:(NSString *)fileName properties:(NSDictionary *)properties
192{
193 NSString *extension = nil;
194 NSString *path = nil;
195
196 if ([fileName length] == 0) return nil;
197
198 extension = [[fileName pathExtension] lowercaseString];
199 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
200 {
201 path = [ResourceManager pathForFileNamed:fileName inFolder:@"Scripts"];
202 if (path == nil)
203 {
204 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
205 return nil;
206 }
207 return [OOJSScript scriptWithPath:path properties:properties];
208 }
209 else if ([extension isEqualToString:@"plist"])
210 {
211 OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
212 return nil;
213 }
214
215 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
216 return nil;
217}
218
219
220+ (id)jsAIScriptFromFileNamed:(NSString *)fileName properties:(NSDictionary *)properties
221{
222 NSString *extension = nil;
223 NSString *path = nil;
224
225 if ([fileName length] == 0) return nil;
226
227 extension = [[fileName pathExtension] lowercaseString];
228 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
229 {
230 path = [ResourceManager pathForFileNamed:fileName inFolder:@"AIs"];
231 if (path == nil)
232 {
233 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
234 return nil;
235 }
236 return [OOJSScript scriptWithPath:path properties:properties];
237 }
238 else if ([extension isEqualToString:@"plist"])
239 {
240 OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
241 return nil;
242 }
243
244 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
245 return nil;
246}
247
248
249- (NSString *)descriptionComponents
250{
251 return [NSString stringWithFormat:@"\"%@\" version %@", [self name], [self version]];
252}
253
254
255- (NSString *)name
256{
257 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
258 return nil;
259}
260
261
262- (NSString *)scriptDescription
263{
264 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
265 return nil;
266}
267
268
269- (NSString *)version
270{
271 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
272 return nil;
273}
274
275
276- (NSString *)displayName
277{
278 NSString *name = [self name];
279 NSString *version = [self version];
280
281 if (version != nil) return [NSString stringWithFormat:@"%@ %@", name, version];
282 else if (name != nil) return [NSString stringWithFormat:@"%@", name];
283 else return nil;
284}
285
286
287- (BOOL) requiresTickle
288{
289 return NO;
290}
291
292
293- (void)runWithTarget:(Entity *)target
294{
295 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
296}
297
298@end
void OOStandardsDeprecated(NSString *message)
BOOL OOEnforceStandards(void)
#define OOLogERR(class, format,...)
Definition OOLogging.h:112
#define OOLogOutdentIf(class)
Definition OOLogging.h:102
#define OOLog(class, format,...)
Definition OOLogging.h:88
#define OOLogIndentIf(class)
Definition OOLogging.h:101
NSString *const kOOLogSubclassResponsibility
Definition OOLogging.m:646
NSArray * OOArrayFromFile(NSString *path)
return nil
static NSString *const kOOLogLoadScriptPList
Definition OOScript.m:37
static NSString *const kOOLogLoadScriptNone
Definition OOScript.m:40
static NSString *const kOOLogLoadScriptOK
Definition OOScript.m:38
static NSString *const kOOLogLoadScriptParseError
Definition OOScript.m:39
static NSString *const kOOLogLoadScriptJavaScript
Definition OOScript.m:36
id scriptWithPath:properties:(NSString *path,[properties] NSDictionary *properties)
Definition OOJSScript.m:112
NSArray * scriptsInPListFile:(NSString *filePath)
NSString * name()
Definition OOScript.m:255
NSArray * scriptsFromList:(NSArray *fileNames)
Definition OOScript.m:144
NSString * version()
Definition OOScript.m:269
BOOL requiresTickle()
Definition OOScript.m:287
NSString * scriptDescription()
Definition OOScript.m:262
NSString * descriptionComponents()
Definition OOScript.m:249
NSString * displayName()
Definition OOScript.m:276
NSString * pathForFileNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)