Ticket #2140: console_global_functions_v1.0.diff
File console_global_functions_v1.0.diff, 14.6 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/text/help.txt
43 43 Class Timer: 44 44 45 45 setTimeout( code, int time ) - time is a delay in milliseconds. When this time has elapsed, "code" is executed once. Code can be a fragment or function 46 Example: setTimeout( console.write("1 second passed"), 1000 ); Returns the ID of the timer (int).46 Example: setTimeout( Engine.Console_Write("1 second passed"), 1000 ); Returns the ID of the timer (int). 47 47 48 48 setIntervel( code, int time ) - nearly the same as setTimeout(), but "code" is executed every "time" milliseconds instead of once after a single delay. 49 49 cancelIntervel() - causes all functions registered with setInterval to no longer be called. -
binaries/data/mods/public/gui/session/session.js
668 668 break; 669 669 670 670 default: 671 console.write("Unrecognized ambient type: " + type);671 Engine.Console_Write("Unrecognized ambient type: " + type); 672 672 break; 673 673 } 674 674 } -
binaries/data/mods/public/gui/session/messages.js
386 386 387 387 if (msg.action) 388 388 { 389 console.write(msg.prefix + "* " + username + " " + message);389 Engine.Console_Write(msg.prefix + "* " + username + " " + message); 390 390 formatted = msg.prefix + "* [color=\"" + playerColor + "\"]" + username + "[/color] " + message; 391 391 } 392 392 else 393 393 { 394 console.write(msg.prefix + "<" + username + "> " + message);394 Engine.Console_Write(msg.prefix + "<" + username + "> " + message); 395 395 formatted = msg.prefix + "<[color=\"" + playerColor + "\"]" + username + "[/color]> " + message; 396 396 } 397 397 break; -
binaries/data/mods/public/gui/common/functions_utility_music.js
53 53 var soundArray = buildDirEntList(randomSoundPath, "*" + soundSubType + "*", false); 54 54 if (soundArray.length == 0) 55 55 { 56 console.write ("Failed to find sounds matching '*"+soundSubType+"*'");56 Engine.Console_Write ("Failed to find sounds matching '*"+soundSubType+"*'"); 57 57 return undefined; 58 58 } 59 59 // Get a random number within the sound's range. … … 64 64 // Build path to random audio file. 65 65 randomSoundPath = randomFileName; 66 66 67 // console.write("Playing " + randomSoundPath + " ...");67 //Engine.Console_Write("Playing " + randomSoundPath + " ..."); 68 68 69 69 switch (soundType) 70 70 { … … 75 75 return new AmbientSound(randomSoundPath); 76 76 break; 77 77 case "effect": 78 console.write("am loading effect '*"+randomSoundPath+"*'");78 Engine.Console_Write("am loading effect '*"+randomSoundPath+"*'"); 79 79 break; 80 80 default: 81 81 break; … … 149 149 // break; 150 150 // 151 151 // default: 152 // console.write("Unrecognized ambient type: " + type);152 // Engine.Console_Write("Unrecognized ambient type: " + type); 153 153 // break; 154 154 // } 155 155 //} -
binaries/data/mods/public/gui/common/functions_utility_list.js
11 11 function removeItem (objectName, pos) 12 12 { 13 13 if (getGUIObjectByName (objectName) == null) 14 console.write ("removeItem(): " + objectName + " not found.");14 Engine.Console_Write ("removeItem(): " + objectName + " not found."); 15 15 16 16 var list = getGUIObjectByName (objectName).list; 17 17 var selected = getGUIObjectByName (objectName).selected; … … 41 41 function addItem (objectName, pos, value) 42 42 { 43 43 if (getGUIObjectByName (objectName) == null) 44 console.write ("addItem(): " + objectName + " not found.");44 Engine.Console_Write ("addItem(): " + objectName + " not found."); 45 45 46 46 var list = getGUIObjectByName (objectName).list; 47 47 var selected = getGUIObjectByName (objectName).selected; … … 66 66 function pushItem (objectName, value) 67 67 { 68 68 if (getGUIObjectByName (objectName) == null) 69 console.write ("pushItem(): " + objectName + " not found.");69 Engine.Console_Write ("pushItem(): " + objectName + " not found."); 70 70 71 71 var list = getGUIObjectByName (objectName).list; 72 72 list.push (value); … … 81 81 function popItem (objectName) 82 82 { 83 83 if (getGUIObjectByName (objectName) == null) 84 console.write ("popItem(): " + objectName + " not found.");84 Engine.Console_Write ("popItem(): " + objectName + " not found."); 85 85 86 86 var selected = getGUIObjectByName (objectName).selected; 87 87 removeItem(objectName, getNumItems(objectName)-1); … … 98 98 function getNumItems (objectName) 99 99 { 100 100 if (getGUIObjectByName (objectName) == null) 101 console.write ("getNumItems(): " + objectName + " not found.");101 Engine.Console_Write ("getNumItems(): " + objectName + " not found."); 102 102 103 103 var list = getGUIObjectByName(objectName).list; 104 104 return list.length; … … 110 110 function getItemValue (objectName, pos) 111 111 { 112 112 if (getGUIObjectByName (objectName) == null) 113 console.write ("getItemValue(): " + objectName + " not found.");113 Engine.Console_Write ("getItemValue(): " + objectName + " not found."); 114 114 115 115 var list = getGUIObjectByName(objectName).list; 116 116 return list[pos]; … … 122 122 function getCurrItemValue (objectName) 123 123 { 124 124 if (getGUIObjectByName (objectName) == null) 125 console.write ("getCurrItemValue(): " + objectName + " not found.");125 Engine.Console_Write ("getCurrItemValue(): " + objectName + " not found."); 126 126 127 127 if (getGUIObjectByName(objectName).selected == -1) 128 128 return ""; … … 137 137 function setCurrItemValue (objectName, string) 138 138 { 139 139 if (getGUIObjectByName(objectName) == null) { 140 console.write ("setCurrItemValue(): " + objectName + " not found.");140 Engine.Console_Write ("setCurrItemValue(): " + objectName + " not found."); 141 141 return -1; 142 142 } 143 143 … … 157 157 } 158 158 159 159 // Return -2 if failed to find value in list. 160 console.write ("Requested string '" + string + "' not found in " + objectName + "'s list.");160 Engine.Console_Write ("Requested string '" + string + "' not found in " + objectName + "'s list."); 161 161 return -2; 162 162 } 163 163 -
source/scripting/ScriptGlue.cpp
402 402 {0} 403 403 }; 404 404 #undef JS_FUNC 405 406 407 //-----------------------------------------------------------------------------408 // property accessors409 //-----------------------------------------------------------------------------410 411 412 enum ScriptGlobalTinyIDs413 {414 GLOBAL_SELECTION,415 GLOBAL_GROUPSARRAY,416 GLOBAL_CAMERA,417 GLOBAL_CONSOLE,418 GLOBAL_LIGHTENV419 };420 421 JSPropertySpec ScriptGlobalTable[] =422 {423 { "console" , GLOBAL_CONSOLE, JSPROP_PERMANENT|JSPROP_READONLY, JSI_Console::getConsole, 0 },424 425 // end of table marker426 { 0, 0, 0, 0, 0 },427 }; -
source/scripting/ScriptGlue.h
23 23 24 24 // referenced by ScriptingHost.cpp 25 25 extern JSFunctionSpec ScriptFunctionTable[]; 26 extern JSPropertySpec ScriptGlobalTable[];27 26 28 27 // dependencies (moved to header to avoid L4 warnings) 29 28 // .. from main.cpp: -
source/scripting/ScriptingHost.cpp
37 37 38 38 if (!JS_DefineFunctions(m_Context, m_GlobalObject, ScriptFunctionTable)) 39 39 throw PSERROR_Scripting_SetupFailed(); 40 41 if (!JS_DefineProperties(m_Context, m_GlobalObject, ScriptGlobalTable))42 throw PSERROR_Scripting_SetupFailed();43 40 } 44 41 45 42 ScriptingHost::~ScriptingHost() -
source/ps/scripting/JSInterface_Console.cpp
15 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 16 */ 17 17 18 // JavaScript interface to native code selection and group objects 18 #include "precompiled.h" 19 19 20 #include " precompiled.h"20 #include "scriptinterface/ScriptInterface.h" 21 21 #include "JSInterface_Console.h" 22 22 #include "ps/CConsole.h" 23 #include " scripting/JSConversions.h"23 #include "ps/CLogger.h" 24 24 25 JSClass JSI_Console::JSI_class = 26 { 27 "Console", 0, 28 JS_PropertyStub, JS_PropertyStub, 29 JSI_Console::getProperty, JSI_Console::setProperty, 30 JS_EnumerateStub, JS_ResolveStub, 31 JS_ConvertStub, JS_FinalizeStub, 32 NULL, NULL, NULL, NULL 33 }; 34 35 JSPropertySpec JSI_Console::JSI_props[] = 36 { 37 { "visible", JSI_Console::console_visible, JSPROP_ENUMERATE }, 38 { 0 } 39 }; 40 41 JSFunctionSpec JSI_Console::JSI_methods[] = 42 { 43 { "write", JSI_Console::writeConsole, 1, 0 }, 44 { 0 }, 45 }; 46 47 JSBool JSI_Console::getProperty(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid id, jsval* vp) 48 { 49 if (!JSID_IS_INT(id)) 50 return JS_TRUE; 51 52 int i = JSID_TO_INT(id); 53 54 switch (i) 25 bool JSI_Console::CheckGlobalInitialized() 26 { 27 if (!g_Console) 55 28 { 56 case console_visible: 57 *vp = BOOLEAN_TO_JSVAL(g_Console->IsActive()); 58 return JS_TRUE; 59 default: 60 *vp = JSVAL_NULL; 61 return JS_TRUE; 29 LOGERROR(L"Trying to access the console when it's not initialized!"); 30 return false; 62 31 } 32 return true; 63 33 } 64 34 65 JSBool JSI_Console::setProperty(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid id, JSBool UNUSED(strict), jsval* vp)35 bool JSI_Console::GetVisibleEnabled(void* UNUSED(cbdata)) 66 36 { 67 if (!JSID_IS_INT(id)) 68 return JS_TRUE; 69 70 int i = JSID_TO_INT(id); 71 72 switch (i) 73 { 74 case console_visible: 75 try 76 { 77 g_Console->SetVisible(ToPrimitive<bool> (*vp)); 78 return JS_TRUE; 79 } 80 catch (PSERROR_Scripting_ConversionFailed&) 81 { 82 return JS_TRUE; 83 } 84 default: 85 return JS_TRUE; 86 } 37 if (!CheckGlobalInitialized()) 38 return false; 39 return g_Console->IsActive(); 87 40 } 88 41 89 void JSI_Console:: init()42 void JSI_Console::SetVisibleEnabled(void* UNUSED(cbdata), bool Enabled) 90 43 { 91 g_ScriptingHost.DefineCustomObjectType(&JSI_class, NULL, 0, JSI_props, JSI_methods, NULL, NULL); 44 if (!CheckGlobalInitialized()) 45 return; 46 g_Console->SetVisible(Enabled); 92 47 } 93 48 94 JSBool JSI_Console::getConsole(JSContext* cx, JSObject* UNUSED(obj), jsid UNUSED(id), jsval* vp)49 void JSI_Console::Write(void* UNUSED(cbdata), std::wstring output) 95 50 { 96 JSObject* console = JS_NewObject(cx, &JSI_Console::JSI_class, NULL, NULL);97 *vp = OBJECT_TO_JSVAL(console);98 return JS_TRUE;51 if (!CheckGlobalInitialized()) 52 return; 53 g_Console->InsertMessage(L"%ls", output.c_str()); 99 54 } 100 55 101 JSBool JSI_Console::writeConsole(JSContext* cx, uintN argc, jsval* vp)56 void JSI_Console::RegisterScriptFunctions(ScriptInterface& scriptInterface) 102 57 { 103 UNUSED2(cx); 104 105 CStrW output; 106 for (uintN i = 0; i < argc; i++) 107 { 108 try 109 { 110 CStrW arg = g_ScriptingHost.ValueToUCString(JS_ARGV(cx, vp)[i]); 111 output += arg; 112 } 113 catch (PSERROR_Scripting_ConversionFailed&) 114 { 115 } 116 } 117 118 // TODO: What if the console has been destroyed already? 119 if (g_Console) 120 g_Console->InsertMessage(L"%ls", output.c_str()); 121 122 JS_SET_RVAL(cx, vp, JSVAL_VOID); 123 return JS_TRUE; 58 scriptInterface.RegisterFunction<bool, &JSI_Console::GetVisibleEnabled>("Console_GetVisibleEnabled"); 59 scriptInterface.RegisterFunction<void, bool, &JSI_Console::SetVisibleEnabled>("Console_SetVisibleEnabled"); 60 scriptInterface.RegisterFunction<void, std::wstring, &JSI_Console::Write>("Console_Write"); 124 61 } -
source/ps/scripting/JSInterface_Console.h
1 /* Copyright (C) 20 09Wildfire Games.1 /* Copyright (C) 2013 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify … … 15 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 16 */ 17 17 18 // JSInterface_Console.h19 //20 // The JavaScript wrapper around the console system21 22 #include "scripting/ScriptingHost.h"23 24 18 #ifndef INCLUDED_JSI_CONSOLE 25 19 #define INCLUDED_JSI_CONSOLE 26 20 21 class ScriptInterface; 22 27 23 namespace JSI_Console 28 24 { 29 enum 30 { 31 console_visible 32 }; 33 extern JSClass JSI_class; 34 extern JSPropertySpec JSI_props[]; 35 extern JSFunctionSpec JSI_methods[]; 36 37 JSBool getProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp); 38 JSBool setProperty(JSContext* cx, JSObject* obj, jsid id, JSBool strict, jsval* vp); 39 40 JSBool getConsole(JSContext* context, JSObject* obj, jsid id, jsval* vp); 41 42 void init(); 43 44 JSBool writeConsole(JSContext* cx, uintN argc, jsval* vp); 25 bool CheckGlobalInitialized(); 26 bool GetVisibleEnabled(void* cbdata); 27 void SetVisibleEnabled(void* cbdata, bool Enabled); 28 void Write(void* cbdata, std::wstring output); 29 30 void RegisterScriptFunctions(ScriptInterface& scriptInterface); 45 31 } 46 32 47 33 #endif -
source/ps/GameSetup/GameSetup.cpp
321 321 // maths 322 322 JSI_Vector3D::init(); 323 323 324 // ps325 JSI_Console::init();326 327 324 // GUI 328 325 CGUI::ScriptingInit(); 329 326 -
source/gui/scripting/ScriptFunctions.cpp
41 41 #include "ps/ProfileViewer.h" 42 42 #include "ps/Pyrogenesis.h" 43 43 #include "ps/SavedGame.h" 44 #include "ps/scripting/JSInterface_Console.h" 44 45 #include "ps/UserReport.h" 45 46 #include "ps/GameSetup/Atlas.h" 46 47 #include "ps/GameSetup/Config.h" … … 654 655 { 655 656 JSI_GameView::RegisterScriptFunctions(scriptInterface); 656 657 JSI_Renderer::RegisterScriptFunctions(scriptInterface); 658 JSI_Console::RegisterScriptFunctions(scriptInterface); 657 659 658 660 // GUI manager functions: 659 661 scriptInterface.RegisterFunction<CScriptVal, &GetActiveGui>("GetActiveGui");