Ticket #2140: console_global_functions_v1.0.diff

File console_global_functions_v1.0.diff, 14.6 KB (added by Yves, 11 years ago)
  • binaries/data/mods/public/gui/text/help.txt

     
    4343Class Timer:
    4444
    4545setTimeout( 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).
     46Example: setTimeout( Engine.Console_Write("1 second passed"), 1000 ); Returns the ID of the timer (int).
    4747
    4848setIntervel( code, int time ) - nearly the same as setTimeout(), but "code" is executed every "time" milliseconds instead of once after a single delay.
    4949cancelIntervel() - causes all functions registered with setInterval to no longer be called.
  • binaries/data/mods/public/gui/session/session.js

     
    668668            break;
    669669
    670670        default:
    671             console.write("Unrecognized ambient type: " + type);
     671            Engine.Console_Write("Unrecognized ambient type: " + type);
    672672            break;
    673673    }
    674674}
  • binaries/data/mods/public/gui/session/messages.js

     
    386386
    387387        if (msg.action)
    388388        {
    389             console.write(msg.prefix + "* " + username + " " + message);
     389            Engine.Console_Write(msg.prefix + "* " + username + " " + message);
    390390            formatted = msg.prefix + "* [color=\"" + playerColor + "\"]" + username + "[/color] " + message;
    391391        }
    392392        else
    393393        {
    394             console.write(msg.prefix + "<" + username + "> " + message);
     394            Engine.Console_Write(msg.prefix + "<" + username + "> " + message);
    395395            formatted = msg.prefix + "<[color=\"" + playerColor + "\"]" + username + "[/color]> " + message;
    396396        }
    397397        break;
  • binaries/data/mods/public/gui/common/functions_utility_music.js

     
    5353    var soundArray = buildDirEntList(randomSoundPath, "*" + soundSubType + "*", false);
    5454    if (soundArray.length == 0)
    5555    {
    56         console.write ("Failed to find sounds matching '*"+soundSubType+"*'");
     56        Engine.Console_Write ("Failed to find sounds matching '*"+soundSubType+"*'");
    5757        return undefined;
    5858    }
    5959    // Get a random number within the sound's range.
     
    6464    // Build path to random audio file.
    6565    randomSoundPath = randomFileName;
    6666
    67     //console.write("Playing " + randomSoundPath + " ...");
     67    //Engine.Console_Write("Playing " + randomSoundPath + " ...");
    6868
    6969    switch (soundType)
    7070    {
     
    7575            return new AmbientSound(randomSoundPath);
    7676        break;
    7777        case "effect":
    78             console.write("am loading effect '*"+randomSoundPath+"*'");
     78            Engine.Console_Write("am loading effect '*"+randomSoundPath+"*'");
    7979        break;
    8080        default:
    8181        break;
     
    149149//          break;
    150150//
    151151//      default:
    152 //          console.write("Unrecognized ambient type: " + type);
     152//          Engine.Console_Write("Unrecognized ambient type: " + type);
    153153//          break;
    154154//  }
    155155//}
  • binaries/data/mods/public/gui/common/functions_utility_list.js

     
    1111function removeItem (objectName, pos)
    1212{
    1313    if (getGUIObjectByName (objectName) == null)
    14         console.write ("removeItem(): " + objectName + " not found.");
     14        Engine.Console_Write ("removeItem(): " + objectName + " not found.");
    1515
    1616    var list = getGUIObjectByName (objectName).list;
    1717    var selected = getGUIObjectByName (objectName).selected;
     
    4141function addItem (objectName, pos, value)
    4242{
    4343    if (getGUIObjectByName (objectName) == null)
    44         console.write ("addItem(): " + objectName + " not found.");
     44        Engine.Console_Write ("addItem(): " + objectName + " not found.");
    4545
    4646    var list = getGUIObjectByName (objectName).list;
    4747    var selected = getGUIObjectByName (objectName).selected;
     
    6666function pushItem (objectName, value)
    6767{
    6868    if (getGUIObjectByName (objectName) == null)
    69         console.write ("pushItem(): " + objectName + " not found.");
     69        Engine.Console_Write ("pushItem(): " + objectName + " not found.");
    7070
    7171    var list = getGUIObjectByName (objectName).list;
    7272    list.push (value);
     
    8181function popItem (objectName)
    8282{
    8383    if (getGUIObjectByName (objectName) == null)
    84         console.write ("popItem(): " + objectName + " not found.");
     84        Engine.Console_Write ("popItem(): " + objectName + " not found.");
    8585
    8686    var selected = getGUIObjectByName (objectName).selected;
    8787    removeItem(objectName, getNumItems(objectName)-1);
     
    9898function getNumItems (objectName)
    9999{
    100100    if (getGUIObjectByName (objectName) == null)
    101         console.write ("getNumItems(): " + objectName + " not found.");
     101        Engine.Console_Write ("getNumItems(): " + objectName + " not found.");
    102102
    103103    var list = getGUIObjectByName(objectName).list;
    104104    return list.length;
     
    110110function getItemValue (objectName, pos)
    111111{
    112112    if (getGUIObjectByName (objectName) == null)
    113         console.write ("getItemValue(): " + objectName + " not found.");
     113        Engine.Console_Write ("getItemValue(): " + objectName + " not found.");
    114114
    115115    var list = getGUIObjectByName(objectName).list;
    116116    return list[pos];
     
    122122function getCurrItemValue (objectName)
    123123{
    124124    if (getGUIObjectByName (objectName) == null)
    125         console.write ("getCurrItemValue(): " + objectName + " not found.");
     125        Engine.Console_Write ("getCurrItemValue(): " + objectName + " not found.");
    126126
    127127    if (getGUIObjectByName(objectName).selected == -1)
    128128        return "";
     
    137137function setCurrItemValue (objectName, string)
    138138{
    139139    if (getGUIObjectByName(objectName) == null) {
    140         console.write ("setCurrItemValue(): " + objectName + " not found.");
     140        Engine.Console_Write ("setCurrItemValue(): " + objectName + " not found.");
    141141        return -1;
    142142    }
    143143
     
    157157    }
    158158
    159159    // 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.");
    161161    return -2;
    162162}
    163163
  • source/scripting/ScriptGlue.cpp

     
    402402    {0}
    403403};
    404404#undef JS_FUNC
    405 
    406 
    407 //-----------------------------------------------------------------------------
    408 // property accessors
    409 //-----------------------------------------------------------------------------
    410 
    411 
    412 enum ScriptGlobalTinyIDs
    413 {
    414     GLOBAL_SELECTION,
    415     GLOBAL_GROUPSARRAY,
    416     GLOBAL_CAMERA,
    417     GLOBAL_CONSOLE,
    418     GLOBAL_LIGHTENV
    419 };
    420 
    421 JSPropertySpec ScriptGlobalTable[] =
    422 {
    423     { "console"    , GLOBAL_CONSOLE,     JSPROP_PERMANENT|JSPROP_READONLY, JSI_Console::getConsole, 0 },
    424 
    425     // end of table marker
    426     { 0, 0, 0, 0, 0 },
    427 };
  • source/scripting/ScriptGlue.h

     
    2323
    2424// referenced by ScriptingHost.cpp
    2525extern JSFunctionSpec ScriptFunctionTable[];
    26 extern JSPropertySpec ScriptGlobalTable[];
    2726
    2827// dependencies (moved to header to avoid L4 warnings)
    2928// .. from main.cpp:
  • source/scripting/ScriptingHost.cpp

     
    3737
    3838    if (!JS_DefineFunctions(m_Context, m_GlobalObject, ScriptFunctionTable))
    3939        throw PSERROR_Scripting_SetupFailed();
    40 
    41     if (!JS_DefineProperties(m_Context, m_GlobalObject, ScriptGlobalTable))
    42         throw PSERROR_Scripting_SetupFailed();
    4340}
    4441
    4542ScriptingHost::~ScriptingHost()
  • source/ps/scripting/JSInterface_Console.cpp

     
    1515 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    1616 */
    1717
    18 // JavaScript interface to native code selection and group objects
     18#include "precompiled.h"
    1919
    20 #include "precompiled.h"
     20#include "scriptinterface/ScriptInterface.h"
    2121#include "JSInterface_Console.h"
    2222#include "ps/CConsole.h"
    23 #include "scripting/JSConversions.h"
     23#include "ps/CLogger.h"
    2424
    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)
     25bool JSI_Console::CheckGlobalInitialized()
     26{   
     27    if (!g_Console)
    5528    {
    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;
    6231    }
     32    return true;
    6333}
    6434
    65 JSBool JSI_Console::setProperty(JSContext* UNUSED(cx), JSObject* UNUSED(obj), jsid id, JSBool UNUSED(strict), jsval* vp)
     35bool JSI_Console::GetVisibleEnabled(void* UNUSED(cbdata))
    6636{
    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();
    8740}
    8841
    89 void JSI_Console::init()
     42void JSI_Console::SetVisibleEnabled(void* UNUSED(cbdata), bool Enabled)
    9043{
    91     g_ScriptingHost.DefineCustomObjectType(&JSI_class, NULL, 0, JSI_props, JSI_methods, NULL, NULL);
     44    if (!CheckGlobalInitialized())
     45        return;
     46    g_Console->SetVisible(Enabled);
    9247}
    9348
    94 JSBool JSI_Console::getConsole(JSContext* cx, JSObject* UNUSED(obj), jsid UNUSED(id), jsval* vp)
     49void JSI_Console::Write(void* UNUSED(cbdata), std::wstring output)
    9550{
    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());
    9954}
    10055
    101 JSBool JSI_Console::writeConsole(JSContext* cx, uintN argc, jsval* vp)
     56void JSI_Console::RegisterScriptFunctions(ScriptInterface& scriptInterface)
    10257{
    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");
    12461}
  • source/ps/scripting/JSInterface_Console.h

     
    1 /* Copyright (C) 2009 Wildfire Games.
     1/* Copyright (C) 2013 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    1515 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
    1616 */
    1717
    18 // JSInterface_Console.h
    19 //
    20 // The JavaScript wrapper around the console system
    21 
    22 #include "scripting/ScriptingHost.h"
    23 
    2418#ifndef INCLUDED_JSI_CONSOLE
    2519#define INCLUDED_JSI_CONSOLE
    2620
     21class ScriptInterface;
     22
    2723namespace JSI_Console
    2824{
    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);
    4531}
    4632
    4733#endif
  • source/ps/GameSetup/GameSetup.cpp

     
    321321    // maths
    322322    JSI_Vector3D::init();
    323323
    324     // ps
    325     JSI_Console::init();
    326 
    327324    // GUI
    328325    CGUI::ScriptingInit();
    329326
  • source/gui/scripting/ScriptFunctions.cpp

     
    4141#include "ps/ProfileViewer.h"
    4242#include "ps/Pyrogenesis.h"
    4343#include "ps/SavedGame.h"
     44#include "ps/scripting/JSInterface_Console.h"
    4445#include "ps/UserReport.h"
    4546#include "ps/GameSetup/Atlas.h"
    4647#include "ps/GameSetup/Config.h"
     
    654655{
    655656    JSI_GameView::RegisterScriptFunctions(scriptInterface);
    656657    JSI_Renderer::RegisterScriptFunctions(scriptInterface);
     658    JSI_Console::RegisterScriptFunctions(scriptInterface);
    657659
    658660    // GUI manager functions:
    659661    scriptInterface.RegisterFunction<CScriptVal, &GetActiveGui>("GetActiveGui");