Oolite 1.91.0.7699-250829-cea269d
Loading...
Searching...
No Matches
OOJSOolite.m
Go to the documentation of this file.
1/*
2
3OOJSOolite.h
4
5JavaScript proxy for Oolite (for version checking and similar).
6
7
8Oolite
9Copyright (C) 2004-2013 Giles C Williams and contributors
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
28#import "OOJSOolite.h"
30#import "OOStringParsing.h"
31#import "OOJSPlayer.h"
32#import "ResourceManager.h"
33#import "MyOpenGLView.h"
34#import "OOConstToString.h"
35
36
37static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
38static JSBool OoliteSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
39
40static NSString *VersionString(void);
41static NSArray *VersionComponents(void);
42
43static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp);
44
45
46static JSClass sOoliteClass =
47{
48 "Oolite",
49 0,
50
51 JS_PropertyStub,
52 JS_PropertyStub,
55 //JS_StrictPropertyStub,
56 JS_EnumerateStub,
57 JS_ResolveStub,
58 JS_ConvertStub,
59 JS_FinalizeStub
60};
61
62
63enum
64{
65 // Property IDs
66 kOolite_version, // version number components, array, read-only
67 kOolite_versionString, // version number as string, string, read-only
68 kOolite_jsVersion, // JavaScript version, integer, read-only
69 kOolite_jsVersionString, // JavaScript version as string, string, read-only
70 kOolite_gameSettings, // Various game settings, object, read-only
71 kOolite_resourcePaths, // Paths containing resources, built-in plus oxp/oxz, read-only
72 kOolite_colorSaturation, // Color saturation, integer, read/write
73 kOolite_postFX, // current post processing effect, integer, read/write
74 kOolite_hdrToneMapper, // currently active HDR tone mapper, string, read/write
75 kOolite_sdrToneMapper, // currently active SDR tone mapper, string, read/write
76#ifndef NDEBUG
77 kOolite_timeAccelerationFactor, // time acceleration, float, read/write
78#endif
79};
80
81
82static JSPropertySpec sOoliteProperties[] =
83{
84 // JS name ID flags
87 { "jsVersionString", kOolite_jsVersionString, OOJS_PROP_READONLY_CB },
89 { "versionString", kOolite_versionString, OOJS_PROP_READONLY_CB },
90 { "resourcePaths", kOolite_resourcePaths, OOJS_PROP_READONLY_CB },
91 { "colorSaturation", kOolite_colorSaturation, OOJS_PROP_READWRITE_CB },
95#ifndef NDEBUG
96 { "timeAccelerationFactor", kOolite_timeAccelerationFactor, OOJS_PROP_READWRITE_CB },
97#endif
98 { 0 }
99};
100
101
102static JSFunctionSpec sOoliteMethods[] =
103{
104 // JS name Function min args
105 { "compareVersion", OoliteCompareVersion, 1 },
106 { 0 }
107};
108
109
110void InitOOJSOolite(JSContext *context, JSObject *global)
111{
112 JSObject *oolitePrototype = JS_InitClass(context, global, NULL, &sOoliteClass, OOJSUnconstructableConstruct, 0, sOoliteProperties, sOoliteMethods, NULL, NULL);
113 JS_DefineObject(context, global, "oolite", &sOoliteClass, oolitePrototype, OOJS_PROP_READONLY);
114}
115
116
117static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
118{
119 if (!JSID_IS_INT(propID)) return YES;
120
121 OOJS_NATIVE_ENTER(context)
122
123 id result = nil;
124 MyOpenGLView *gameView = [UNIVERSE gameView];
125
126 switch (JSID_TO_INT(propID))
127 {
128 case kOolite_version:
129 result = VersionComponents();
130 break;
131
133 result = VersionString();
134 break;
135
137 *value = INT_TO_JSVAL(JS_GetVersion(context));
138 return YES;
139
141 *value = STRING_TO_JSVAL(JS_NewStringCopyZ(context, JS_VersionToString(JS_GetVersion(context))));
142 return YES;
143
145 result = [UNIVERSE gameSettings];
146 break;
147
149 result = [ResourceManager paths];
150 break;
151
153 return JS_NewNumberValue(context, [gameView colorSaturation], value);
154
155 case kOolite_postFX:
156 *value = INT_TO_JSVAL([UNIVERSE currentPostFX]);
157 return YES;
158
160 {
161 NSString *toneMapperStr = @"OOHDR_TONEMAPPER_UNDEFINED";
162#if OOLITE_WINDOWS
163 if ([gameView hdrOutput])
164 {
165 toneMapperStr = OOStringFromHDRToneMapper([gameView hdrToneMapper]);
166 }
167#endif
168 result = toneMapperStr;
169 break;
170 }
171
173 {
174 NSString *toneMapperStr = @"OOSDR_TONEMAPPER_UNDEFINED";
175 if (![gameView hdrOutput])
176 {
177 toneMapperStr = OOStringFromSDRToneMapper([gameView sdrToneMapper]);
178 }
179 result = toneMapperStr;
180 break;
181 }
182
183#ifndef NDEBUG
185 return JS_NewNumberValue(context, [UNIVERSE timeAccelerationFactor], value);
186#endif
187
188 default:
189 OOJSReportBadPropertySelector(context, this, propID, sOoliteProperties);
190 return NO;
191 }
192
193 *value = OOJSValueFromNativeObject(context, result);
194 return YES;
195
197}
198
199
200static JSBool OoliteSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
201{
202 if (!JSID_IS_INT(propID)) return YES;
203
204 OOJS_NATIVE_ENTER(context)
205
206 jsdouble fValue;
207 int32 iValue;
208 NSString *sValue = nil;
209 MyOpenGLView *gameView = [UNIVERSE gameView];
210
211 switch (JSID_TO_INT(propID))
212 {
214 if (JS_ValueToNumber(context, *value, &fValue))
215 {
216 float currentColorSaturation = [gameView colorSaturation];
217 [gameView adjustColorSaturation:fValue - currentColorSaturation];
218 return YES;
219 }
220 break;
221
222 case kOolite_postFX:
223 if (JS_ValueToInt32(context, *value, &iValue))
224 {
225 iValue = MAX(iValue, 0);
226 [UNIVERSE setCurrentPostFX:iValue];
227 return YES;
228 }
229 break;
230
232 if (!JSVAL_IS_STRING(*value)) break; // non-string is not allowed
233 sValue = OOStringFromJSValue(context,*value);
234 if (sValue != nil)
235 {
236#if OOLITE_WINDOWS
237 if ([gameView hdrOutput]) [gameView setHDRToneMapper:OOHDRToneMapperFromString(sValue)];
238 else OOJSReportWarning(context, @"hdrToneMapper cannot be set if not running in HDR mode");
239#endif
240 return YES;
241 }
242 break;
243
245 if (!JSVAL_IS_STRING(*value)) break; // non-string is not allowed
246 sValue = OOStringFromJSValue(context,*value);
247 if (sValue != nil)
248 {
249 if (![gameView hdrOutput]) [gameView setSDRToneMapper:OOSDRToneMapperFromString(sValue)];
250 else OOJSReportWarning(context, @"sdrToneMapper cannot be set if not running in SDR mode");
251 return YES;
252 }
253 break;
254
255#ifndef NDEBUG
257 if (JS_ValueToNumber(context, *value, &fValue))
258 {
259 [UNIVERSE setTimeAccelerationFactor:fValue];
260 return YES;
261 }
262 break;
263#endif
264
265 default:
266 OOJSReportBadPropertySelector(context, this, propID, sOoliteProperties);
267 return NO;
268 }
269
270 OOJSReportBadPropertyValue(context, this, propID, sOoliteProperties, *value);
271 return NO;
272
274}
275
276
277static NSString *VersionString(void)
278{
279 return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
280}
281
282
283static NSArray *VersionComponents(void)
284{
286}
287
288
289/* oolite.compareVersion(versionSpec) : Number
290 returns -1 if the current version of Oolite is less than versionSpec, 0 if
291 they are equal, and 1 if the current version is newer. versionSpec may be
292 a string or an array. Example:
293 if (0 < oolite.compareVersion("1.70")) log("Old version of Oolite!")
294 else this.doStuffThatRequires170()
295*/
296static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp)
297{
298 OOJS_NATIVE_ENTER(context)
299
300 id components = nil;
301 NSEnumerator *componentEnum = nil;
302 id component = nil;
303
304 if (argc == 0) OOJS_RETURN_VOID; // Backwards-compatibility: be overly lenient.
305
306 components = OOJSNativeObjectFromJSValue(context, OOJS_ARGV[0]);
307 if ([components isKindOfClass:[NSArray class]])
308 {
309 // Require each element to be a number
310 for (componentEnum = [components objectEnumerator]; (component = [componentEnum nextObject]); )
311 {
312 if (![component isKindOfClass:[NSNumber class]])
313 {
314 components = nil;
315 break;
316 }
317 }
318 }
319 else if ([components isKindOfClass:[NSString class]])
320 {
321 components = ComponentsFromVersionString(components);
322 }
323 else components = nil;
324
325 if (components != nil)
326 {
327 OOJS_RETURN_INT((int32_t)CompareVersions(components, VersionComponents()));
328 }
329 else
330 {
332 }
333
335}
NSString * OOStringFromHDRToneMapper(OOHDRToneMapper toneMapper)
NSString * OOStringFromSDRToneMapper(OOSDRToneMapper toneMapper)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSOolite.m:117
static JSFunctionSpec sOoliteMethods[]
Definition OOJSOolite.m:102
static JSBool OoliteSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSOolite.m:200
@ kOolite_version
Definition OOJSOolite.m:66
@ kOolite_sdrToneMapper
Definition OOJSOolite.m:75
@ kOolite_timeAccelerationFactor
Definition OOJSOolite.m:77
@ kOolite_colorSaturation
Definition OOJSOolite.m:72
@ kOolite_jsVersionString
Definition OOJSOolite.m:69
@ kOolite_jsVersion
Definition OOJSOolite.m:68
@ kOolite_hdrToneMapper
Definition OOJSOolite.m:74
@ kOolite_gameSettings
Definition OOJSOolite.m:70
@ kOolite_resourcePaths
Definition OOJSOolite.m:71
@ kOolite_versionString
Definition OOJSOolite.m:67
@ kOolite_postFX
Definition OOJSOolite.m:73
static JSPropertySpec sOoliteProperties[]
Definition OOJSOolite.m:82
static JSClass sOoliteClass
Definition OOJSOolite.m:46
static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp)
Definition OOJSOolite.m:296
void InitOOJSOolite(JSContext *context, JSObject *global)
Definition OOJSOolite.m:110
static NSString * VersionString(void)
Definition OOJSOolite.m:277
static NSArray * VersionComponents(void)
Definition OOJSOolite.m:283
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOJS_PROP_READWRITE_CB
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
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)
#define OOJS_PROP_READONLY_CB
#define OOJS_RETURN_VOID
#define OOJS_RETURN_INT(v)
#define MAX(A, B)
Definition OOMaths.h:114
return nil
NSArray * ComponentsFromVersionString(NSString *string)
NSComparisonResult CompareVersions(NSArray *version1, NSArray *version2)
#define UNIVERSE
Definition Universe.h:840
void adjustColorSaturation:(float colorSaturationAdjustment)
float colorSaturation()
void setSDRToneMapper:(OOSDRToneMapper newToneMapper)