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