Ticket #2517: global_components.diff

File global_components.diff, 5.0 KB (added by sanderd17, 10 years ago)
  • source/simulation2/system/ComponentManager.cpp

     
    6969    // these functions, so we skip registering them here in those cases
    7070    if (!skipScriptFunctions)
    7171    {
    72         m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_RegisterComponentType> ("RegisterComponentType");
     72        m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_RegisterNewComponentType> ("RegisterComponentType");
     73        m_ScriptInterface.RegisterFunction<void, int, std::string, CScriptVal, CComponentManager::Script_ReRegisterComponentType> ("ReRegisterComponentType");
    7374        m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterInterface> ("RegisterInterface");
    7475        m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterMessageType> ("RegisterMessageType");
    7576        m_ScriptInterface.RegisterFunction<void, std::string, CScriptVal, CComponentManager::Script_RegisterGlobal> ("RegisterGlobal");
     
    143144    return ok;
    144145}
    145146
    146 void CComponentManager::Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor)
     147void CComponentManager::Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister)
    147148{
    148149    CComponentManager* componentManager = static_cast<CComponentManager*> (pCxPrivate->pCBData);
    149150    JSContext* cx = componentManager->m_ScriptInterface.GetContext();
     
    163164    ComponentTypeId cid = componentManager->LookupCID(cname);
    164165    if (cid == CID__Invalid)
    165166    {
     167        if (reRegister)
     168        {
     169            componentManager->m_ScriptInterface.ReportError("ReRegistering component type that was not registered before"); // TODO: report the actual name
     170            return;
     171        }
    166172        // Allocate a new cid number
    167173        cid = componentManager->m_NextScriptComponentTypeId++;
    168174        componentManager->m_ComponentTypeIdsByName[cname] = cid;
     
    171177    {
    172178        // Component type is already loaded, so do hotloading:
    173179
    174         if (!componentManager->m_CurrentlyHotloading)
     180        if (!componentManager->m_CurrentlyHotloading && !reRegister)
    175181        {
    176182            componentManager->m_ScriptInterface.ReportError("Registering component type with already-registered name"); // TODO: report the actual name
    177183            return;
     
    182188        // We can only replace scripted component types, not native ones
    183189        if (ctPrevious.type != CT_Script)
    184190        {
    185             componentManager->m_ScriptInterface.ReportError("Hotloading script component type with same name as native component");
     191            componentManager->m_ScriptInterface.ReportError("Loading script component type with same name as native component");
    186192            return;
    187193        }
    188194
     
    303309    }
    304310}
    305311
     312void CComponentManager::Script_RegisterNewComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor)
     313{
     314    CComponentManager* componentManager = static_cast<CComponentManager*> (pCxPrivate->pCBData);
     315    componentManager->Script_RegisterComponentType(pCxPrivate, iid, cname, ctor, false);
     316    componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading);
     317}
     318
     319void CComponentManager::Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor)
     320{
     321    CComponentManager* componentManager = static_cast<CComponentManager*> (pCxPrivate->pCBData);
     322    componentManager->Script_RegisterComponentType(pCxPrivate, iid, cname, ctor, true);
     323}
     324
    306325void CComponentManager::Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, std::string name)
    307326{
    308327    CComponentManager* componentManager = static_cast<CComponentManager*> (pCxPrivate->pCBData);
  • source/simulation2/system/ComponentManager.h

     
    242242
    243243private:
    244244    // Implementations of functions exposed to scripts
    245     static void Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor);
     245    static void Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor, bool reRegister);
     246    static void Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor);
     247    static void Script_RegisterNewComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, std::string cname, CScriptVal ctor);
    246248    static void Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, std::string name);
    247249    static void Script_RegisterMessageType(ScriptInterface::CxPrivate* pCxPrivate, std::string name);
    248250    static void Script_RegisterGlobal(ScriptInterface::CxPrivate* pCxPrivate, std::string name, CScriptVal value);