Oolite
Loading...
Searching...
No Matches
OOJSGlobal.m
Go to the documentation of this file.
1/*
2
3OOJSGlobal.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSGlobal.h"
28
29#import "GuiDisplayGen.h"
30#import "MyOpenGLView.h"
33#import "OOConstToJSString.h"
34#import "OOConstToString.h"
36#import "OOJSPlayer.h"
37#import "OOStringExpander.h"
39#import "OOTexture.h"
41#import "ResourceManager.h"
42
43#if OOJSENGINE_MONITOR_SUPPORT
44
45@interface OOJavaScriptEngine (OOMonitorSupportInternal)
46
47- (void)sendMonitorLogMessage:(NSString*)message
48 withMessageClass:(NSString*)messageClass
49 inContext:(JSContext*)context;
50
51@end
52
53#endif
54
55static NSString* const kOOLogDebugMessage = @"script.debug.message";
56
58#ifdef OO_DEBUG_MONITOR_ENABLED
59static JSValue GlobalSetProperty(OOJS_SETTER_ARGS);
60#endif
61
62static JSValue GlobalLog(OOJS_NATIVE_ARGS);
74static JSValue GlobalPauseGame(OOJS_NATIVE_ARGS);
79
80#ifdef OO_DEBUG_MONITOR_ENABLED
81static JSValue GlobalTakeSnapShot(OOJS_NATIVE_ARGS);
82static JSValue GlobalQuitGame(OOJS_NATIVE_ARGS);
83#endif
84
85/* QuickJS's global object is an ordinary object, so the SpiderMonkey global
86 class's tinyid getter and setter become accessor properties defined on
87 the global object.
88*/
89
90enum {
91 // Property IDs
92 kGlobal_galaxyNumber, // galaxy number, integer, read-only
93 kGlobal_global, // global.global.global.global, integer, read-only
94 kGlobal_guiScreen, // current GUI screen, string, read-only
95#ifdef OO_DEBUG_MONITOR_ENABLED
96 kGlobal_timeAccelerationFactor // time acceleration, float, read/write
97#endif
98};
99
100static const JSCFunctionListEntry sGlobalProperties[] = {
101 // JS name Getter Setter ID
104#ifdef OO_DEBUG_MONITOR_ENABLED
105 OOJS_PROPERTY_RW("timeAccelerationFactor", GlobalGetProperty, GlobalSetProperty, kGlobal_timeAccelerationFactor),
106#endif
107};
108
109static const JSCFunctionListEntry sGlobalMethods[] = {
110 // JS name Function min args
111 JS_CFUNC_DEF("log", 1, GlobalLog),
112 JS_CFUNC_DEF("autoAIForRole", 1, GlobalAutoAIForRole),
113 JS_CFUNC_DEF("expandDescription", 1, GlobalExpandDescription),
114 JS_CFUNC_DEF("expandMissionText", 1, GlobalExpandMissionText),
115 JS_CFUNC_DEF("displayNameForCommodity", 1, GlobalDisplayNameForCommodity),
116 JS_CFUNC_DEF("randomName", 0, GlobalRandomName),
117 JS_CFUNC_DEF("randomInhabitantsDescription", 1, GlobalRandomInhabitantsDescription),
118 JS_CFUNC_DEF("setScreenBackground", 1, GlobalSetScreenBackground),
119 JS_CFUNC_DEF("getScreenBackgroundForKey", 1, GlobalGetScreenBackgroundForKey),
120 JS_CFUNC_DEF("setScreenBackgroundForKey", 2, GlobalSetScreenBackgroundForKey),
121 JS_CFUNC_DEF("setScreenOverlay", 1, GlobalSetScreenOverlay),
122 JS_CFUNC_DEF("getGuiColorSettingForKey", 1, GlobalGetGuiColorSettingForKey),
123 JS_CFUNC_DEF("setGuiColorSettingForKey", 2, GlobalSetGuiColorSettingForKey),
124 JS_CFUNC_DEF("keyBindingDescription", 1, GlobalKeyBindingDescription),
125 JS_CFUNC_DEF("setExtraGuiScreenKeys", 2, GlobalSetExtraGuiScreenKeys),
126 JS_CFUNC_DEF("clearExtraGuiScreenKeys", 2, GlobalClearExtraGuiScreenKeys),
127
128#ifdef OO_DEBUG_MONITOR_ENABLED
129 JS_CFUNC_DEF("takeSnapShot", 1, GlobalTakeSnapShot),
130 JS_CFUNC_DEF("quitGame", 0, GlobalQuitGame),
131#endif
132 JS_CFUNC_DEF("pauseGame", 0, GlobalPauseGame),
133};
134
135void SetUpOOJSGlobal(JSContext* context, JSValueConst global)
136{
137 JS_SetPropertyFunctionList(context, global, sGlobalProperties, OOJS_COUNT(sGlobalProperties));
138 JS_SetPropertyFunctionList(context, global, sGlobalMethods, OOJS_COUNT(sGlobalMethods));
139}
140
142{
143 OOJS_NATIVE_ENTER(context)
144
146
147 switch (magic) {
149 return JS_NewInt32(context, [player currentGalaxyID]);
150
152 return OOJSValueFromGUIScreenID(context, [player guiScreen]);
153
154#ifdef OO_DEBUG_MONITOR_ENABLED
155 case kGlobal_timeAccelerationFactor:
156 return JS_NewNumber(context, [UNIVERSE timeAccelerationFactor]);
157#endif
158
159 default:
161 return JS_EXCEPTION;
162 }
163
165}
166
167#ifdef OO_DEBUG_MONITOR_ENABLED
168static JSValue GlobalSetProperty(OOJS_SETTER_ARGS)
169{
170 OOJS_NATIVE_ENTER(context)
171
172 double fValue;
173
174 switch (magic) {
175 case kGlobal_timeAccelerationFactor:
176 if (JS_ToFloat64(context, &fValue, value) == 0) {
177 [UNIVERSE setTimeAccelerationFactor:fValue];
178 return JS_UNDEFINED;
179 }
180 JS_FreeValue(context, JS_GetException(context));
181 break;
182
183 default:
185 return JS_EXCEPTION;
186 }
187
189 return JS_EXCEPTION;
190
192}
193#endif
194
195// *** Methods ***
196
197// log([messageClass : String,] message : string, ...)
199{
200 OOJS_NATIVE_ENTER(context)
201
202 NSString* message = nil;
203 NSString* messageClass = nil;
204
205 if (EXPECT_NOT(argc < 1)) {
207 }
208 if (argc < 2) {
209 messageClass = kOOLogDebugMessage;
210 message = OOStringFromJSValue(context, OOJS_ARGV[0]);
211 } else {
212 messageClass = OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[0]);
213 if (!OOLogWillDisplayMessagesInClass(messageClass)) {
214 // Do nothing (and short-circuit) if message class is filtered out.
216 }
217
218 message = [NSString concatenationOfStringsFromJavaScriptValues:OOJS_ARGV + 1 count:argc - 1 separator:@", " inContext:context];
219 }
220
222 OOLog(messageClass, @"%@", message);
223
224#if OOJSENGINE_MONITOR_SUPPORT
225 [[OOJavaScriptEngine sharedEngine] sendMonitorLogMessage:message
226 withMessageClass:nil
227 inContext:context];
228#endif
230
232
234}
235
236// expandDescription(description : String [, overrides : object (dictionary)]) : String
238{
239 OOJS_NATIVE_ENTER(context)
240
241 NSString* string = nil;
242 NSDictionary* overrides = nil;
243
244 if (argc > 0)
245 string = OOStringFromJSValue(context, OOJS_ARGV[0]);
246 if (string == nil) {
247 OOJSReportBadArguments(context, nil, @"expandDescription", MIN(argc, 1), OOJS_ARGV, nil, @"string");
248 return JS_EXCEPTION;
249 }
250 if (argc > 1) {
251 overrides = OOJSDictionaryFromStringTable(context, OOJS_ARGV[1]);
252 }
253
257
258 OOJS_RETURN_OBJECT(string);
259
261}
262
264{
265 OOJS_NATIVE_ENTER(context)
266
267 NSString* string = nil;
269
270 if (argc > 0)
271 string = OOStringFromJSValue(context, OOJS_ARGV[0]);
272 if (string == nil) {
273 OOJSReportBadArguments(context, nil, @"keyBindingDescription", MIN(argc, 1), OOJS_ARGV, nil, @"string");
274 return JS_EXCEPTION;
275 }
276
278 string = [player keyBindingDescription2:string];
280
281 OOJS_RETURN_OBJECT(string);
282
284}
285
286// expandMissionText(textKey : String [, overrides : object (dictionary)]) : String
288{
289 OOJS_NATIVE_ENTER(context)
290
291 NSString* string = nil;
292 NSDictionary* overrides = nil;
293
294 if (argc > 0)
295 string = OOStringFromJSValue(context, OOJS_ARGV[0]);
296 if (string == nil) {
297 OOJSReportBadArguments(context, nil, @"expandMissionText", MIN(argc, 1), OOJS_ARGV, nil, @"string");
298 return JS_EXCEPTION;
299 }
300 if (argc > 1) {
301 overrides = OOJSDictionaryFromStringTable(context, OOJS_ARGV[1]);
302 }
303
304 string = [[UNIVERSE missiontext] oo_stringForKey:string];
306
307 OOJS_RETURN_OBJECT(string);
308
310}
311
312// displayNameForCommodity(commodityName : String) : String
314{
315 OOJS_NATIVE_ENTER(context)
316
317 NSString* string = nil;
318
319 if (argc > 0)
320 string = OOStringFromJSValue(context, OOJS_ARGV[0]);
321 if (string == nil) {
322 OOJSReportBadArguments(context, nil, @"displayNameForCommodity", MIN(argc, 1), OOJS_ARGV, nil, @"string");
323 return JS_EXCEPTION;
324 }
326
328}
329
330// randomName() : String
332{
333 OOJS_NATIVE_ENTER(context)
334
335 /* Temporarily set the system generation seed to a "really random" seed,
336 so randomName() isn't repeatable.
337 */
338 RNG_Seed savedSeed = currentRandomSeed();
340
341 NSString* result = OOExpand(@"%N");
342
343 // Restore seed.
344 setRandomSeed(savedSeed);
345
346 OOJS_RETURN_OBJECT(result);
347
349}
350
351// randomInhabitantsDescription() : String
353{
354 OOJS_NATIVE_ENTER(context)
355
356 NSString* string = nil;
357 Random_Seed aSeed;
358 int isPlural = YES;
359
360 if (argc > 0 && (isPlural = JS_ToBool(context, OOJS_ARGV[0])) < 0) {
361 JS_FreeValue(context, JS_GetException(context));
362 OOJSReportBadArguments(context, nil, @"randomInhabitantsDescription", 1, OOJS_ARGV, nil, @"boolean");
363 return JS_EXCEPTION;
364 }
365
367 string = [UNIVERSE getSystemInhabitants:Ranrot() % OO_SYSTEMS_PER_GALAXY plural:isPlural];
368 OOJS_RETURN_OBJECT(string);
369
371}
372
374{
375 OOJS_NATIVE_ENTER(context)
376
377 BOOL result = NO;
379
380 if (EXPECT_NOT(argc < 2)) {
381 OOJSReportBadArguments(context, nil, @"setExtraGuiScreenKeys", 0, OOJS_ARGV, nil, @"missing arguments");
382 return JS_EXCEPTION;
383 }
384
385 NSString* key = OOStringFromJSValue(context, OOJS_ARGV[0]);
386 if (EXPECT_NOT(key == nil || [key isEqualToString:@""])) {
387 OOJSReportBadArguments(context, nil, @"clearExtraGuiScreenKeys", 1, OOJS_ARGV, nil, @"key");
388 return JS_EXCEPTION;
389 }
390
392 if (!gui) {
393 OOJSReportBadArguments(context, nil, @"clearExtraGuiScreenKeys", 0, OOJS_ARGV, nil, @"guiScreen invalid entry");
394 return JS_EXCEPTION;
395 }
396
397 [player clearExtraGuiScreenKeys:gui key:key];
398
399 result = YES;
400 OOJS_RETURN_BOOL(result);
401
403}
404
405/* Get a property of a parameter object. Returns an owned value. If a getter
406 throws, the exception is reported and cleared and the property is treated
407 as undefined. (SpiderMonkey's JS_GetProperty() returned false, which the
408 callers below treat as a missing property; a native must not return
409 normally with an exception pending in QuickJS.)
410*/
411static JSValue GetParam(JSContext* context, JSValueConst object, const char* key)
412{
413 JSValue value = JS_GetPropertyStr(context, object, key);
414 if (JS_IsException(value)) {
416 return JS_UNDEFINED;
417 }
418 return value;
419}
420
422{
423 OOJS_NATIVE_ENTER(context)
424
425 BOOL result = NO;
426 JSValue callback = JS_UNDEFINED;
427 JSValue value = JS_UNDEFINED;
428 NSString* key = nil;
429 OOGUIScreenID gui;
430 NSDictionary* keydefs = NULL;
431 JSValue params = JS_UNDEFINED;
433 // The SpiderMonkey version always described two arguments from argv[1] on; don't read past argv.
434 int defArgc = MIN(argc - 1, 2);
435
436 if (EXPECT_NOT(argc < 1)) {
437 OOJSReportBadArguments(context, nil, @"setExtraGuiScreenKeys", 0, OOJS_ARGV, nil, @"key, definition");
438 return JS_EXCEPTION;
439 }
440 key = OOStringFromJSValue(context, OOJS_ARGV[0]);
441
442 // Validate arguments.
443 // (SpiderMonkey's JS_ValueToObject() accepted null and undefined, yielding no object; treat them as invalid.)
444 if (argc >= 2 && !JS_IsNull(OOJS_ARGV[1]) && !JS_IsUndefined(OOJS_ARGV[1])) {
445 params = JS_ToObject(context, OOJS_ARGV[1]);
446 if (JS_IsException(params)) {
447 JS_FreeValue(context, JS_GetException(context));
448 params = JS_UNDEFINED;
449 }
450 }
451 if (!JS_IsObject(params)) {
452 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], nil, @"key, definition: definition is not a valid dictionary.");
453 return JS_EXCEPTION;
454 }
455
456 value = GetParam(context, params, "guiScreen");
457 if (JS_IsUndefined(value)) {
458 JS_FreeValue(context, params);
459 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], nil, @"key, definition: must have a 'guiScreen' property.");
460 return JS_EXCEPTION;
461 }
462
463 gui = OOGUIScreenIDFromJSValue(context, value);
464 JS_FreeValue(context, value);
465 // gui will be 0 for invalid screen id's as well as GUI_SCREEN_MAIN
466 if (gui == 0 || gui == GUI_SCREEN_LOAD || gui == GUI_SCREEN_SAVE || gui == GUI_SCREEN_STICKMAPPER || gui == GUI_SCREEN_OXZMANAGER || gui == GUI_SCREEN_NEWGAME || gui == GUI_SCREEN_SAVE_OVERWRITE || gui == GUI_SCREEN_KEYBOARD || gui == GUI_SCREEN_STICKPROFILE || gui == GUI_SCREEN_KEYBOARD_CONFIRMCLEAR || gui == GUI_SCREEN_KEYBOARD_CONFIG || gui == GUI_SCREEN_KEYBOARD_ENTRY || gui == GUI_SCREEN_KEYBOARD_LAYOUT) {
467 JS_FreeValue(context, params);
468 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], nil, @"key, definition: 'guiScreen' property must be a permitted and valid GUI_SCREEN idenfifier.");
469 return JS_EXCEPTION;
470 }
471
472 value = GetParam(context, params, "registerKeys");
473 if (JS_IsUndefined(value)) {
474 JS_FreeValue(context, params);
475 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], nil, @"key, definition: must have a 'registerKeys' property.");
476 return JS_EXCEPTION;
477 }
478 if (!JS_IsNull(value)) {
479 if (JS_IsObject(value)) {
480 keydefs = OOJSNativeObjectFromJSObject(context, value);
481 } else {
482 JS_FreeValue(context, value);
483 JS_FreeValue(context, params);
484 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], nil, @"key, definition: registerKeys is not a valid dictionary.");
485 return JS_EXCEPTION;
486 }
487 }
488 JS_FreeValue(context, value);
489
490 callback = GetParam(context, params, "callback");
491 if (JS_IsUndefined(callback)) {
492 JS_FreeValue(context, params);
493 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], NULL, @"key, definition; must have a 'callback' property.");
494 return JS_EXCEPTION;
495 }
496 if (!OOJSValueIsFunction(context, callback)) {
497 JS_FreeValue(context, callback);
498 JS_FreeValue(context, params);
499 OOJSReportBadArguments(context, @"global", @"setExtraGuiScreenKeys", defArgc, &OOJS_ARGV[1], NULL, @"key, definition; 'callback' property must be a function.");
500 return JS_EXCEPTION;
501 }
502
503 OOJSGuiScreenKeyDefinition* definition = [[OOJSGuiScreenKeyDefinition alloc] init];
504 [definition setName:key];
505 [definition setRegisterKeys:keydefs];
506 [definition setCallback:callback]; // Takes its own reference.
507 JS_FreeValue(context, callback);
508
509 // get callback 'this'
510 value = GetParam(context, params, "cbThis");
511 if (!JS_IsUndefined(value)) {
512 // As with SpiderMonkey's JS_ValueToObject(), null gives no object; primitives are boxed.
513 if (!JS_IsNull(value)) {
514 JSValue callbackThis = JS_ToObject(context, value);
515 if (JS_IsException(callbackThis)) {
516 JS_FreeValue(context, JS_GetException(context));
517 } else {
518 [definition setCallbackThis:callbackThis];
519 }
520 JS_FreeValue(context, callbackThis);
521 }
522 // can do .bind(this) for callback instead
523 }
524 JS_FreeValue(context, value);
525 JS_FreeValue(context, params);
526
527 result = [player setExtraGuiScreenKeys:gui definition:definition];
528 [definition release];
529
530 OOJS_RETURN_BOOL(result);
531
533}
534
535// setScreenBackground(descriptor : guiTextureDescriptor) : Boolean
537{
538 OOJS_NATIVE_ENTER(context)
539
540 BOOL result = NO;
541 JSValueConst value = (argc > 0) ? OOJS_ARGV[0] : JS_NULL;
542
543 if (EXPECT_NOT(argc == 0)) {
544 OOJSReportWarning(context, @"Usage error: %@() called with no arguments. Treating as %@(null). This call may fail in a future version of Oolite.", @"setScreenBackground", @"setScreenBackground");
545 } else if (EXPECT_NOT(JS_IsUndefined(value))) {
546 OOJSReportBadArguments(context, nil, @"setScreenBackground", 1, &value, nil, @"GUI texture descriptor");
547 return JS_EXCEPTION;
548 }
549
550 if ([UNIVERSE viewDirection] == VIEW_GUI_DISPLAY) {
551 GuiDisplayGen* gui = [UNIVERSE gui];
552 NSDictionary* descriptor = [gui textureDescriptorFromJSValue:value inContext:context callerDescription:@"setScreenBackground()"];
553
554 result = [gui setBackgroundTextureDescriptor:descriptor];
555
556 // add some permanence to the override if we're in the equip ship screen
557 if (result && [PLAYER guiScreen] == GUI_SCREEN_EQUIP_SHIP)
558 [PLAYER setEquipScreenBackgroundDescriptor:descriptor];
559 }
560
561 OOJS_RETURN_BOOL(result);
562
564}
565
567{
568 OOJS_NATIVE_ENTER(context)
569
570 if (EXPECT_NOT(argc == 0)) {
571 OOJSReportBadArguments(context, nil, @"getScreenBackgroundDefault", 0, OOJS_ARGV, nil, @"missing arguments");
572 return JS_EXCEPTION;
573 }
574 NSString* key = OOStringFromJSValue(context, OOJS_ARGV[0]);
575 if (EXPECT_NOT(key == nil || [key isEqualToString:@""])) {
576 OOJSReportBadArguments(context, nil, @"getScreenBackgroundDefault", 0, OOJS_ARGV, nil, @"key");
577 return JS_EXCEPTION;
578 }
579 NSDictionary* descriptor = [UNIVERSE screenTextureDescriptorForKey:key];
580
581 OOJS_RETURN_OBJECT(descriptor);
582
584}
585
586// setScreenBackgroundDefault (key : NSString, descriptor : guiTextureDescriptor) : boolean
588{
589 OOJS_NATIVE_ENTER(context)
590
591 BOOL result = NO;
592
593 if (EXPECT_NOT(argc < 2)) {
594 OOJSReportBadArguments(context, nil, @"setScreenBackgroundDefault", 0, OOJS_ARGV, nil, @"missing arguments");
595 return JS_EXCEPTION;
596 }
597
598 NSString* key = OOStringFromJSValue(context, OOJS_ARGV[0]);
599 JSValueConst value = OOJS_ARGV[1];
600 if (EXPECT_NOT(key == nil || [key isEqualToString:@""])) {
601 OOJSReportBadArguments(context, nil, @"setScreenBackgroundDefault", 0, OOJS_ARGV, nil, @"key");
602 return JS_EXCEPTION;
603 }
604
605 GuiDisplayGen* gui = [UNIVERSE gui];
606 NSDictionary* descriptor = [gui textureDescriptorFromJSValue:value inContext:context callerDescription:@"setScreenBackgroundDefault()"];
607
608 [UNIVERSE setScreenTextureDescriptorForKey:key descriptor:descriptor];
609 result = YES;
610
611 OOJS_RETURN_BOOL(result);
612
614}
615
616// setScreenOverlay(descriptor : guiTextureDescriptor) : Boolean
618{
619 OOJS_NATIVE_ENTER(context)
620
621 BOOL result = NO;
622 JSValueConst value = (argc > 0) ? OOJS_ARGV[0] : JS_NULL;
623
624 if (EXPECT_NOT(argc == 0)) {
625 OOJSReportWarning(context, @"Usage error: %@() called with no arguments. Treating as %@(null). This call may fail in a future version of Oolite.", @"setScreenOverlay", @"setScreenOverlay");
626 } else if (EXPECT_NOT(JS_IsUndefined(value))) {
627 OOJSReportBadArguments(context, nil, @"setScreenOverlay", 1, &value, nil, @"GUI texture descriptor");
628 return JS_EXCEPTION;
629 }
630
631 if ([UNIVERSE viewDirection] == VIEW_GUI_DISPLAY) {
632 GuiDisplayGen* gui = [UNIVERSE gui];
633 NSDictionary* descriptor = [gui textureDescriptorFromJSValue:value inContext:context callerDescription:@"setScreenOverlay()"];
634
635 result = [gui setForegroundTextureDescriptor:descriptor];
636 }
637
638 OOJS_RETURN_BOOL(result);
639
641}
642
644{
645 OOJS_NATIVE_ENTER(context)
646
647 if (EXPECT_NOT(argc == 0)) {
648 OOJSReportBadArguments(context, nil, @"getGuiColorForKey", 0, OOJS_ARGV, nil, @"missing arguments");
649 return JS_EXCEPTION;
650 }
651 NSString* key = OOStringFromJSValue(context, OOJS_ARGV[0]);
652 if (EXPECT_NOT(key == nil || [key isEqualToString:@""])) {
653 OOJSReportBadArguments(context, nil, @"getGuiColorForKey", 0, OOJS_ARGV, nil, @"key");
654 return JS_EXCEPTION;
655 }
656 if ([key rangeOfString:@"color"].location == NSNotFound) {
657 OOJSReportBadArguments(context, nil, @"getGuiColorForKey", 0, OOJS_ARGV, nil, @"valid color key setting");
658 return JS_EXCEPTION;
659 }
660
661 GuiDisplayGen* gui = [UNIVERSE gui];
662 OOColor* col = [gui colorFromSetting:key defaultValue:nil];
663
664 OOJS_RETURN_OBJECT([col normalizedArray]);
665
667}
668
669// setGuiColorForKey(descriptor : OOColor) : boolean
671{
672 OOJS_NATIVE_ENTER(context)
673
674 BOOL result = NO;
675 OOColor* col = nil;
676
677 if (EXPECT_NOT(argc != 2)) {
678 OOJSReportBadArguments(context, nil, @"setGuiColorForKey", 0, OOJS_ARGV, nil, @"missing arguments");
679 return JS_EXCEPTION;
680 }
681
682 NSString* key = OOStringFromJSValue(context, OOJS_ARGV[0]);
683 JSValueConst value = OOJS_ARGV[1];
684 if (EXPECT_NOT(key == nil || [key isEqualToString:@""])) {
685 OOJSReportBadArguments(context, nil, @"setGuiColorForKey", 0, OOJS_ARGV, nil, @"key");
686 return JS_EXCEPTION;
687 }
688 if ([key rangeOfString:@"color"].location == NSNotFound) {
689 OOJSReportBadArguments(context, nil, @"setGuiColorForKey", 0, OOJS_ARGV, nil, @"valid color key setting");
690 return JS_EXCEPTION;
691 }
692
693 if (!JS_IsNull(value)) {
694 col = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, value)];
695 if (col == nil) {
696 OOJSReportBadArguments(context, nil, @"setGuiColorForKey", 1, OOJS_ARGV, nil, @"color descriptor");
697 return JS_EXCEPTION;
698 }
699 }
700
701 GuiDisplayGen* gui = [UNIVERSE gui];
702 [gui setGuiColorSettingFromKey:key color:col];
703 result = YES;
704
705 OOJS_RETURN_BOOL(result);
706
708}
709
710#ifdef OO_DEBUG_MONITOR_ENABLED
711// takeSnapShot([name : alphanumeric String]) : Boolean
712static JSValue GlobalTakeSnapShot(OOJS_NATIVE_ARGS)
713{
714 OOJS_NATIVE_ENTER(context)
715
716 NSString* value = nil;
717 NSMutableCharacterSet* allowedChars = (NSMutableCharacterSet*)[NSMutableCharacterSet alphanumericCharacterSet];
718 BOOL result = NO;
719
720 [allowedChars addCharactersInString:@"_-"];
721
722 if (argc > 0) {
723 value = OOStringFromJSValue(context, OOJS_ARGV[0]);
724 if (EXPECT_NOT(value == nil || [value rangeOfCharacterFromSet:[allowedChars invertedSet]].location != NSNotFound)) {
725 OOJSReportBadArguments(context, nil, @"takeSnapShot", argc, OOJS_ARGV, nil, @"alphanumeric string");
726 return JS_EXCEPTION;
727 }
728 }
729
730 NSString* playerFileDirectory = [[NSFileManager defaultManager] defaultCommanderPath];
731 NSDictionary* attr = [[NSFileManager defaultManager] oo_fileSystemAttributesAtPath:playerFileDirectory];
732
733 if (attr != nil) {
734 double freeSpace = [attr oo_doubleForKey:NSFileSystemFreeSize];
735 if (freeSpace < 1073741824) // less than 1 GB free on disk?
736 {
737 OOJSReportWarning(context, @"takeSnapShot: function disabled when free disk space is less than 1GB.");
739 }
740 }
741
743 result = [[UNIVERSE gameView] snapShot:value];
745
746 OOJS_RETURN_BOOL(result);
747
749}
750#endif
751
752// autoAIForRole(role : String) : String
754{
755 OOJS_NATIVE_ENTER(context)
756
757 NSString* string = nil;
758
759 if (argc > 0)
760 string = OOStringFromJSValue(context, OOJS_ARGV[0]);
761 if (string == nil) {
762 OOJSReportBadArguments(context, nil, @"autoAIForRole", MIN(argc, 1), OOJS_ARGV, nil, @"string");
763 return JS_EXCEPTION;
764 }
765
766 NSDictionary* autoAIMap = [ResourceManager dictionaryFromFilesNamed:@"autoAImap.plist" inFolder:@"Config" andMerge:YES];
767 NSString* autoAI = [autoAIMap oo_stringForKey:string];
768
769 OOJS_RETURN_OBJECT(autoAI);
770
772}
773
774// pauseGame() : Boolean
776{
777 OOJS_NATIVE_ENTER(context)
778
779 BOOL result = NO;
780 PlayerEntity* player = PLAYER;
781
782 if (player) {
783 OOGUIScreenID guiScreen = [player guiScreen];
784
785 if (guiScreen != GUI_SCREEN_LONG_RANGE_CHART && guiScreen != GUI_SCREEN_MISSION && guiScreen != GUI_SCREEN_REPORT && guiScreen != GUI_SCREEN_KEYBOARD_ENTRY && guiScreen != GUI_SCREEN_SAVE) {
786 [UNIVERSE pauseGame];
787 result = YES;
788 }
789 }
790
791 OOJS_RETURN_BOOL(result);
792
794}
795
796#ifdef OO_DEBUG_MONITOR_ENABLED
797// quitGame() : Boolean
798static JSValue GlobalQuitGame(OOJS_NATIVE_ARGS)
799{
800 OOJS_NATIVE_ENTER(context)
801
802 OOLog(@"script.debug.quit", @"%@", @"Quit requested via JavaScript global.quitGame()");
803
804 /* Quit once the calling script has returned, so that the JavaScript
805 runtime can be shut down and leak-checked with no script on the stack.
806 */
807 [UNIVERSE performSelector:@selector(quitGame) withObject:nil afterDelay:0.0];
808
809 OOJS_RETURN_BOOL(YES);
810
812}
813#endif
OOINLINE OOGUIScreenID OOGUIScreenIDFromJSValue(JSContext *context, jsval value)
OOINLINE jsval OOJSValueFromGUIScreenID(JSContext *context, OOGUIScreenID value)
NSString * CommodityDisplayNameForSymbolicName(NSString *symbolicName)
#define EXPECT_NOT(x)
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
void SetUpOOJSGlobal(JSContext *context, JSObject *global)
Definition OOJSGlobal.m:161
static JSBool GlobalSetScreenBackground(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:517
static JSBool GlobalExpandDescription(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:269
static JSBool GlobalPauseGame(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:756
static JSBool GlobalSetScreenOverlay(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:598
static JSBool GlobalGetScreenBackgroundForKey(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:547
static JSBool GlobalLog(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:230
static JSBool GlobalGetGuiColorSettingForKey(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:624
static JSBool GlobalGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSGlobal.m:167
static JSFunctionSpec sGlobalMethods[]
Definition OOJSGlobal.m:124
@ kGlobal_guiScreen
Definition OOJSGlobal.m:108
@ kGlobal_global
Definition OOJSGlobal.m:107
@ kGlobal_galaxyNumber
Definition OOJSGlobal.m:106
static JSPropertySpec sGlobalProperties[]
Definition OOJSGlobal.m:114
static JSBool GlobalExpandMissionText(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:319
static JSBool GlobalSetScreenBackgroundForKey(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:568
static JSBool GlobalKeyBindingDescription(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:295
static JSBool GlobalSetExtraGuiScreenKeys(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:436
static JSBool GlobalDisplayNameForCommodity(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:345
static JSBool GlobalRandomInhabitantsDescription(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:384
static JSBool GlobalClearExtraGuiScreenKeys(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:404
static JSBool GlobalQuitGame(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:778
static JSBool GlobalSetGuiColorSettingForKey(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:651
static JSBool GlobalAutoAIForRole(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:734
static JSBool GlobalRandomName(JSContext *context, uintN argc, jsval *vp)
Definition OOJSGlobal.m:363
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
void OOJSReportWarning(JSContext *context, NSString *format,...)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
#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)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
OOINLINE void OOJSReportPendingException(JSContext *context)
#define OOJS_ARGV
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, jsval value)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
NSDictionary * OOJSDictionaryFromStringTable(JSContext *context, jsval value)
#define OOJS_RETURN_VOID
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define MIN(A, B)
Definition OOMaths.h:91
return nil
@ kOOExpandForJavaScript
Report warnings through JavaScript runtime system instead of normal logging.
@ kOOExpandBackslashN
Convert literal "\\n"s to line breaks (used for missiontext.plist for historical reasons).
@ kOOExpandGoodRNG
Use RANDROT for selecting from description arrays and for N expansion.
NSString * OOExpandDescriptionString(Random_Seed seed, NSString *string, NSDictionary *overrides, NSDictionary *legacyLocals, NSString *systemName, OOExpandOptions options)
#define OOExpand(string,...)
static NSString *const kOOLogDebugMessage
OOGUIScreenID
#define PLAYER
static JSValue GetParam(JSContext *context, JSValueConst object, const char *key)
Definition OOJSGlobal.m:411
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_SETTER_ARGS
#define OOJS_PROPERTY_RO(name, getter, magic)
#define OOJS_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
#define UNIVERSE
Definition Universe.h:827
OOColor * colorFromSetting:defaultValue:(NSString *setting,[defaultValue] OOColor *def)
BOOL setForegroundTextureDescriptor:(NSDictionary *descriptor)
void setGuiColorSettingFromKey:color:(NSString *key,[color] OOColor *col)
NSDictionary * textureDescriptorFromJSValue:inContext:callerDescription:(jsval value,[inContext] JSContext *context,[callerDescription] NSString *callerDescription)
BOOL setBackgroundTextureDescriptor:(NSDictionary *descriptor)
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
void setRegisterKeys:(NSDictionary *registerKeys)
void setCallbackThis:(JSObject *callbackthis)
OOJavaScriptEngine * sharedEngine()
BOOL setExtraGuiScreenKeys:definition:(OOGUIScreenID gui,[definition] OOJSGuiScreenKeyDefinition *definition)
NSString * keyBindingDescription2:(NSString *binding)
void clearExtraGuiScreenKeys:key:(OOGUIScreenID gui,[key] NSString *key)
OOGUIScreenID guiScreen()
NSDictionary * dictionaryFromFilesNamed:inFolder:andMerge:(NSString *fileName,[inFolder] NSString *folderName,[andMerge] BOOL mergeFiles)
const Random_Seed kNilRandomSeed
void make_pseudo_random_seed(Random_Seed *seed_ptr)
void setRandomSeed(RNG_Seed a_seed)
RNG_Seed currentRandomSeed(void)
unsigned Ranrot(void)