Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOJSManifest.m
Go to the documentation of this file.
1/*
2
3OOJSManifest.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 "OOJSManifest.h"
27#import "PlayerEntity.h"
30#import "Universe.h"
31#import "OOJSPlayer.h"
32#import "OOJSPlayerShip.h"
33#import "OOIsNumberLiteral.h"
34
35
36static JSObject *sManifestPrototype;
37static JSObject *sManifestObject;
38
39static JSBool ManifestComment(JSContext *context, uintN argc, jsval *vp);
40static JSBool ManifestSetComment(JSContext *context, uintN argc, jsval *vp);
41static JSBool ManifestShortComment(JSContext *context, uintN argc, jsval *vp);
42static JSBool ManifestSetShortComment(JSContext *context, uintN argc, jsval *vp);
43
44
45static JSBool ManifestDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
46static JSBool ManifestGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
47static JSBool ManifestSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
48
49
50static JSClass sManifestClass =
51{
52 "Manifest",
53 JSCLASS_HAS_PRIVATE,
54
55 JS_PropertyStub,
59 JS_EnumerateStub,
60 JS_ResolveStub,
61 JS_ConvertStub,
63 JSCLASS_NO_OPTIONAL_MEMBERS
64};
65
66
67enum
68{
69 kManifest_list // manifest list, array of commodities: name, unit, quantity, displayName - read-only
70};
71
72
73static JSPropertySpec sManifestProperties[] =
74{
75 // JS name ID flags
77 { 0 }
78};
79
80
81static JSFunctionSpec sManifestMethods[] =
82{
83 // JS name Function min args
84 { "shortComment", ManifestShortComment, 1 },
85 { "setShortComment", ManifestSetShortComment, 2 },
86 { "comment", ManifestComment, 1 },
87 { "setComment", ManifestSetComment, 2 },
88 { 0 }
89};
90
91
92// Helper class wrapped by JS Manifest objects
93@interface OOManifest: NSObject
94@end
95
96
97@implementation OOManifest
98
99- (void) dealloc
100{
101 [super dealloc];
102}
103
104
105- (NSString *) oo_jsClassName
106{
107 return @"Manifest";
108}
109
110
111- (jsval) oo_jsValueInContext:(JSContext *)context
112{
113 JSObject *jsSelf = NULL;
114 jsval result = JSVAL_NULL;
115
116 jsSelf = JS_NewObject(context, &sManifestClass, sManifestPrototype, NULL);
117 if (jsSelf != NULL)
118 {
119 if (!JS_SetPrivate(context, jsSelf, [self retain])) jsSelf = NULL;
120 }
121 if (jsSelf != NULL) result = OBJECT_TO_JSVAL(jsSelf);
122
123 return result;
124}
125
126@end
127
128
129void InitOOJSManifest(JSContext *context, JSObject *global)
130{
131 sManifestPrototype = JS_InitClass(context, global, NULL, &sManifestClass, OOJSUnconstructableConstruct, 0, sManifestProperties, sManifestMethods, NULL, NULL);
133
134 // Create manifest object as a property of the player.ship object.
135 sManifestObject = JS_DefineObject(context, JSPlayerShipObject(), "manifest", &sManifestClass, sManifestPrototype, OOJS_PROP_READONLY);
136 JS_SetPrivate(context, sManifestObject, NULL);
137
138 // Also define manifest object as a property of the global object.
139 // Wait, what? Why? Oh well, too late now. Deprecate for EMMSTRAN? -- Ahruman 2011-02-10
140 JS_DefineObject(context, global, "manifest", &sManifestClass, sManifestPrototype, OOJS_PROP_READONLY);
141
142}
143
144
145static JSBool ManifestDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
146{
147 jsval v = JSVAL_VOID;
148 return ManifestSetProperty(context, this, propID, NO, &v);
149}
150
151
152static JSBool ManifestGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
153{
154 OOJS_NATIVE_ENTER(context)
155
156 id result = nil;
158
159 if (JSID_IS_INT(propID))
160 {
161 switch (JSID_TO_INT(propID))
162 {
163 case kManifest_list:
164 result = [entity cargoListForScripting];
165 break;
166
167 default:
169 return NO;
170 }
171 }
172 else if (JSID_IS_STRING(propID))
173 {
174 /* 'list' property is hard-coded
175 * others map to the commodity keys in trade-goods.plist
176 * compatible-ish with 1.80 and earlier except that
177 * alienItems and similar aliases don't work */
178 NSString *key = OOStringFromJSString(context, JSID_TO_STRING(propID));
179 if ([[UNIVERSE commodities] goodDefined:key])
180 {
181 *value = INT_TO_JSVAL([entity cargoQuantityForType:key]);
182 return YES;
183 }
184 else
185 {
186 return YES;
187 }
188 }
189
190 *value = OOJSValueFromNativeObject(context, result);
191 return YES;
192
194}
195
196
197static JSBool ManifestSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
198{
199 OOJS_NATIVE_ENTER(context)
200
202 int32 iValue;
203
204 if (JSID_IS_STRING(propID))
205 {
206 NSString *key = OOStringFromJSString(context, JSID_TO_STRING(propID));
207
208 OOMassUnit unit = [[UNIVERSE commodityMarket] massUnitForGood:key];
209 // we can always change gold, platinum & gem-stones quantities, even with special cargo
210 if (unit == UNITS_TONS && [entity specialCargo])
211 {
212 OOJSReportWarning(context, @"PlayerShip.manifest['foo'] - cannot modify cargo tonnage when Special Cargo is in use.");
213 return YES;
214 }
215
216 if (JS_ValueToInt32(context, *value, &iValue))
217 {
218 if (iValue < 0) iValue = 0;
219 [entity setCargoQuantityForType:key amount:iValue];
220 }
221 else
222 {
223 OOJSReportBadPropertyValue(context, this, propID, sManifestProperties, *value);
224 }
225 }
226 return YES;
227
229}
230
231
232static JSBool ManifestComment(JSContext *context, uintN argc, jsval *vp)
233{
234 OOJS_NATIVE_ENTER(context)
235
236 OOCommodityType good = nil;
237 NSString * information = nil;
238
239 if (argc > 0)
240 {
241 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
242 }
243 if (good == nil)
244 {
245 OOJSReportBadArguments(context, @"Manifest", @"comment", MIN(argc, 1U), OOJS_ARGV, nil, @"good");
246 return NO;
247 }
248
249 information = [[PLAYER shipCommodityData] commentForGood:good];
250
251 OOJS_RETURN_OBJECT(information);
252
254}
255
256
257static JSBool ManifestSetComment(JSContext *context, uintN argc, jsval *vp)
258{
259 OOJS_NATIVE_ENTER(context)
260
261 BOOL OK;
262 OOCommodityType good = nil;
263 NSString * information = nil;
264
265 if (argc > 1)
266 {
267 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
268 information = OOStringFromJSValue(context, OOJS_ARGV[1]);
269 }
270 if (good == nil || information == nil)
271 {
272 OOJSReportBadArguments(context, @"Manifest", @"setComment", MIN(argc, 2U), OOJS_ARGV, nil, @"good and information text");
273 return NO;
274 }
275
276 OK = [[PLAYER shipCommodityData] setComment:information forGood:good];
277
279
281}
282
283
284static JSBool ManifestShortComment(JSContext *context, uintN argc, jsval *vp)
285{
286 OOJS_NATIVE_ENTER(context)
287
288 OOCommodityType good = nil;
289 NSString * information = nil;
290
291 if (argc > 0)
292 {
293 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
294 }
295 if (good == nil)
296 {
297 OOJSReportBadArguments(context, @"Manifest", @"shortComment", MIN(argc, 1U), OOJS_ARGV, nil, @"good");
298 return NO;
299 }
300
301 information = [[PLAYER shipCommodityData] shortCommentForGood:good];
302
303 OOJS_RETURN_OBJECT(information);
304
306}
307
308
309static JSBool ManifestSetShortComment(JSContext *context, uintN argc, jsval *vp)
310{
311 OOJS_NATIVE_ENTER(context)
312
313 BOOL OK;
314 OOCommodityType good = nil;
315 NSString * information = nil;
316
317 if (argc > 1)
318 {
319 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
320 information = OOStringFromJSValue(context, OOJS_ARGV[1]);
321 }
322 if (good == nil || information == nil)
323 {
324 OOJSReportBadArguments(context, @"Manifest", @"setShortComment", MIN(argc, 2U), OOJS_ARGV, nil, @"good and information text");
325 return NO;
326 }
327
328 OK = [[PLAYER shipCommodityData] setShortComment:information forGood:good];
329
331
333}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static JSBool ManifestShortComment(JSContext *context, uintN argc, jsval *vp)
static JSObject * sManifestPrototype
void InitOOJSManifest(JSContext *context, JSObject *global)
static JSBool ManifestDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSFunctionSpec sManifestMethods[]
static JSBool ManifestGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ManifestSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSObject * sManifestObject
@ kManifest_list
static JSBool ManifestSetShortComment(JSContext *context, uintN argc, jsval *vp)
static JSClass sManifestClass
static JSBool ManifestComment(JSContext *context, uintN argc, jsval *vp)
static JSPropertySpec sManifestProperties[]
static JSBool ManifestSetComment(JSContext *context, uintN argc, jsval *vp)
JSObject * JSPlayerShipObject(void)
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:191
void OOJSReportWarning(JSContext *context, NSString *format,...)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
#define OOJS_ARGV
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
NSString * OOStringFromJSString(JSContext *context, JSString *string)
#define MIN(A, B)
Definition OOMaths.h:111
return nil
NSString * OOCommodityType
Definition OOTypes.h:106
OOMassUnit
Definition OOTypes.h:123
@ UNITS_TONS
Definition OOTypes.h:124
#define UNIVERSE
Definition Universe.h:833
NSString * oo_jsClassName()
OOCargoQuantity setCargoQuantityForType:amount:(OOCommodityType type,[amount] OOCargoQuantity amount)
NSArray * cargoListForScripting()