Changeset 24969
- Timestamp:
- Mar 1, 2021, 9:52:24 PM (3 years ago)
- Location:
- ps/trunk/source
- Files:
-
- 2 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
ps/trunk/source/gui/Scripting/ScriptFunctions.cpp
r24229 r24969 1 /* Copyright (C) 20 19Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 51 51 void GuiScriptingInit(ScriptInterface& scriptInterface) 52 52 { 53 54 53 55 JSI_GUISize::RegisterScriptClass(scriptInterface); 54 56 JSI_ConfigDB::RegisterScriptFunctions(scriptInterface); 55 JSI_Console::RegisterScriptFunctions( scriptInterface);57 JSI_Console::RegisterScriptFunctions(); 56 58 JSI_Debug::RegisterScriptFunctions(scriptInterface); 57 59 JSI_GUIManager::RegisterScriptFunctions(scriptInterface); -
ps/trunk/source/ps/CConsole.h
r22344 r24969 1 /* Copyright (C) 20 19Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 83 83 void FlushBuffer(); 84 84 85 bool IsActive() { return m_bVisible; }85 bool IsActive() { return m_bVisible; } 86 86 87 87 int m_iFontHeight; -
ps/trunk/source/ps/GameSetup/HWDetect.cpp
r24625 r24969 18 18 #include "precompiled.h" 19 19 20 #include "scriptinterface/ScriptInterface.h"21 22 20 #include "lib/ogl.h" 23 21 #include "lib/svn_revision.h" … … 46 44 #include "ps/UserReport.h" 47 45 #include "ps/VideoMode.h" 46 47 48 48 49 49 // TODO: Support OpenGL platforms which don't use GLX as well. … … 75 75 static void ReportGLLimits(const ScriptInterface& scriptInterface, JS::HandleValue settings); 76 76 77 void SetDisableAudio( ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate),bool disabled)77 void SetDisableAudio(bool disabled) 78 78 { 79 79 g_DisableAudio = disabled; … … 91 91 JSI_ConfigDB::RegisterScriptFunctions(scriptInterface); 92 92 93 scriptInterface.RegisterFunction<void, bool, &SetDisableAudio>("SetDisableAudio");93 "SetDisableAudio"); 94 94 95 95 // Load the detection script: -
ps/trunk/source/ps/scripting/JSInterface_Console.cpp
r24177 r24969 1 /* Copyright (C) 20 18Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 22 22 #include "ps/CConsole.h" 23 23 #include "ps/CLogger.h" 24 #include "scriptinterface/ ScriptInterface.h"24 #include "scriptinterface/.h" 25 25 26 bool JSI_Console::CheckGlobalInitialized() 26 namespace 27 { 28 CConsole* ConsoleGetter(const ScriptRequest&, JS::CallArgs&) 27 29 { 28 30 if (!g_Console) 29 31 { 30 32 LOGERROR("Trying to access the console when it's not initialized!"); 31 return false;33 return ; 32 34 } 33 return true; 35 return g_Console; 36 } 34 37 } 35 38 36 bool JSI_Console::GetVisibleEnabled(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))39 ) 37 40 { 38 if (!CheckGlobalInitialized()) 39 return false; 40 return g_Console->IsActive(); 41 ScriptFunction::Register<&CConsole::IsActive, ConsoleGetter>(rq, "Console_GetVisibleEnabled"); 42 ScriptFunction::Register<&CConsole::SetVisible, ConsoleGetter>(rq, "Console_SetVisibleEnabled"); 41 43 } 42 43 void JSI_Console::SetVisibleEnabled(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), bool Enabled)44 {45 if (!CheckGlobalInitialized())46 return;47 g_Console->SetVisible(Enabled);48 }49 50 void JSI_Console::RegisterScriptFunctions(const ScriptInterface& scriptInterface)51 {52 scriptInterface.RegisterFunction<bool, &JSI_Console::GetVisibleEnabled>("Console_GetVisibleEnabled");53 scriptInterface.RegisterFunction<void, bool, &JSI_Console::SetVisibleEnabled>("Console_SetVisibleEnabled");54 } -
ps/trunk/source/ps/scripting/JSInterface_Console.h
r24177 r24969 1 /* Copyright (C) 20 18Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 19 19 #define INCLUDED_JSI_CONSOLE 20 20 21 #include "scriptinterface/ScriptInterface.h" 21 class ScriptRequest; 22 22 23 23 namespace JSI_Console 24 24 { 25 bool CheckGlobalInitialized(); 26 bool GetVisibleEnabled(ScriptInterface::CmptPrivate* pCmptPrivate); 27 void SetVisibleEnabled(ScriptInterface::CmptPrivate* pCmptPrivate, bool Enabled); 28 29 void RegisterScriptFunctions(const ScriptInterface& scriptInterface); 25 void RegisterScriptFunctions(const ScriptRequest& rq); 30 26 } 31 27 -
ps/trunk/source/ps/scripting/JSInterface_Hotkey.cpp
r24675 r24969 25 25 #include "ps/Hotkey.h" 26 26 #include "ps/KeyName.h" 27 27 28 #include "scriptinterface/ScriptConversions.h" 28 29 … … 67 68 } 68 69 70 71 69 72 /** 70 73 * @return a (js) object mapping hotkey name (from cfg files) to a list ofscancode names 71 74 */ 72 JS::Value GetHotkeyMap( ScriptInterface::CmptPrivate* pCmptPrivate)75 JS::Value GetHotkeyMap() 73 76 { 74 ScriptRequest rq(*pCmptPrivate->pScriptInterface);75 76 77 JS::RootedValue hotkeyMap(rq.cx); 77 78 … … 90 91 hotkeys[mapping.name].emplace_back(keymap); 91 92 } 92 pCmptPrivate->pScriptInterface->ToJSVal(rq, &hotkeyMap, hotkeys);93 ToJSVal(rq, &hotkeyMap, hotkeys); 93 94 94 95 return hotkeyMap; … … 98 99 * @return a (js) object mapping scancode names to their locale-dependent name. 99 100 */ 100 JS::Value GetScancodeKeyNames( ScriptInterface::CmptPrivate* pCmptPrivate)101 JS::Value GetScancodeKeyNames() 101 102 { 102 ScriptRequest rq(*pCmptPrivate->pScriptInterface);103 104 103 JS::RootedValue obj(rq.cx); 105 104 std::unordered_map<std::string, std::string> map; … … 109 108 for (int i = 0; i < MOUSE_LAST; ++i) 110 109 map[FindScancodeName(static_cast<SDL_Scancode>(i))] = FindKeyName(static_cast<SDL_Scancode>(i)); 111 pCmptPrivate->pScriptInterface->ToJSVal(rq, &obj, map);110 ToJSVal(rq, &obj, map); 112 111 113 112 return obj; 114 113 } 115 114 116 void ReloadHotkeys( ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))115 void ReloadHotkeys() 117 116 { 118 117 UnloadHotkeys(); … … 120 119 } 121 120 122 JS::Value GetConflicts( ScriptInterface::CmptPrivate* pCmptPrivate, JS::HandleValue combination)121 JS::Value GetConflicts(, JS::HandleValue combination) 123 122 { 124 ScriptInterface* scriptInterface = pCmptPrivate->pScriptInterface;125 ScriptRequest rq(*scriptInterface);126 127 123 std::vector<std::string> keys; 128 if (! scriptInterface->FromJSVal(rq, combination, keys))124 if (!FromJSVal(rq, combination, keys)) 129 125 { 130 126 LOGERROR("Invalid hotkey combination"); … … 162 158 163 159 JS::RootedValue ret(rq.cx); 164 scriptInterface->ToJSVal(rq, &ret, conflicts);160 ToJSVal(rq, &ret, conflicts); 165 161 return ret; 166 162 } 163 167 164 168 void JSI_Hotkey::RegisterScriptFunctions(const Script Interface& scriptInterface)165 void JSI_Hotkey::RegisterScriptFunctions(const Script) 169 166 { 170 scriptInterface.RegisterFunction<JS::Value, &GetHotkeyMap>("GetHotkeyMap");171 scriptInterface.RegisterFunction<JS::Value, &GetScancodeKeyNames>("GetScancodeKeyNames");172 scriptInterface.RegisterFunction<void, &ReloadHotkeys>("ReloadHotkeys");173 scriptInterface.RegisterFunction<JS::Value, JS::HandleValue, &GetConflicts>("GetConflicts");167 "GetHotkeyMap"); 168 "GetScancodeKeyNames"); 169 "ReloadHotkeys"); 170 "GetConflicts"); 174 171 } -
ps/trunk/source/ps/scripting/JSInterface_Hotkey.h
r24215 r24969 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 19 19 #define INCLUDED_JSI_HOTKEY 20 20 21 #include "scriptinterface/ScriptInterface.h" 21 class ScriptRequest; 22 22 23 23 namespace JSI_Hotkey 24 24 { 25 void RegisterScriptFunctions(const Script Interface& ScriptInterface);25 void RegisterScriptFunctions(const Script); 26 26 } 27 27 -
ps/trunk/source/ps/scripting/JSInterface_Mod.cpp
r24177 r24969 1 /* Copyright (C) 20 18Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 21 21 22 22 #include "ps/Mod.h" 23 23 24 #include "scriptinterface/ScriptInterface.h" 24 25 25 26 extern void RestartEngine(); 26 27 27 JS::Value JSI_Mod::GetEngineInfo(ScriptInterface::CmptPrivate* pCmptPrivate) 28 namespace 29 { 30 JS::Value GetEngineInfo(ScriptInterface::CmptPrivate* pCmptPrivate) 28 31 { 29 32 return Mod::GetEngineInfo(*(pCmptPrivate->pScriptInterface)); … … 40 43 * properties. 41 44 */ 42 JS::Value JSI_Mod::GetAvailableMods(ScriptInterface::CmptPrivate* pCmptPrivate)45 JS::Value GetAvailableMods(ScriptInterface::CmptPrivate* pCmptPrivate) 43 46 { 44 47 return Mod::GetAvailableMods(*(pCmptPrivate->pScriptInterface)); 45 48 } 46 49 47 void JSI_Mod::RestartEngine(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate)) 48 { 49 ::RestartEngine(); 50 } 51 52 void JSI_Mod::SetMods(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), const std::vector<CStr>& mods) 50 void SetMods(const std::vector<CStr>& mods) 53 51 { 54 52 g_modsLoaded = mods; 55 53 } 54 56 55 57 void JSI_Mod::RegisterScriptFunctions(const Script Interface& scriptInterface)56 void JSI_Mod::RegisterScriptFunctions(const Script) 58 57 { 59 scriptInterface.RegisterFunction<JS::Value, &GetEngineInfo>("GetEngineInfo");60 scriptInterface.RegisterFunction<JS::Value, &JSI_Mod::GetAvailableMods>("GetAvailableMods");61 scriptInterface.RegisterFunction<void, &JSI_Mod::RestartEngine>("RestartEngine");62 scriptInterface.RegisterFunction<void, std::vector<CStr>, &JSI_Mod::SetMods>("SetMods");58 "GetEngineInfo"); 59 "GetAvailableMods"); 60 "RestartEngine"); 61 "SetMods"); 63 62 } -
ps/trunk/source/ps/scripting/JSInterface_Mod.h
r24177 r24969 1 /* Copyright (C) 20 18Wildfire Games.1 /* Copyright (C) 20 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 19 19 #define INCLUDED_JSI_MOD 20 20 21 #include "ps/CStr.h" 22 #include "scriptinterface/ScriptInterface.h" 21 class ScriptRequest; 23 22 24 23 namespace JSI_Mod 25 24 { 26 void RegisterScriptFunctions(const ScriptInterface& scriptInterface); 27 28 JS::Value GetEngineInfo(ScriptInterface::CmptPrivate* pCmptPrivate); 29 JS::Value GetAvailableMods(ScriptInterface::CmptPrivate* pCmptPrivate); 30 void RestartEngine(ScriptInterface::CmptPrivate* pCmptPrivate); 31 void SetMods(ScriptInterface::CmptPrivate* pCmptPrivate, const std::vector<CStr>& mods); 25 void RegisterScriptFunctions(const ScriptRequest& rq); 32 26 } 33 27 -
ps/trunk/source/scriptinterface/ScriptInterface.cpp
r24915 r24969 73 73 74 74 ScriptRequest::ScriptRequest(const ScriptInterface& scriptInterface) : 75 cx(scriptInterface.m->m_cx) 75 cx(scriptInterface.m->m_cx) 76 76 { 77 77 m_formerRealm = JS::EnterRealm(cx, scriptInterface.m->m_glob); -
ps/trunk/source/scriptinterface/ScriptInterface.h
r24462 r24969 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 84 84 JSContext* cx; 85 85 JSObject* glob; 86 86 87 private: 87 88 JS::Realm* m_formerRealm; -
ps/trunk/source/simulation2/components/tests/test_scripts.h
r24462 r24969 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 22 22 #include "ps/Filesystem.h" 23 23 24 24 25 #include "scriptinterface/ScriptContext.h" 25 26 … … 119 120 ScriptTestSetup(componentManager.GetScriptInterface()); 120 121 121 componentManager.GetScriptInterface().RegisterFunction<void, VfsPath, Script_LoadComponentScript> ("LoadComponentScript"); 122 componentManager.GetScriptInterface().RegisterFunction<void, VfsPath, Script_LoadHelperScript> ("LoadHelperScript"); 123 componentManager.GetScriptInterface().RegisterFunction<JS::Value, JS::HandleValue, Script_SerializationRoundTrip> ("SerializationRoundTrip"); 122 ScriptRequest rq(componentManager.GetScriptInterface()); 123 ScriptFunction::Register<Script_LoadComponentScript>(rq, "LoadComponentScript"); 124 ScriptFunction::Register<Script_LoadHelperScript>(rq, "LoadHelperScript"); 125 ScriptFunction::Register<Script_SerializationRoundTrip>(rq, "SerializationRoundTrip"); 124 126 125 127 componentManager.LoadComponentTypes(); -
ps/trunk/source/simulation2/system/ComponentManager.cpp
r24915 r24969 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 25 25 #include "ps/Profile.h" 26 26 #include "ps/scripting/JSInterface_VFS.h" 27 27 28 #include "simulation2/components/ICmpTemplateManager.h" 28 29 #include "simulation2/MessageTypes.h" … … 69 70 { 70 71 JSI_VFS::RegisterScriptFunctions_Simulation(m_ScriptInterface); 71 m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterComponentType> ("RegisterComponentType"); 72 m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterSystemComponentType> ("RegisterSystemComponentType"); 73 m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_ReRegisterComponentType> ("ReRegisterComponentType"); 74 m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterInterface> ("RegisterInterface"); 75 m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterMessageType> ("RegisterMessageType"); 76 m_ScriptInterface.RegisterFunction<void, std::string, JS::HandleValue, CComponentManager::Script_RegisterGlobal> ("RegisterGlobal"); 77 m_ScriptInterface.RegisterFunction<IComponent*, int, int, CComponentManager::Script_QueryInterface> ("QueryInterface"); 78 m_ScriptInterface.RegisterFunction<std::vector<int>, int, CComponentManager::Script_GetEntitiesWithInterface> ("GetEntitiesWithInterface"); 79 m_ScriptInterface.RegisterFunction<std::vector<IComponent*>, int, CComponentManager::Script_GetComponentsWithInterface> ("GetComponentsWithInterface"); 80 m_ScriptInterface.RegisterFunction<void, int, int, JS::HandleValue, CComponentManager::Script_PostMessage> ("PostMessage"); 81 m_ScriptInterface.RegisterFunction<void, int, JS::HandleValue, CComponentManager::Script_BroadcastMessage> ("BroadcastMessage"); 82 m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddEntity> ("AddEntity"); 83 m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddLocalEntity> ("AddLocalEntity"); 84 m_ScriptInterface.RegisterFunction<void, int, CComponentManager::Script_DestroyEntity> ("DestroyEntity"); 85 m_ScriptInterface.RegisterFunction<void, CComponentManager::Script_FlushDestroyedEntities> ("FlushDestroyedEntities"); 72 ScriptRequest rq(m_ScriptInterface); 73 constexpr ScriptFunction::ObjectGetter<CComponentManager> Getter = &ScriptFunction::ObjectFromCBData<CComponentManager>; 74 ScriptFunction::Register<&CComponentManager::Script_RegisterComponentType, Getter>(rq, "RegisterComponentType"); 75 ScriptFunction::Register<&CComponentManager::Script_RegisterSystemComponentType, Getter>(rq, "RegisterSystemComponentType"); 76 ScriptFunction::Register<&CComponentManager::Script_ReRegisterComponentType, Getter>(rq, "ReRegisterComponentType"); 77 ScriptFunction::Register<&CComponentManager::Script_RegisterInterface, Getter>(rq, "RegisterInterface"); 78 ScriptFunction::Register<&CComponentManager::Script_RegisterMessageType, Getter>(rq, "RegisterMessageType"); 79 ScriptFunction::Register<&CComponentManager::Script_RegisterGlobal, Getter>(rq, "RegisterGlobal"); 80 ScriptFunction::Register<&CComponentManager::Script_GetEntitiesWithInterface, Getter>(rq, "GetEntitiesWithInterface"); 81 ScriptFunction::Register<&CComponentManager::Script_GetComponentsWithInterface, Getter>(rq, "GetComponentsWithInterface"); 82 ScriptFunction::Register<&CComponentManager::Script_PostMessage, Getter>(rq, "PostMessage"); 83 ScriptFunction::Register<&CComponentManager::Script_BroadcastMessage, Getter>(rq, "BroadcastMessage"); 84 ScriptFunction::Register<&CComponentManager::Script_AddEntity, Getter>(rq, "AddEntity"); 85 ScriptFunction::Register<&CComponentManager::Script_AddLocalEntity, Getter>(rq, "AddLocalEntity"); 86 ScriptFunction::Register<&CComponentManager::QueryInterface, Getter>(rq, "QueryInterface"); 87 ScriptFunction::Register<&CComponentManager::DestroyComponentsSoon, Getter>(rq, "DestroyEntity"); 88 ScriptFunction::Register<&CComponentManager::FlushDestroyedComponents, Getter>(rq, "FlushDestroyedEntities"); 86 89 } 87 90 … … 150 153 } 151 154 152 void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent) 153 { 154 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 155 ScriptRequest rq(componentManager->m_ScriptInterface); 155 void CComponentManager::Script_RegisterComponentType_Common(int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent) 156 { 157 ScriptRequest rq(m_ScriptInterface); 156 158 157 159 // Find the C++ component that wraps the interface 158 int cidWrapper = componentManager->GetScriptWrapper(iid);160 int cidWrapper = GetScriptWrapper(iid); 159 161 if (cidWrapper == CID__Invalid) 160 162 { … … 162 164 return; 163 165 } 164 const ComponentType& ctWrapper = componentManager->m_ComponentTypesById[cidWrapper];166 const ComponentType& ctWrapper = m_ComponentTypesById[cidWrapper]; 165 167 166 168 bool mustReloadComponents = false; // for hotloading 167 169 168 ComponentTypeId cid = componentManager->LookupCID(cname);170 ComponentTypeId cid = LookupCID(cname); 169 171 if (cid == CID__Invalid) 170 172 { … … 175 177 } 176 178 // Allocate a new cid number 177 cid = componentManager->m_NextScriptComponentTypeId++;178 componentManager->m_ComponentTypeIdsByName[cname] = cid;179 cid = m_NextScriptComponentTypeId++; 180 m_ComponentTypeIdsByName[cname] = cid; 179 181 if (systemComponent) 180 componentManager->MarkScriptedComponentForSystemEntity(cid);182 MarkScriptedComponentForSystemEntity(cid); 181 183 } 182 184 else … … 184 186 // Component type is already loaded, so do hotloading: 185 187 186 if (! componentManager->m_CurrentlyHotloading && !reRegister)188 if (!m_CurrentlyHotloading && !reRegister) 187 189 { 188 190 ScriptException::Raise(rq, "Registering component type with already-registered name '%s'", cname.c_str()); … … 190 192 } 191 193 192 const ComponentType& ctPrevious = componentManager->m_ComponentTypesById[cid];194 const ComponentType& ctPrevious = m_ComponentTypesById[cid]; 193 195 194 196 // We can only replace scripted component types, not native ones … … 204 206 { 205 207 // ...though it only matters if any components exist with this type 206 if (! componentManager->m_ComponentsByTypeId[cid].empty())208 if (!m_ComponentsByTypeId[cid].empty()) 207 209 { 208 210 ScriptException::Raise(rq, "Hotloading script component type mustn't change interface ID"); … … 213 215 // Remove the old component type's message subscriptions 214 216 std::map<MessageTypeId, std::vector<ComponentTypeId> >::iterator it; 215 for (it = componentManager->m_LocalMessageSubscriptions.begin(); it != componentManager->m_LocalMessageSubscriptions.end(); ++it)217 for (it = m_LocalMessageSubscriptions.end(); ++it) 216 218 { 217 219 std::vector<ComponentTypeId>& types = it->second; … … 220 222 types.erase(ctit); 221 223 } 222 for (it = componentManager->m_GlobalMessageSubscriptions.begin(); it != componentManager->m_GlobalMessageSubscriptions.end(); ++it)224 for (it = m_GlobalMessageSubscriptions.end(); ++it) 223 225 { 224 226 std::vector<ComponentTypeId>& types = it->second; … … 232 234 233 235 JS::RootedValue protoVal(rq.cx); 234 if (! componentManager->m_ScriptInterface.GetProperty(ctor, "prototype", &protoVal))236 if (!m_ScriptInterface.GetProperty(ctor, "prototype", &protoVal)) 235 237 { 236 238 ScriptException::Raise(rq, "Failed to get property 'prototype'"); … … 244 246 std::string schema = "<empty/>"; 245 247 246 if ( componentManager->m_ScriptInterface.HasProperty(protoVal, "Schema"))247 componentManager->m_ScriptInterface.GetProperty(protoVal, "Schema", schema);248 if (m_ScriptInterface.HasProperty(protoVal, "Schema")) 249 m_ScriptInterface.GetProperty(protoVal, "Schema", schema); 248 250 249 251 // Construct a new ComponentType, using the wrapper's alloc functions … … 257 259 std::make_unique<JS::PersistentRootedValue>(rq.cx, ctor) 258 260 }; 259 componentManager->m_ComponentTypesById[cid] = std::move(ct);260 261 componentManager->m_CurrentComponent = cid; // needed by Subscribe261 m_ComponentTypesById[cid] = std::move(ct); 262 263 m_CurrentComponent = cid; // needed by Subscribe 262 264 263 265 // Find all the ctor prototype's On* methods, and subscribe to the appropriate messages: 264 266 std::vector<std::string> methods; 265 267 266 if (! componentManager->m_ScriptInterface.EnumeratePropertyNames(protoVal, false, methods))268 if (!m_ScriptInterface.EnumeratePropertyNames(protoVal, false, methods)) 267 269 { 268 270 ScriptException::Raise(rq, "Failed to enumerate component properties."); … … 286 288 } 287 289 288 std::map<std::string, MessageTypeId>::const_iterator mit = componentManager->m_MessageTypeIdsByName.find(name);289 if (mit == componentManager->m_MessageTypeIdsByName.end())290 std::map<std::string, MessageTypeId>::const_iterator mit = m_MessageTypeIdsByName.find(name); 291 if (mit == m_MessageTypeIdsByName.end()) 290 292 { 291 293 ScriptException::Raise(rq, "Registered component has unrecognized '%s' message handler method", it->c_str()); … … 294 296 295 297 if (isGlobal) 296 componentManager->SubscribeGloballyToMessageType(mit->second);298 SubscribeGloballyToMessageType(mit->second); 297 299 else 298 componentManager->SubscribeToMessageType(mit->second);299 } 300 301 componentManager->m_CurrentComponent = CID__Invalid;300 SubscribeToMessageType(mit->second); 301 } 302 303 m_CurrentComponent = CID__Invalid; 302 304 303 305 if (mustReloadComponents) … … 305 307 // For every script component with this cid, we need to switch its 306 308 // prototype from the old constructor's prototype property to the new one's 307 const std::map<entity_id_t, IComponent*>& comps = componentManager->m_ComponentsByTypeId[cid];309 const std::map<entity_id_t, IComponent*>& comps = m_ComponentsByTypeId[cid]; 308 310 std::map<entity_id_t, IComponent*>::const_iterator eit = comps.begin(); 309 311 for (; eit != comps.end(); ++eit) … … 311 313 JS::RootedValue instance(rq.cx, eit->second->GetJSInstance()); 312 314 if (!instance.isNull()) 313 componentManager->m_ScriptInterface.SetPrototype(instance, protoVal); 314 } 315 } 316 } 317 318 void CComponentManager::Script_RegisterComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor) 319 { 320 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 321 componentManager->Script_RegisterComponentType_Common(pCmptPrivate, iid, cname, ctor, false, false); 322 componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading); 323 } 324 325 void CComponentManager::Script_RegisterSystemComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor) 326 { 327 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 328 componentManager->Script_RegisterComponentType_Common(pCmptPrivate, iid, cname, ctor, false, true); 329 componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading); 330 } 331 332 void CComponentManager::Script_ReRegisterComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor) 333 { 334 Script_RegisterComponentType_Common(pCmptPrivate, iid, cname, ctor, true, false); 335 } 336 337 void CComponentManager::Script_RegisterInterface(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name) 338 { 339 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 340 341 std::map<std::string, InterfaceId>::iterator it = componentManager->m_InterfaceIdsByName.find(name); 342 if (it != componentManager->m_InterfaceIdsByName.end()) 315 m_ScriptInterface.SetPrototype(instance, protoVal); 316 } 317 } 318 } 319 320 void CComponentManager::Script_RegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor) 321 { 322 Script_RegisterComponentType_Common(iid, cname, ctor, false, false); 323 m_ScriptInterface.SetGlobal(cname.c_str(), ctor, m_CurrentlyHotloading); 324 } 325 326 void CComponentManager::Script_RegisterSystemComponentType(int iid, const std::string& cname, JS::HandleValue ctor) 327 { 328 Script_RegisterComponentType_Common(iid, cname, ctor, false, true); 329 m_ScriptInterface.SetGlobal(cname.c_str(), ctor, m_CurrentlyHotloading); 330 } 331 332 void CComponentManager::Script_ReRegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor) 333 { 334 Script_RegisterComponentType_Common(iid, cname, ctor, true, false); 335 } 336 337 void CComponentManager::Script_RegisterInterface(const std::string& name) 338 { 339 std::map<std::string, InterfaceId>::iterator it = m_InterfaceIdsByName.find(name); 340 if (it != m_InterfaceIdsByName.end()) 343 341 { 344 342 // Redefinitions are fine (and just get ignored) when hotloading; otherwise 345 343 // they're probably unintentional and should be reported 346 if (! componentManager->m_CurrentlyHotloading)347 { 348 ScriptRequest rq( componentManager->m_ScriptInterface);344 if (!m_CurrentlyHotloading) 345 { 346 ScriptRequest rq(m_ScriptInterface); 349 347 ScriptException::Raise(rq, "Registering interface with already-registered name '%s'", name.c_str()); 350 348 } … … 353 351 354 352 // IIDs start at 1, so size+1 is the next unused one 355 size_t id = componentManager->m_InterfaceIdsByName.size() + 1; 356 componentManager->m_InterfaceIdsByName[name] = (InterfaceId)id; 357 componentManager->m_ComponentsByInterface.resize(id+1); // add one so we can index by InterfaceId 358 componentManager->m_ScriptInterface.SetGlobal(("IID_" + name).c_str(), (int)id); 359 } 360 361 void CComponentManager::Script_RegisterMessageType(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name) 362 { 363 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 364 365 std::map<std::string, MessageTypeId>::iterator it = componentManager->m_MessageTypeIdsByName.find(name); 366 if (it != componentManager->m_MessageTypeIdsByName.end()) 353 size_t id = m_InterfaceIdsByName.size() + 1; 354 m_InterfaceIdsByName[name] = (InterfaceId)id; 355 m_ComponentsByInterface.resize(id+1); // add one so we can index by InterfaceId 356 m_ScriptInterface.SetGlobal(("IID_" + name).c_str(), (int)id); 357 } 358 359 void CComponentManager::Script_RegisterMessageType(const std::string& name) 360 { 361 std::map<std::string, MessageTypeId>::iterator it = m_MessageTypeIdsByName.find(name); 362 if (it != m_MessageTypeIdsByName.end()) 367 363 { 368 364 // Redefinitions are fine (and just get ignored) when hotloading; otherwise 369 365 // they're probably unintentional and should be reported 370 if (! componentManager->m_CurrentlyHotloading)371 { 372 ScriptRequest rq( componentManager->m_ScriptInterface);366 if (!m_CurrentlyHotloading) 367 { 368 ScriptRequest rq(m_ScriptInterface); 373 369 ScriptException::Raise(rq, "Registering message type with already-registered name '%s'", name.c_str()); 374 370 } … … 377 373 378 374 // MTIDs start at 1, so size+1 is the next unused one 379 size_t id = componentManager->m_MessageTypeIdsByName.size() + 1; 380 componentManager->RegisterMessageType((MessageTypeId)id, name.c_str()); 381 componentManager->m_ScriptInterface.SetGlobal(("MT_" + name).c_str(), (int)id); 382 } 383 384 void CComponentManager::Script_RegisterGlobal(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name, JS::HandleValue value) 385 { 386 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 387 componentManager->m_ScriptInterface.SetGlobal(name.c_str(), value, componentManager->m_CurrentlyHotloading); 388 } 389 390 IComponent* CComponentManager::Script_QueryInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int ent, int iid) 391 { 392 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 393 IComponent* component = componentManager->QueryInterface((entity_id_t)ent, iid); 394 return component; 395 } 396 397 std::vector<int> CComponentManager::Script_GetEntitiesWithInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int iid) 398 { 399 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 400 375 size_t id = m_MessageTypeIdsByName.size() + 1; 376 RegisterMessageType((MessageTypeId)id, name.c_str()); 377 m_ScriptInterface.SetGlobal(("MT_" + name).c_str(), (int)id); 378 } 379 380 void CComponentManager::Script_RegisterGlobal(const std::string& name, JS::HandleValue value) 381 { 382 m_ScriptInterface.SetGlobal(name.c_str(), value, m_CurrentlyHotloading); 383 } 384 385 std::vector<int> CComponentManager::Script_GetEntitiesWithInterface(int iid) 386 { 401 387 std::vector<int> ret; 402 const InterfaceListUnordered& ents = componentManager->GetEntitiesWithInterfaceUnordered(iid);388 const InterfaceListUnordered& ents = GetEntitiesWithInterfaceUnordered(iid); 403 389 for (InterfaceListUnordered::const_iterator it = ents.begin(); it != ents.end(); ++it) 404 390 if (!ENTITY_IS_LOCAL(it->first)) … … 408 394 } 409 395 410 std::vector<IComponent*> CComponentManager::Script_GetComponentsWithInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int iid) 411 { 412 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 413 396 std::vector<IComponent*> CComponentManager::Script_GetComponentsWithInterface(int iid) 397 { 414 398 std::vector<IComponent*> ret; 415 InterfaceList ents = componentManager->GetEntitiesWithInterface(iid);399 InterfaceList ents = GetEntitiesWithInterface(iid); 416 400 for (InterfaceList::const_iterator it = ents.begin(); it != ents.end(); ++it) 417 401 ret.push_back(it->second); // TODO: maybe we should exclude local entities … … 434 418 } 435 419 436 void CComponentManager::Script_PostMessage(ScriptInterface::CmptPrivate* pCmptPrivate, int ent, int mtid, JS::HandleValue data) 437 { 438 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 439 440 CMessage* msg = componentManager->ConstructMessage(mtid, data); 420 void CComponentManager::Script_PostMessage(int ent, int mtid, JS::HandleValue data) 421 { 422 CMessage* msg = ConstructMessage(mtid, data); 441 423 if (!msg) 442 424 return; // error 443 425 444 componentManager->PostMessage(ent, *msg);426 PostMessage(ent, *msg); 445 427 446 428 delete msg; 447 429 } 448 430 449 void CComponentManager::Script_BroadcastMessage(ScriptInterface::CmptPrivate* pCmptPrivate, int mtid, JS::HandleValue data) 450 { 451 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 452 453 CMessage* msg = componentManager->ConstructMessage(mtid, data); 431 void CComponentManager::Script_BroadcastMessage(int mtid, JS::HandleValue data) 432 { 433 CMessage* msg = ConstructMessage(mtid, data); 454 434 if (!msg) 455 435 return; // error 456 436 457 componentManager->BroadcastMessage(*msg);437 BroadcastMessage(*msg); 458 438 459 439 delete msg; 460 440 } 461 441 462 int CComponentManager::Script_AddEntity(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& templateName) 463 { 464 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 465 466 std::wstring name(templateName.begin(), templateName.end()); 442 int CComponentManager::Script_AddEntity(const std::wstring& templateName) 443 { 467 444 // TODO: should validate the string to make sure it doesn't contain scary characters 468 445 // that will let it access non-component-template files 469 470 entity_id_t ent = componentManager->AddEntity(name, componentManager->AllocateNewEntity()); 471 return (int)ent; 472 } 473 474 int CComponentManager::Script_AddLocalEntity(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& templateName) 475 { 476 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 477 478 std::wstring name(templateName.begin(), templateName.end()); 446 return AddEntity(templateName, AllocateNewEntity()); 447 } 448 449 int CComponentManager::Script_AddLocalEntity(const std::wstring& templateName) 450 { 479 451 // TODO: should validate the string to make sure it doesn't contain scary characters 480 452 // that will let it access non-component-template files 481 482 entity_id_t ent = componentManager->AddEntity(name, componentManager->AllocateNewLocalEntity()); 483 return (int)ent; 484 } 485 486 void CComponentManager::Script_DestroyEntity(ScriptInterface::CmptPrivate* pCmptPrivate, int ent) 487 { 488 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 489 490 componentManager->DestroyComponentsSoon(ent); 491 } 492 493 void CComponentManager::Script_FlushDestroyedEntities(ScriptInterface::CmptPrivate *pCmptPrivate) 494 { 495 CComponentManager* componentManager = static_cast<CComponentManager*> (pCmptPrivate->pCBData); 496 componentManager->FlushDestroyedComponents(); 453 return AddEntity(templateName, AllocateNewLocalEntity()); 497 454 } 498 455 -
ps/trunk/source/simulation2/system/ComponentManager.h
r24203 r24969 1 /* Copyright (C) 202 0Wildfire Games.1 /* Copyright (C) 202 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * … … 274 274 private: 275 275 // Implementations of functions exposed to scripts 276 static void Script_RegisterComponentType_Common(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent); 277 static void Script_RegisterComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor); 278 static void Script_RegisterSystemComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor); 279 static void Script_ReRegisterComponentType(ScriptInterface::CmptPrivate* pCmptPrivate, int iid, const std::string& cname, JS::HandleValue ctor); 280 static void Script_RegisterInterface(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name); 281 static void Script_RegisterMessageType(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name); 282 static void Script_RegisterGlobal(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& name, JS::HandleValue value); 283 static IComponent* Script_QueryInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int ent, int iid); 284 static std::vector<int> Script_GetEntitiesWithInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int iid); 285 static std::vector<IComponent*> Script_GetComponentsWithInterface(ScriptInterface::CmptPrivate* pCmptPrivate, int iid); 286 static void Script_PostMessage(ScriptInterface::CmptPrivate* pCmptPrivate, int ent, int mtid, JS::HandleValue data); 287 static void Script_BroadcastMessage(ScriptInterface::CmptPrivate* pCmptPrivate, int mtid, JS::HandleValue data); 288 static int Script_AddEntity(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& templateName); 289 static int Script_AddLocalEntity(ScriptInterface::CmptPrivate* pCmptPrivate, const std::string& templateName); 290 static void Script_DestroyEntity(ScriptInterface::CmptPrivate* pCmptPrivate, int ent); 291 static void Script_FlushDestroyedEntities(ScriptInterface::CmptPrivate* pCmptPrivate); 276 void Script_RegisterComponentType_Common(int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent); 277 void Script_RegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor); 278 void Script_RegisterSystemComponentType(int iid, const std::string& cname, JS::HandleValue ctor); 279 void Script_ReRegisterComponentType(int iid, const std::string& cname, JS::HandleValue ctor); 280 void Script_RegisterInterface(const std::string& name); 281 void Script_RegisterMessageType(const std::string& name); 282 void Script_RegisterGlobal(const std::string& name, JS::HandleValue value); 283 std::vector<int> Script_GetEntitiesWithInterface(int iid); 284 std::vector<IComponent*> Script_GetComponentsWithInterface(int iid); 285 void Script_PostMessage(int ent, int mtid, JS::HandleValue data); 286 void Script_BroadcastMessage(int mtid, JS::HandleValue data); 287 int Script_AddEntity(const std::wstring& templateName); 288 int Script_AddLocalEntity(const std::wstring& templateName); 292 289 293 290 CMessage* ConstructMessage(int mtid, JS::HandleValue data); -
ps/trunk/source/test_setup.cpp
r24530 r24969 37 37 #include "lib/sysdep/sysdep.h" 38 38 #include "ps/Profiler2.h" 39 39 40 #include "scriptinterface/ScriptEngine.h" 40 41 #include "scriptinterface/ScriptContext.h" … … 139 140 namespace 140 141 { 141 void script_TS_FAIL( ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate),const std::wstring& msg)142 void script_TS_FAIL(const std::wstring& msg) 142 143 { 143 144 TS_FAIL(utf8_from_wstring(msg).c_str()); … … 145 146 } 146 147 147 void ScriptTestSetup(const ScriptInterface& script interface)148 void ScriptTestSetup(const ScriptInterface& scriptnterface) 148 149 { 149 scriptinterface.RegisterFunction<void, std::wstring, script_TS_FAIL>("TS_FAIL"); 150 ScriptRequest rq(scriptInterface); 151 ScriptFunction::Register<script_TS_FAIL>(rq, "TS_FAIL"); 150 152 151 153 // Load the TS_* function definitions … … 155 157 ENSURE(ifs.good()); 156 158 std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); 157 ENSURE(script interface.LoadScript(L"test_setup.js", content));159 ENSURE(scriptnterface.LoadScript(L"test_setup.js", content)); 158 160 }
Note:
See TracChangeset
for help on using the changeset viewer.