Line data Source code
1 0 : /*
2 :
3 : OOScript.m
4 :
5 : Oolite
6 : Copyright (C) 2004-2013 Giles C Williams and contributors
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License
10 : as published by the Free Software Foundation; either version 2
11 : of the License, or (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 : MA 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"
30 : #import "OOJavaScriptEngine.h"
31 : #import "OOPListParsing.h"
32 : #import "ResourceManager.h"
33 : #import "OODebugStandards.h"
34 :
35 :
36 0 : static NSString * const kOOLogLoadScriptJavaScript = @"script.load.javaScript";
37 0 : static NSString * const kOOLogLoadScriptPList = @"script.load.pList";
38 0 : static NSString * const kOOLogLoadScriptOK = @"script.load.parseOK";
39 0 : static NSString * const kOOLogLoadScriptParseError = @"script.load.parseError";
40 0 : static 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);
81 : OOLogIndentIf(kOOLogLoadScriptJavaScript);
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 :
91 : OOLogOutdentIf(kOOLogLoadScriptJavaScript);
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);
106 : OOLogIndentIf(kOOLogLoadScriptPList);
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 :
112 : OOLogOutdentIf(kOOLogLoadScriptPList);
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 : NSEnumerator *nameEnum = nil;
147 : NSString *name = nil;
148 : NSMutableArray *result = nil;
149 : NSArray *scripts = nil;
150 :
151 : result = [NSMutableArray arrayWithCapacity:[fileNames count]];
152 :
153 : for (nameEnum = [fileNames objectEnumerator]; (name = [nameEnum nextObject]); )
154 : {
155 : scripts = [self scriptsFromFileNamed:name];
156 : if (scripts != nil) [result addObjectsFromArray:scripts];
157 : }
158 :
159 : return result;
160 : }
161 :
162 :
163 : + (NSArray *)scriptsFromFileAtPath:(NSString *)filePath
164 : {
165 : // oo_oxzFile always returns false for directories
166 : if (![[NSFileManager defaultManager] oo_oxzFileExistsAtPath:filePath]) return nil;
167 :
168 : NSString *extension = [[filePath pathExtension] lowercaseString];
169 :
170 : if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
171 : {
172 : NSArray *result = nil;
173 : OOScript *script = [OOJSScript scriptWithPath:filePath properties:nil];
174 : if (script != nil) result = [NSArray arrayWithObject:script];
175 : return result;
176 : }
177 : else if ([extension isEqualToString:@"plist"])
178 : {
179 : OOStandardsDeprecated([NSString stringWithFormat:@"Legacy script %@ is deprecated",filePath]);
180 : if (OOEnforceStandards())
181 : {
182 : return nil;
183 : }
184 : return [OOPListScript scriptsInPListFile:filePath];
185 : }
186 :
187 : OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", filePath);
188 : return nil;
189 : }
190 :
191 :
192 : + (id)jsScriptFromFileNamed:(NSString *)fileName properties:(NSDictionary *)properties
193 : {
194 : NSString *extension = nil;
195 : NSString *path = nil;
196 :
197 : if ([fileName length] == 0) return nil;
198 :
199 : extension = [[fileName pathExtension] lowercaseString];
200 : if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
201 : {
202 : path = [ResourceManager pathForFileNamed:fileName inFolder:@"Scripts"];
203 : if (path == nil)
204 : {
205 : OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
206 : return nil;
207 : }
208 : return [OOJSScript scriptWithPath:path properties:properties];
209 : }
210 : else if ([extension isEqualToString:@"plist"])
211 : {
212 : OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
213 : return nil;
214 : }
215 :
216 : OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
217 : return nil;
218 : }
219 :
220 :
221 : + (id)jsAIScriptFromFileNamed:(NSString *)fileName properties:(NSDictionary *)properties
222 : {
223 : NSString *extension = nil;
224 : NSString *path = nil;
225 :
226 : if ([fileName length] == 0) return nil;
227 :
228 : extension = [[fileName pathExtension] lowercaseString];
229 : if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"])
230 : {
231 : path = [ResourceManager pathForFileNamed:fileName inFolder:@"AIs"];
232 : if (path == nil)
233 : {
234 : OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
235 : return nil;
236 : }
237 : return [OOJSScript scriptWithPath:path properties:properties];
238 : }
239 : else if ([extension isEqualToString:@"plist"])
240 : {
241 : OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
242 : return nil;
243 : }
244 :
245 : OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
246 : return nil;
247 : }
248 :
249 :
250 0 : - (NSString *)descriptionComponents
251 : {
252 : return [NSString stringWithFormat:@"\"%@\" version %@", [self name], [self version]];
253 : }
254 :
255 :
256 : - (NSString *)name
257 : {
258 : OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
259 : return nil;
260 : }
261 :
262 :
263 : - (NSString *)scriptDescription
264 : {
265 : OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
266 : return nil;
267 : }
268 :
269 :
270 : - (NSString *)version
271 : {
272 : OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
273 : return nil;
274 : }
275 :
276 :
277 : - (NSString *)displayName
278 : {
279 : NSString *name = [self name];
280 : NSString *version = [self version];
281 :
282 : if (version != nil) return [NSString stringWithFormat:@"%@ %@", name, version];
283 : else if (name != nil) return [NSString stringWithFormat:@"%@", name];
284 : else return nil;
285 : }
286 :
287 :
288 : - (BOOL) requiresTickle
289 : {
290 : return NO;
291 : }
292 :
293 :
294 : - (void)runWithTarget:(Entity *)target
295 : {
296 : OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
297 : }
298 :
299 : @end
|