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 "OODebugStandards.h"
27// Angle brackets so the engine-specific headers come from the include path
28// (QuickJS/ first when building with QuickJS-ng), not this directory.
29#import "OOLogging.h"
30#import "OOPListParsing.h"
31#import "OOPListScript.h"
32#import "ResourceManager.h"
33#import "Universe.h"
34#import <OOJSScript.h>
36
37static NSString* const kOOLogLoadScriptJavaScript = @"script.load.javaScript";
38static NSString* const kOOLogLoadScriptPList = @"script.load.pList";
39static NSString* const kOOLogLoadScriptOK = @"script.load.parseOK";
40static NSString* const kOOLogLoadScriptParseError = @"script.load.parseError";
41static NSString* const kOOLogLoadScriptNone = @"script.load.none";
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 names = OOArrayFromFile(filePath);
60 if (names != nil) {
61 foundScript = YES;
62 result = [self scriptsFromList:names];
63 }
64 }
65
66 // Second, try to load a JavaScript.
67 if (result == nil) {
68 filePath = [path stringByAppendingPathComponent:@"script.js"];
69 if ([fmgr oo_oxzFileExistsAtPath:filePath])
70 foundScript = YES;
71 else {
72 filePath = [path stringByAppendingPathComponent:@"script.es"];
73 if ([fmgr oo_oxzFileExistsAtPath:filePath])
74 foundScript = YES;
75 }
76 if (foundScript) {
77 OOLog(kOOLogLoadScriptJavaScript, @"Trying to load JavaScript script %@", filePath);
79
80 script = [OOJSScript scriptWithPath:filePath properties:nil];
81 if (script != nil) {
82 result = [NSArray arrayWithObject:script];
83 OOLog(kOOLogLoadScriptOK, @"Successfully loaded JavaScript script %@", filePath);
84 } else
85 OOLogERR(kOOLogLoadScriptParseError, @"Failed to load JavaScript script %@", filePath);
86
88 }
89 }
90
91 // Third, try to load a plist script.
92 if (result == nil) {
93 filePath = [path stringByAppendingPathComponent:@"script.plist"];
94 if ([fmgr oo_oxzFileExistsAtPath:filePath]) {
95 OOStandardsDeprecated([NSString stringWithFormat:@"Legacy script %@ is deprecated", filePath]);
96 if (!OOEnforceStandards()) {
97 foundScript = YES;
98 OOLog(kOOLogLoadScriptPList, @"Trying to load property list script %@", filePath);
100
101 result = [OOPListScript scriptsInPListFile:filePath];
102 if (result != nil)
103 OOLog(kOOLogLoadScriptOK, @"Successfully loaded property list script %@", filePath);
104 else
105 OOLogERR(kOOLogLoadScriptParseError, @"Failed to load property list script %@", filePath);
106
108 }
109 }
110 }
111
112 if (result == nil && foundScript) {
113 OOLog(kOOLogLoadScriptNone, @"No script could be loaded from %@", path);
114 }
115
116 return result;
117}
118
119+ (NSArray*)scriptsFromFileNamed:(NSString*)fileName
120{
121 NSArray* result = nil;
122 NSString* path = [ResourceManager pathForFileNamed:fileName inFolder:@"Scripts"];
123 if (path != nil) {
124 result = [self scriptsFromFileAtPath:path];
125 }
126
127 if (result == nil) {
128 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
129 }
130
131 return result;
132}
133
134+ (NSArray*)scriptsFromList:(NSArray*)fileNames
135{
136 NSString* name = nil;
137 NSMutableArray* result = nil;
138 NSArray* scripts = nil;
139
140 result = [NSMutableArray arrayWithCapacity:[fileNames count]];
141
142 foreach (name, fileNames) {
143 scripts = [self scriptsFromFileNamed:name];
144 if (scripts != nil)
145 [result addObjectsFromArray:scripts];
146 }
147
148 return result;
149}
150
151+ (NSArray*)scriptsFromFileAtPath:(NSString*)filePath
152{
153 // oo_oxzFile always returns false for directories
154 if (![[NSFileManager defaultManager] oo_oxzFileExistsAtPath:filePath])
155 return nil;
156
157 NSString* extension = [[filePath pathExtension] lowercaseString];
158
159 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"]) {
160 NSArray* result = nil;
161 OOScript* script = [OOJSScript scriptWithPath:filePath properties:nil];
162 if (script != nil)
163 result = [NSArray arrayWithObject:script];
164 return result;
165 } else if ([extension isEqualToString:@"plist"]) {
166 OOStandardsDeprecated([NSString stringWithFormat:@"Legacy script %@ is deprecated", filePath]);
167 if (OOEnforceStandards()) {
168 return nil;
169 }
170 return [OOPListScript scriptsInPListFile:filePath];
171 }
172
173 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", filePath);
174 return nil;
175}
176
177+ (id)jsScriptFromFileNamed:(NSString*)fileName properties:(NSDictionary*)properties
178{
179 NSString* extension = nil;
180 NSString* path = nil;
181
182 if ([fileName length] == 0)
183 return nil;
184
185 extension = [[fileName pathExtension] lowercaseString];
186 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"]) {
187 path = [ResourceManager pathForFileNamed:fileName inFolder:@"Scripts"];
188 if (path == nil) {
189 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
190 return nil;
191 }
192 return [OOJSScript scriptWithPath:path properties:properties];
193 } else if ([extension isEqualToString:@"plist"]) {
194 OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
195 return nil;
196 }
197
198 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
199 return nil;
200}
201
202+ (id)jsAIScriptFromFileNamed:(NSString*)fileName properties:(NSDictionary*)properties
203{
204 NSString* extension = nil;
205 NSString* path = nil;
206
207 if ([fileName length] == 0)
208 return nil;
209
210 extension = [[fileName pathExtension] lowercaseString];
211 if ([extension isEqualToString:@"js"] || [extension isEqualToString:@"es"]) {
212 path = [ResourceManager pathForFileNamed:fileName inFolder:@"AIs"];
213 if (path == nil) {
214 OOLogERR(@"script.load.notFound", @"Could not find script file %@.", fileName);
215 return nil;
216 }
217 return [OOJSScript scriptWithPath:path properties:properties];
218 } else if ([extension isEqualToString:@"plist"]) {
219 OOLogERR(@"script.load.badName", @"Can't load script named %@ - legacy scripts are not supported in this context.", fileName);
220 return nil;
221 }
222
223 OOLogERR(@"script.load.badName", @"Don't know how to load a script from %@.", fileName);
224 return nil;
225}
226
228{
229 return [NSString stringWithFormat:@"\"%@\" version %@", [self name], [self version]];
230}
231
232- (NSString*)name
233{
234 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
235 return nil;
236}
237
239{
240 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
241 return nil;
242}
243
244- (NSString*)version
245{
246 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
247 return nil;
248}
249
250- (NSString*)displayName
251{
252 NSString* name = [self name];
253 NSString* version = [self version];
254
255 if (version != nil)
256 return [NSString stringWithFormat:@"%@ %@", name, version];
257 else if (name != nil)
258 return [NSString stringWithFormat:@"%@", name];
259 else
260 return nil;
261}
262
264{
265 return NO;
266}
267
268- (void)runWithTarget:(Entity*)target
269{
270 OOLogERR(kOOLogSubclassResponsibility, @"%@", @"OOScript should not be used directly!");
271}
272
273@end
void OOStandardsDeprecated(NSString *message)
BOOL OOEnforceStandards(void)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define OOLogIndentIf(class)
Definition OOLogging.h:107
NSString *const kOOLogSubclassResponsibility
Definition OOLogging.m:595
NSArray * OOArrayFromFile(NSString *path)
return nil
static NSString *const kOOLogLoadScriptPList
Definition OOScript.m:38
static NSString *const kOOLogLoadScriptNone
Definition OOScript.m:41
static NSString *const kOOLogLoadScriptOK
Definition OOScript.m:39
static NSString *const kOOLogLoadScriptParseError
Definition OOScript.m:40
static NSString *const kOOLogLoadScriptJavaScript
Definition OOScript.m:37
id scriptWithPath:properties:(NSString *path,[properties] NSDictionary *properties)
Definition OOJSScript.m:89
NSArray * scriptsInPListFile:(NSString *filePath)
NSString * name()
Definition OOScript.m:232
NSArray * scriptsFromList:(NSArray *fileNames)
Definition OOScript.m:134
NSString * version()
Definition OOScript.m:244
BOOL requiresTickle()
Definition OOScript.m:263
NSString * scriptDescription()
Definition OOScript.m:238
NSString * descriptionComponents()
Definition OOScript.m:227
NSString * displayName()
Definition OOScript.m:250
NSArray * scriptsFromFileNamed:(NSString *fileName)
Definition OOScript.m:119
NSArray * scriptsFromFileAtPath:(NSString *filePath)
Definition OOScript.m:151
NSString * pathForFileNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)