Oolite 1.91.0.7644-241112-7f5034b
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
34
35static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
36
37static NSString *VersionString(void);
38static NSArray *VersionComponents(void);
39
40static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp);
41
42
43static JSClass sOoliteClass =
44{
45 "Oolite",
46 0,
47
48 JS_PropertyStub,
49 JS_PropertyStub,
51 JS_StrictPropertyStub,
52 JS_EnumerateStub,
53 JS_ResolveStub,
54 JS_ConvertStub,
55 JS_FinalizeStub
56};
57
58
59enum
60{
61 // Property IDs
62 kOolite_version, // version number components, array, read-only
63 kOolite_versionString, // version number as string, string, read-only
64 kOolite_jsVersion, // JavaScript version, integer, read-only
65 kOolite_jsVersionString, // JavaScript version as string, string, read-only
66 kOolite_gameSettings, // Various game settings, object, read-only
67 kOolite_resourcePaths, // Paths containing resources, built-in plus oxp/oxz, read-only
68};
69
70
71static JSPropertySpec sOoliteProperties[] =
72{
73 // JS name ID flags
76 { "jsVersionString", kOolite_jsVersionString, OOJS_PROP_READONLY_CB },
78 { "versionString", kOolite_versionString, OOJS_PROP_READONLY_CB },
79 { "resourcePaths", kOolite_resourcePaths, OOJS_PROP_READONLY_CB },
80 { 0 }
81};
82
83
84static JSFunctionSpec sOoliteMethods[] =
85{
86 // JS name Function min args
87 { "compareVersion", OoliteCompareVersion, 1 },
88 { 0 }
89};
90
91
92void InitOOJSOolite(JSContext *context, JSObject *global)
93{
94 JSObject *oolitePrototype = JS_InitClass(context, global, NULL, &sOoliteClass, OOJSUnconstructableConstruct, 0, sOoliteProperties, sOoliteMethods, NULL, NULL);
95 JS_DefineObject(context, global, "oolite", &sOoliteClass, oolitePrototype, OOJS_PROP_READONLY);
96}
97
98
99static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
100{
101 if (!JSID_IS_INT(propID)) return YES;
102
103 OOJS_NATIVE_ENTER(context)
104
105 id result = nil;
106
107 switch (JSID_TO_INT(propID))
108 {
109 case kOolite_version:
110 result = VersionComponents();
111 break;
112
114 result = VersionString();
115 break;
116
118 *value = INT_TO_JSVAL(JS_GetVersion(context));
119 return YES;
120
122 *value = STRING_TO_JSVAL(JS_NewStringCopyZ(context, JS_VersionToString(JS_GetVersion(context))));
123 return YES;
124
126 result = [UNIVERSE gameSettings];
127 break;
128
130 result = [ResourceManager paths];
131 break;
132
133 default:
134 OOJSReportBadPropertySelector(context, this, propID, sOoliteProperties);
135 return NO;
136 }
137
138 *value = OOJSValueFromNativeObject(context, result);
139 return YES;
140
142}
143
144
145static NSString *VersionString(void)
146{
147 return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
148}
149
150
151static NSArray *VersionComponents(void)
152{
154}
155
156
157/* oolite.compareVersion(versionSpec) : Number
158 returns -1 if the current version of Oolite is less than versionSpec, 0 if
159 they are equal, and 1 if the current version is newer. versionSpec may be
160 a string or an array. Example:
161 if (0 < oolite.compareVersion("1.70")) log("Old version of Oolite!")
162 else this.doStuffThatRequires170()
163*/
164static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp)
165{
166 OOJS_NATIVE_ENTER(context)
167
168 id components = nil;
169 NSEnumerator *componentEnum = nil;
170 id component = nil;
171
172 if (argc == 0) OOJS_RETURN_VOID; // Backwards-compatibility: be overly lenient.
173
174 components = OOJSNativeObjectFromJSValue(context, OOJS_ARGV[0]);
175 if ([components isKindOfClass:[NSArray class]])
176 {
177 // Require each element to be a number
178 for (componentEnum = [components objectEnumerator]; (component = [componentEnum nextObject]); )
179 {
180 if (![component isKindOfClass:[NSNumber class]])
181 {
182 components = nil;
183 break;
184 }
185 }
186 }
187 else if ([components isKindOfClass:[NSString class]])
188 {
189 components = ComponentsFromVersionString(components);
190 }
191 else components = nil;
192
193 if (components != nil)
194 {
195 OOJS_RETURN_INT((int32_t)CompareVersions(components, VersionComponents()));
196 }
197 else
198 {
200 }
201
203}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static JSBool OoliteGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSOolite.m:99
static JSFunctionSpec sOoliteMethods[]
Definition OOJSOolite.m:84
static JSPropertySpec sOoliteProperties[]
Definition OOJSOolite.m:71
static JSClass sOoliteClass
Definition OOJSOolite.m:43
static JSBool OoliteCompareVersion(JSContext *context, uintN argc, jsval *vp)
Definition OOJSOolite.m:164
void InitOOJSOolite(JSContext *context, JSObject *global)
Definition OOJSOolite.m:92
@ kOolite_version
Definition OOJSOolite.m:62
@ kOolite_jsVersionString
Definition OOJSOolite.m:65
@ kOolite_jsVersion
Definition OOJSOolite.m:64
@ kOolite_gameSettings
Definition OOJSOolite.m:66
@ kOolite_resourcePaths
Definition OOJSOolite.m:67
@ kOolite_versionString
Definition OOJSOolite.m:63
static NSString * VersionString(void)
Definition OOJSOolite.m:145
static NSArray * VersionComponents(void)
Definition OOJSOolite.m:151
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
#define OOJS_ARGV
#define OOJS_PROP_READONLY_CB
#define OOJS_RETURN_VOID
#define OOJS_RETURN_INT(v)
return nil
NSArray * ComponentsFromVersionString(NSString *string)
NSComparisonResult CompareVersions(NSArray *version1, NSArray *version2)