Ticket #2172: ConfigDB_global_functions_v1.0.diff
File ConfigDB_global_functions_v1.0.diff, 14.4 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/session/input.js
1539 1539 1540 1540 function jumpCamera(index) 1541 1541 { 1542 var position = jumpCameraPositions[index], distanceThreshold = g_ConfigDB.system["camerajump.threshold"];1542 var position = jumpCameraPositions[index], distanceThreshold = Engine.ConfigDB_GetValue("system", "camerajump.threshold"); 1543 1543 if (position) 1544 1544 { 1545 1545 if (jumpCameraLast && -
binaries/data/mods/public/gui/session/unit_commands.js
446 446 447 447 case TRAINING: 448 448 var tooltip = getEntityNamesFormatted(template); 449 var key = g_ConfigDB.system["hotkey.session.queueunit." + (i + 1)];449 var key = Engine.ConfigDB_GetValue("system", "hotkey.session.queueunit." + (i + 1)); 450 450 if (key) 451 451 tooltip = "[color=\"255 251 131\"][font=\"serif-bold-16\"][" + key + "][/font][/color] " + tooltip; 452 452 -
binaries/data/mods/public/gui/common/functions_utility_music.js
118 118 if (inHandle) 119 119 { 120 120 inHandle.play(); 121 fadeIn(inHandle, g_ConfigDB.system["sound.mastergain"], fadeDuration);121 fadeIn(inHandle, Engine.ConfigDB_GetValue("system", "sound.mastergain"), fadeDuration); 122 122 } 123 123 124 124 return true; … … 168 168 //{ 169 169 // var buttonSound = new Sound(BUTTON_SOUND); 170 170 // buttonSound.play(); 171 //} 172 Kein Zeilenumbruch am Ende der Datei 171 //} -
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_ConfigDB.h" 44 45 #include "ps/scripting/JSInterface_Console.h" 45 46 #include "ps/UserReport.h" 46 47 #include "ps/GameSetup/Atlas.h" … … 656 657 JSI_GameView::RegisterScriptFunctions(scriptInterface); 657 658 JSI_Renderer::RegisterScriptFunctions(scriptInterface); 658 659 JSI_Console::RegisterScriptFunctions(scriptInterface); 660 JSI_ConfigDB::RegisterScriptFunctions(scriptInterface); 659 661 660 662 // GUI manager functions: 661 663 scriptInterface.RegisterFunction<CScriptVal, &GetActiveGui>("GetActiveGui"); -
source/ps/scripting/JSInterface_ConfigDB.h
1 /* Copyright (C) 2013 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef INCLUDED_JSI_CONFIGDB 19 #define INCLUDED_JSI_CONFIGDB 20 21 #include "ps/ConfigDB.h" 22 23 class ScriptInterface; 24 25 namespace JSI_ConfigDB 26 { 27 bool GetConfigNamespace(std::wstring cfgNsString, EConfigNamespace& cfgNs); 28 std::string GetValue(void* cbdata, std::wstring cfgNsString, std::string name); 29 bool CreateValue(void* cbdata, std::wstring cfgNsString, std::string name, std::string value); 30 bool WriteFile(void* cbdata, std::wstring cfgNsString, Path path); 31 bool Reload(void* cbdata, std::wstring cfgNsString); 32 bool SetFile(void* cbdata, std::wstring cfgNsString, Path path); 33 void RegisterScriptFunctions(ScriptInterface& scriptInterface); 34 } 35 36 #endif -
source/ps/scripting/JSInterface_ConfigDB.cpp
1 /* Copyright (C) 2013 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 20 #include "JSInterface_ConfigDB.h" 21 22 #include "ps/ConfigDB.h" 23 #include "ps/CLogger.h" 24 #include "scriptinterface/ScriptInterface.h" 25 26 bool JSI_ConfigDB::GetConfigNamespace(std::wstring cfgNsString, EConfigNamespace& cfgNs) 27 { 28 if (cfgNsString == L"default") 29 cfgNs = CFG_DEFAULT; 30 else if (cfgNsString == L"system") 31 cfgNs = CFG_SYSTEM; 32 else if (cfgNsString == L"user") 33 cfgNs = CFG_USER; 34 else if (cfgNsString == L"mod") 35 cfgNs = CFG_MOD; 36 else 37 { 38 LOGERROR(L"Invalid namespace name passed to the ConfigDB!"); 39 return false; 40 } 41 return true; 42 } 43 44 std::string JSI_ConfigDB::GetValue(void* UNUSED(cbdata), std::wstring cfgNsString, std::string name) 45 { 46 EConfigNamespace cfgNs; 47 if (!GetConfigNamespace(cfgNsString, cfgNs)) 48 return std::string(); 49 50 CConfigValue *val = g_ConfigDB.GetValue(cfgNs, name); 51 if (val) 52 { 53 return val->m_String; 54 } 55 else 56 { 57 LOGWARNING(L"Config setting %s does not exist!", name.c_str()); 58 return std::string(); 59 } 60 } 61 62 bool JSI_ConfigDB::CreateValue(void* UNUSED(cbdata), std::wstring cfgNsString, std::string name, std::string value) 63 { 64 EConfigNamespace cfgNs; 65 if (!GetConfigNamespace(cfgNsString, cfgNs)) 66 return false; 67 68 CConfigValue *val = g_ConfigDB.CreateValue(cfgNs, name); 69 val->m_String = value; 70 return true; 71 } 72 73 bool JSI_ConfigDB::WriteFile(void* UNUSED(cbdata), std::wstring cfgNsString, Path path) 74 { 75 EConfigNamespace cfgNs; 76 if (!GetConfigNamespace(cfgNsString, cfgNs)) 77 return false; 78 79 bool ret = g_ConfigDB.WriteFile(cfgNs, path); 80 return ret; 81 } 82 83 bool JSI_ConfigDB::Reload(void* UNUSED(cbdata), std::wstring cfgNsString) 84 { 85 EConfigNamespace cfgNs; 86 if (!GetConfigNamespace(cfgNsString, cfgNs)) 87 return false; 88 89 bool ret = g_ConfigDB.Reload(cfgNs); 90 return ret; 91 } 92 93 bool JSI_ConfigDB::SetFile(void* UNUSED(cbdata), std::wstring cfgNsString, Path path) 94 { 95 EConfigNamespace cfgNs; 96 if (!GetConfigNamespace(cfgNsString, cfgNs)) 97 return false; 98 99 g_ConfigDB.SetConfigFile(cfgNs, path); 100 return true; 101 } 102 103 void JSI_ConfigDB::RegisterScriptFunctions(ScriptInterface& scriptInterface) 104 { 105 scriptInterface.RegisterFunction<std::string, std::wstring, std::string, &JSI_ConfigDB::GetValue>("ConfigDB_GetValue"); 106 scriptInterface.RegisterFunction<bool, std::wstring, std::string, std::string, &JSI_ConfigDB::CreateValue>("ConfigDB_CreateValue"); 107 scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::WriteFile>("ConfigDB_WriteFile"); 108 scriptInterface.RegisterFunction<bool, std::wstring, Path, &JSI_ConfigDB::SetFile>("ConfigDB_SetFile"); 109 scriptInterface.RegisterFunction<bool, std::wstring, &JSI_ConfigDB::Reload>("ConfigDB_Reload"); 110 111 } -
source/ps/GameSetup/GameSetup.cpp
919 919 920 920 InitScripting(); // before GUI 921 921 922 g_ConfigDB.RegisterJSConfigDB(); // after scripting923 924 922 // Optionally start profiler HTTP output automatically 925 923 // (By default it's only enabled by a hotkey, for security/performance) 926 924 bool profilerHTTPEnable = false; -
source/ps/ConfigDB.h
81 81 82 82 public: 83 83 CConfigDB(); 84 85 // NOTE: Construct the Singleton Object *after* JavaScript init, so that86 // the JS interface can be registered. ConfigDB (C++) needs to be initialized before87 // The ScriptInterface because the ScriptInterface requires some configuration information too.88 void RegisterJSConfigDB();89 84 90 85 /** 91 86 * Attempt to find a config variable with the given name; will search -
source/ps/ConfigDB.cpp
24 24 #include "ConfigDB.h" 25 25 #include "CLogger.h" 26 26 #include "Filesystem.h" 27 #include "scripting/ScriptingHost.h"28 27 #include "lib/allocators/shared_ptr.h" 29 28 30 #include "scriptinterface/ScriptInterface.h"31 32 29 typedef std::map <CStr, CConfigValueSet> TConfigMap; 33 30 TConfigMap CConfigDB::m_Map[CFG_LAST]; 34 31 VfsPath CConfigDB::m_ConfigFile[CFG_LAST]; 35 32 36 #define GET_NS_PRIVATE(cx, obj) (EConfigNamespace)((intptr_t)JS_GetPrivate(cx, obj) >> 1)37 38 namespace ConfigNamespace_JS39 {40 JSBool GetProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp)41 {42 EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);43 if (cfgNs < 0 || cfgNs >= CFG_LAST)44 return JS_FALSE;45 46 jsval idval;47 if (!JS_IdToValue(cx, id, &idval))48 return JS_FALSE;49 50 std::string propName;51 if (!ScriptInterface::FromJSVal(cx, idval, propName))52 return JS_FALSE;53 54 CConfigValue *val = g_ConfigDB.GetValue(cfgNs, propName);55 if (val)56 {57 JSString *js_str = JS_NewStringCopyN(cx, val->m_String.c_str(), val->m_String.size());58 *vp = STRING_TO_JSVAL(js_str);59 }60 return JS_TRUE;61 }62 63 JSBool SetProperty(JSContext* cx, JSObject* obj, jsid id, JSBool UNUSED(strict), jsval* vp)64 {65 EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj);66 if (cfgNs < 0 || cfgNs >= CFG_LAST)67 return JS_FALSE;68 69 jsval idval;70 if (!JS_IdToValue(cx, id, &idval))71 return JS_FALSE;72 73 std::string propName;74 if (!ScriptInterface::FromJSVal(cx, idval, propName))75 return JS_FALSE;76 77 CConfigValue *val = g_ConfigDB.CreateValue(cfgNs, propName);78 79 if (!ScriptInterface::FromJSVal(cx, *vp, val->m_String))80 return JS_FALSE;81 82 return JS_TRUE;83 }84 85 JSClass Class = {86 "ConfigNamespace", JSCLASS_HAS_PRIVATE,87 JS_PropertyStub, JS_PropertyStub,88 GetProperty, SetProperty,89 JS_EnumerateStub, JS_ResolveStub,90 JS_ConvertStub, JS_FinalizeStub91 };92 93 JSBool Construct(JSContext* cx, uintN argc, jsval* vp)94 {95 UNUSED2(argc);96 97 JSObject *newObj = JS_NewObject(cx, &Class, NULL, NULL);98 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObj));99 return JS_TRUE;100 }101 102 void SetNamespace(JSContext *cx, JSObject *obj, EConfigNamespace cfgNs)103 {104 JS_SetPrivate(cx, obj, (void *)((uintptr_t)cfgNs << 1)); // JS requires bottom bit = 0105 }106 107 JSBool WriteFile(JSContext* cx, uintN argc, jsval* vp)108 {109 EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, JS_THIS_OBJECT(cx, vp));110 if (cfgNs < 0 || cfgNs >= CFG_LAST)111 return JS_FALSE;112 113 if (argc != 1)114 return JS_FALSE;115 116 VfsPath path;117 if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], path))118 return JS_FALSE;119 120 bool res = g_ConfigDB.WriteFile(cfgNs, path);121 122 JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(res));123 return JS_TRUE;124 }125 126 JSBool Reload(JSContext* cx, uintN argc, jsval* vp)127 {128 if (argc != 0)129 return JS_FALSE;130 131 EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, JS_THIS_OBJECT(cx, vp));132 if (cfgNs < 0 || cfgNs >= CFG_LAST)133 return JS_FALSE;134 135 JSBool ret = g_ConfigDB.Reload(cfgNs);136 JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(ret));137 return JS_TRUE;138 }139 140 JSBool SetFile(JSContext* cx, uintN argc, jsval* vp)141 {142 EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, JS_THIS_OBJECT(cx, vp));143 if (cfgNs < 0 || cfgNs >= CFG_LAST)144 return JS_FALSE;145 146 if (argc != 1)147 return JS_FALSE;148 149 VfsPath path;150 if (!ScriptInterface::FromJSVal(cx, JS_ARGV(cx, vp)[0], path))151 return JS_FALSE;152 153 g_ConfigDB.SetConfigFile(cfgNs, path);154 155 JS_SET_RVAL(cx, vp, JSVAL_VOID);156 return JS_TRUE;157 }158 159 JSFunctionSpec Funcs[] = {160 { "writeFile", WriteFile, 2, 0 },161 { "reload", Reload, 0, 0 },162 { "setFile", SetFile, 2, 0 },163 {0}164 };165 };166 167 namespace ConfigDB_JS168 {169 JSClass Class = {170 "ConfigDB", 0,171 JS_PropertyStub, JS_PropertyStub,172 JS_PropertyStub, JS_StrictPropertyStub,173 JS_EnumerateStub, JS_ResolveStub,174 JS_ConvertStub, JS_FinalizeStub175 };176 177 JSPropertySpec Props[] = {178 {0}179 };180 181 JSFunctionSpec Funcs[] = {182 {0}183 };184 185 JSBool Construct(JSContext* cx, uintN argc, jsval* vp)186 {187 UNUSED2(argc);188 189 JSObject *newObj = JS_NewObject(cx, &Class, NULL, NULL);190 JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObj));191 192 int flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT;193 #define cfg_ns(_propname, _enum) STMT (\194 JSObject *nsobj=g_ScriptingHost.CreateCustomObject("ConfigNamespace"); \195 ENSURE(nsobj); \196 ConfigNamespace_JS::SetNamespace(cx, nsobj, _enum); \197 ENSURE(JS_DefineProperty(cx, newObj, _propname, OBJECT_TO_JSVAL(nsobj), NULL, NULL, flags)); )198 199 cfg_ns("default", CFG_DEFAULT);200 cfg_ns("system", CFG_SYSTEM);201 cfg_ns("user", CFG_USER);202 cfg_ns("mod", CFG_MOD);203 204 #undef cfg_ns205 206 return JS_TRUE;207 }208 209 };210 211 33 CConfigDB::CConfigDB() 212 34 { 213 35 } 214 36 215 void CConfigDB::RegisterJSConfigDB()216 {217 g_ScriptingHost.DefineCustomObjectType(&ConfigDB_JS::Class, ConfigDB_JS::Construct, 0, ConfigDB_JS::Props, ConfigDB_JS::Funcs, NULL, NULL);218 g_ScriptingHost.DefineCustomObjectType(&ConfigNamespace_JS::Class, ConfigNamespace_JS::Construct, 0, NULL, ConfigNamespace_JS::Funcs, NULL, NULL);219 JSObject *js_ConfigDB = g_ScriptingHost.CreateCustomObject("ConfigDB");220 g_ScriptingHost.SetGlobal("g_ConfigDB", OBJECT_TO_JSVAL(js_ConfigDB));221 }222 223 37 CConfigValue *CConfigDB::GetValue(EConfigNamespace ns, const CStr& name) 224 38 { 225 39 CConfigValueSet* values = GetValues(ns, name);