Oolite
Loading...
Searching...
No Matches
OOPListScript.m
Go to the documentation of this file.
1/*
2
3OOPListScript.h
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 "OOPListScript.h"
26#import "OOCacheManager.h"
29#import "OOPListParsing.h"
31
32static NSString* const kMDKeyName = @"name";
33static NSString* const kMDKeyDescription = @"description";
34static NSString* const kMDKeyVersion = @"version";
35static NSString* const kKeyMetadata = @"!metadata!";
36static NSString* const kKeyScript = @"script";
37
38static NSString* const kCacheName = @"sanitized legacy scripts";
39
40@interface OOPListScript (SetUp)
41
42+ (NSArray*)scriptsFromDictionaryOfScripts:(NSDictionary*)dictionary filePath:(NSString*)filePath;
43+ (NSArray*)loadCachedScripts:(NSDictionary*)cachedScripts;
44- (id)initWithName:(NSString*)name scriptArray:(NSArray*)script metadata:(NSDictionary*)metadata;
45
46@end
47
48@implementation OOPListScript
49
50+ (NSArray*)scriptsInPListFile:(NSString*)filePath
51{
52 NSDictionary* cachedScripts = [[OOCacheManager sharedCache] objectForKey:filePath inCache:kCacheName];
53 if (cachedScripts != nil) {
54 return [self loadCachedScripts:cachedScripts];
55 } else {
56 NSDictionary* dict = OODictionaryFromFile(filePath);
57 if (dict == nil)
58 return nil;
59 return [self scriptsFromDictionaryOfScripts:dict filePath:filePath];
60 }
61}
62
63- (void)dealloc
64{
65 [_script release];
66 [_metadata release];
67
68 [super dealloc];
69}
70
71- (NSString*)name
72{
73 return [_metadata objectForKey:kMDKeyName];
74}
75
77{
78 return [_metadata objectForKey:kMDKeyDescription];
79}
80
81- (NSString*)version
82{
83 return [_metadata objectForKey:kMDKeyVersion];
84}
85
87{
88 return YES;
89}
90
91- (void)runWithTarget:(Entity*)target
92{
93 if (target != nil && ![target isKindOfClass:[ShipEntity class]]) {
94 OOLog(@"script.legacy.run.badTarget", @"Expected ShipEntity or nil for target, got %@.", [target class]);
95 return;
96 }
97
98 OOLog(@"script.legacy.run", @"Running script %@", [self displayName]);
99 OOLogIndentIf(@"script.legacy.run");
100
101 [PLAYER runScriptActions:_script
102 withContextName:[self name]
103 forTarget:(ShipEntity*)target];
104
105 OOLogOutdentIf(@"script.legacy.run");
106}
107
108@end
109
110@implementation OOPListScript (SetUp)
111
112+ (NSArray*)scriptsFromDictionaryOfScripts:(NSDictionary*)dictionary filePath:(NSString*)filePath
113{
114 NSMutableArray* result = nil;
115 NSString* key = nil;
116 NSArray* scriptArray = nil;
117 NSDictionary* metadata = nil;
118 NSMutableDictionary* cachedScripts = nil;
119 OOPListScript* script = nil;
120
121 NSUInteger count = [dictionary count];
122 result = [NSMutableArray arrayWithCapacity:count];
123 cachedScripts = [NSMutableDictionary dictionaryWithCapacity:count];
124
125 metadata = [dictionary objectForKey:kKeyMetadata];
126 if (![metadata isKindOfClass:[NSDictionary class]])
127 metadata = nil;
128
129 foreachkey(key, dictionary)
130 {
131 scriptArray = [dictionary objectForKey:key];
132 if ([key isKindOfClass:[NSString class]] &&
133 [scriptArray isKindOfClass:[NSArray class]] && ![key isEqual:kKeyMetadata]) {
134 scriptArray = OOSanitizeLegacyScript(scriptArray, key, NO);
135 if (scriptArray != nil) {
136 script = [[self alloc] initWithName:key scriptArray:scriptArray metadata:metadata];
137 if (script != nil) {
138 [result addObject:script];
139 [cachedScripts setObject:[NSDictionary dictionaryWithObjectsAndKeys:scriptArray, kKeyScript, metadata, kKeyMetadata, nil] forKey:key];
140
141 [script release];
142 }
143 }
144 }
145 }
146
147 [[OOCacheManager sharedCache] setObject:cachedScripts forKey:filePath inCache:kCacheName];
148
149 return [[result copy] autorelease];
150}
151
152+ (NSArray*)loadCachedScripts:(NSDictionary*)cachedScripts
153{
154 NSString* key = nil;
155
156 NSMutableArray* result = [NSMutableArray arrayWithCapacity:[cachedScripts count]];
157
158 foreachkey(key, cachedScripts)
159 {
160 NSDictionary* cacheValue = [cachedScripts oo_dictionaryForKey:key];
161 NSArray* scriptArray = [cacheValue oo_arrayForKey:kKeyScript];
162 NSDictionary* metadata = [cacheValue oo_dictionaryForKey:kKeyMetadata];
163 OOPListScript* script = [[self alloc] initWithName:key scriptArray:scriptArray metadata:metadata];
164 if (script != nil) {
165 [result addObject:script];
166 [script release];
167 }
168 }
169
170 return [[result copy] autorelease];
171}
172
173- (id)initWithName:(NSString*)name scriptArray:(NSArray*)script metadata:(NSDictionary*)metadata
174{
175 self = [super init];
176 if (self != nil) {
177 _script = [script retain];
178 if (name != nil) {
179 if (metadata == nil)
180 metadata = [NSDictionary dictionaryWithObject:name forKey:kMDKeyName];
181 else {
182 NSMutableDictionary* mutableMetadata = [[metadata mutableCopy] autorelease];
183 [mutableMetadata setObject:name forKey:kMDKeyName];
184 metadata = mutableMetadata;
185 }
186 }
187 _metadata = [metadata copy];
188 }
189
190 return self;
191}
192
193@end
#define foreachkey(VAR, DICT)
NSArray * OOSanitizeLegacyScript(NSArray *script, NSString *context, BOOL allowAIMethods)
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define OOLogIndentIf(class)
Definition OOLogging.h:107
NSDictionary * OODictionaryFromFile(NSString *path)
static NSString *const kCacheName
static NSString *const kMDKeyDescription
static NSString *const kKeyMetadata
static NSString *const kKeyScript
static NSString *const kMDKeyVersion
static NSString *const kMDKeyName
unsigned count
return nil
void setObject:forKey:inCache:(id inElement,[forKey] NSString *inKey,[inCache] NSString *inCacheKey)
id objectForKey:inCache:(NSString *inKey,[inCache] NSString *inCacheKey)
OOCacheManager * sharedCache()
NSArray * scriptsFromDictionaryOfScripts:filePath:(NSDictionary *dictionary,[filePath] NSString *filePath)
NSArray * loadCachedScripts:(NSDictionary *cachedScripts)
NSString * name()
NSString * scriptDescription()
NSString * version()
NSString * displayName()
Definition OOScript.m:250