!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / CryEngine / CrySchematyc2 / Env / EnvRegistry.cpp
blob152e45fc2cd45816aed6b88965ab87f419b2dbca
1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
3 // #SchematycTODO : Replace CRY_ASSERT with SCHEMATYC2_SYSTEM_ASSERT.
5 #include "StdAfx.h"
6 #include "EnvRegistry.h"
8 #include <CrySerialization/IArchiveHost.h>
9 #include <CrySchematyc2/IActionFactory.h>
10 #include <CrySchematyc2/IComponentFactory.h>
11 #include <CrySchematyc2/EnvTypeDesc.h>
12 #include <CrySchematyc2/Deprecated/IGlobalFunction.h>
13 #include <CrySchematyc2/Env/IEnvFunctionDescriptor.h>
15 #include "AbstractInterface.h"
16 #include "Foundation.h"
17 #include "Signal.h"
19 namespace Schematyc2
21 //////////////////////////////////////////////////////////////////////////
22 bool CEnvRegistry::RegisterTypeDesc(const IEnvTypeDescPtr& pTypeDesc)
24 CRY_ASSERT(pTypeDesc);
25 if(pTypeDesc)
27 const EnvTypeId id = pTypeDesc->GetEnvTypeId();
28 const SGUID guid = pTypeDesc->GetGUID();
29 CRY_ASSERT((id != GetEnvTypeId<void>()) && guid);
30 if((id != GetEnvTypeId<void>()) && guid)
32 if(!GetTypeDesc(id) && !GetTypeDesc(guid))
34 m_typeDescsById.insert(TypeDescByIdMap::value_type(id, pTypeDesc));
35 m_typeDescsByGUID.insert(TypeDescByGUIDMap::value_type(guid, pTypeDesc));
36 return true;
40 return false;
43 //////////////////////////////////////////////////////////////////////////
44 const IEnvTypeDesc* CEnvRegistry::GetTypeDesc(const EnvTypeId& id) const
46 TypeDescByIdMap::const_iterator itTypeDesc = m_typeDescsById.find(id);
47 return itTypeDesc != m_typeDescsById.end() ? itTypeDesc->second.get() : nullptr;
50 //////////////////////////////////////////////////////////////////////////
51 const IEnvTypeDesc* CEnvRegistry::GetTypeDesc(const SGUID& guid) const
53 TypeDescByGUIDMap::const_iterator itTypeDesc = m_typeDescsByGUID.find(guid);
54 return itTypeDesc != m_typeDescsByGUID.end() ? itTypeDesc->second.get() : nullptr;
57 //////////////////////////////////////////////////////////////////////////
58 void CEnvRegistry::VisitTypeDescs(const EnvTypeDescVisitor& visitor) const
60 CRY_ASSERT(visitor);
61 if(visitor)
63 for(const TypeDescByIdMap::value_type& typeDesc : m_typeDescsById)
65 if(visitor(*typeDesc.second) != EVisitStatus::Continue)
67 break;
73 //////////////////////////////////////////////////////////////////////////
74 ISignalPtr CEnvRegistry::CreateSignal(const SGUID& guid)
76 if(GetSignal(guid) == NULL)
78 ISignalPtr pSignal(new CSignal(guid));
79 m_signals.insert(TSignalMap::value_type(guid, pSignal));
80 return pSignal;
82 return ISignalPtr();
85 //////////////////////////////////////////////////////////////////////////
86 ISignalConstPtr CEnvRegistry::GetSignal(const SGUID& guid) const
88 TSignalMap::const_iterator iSignal = m_signals.find(guid);
89 return iSignal != m_signals.end() ? iSignal->second : ISignalConstPtr();
92 //////////////////////////////////////////////////////////////////////////
93 void CEnvRegistry::VisitSignals(const EnvSignalVisitor& visitor) const
95 CRY_ASSERT(visitor);
96 if(visitor)
98 for(TSignalMap::const_iterator iSignal = m_signals.begin(), iEndSignal = m_signals.end(); iSignal != iEndSignal; ++ iSignal)
100 if(visitor(iSignal->second) != EVisitStatus::Continue)
102 break;
108 //////////////////////////////////////////////////////////////////////////
109 bool CEnvRegistry::RegisterGlobalFunction(const IGlobalFunctionPtr& pFunction)
111 CRY_ASSERT(pFunction != NULL);
112 if(pFunction != NULL)
114 const SGUID guid = pFunction->GetGUID();
115 CRY_ASSERT(guid);
116 if(guid && !GetGlobalFunction(guid))
118 m_globalFunctions.insert(TGlobalFunctionMap::value_type(guid, pFunction));
119 return true;
122 return false;
125 //////////////////////////////////////////////////////////////////////////
126 IGlobalFunctionConstPtr CEnvRegistry::GetGlobalFunction(const SGUID& guid) const
128 TGlobalFunctionMap::const_iterator iGlobalFunction = m_globalFunctions.find(guid);
129 return iGlobalFunction != m_globalFunctions.end() ? iGlobalFunction->second : IGlobalFunctionConstPtr();
132 //////////////////////////////////////////////////////////////////////////
133 void CEnvRegistry::VisitGlobalFunctions(const EnvGlobalFunctionVisitor& visitor) const
135 CRY_ASSERT(visitor);
136 if(visitor)
138 for(TGlobalFunctionMap::const_iterator iGlobalFunction = m_globalFunctions.begin(), iEndGlobalFunction = m_globalFunctions.end(); iGlobalFunction != iEndGlobalFunction; ++ iGlobalFunction)
140 if(visitor(iGlobalFunction->second) != EVisitStatus::Continue)
142 break;
148 //////////////////////////////////////////////////////////////////////////
149 void CEnvRegistry::RegisterFunction(const IEnvFunctionDescriptorPtr& pFunctionDescriptor)
151 SCHEMATYC2_SYSTEM_ASSERT_FATAL(pFunctionDescriptor);
152 const SGUID guid = pFunctionDescriptor->GetGUID();
153 SCHEMATYC2_SYSTEM_ASSERT_FATAL(!GetFunction(guid));
154 m_functions.insert(Functions::value_type(guid, pFunctionDescriptor));
157 //////////////////////////////////////////////////////////////////////////
158 const IEnvFunctionDescriptor* CEnvRegistry::GetFunction(const SGUID& guid) const
160 Functions::const_iterator itFunction = m_functions.find(guid);
161 return itFunction != m_functions.end() ? itFunction->second.get() : nullptr;
164 //////////////////////////////////////////////////////////////////////////
165 void CEnvRegistry::VisitFunctions(const EnvFunctionVisitor& visitor) const
167 SCHEMATYC2_SYSTEM_ASSERT(visitor);
168 if(visitor)
170 for(const Functions::value_type& function : m_functions)
172 if(visitor(*function.second) != EVisitStatus::Continue)
174 break;
180 //////////////////////////////////////////////////////////////////////////
181 IFoundationPtr CEnvRegistry::CreateFoundation(const SGUID& guid, const char* name)
183 if(GetFoundation(guid) == NULL)
185 IFoundationPtr pFoundation(new CFoundation(guid, name));
186 m_foundations.insert(TFoundationMap::value_type(guid, pFoundation));
187 return pFoundation;
189 return IFoundationPtr();
192 //////////////////////////////////////////////////////////////////////////
193 IFoundationConstPtr CEnvRegistry::GetFoundation(const SGUID& guid) const
195 TFoundationMap::const_iterator itFoundation = m_foundations.find(guid);
196 return itFoundation != m_foundations.end() ? itFoundation->second : IFoundationConstPtr();
199 //////////////////////////////////////////////////////////////////////////
200 void CEnvRegistry::VisitFoundations(const EnvFoundationVisitor& visitor, EVisitFlags flags) const
202 SCHEMATYC2_SYSTEM_ASSERT(visitor);
203 if(visitor)
205 for(const TFoundationMap::value_type& foundation : m_foundations)
207 if(((flags & EVisitFlags::IgnoreBlacklist) != 0) || !IsBlacklistedElement(foundation.first))
209 if(visitor(foundation.second) != EVisitStatus::Continue)
211 break;
218 //////////////////////////////////////////////////////////////////////////
219 IAbstractInterfacePtr CEnvRegistry::CreateAbstractInterface(const SGUID& guid)
221 return IAbstractInterfacePtr(new CAbstractInterface(guid));
224 //////////////////////////////////////////////////////////////////////////
225 bool CEnvRegistry::RegisterAbstractInterface(const IAbstractInterfacePtr& pAbstractInterface)
227 CRY_ASSERT(pAbstractInterface);
228 if(pAbstractInterface)
230 const SGUID guid = pAbstractInterface->GetGUID();
231 CRY_ASSERT(guid);
232 if(guid && !GetAbstractInterface(guid))
234 bool error = false;
235 const size_t functionCount = pAbstractInterface->GetFunctionCount();
236 for(size_t iFunction = 0; iFunction < functionCount; ++ iFunction)
238 IAbstractInterfaceFunctionConstPtr pFunction = pAbstractInterface->GetFunction(iFunction);
239 if(!pFunction || GetAbstractInterfaceFunction(pAbstractInterface->GetFunction(iFunction)->GetGUID()) || (pFunction->GetOwnerGUID() != guid))
241 error = true;
242 break;
245 if(!error)
247 m_abstractInterfaces.insert(TAbstractInterfaceMap::value_type(guid, pAbstractInterface));
248 for(size_t iFunction = 0; iFunction < functionCount; ++ iFunction)
250 IAbstractInterfaceFunctionConstPtr pFunction = pAbstractInterface->GetFunction(iFunction);
251 m_abstractInterfaceFunctions.insert(TAbstractInterfaceFunctionMap::value_type(pFunction->GetGUID(), pFunction));
253 return true;
257 return false;
260 //////////////////////////////////////////////////////////////////////////
261 IAbstractInterfaceConstPtr CEnvRegistry::GetAbstractInterface(const SGUID& guid) const
263 TAbstractInterfaceMap::const_iterator iAbstractInterface = m_abstractInterfaces.find(guid);
264 return iAbstractInterface != m_abstractInterfaces.end() ? iAbstractInterface->second : IAbstractInterfaceConstPtr();
267 //////////////////////////////////////////////////////////////////////////
268 void CEnvRegistry::VisitAbstractInterfaces(const EnvAbstractInterfaceVisitor& visitor) const
270 CRY_ASSERT(visitor);
271 if(visitor)
273 for(TAbstractInterfaceMap::const_iterator iAbstractInterface = m_abstractInterfaces.begin(), iEndAbstractInterface = m_abstractInterfaces.end(); iAbstractInterface != iEndAbstractInterface; ++ iAbstractInterface)
275 if(visitor(iAbstractInterface->second) != EVisitStatus::Continue)
277 break;
283 //////////////////////////////////////////////////////////////////////////
284 IAbstractInterfaceFunctionConstPtr CEnvRegistry::GetAbstractInterfaceFunction(const SGUID& guid) const
286 TAbstractInterfaceFunctionMap::const_iterator iFunction = m_abstractInterfaceFunctions.find(guid);
287 return iFunction != m_abstractInterfaceFunctions.end() ? iFunction->second : IAbstractInterfaceFunctionConstPtr();
290 //////////////////////////////////////////////////////////////////////////
291 void CEnvRegistry::VisitAbstractInterfaceFunctions(const EnvAbstractInterfaceFunctionVisitor& visitor) const
293 CRY_ASSERT(visitor);
294 if(visitor)
296 for(TAbstractInterfaceFunctionMap::const_iterator iFunction = m_abstractInterfaceFunctions.begin(), iEndFunction = m_abstractInterfaceFunctions.end(); iFunction != iEndFunction; ++ iFunction)
298 if(visitor(iFunction->second) != EVisitStatus::Continue)
300 break;
306 //////////////////////////////////////////////////////////////////////////
307 bool CEnvRegistry::RegisterComponentFactory(const IComponentFactoryPtr& pComponentFactory)
309 CRY_ASSERT(pComponentFactory != NULL);
310 if(pComponentFactory != NULL)
312 const SGUID guid = pComponentFactory->GetComponentGUID();
313 CRY_ASSERT(guid);
314 if(guid && !GetComponentFactory(guid))
316 m_componentFactories.insert(TComponentFactoryMap::value_type(guid, pComponentFactory));
317 return true;
320 return false;
323 //////////////////////////////////////////////////////////////////////////
324 IComponentFactoryConstPtr CEnvRegistry::GetComponentFactory(const SGUID& guid) const
326 TComponentFactoryMap::const_iterator iComponentFactory = m_componentFactories.find(guid);
327 return iComponentFactory != m_componentFactories.end() ? iComponentFactory->second : IComponentFactoryConstPtr();
330 //////////////////////////////////////////////////////////////////////////
331 void CEnvRegistry::VisitComponentFactories(const EnvComponentFactoryVisitor& visitor) const
333 CRY_ASSERT(visitor);
334 if(visitor)
336 for(TComponentFactoryMap::const_iterator iComponentFactory = m_componentFactories.begin(), iEndComponentFactory = m_componentFactories.end(); iComponentFactory != iEndComponentFactory; ++ iComponentFactory)
338 if(visitor(iComponentFactory->second) != EVisitStatus::Continue)
340 break;
346 //////////////////////////////////////////////////////////////////////////
347 bool CEnvRegistry::RegisterComponentMemberFunction(const IComponentMemberFunctionPtr& pFunction)
349 CRY_ASSERT(pFunction != NULL);
350 if(pFunction != NULL)
352 const SGUID guid = pFunction->GetGUID();
353 CRY_ASSERT(guid);
354 if(guid && !GetComponentMemberFunction(guid))
356 m_componentMemberFunctions.insert(TComponentMemberFunctionMap::value_type(guid, pFunction));
357 return true;
360 return false;
363 //////////////////////////////////////////////////////////////////////////
364 IComponentMemberFunctionConstPtr CEnvRegistry::GetComponentMemberFunction(const SGUID& guid) const
366 TComponentMemberFunctionMap::const_iterator iComponentMemberFunction = m_componentMemberFunctions.find(guid);
367 return iComponentMemberFunction != m_componentMemberFunctions.end() ? iComponentMemberFunction->second : IComponentMemberFunctionConstPtr();
370 //////////////////////////////////////////////////////////////////////////
371 void CEnvRegistry::VisitComponentMemberFunctions(const EnvComponentMemberFunctionVisitor& visitor) const
373 CRY_ASSERT(visitor);
374 if(visitor)
376 for(TComponentMemberFunctionMap::const_iterator iComponentMemberFunction = m_componentMemberFunctions.begin(), iEndComponentMemberFunction = m_componentMemberFunctions.end(); iComponentMemberFunction != iEndComponentMemberFunction; ++ iComponentMemberFunction)
378 if(visitor(iComponentMemberFunction->second) != EVisitStatus::Continue)
380 break;
386 //////////////////////////////////////////////////////////////////////////
387 bool CEnvRegistry::RegisterActionFactory(const IActionFactoryPtr& pActionFactory)
389 CRY_ASSERT(pActionFactory != NULL);
390 if(pActionFactory != NULL)
392 const SGUID guid = pActionFactory->GetActionGUID();
393 CRY_ASSERT(guid);
394 if(guid && !GetActionFactory(guid))
396 m_actionFactories.insert(TActionFactoryMap::value_type(guid, pActionFactory));
397 return true;
400 return false;
403 //////////////////////////////////////////////////////////////////////////
404 IActionFactoryConstPtr CEnvRegistry::GetActionFactory(const SGUID& guid) const
406 TActionFactoryMap::const_iterator iActionFactory = m_actionFactories.find(guid);
407 return iActionFactory != m_actionFactories.end() ? iActionFactory->second : IActionFactoryConstPtr();
410 //////////////////////////////////////////////////////////////////////////
411 void CEnvRegistry::VisitActionFactories(const EnvActionFactoryVisitor& visitor) const
413 CRY_ASSERT(visitor);
414 if(visitor)
416 for(TActionFactoryMap::const_iterator iActionFactory = m_actionFactories.begin(), iEndActionFactory = m_actionFactories.end(); iActionFactory != iEndActionFactory; ++ iActionFactory)
418 if(visitor(iActionFactory->second) != EVisitStatus::Continue)
420 break;
426 //////////////////////////////////////////////////////////////////////////
427 bool CEnvRegistry::RegisterActionMemberFunction(const IActionMemberFunctionPtr& pFunction)
429 CRY_ASSERT(pFunction != NULL);
430 if(pFunction != NULL)
432 const SGUID guid = pFunction->GetGUID();
433 CRY_ASSERT(guid);
434 if(guid && !GetActionMemberFunction(guid))
436 m_actionMemberFunctions.insert(TActionMemberFunctionMap::value_type(guid, pFunction));
437 return true;
440 return false;
443 //////////////////////////////////////////////////////////////////////////
444 IActionMemberFunctionConstPtr CEnvRegistry::GetActionMemberFunction(const SGUID& guid) const
446 TActionMemberFunctionMap::const_iterator iActionMemberFunction = m_actionMemberFunctions.find(guid);
447 return iActionMemberFunction != m_actionMemberFunctions.end() ? iActionMemberFunction->second : IActionMemberFunctionConstPtr();
450 //////////////////////////////////////////////////////////////////////////
451 void CEnvRegistry::VisitActionMemberFunctions(const EnvActionMemberFunctionVisitor& visitor) const
453 CRY_ASSERT(visitor);
454 if(visitor)
456 for(TActionMemberFunctionMap::const_iterator iActionMemberFunction = m_actionMemberFunctions.begin(), iEndActionMemberFunction = m_actionMemberFunctions.end(); iActionMemberFunction != iEndActionMemberFunction; ++ iActionMemberFunction)
458 if(visitor(iActionMemberFunction->second) != EVisitStatus::Continue)
460 break;
466 //////////////////////////////////////////////////////////////////////////
467 void CEnvRegistry::BlacklistElement(const SGUID& guid)
469 m_blacklist.insert(guid);
472 //////////////////////////////////////////////////////////////////////////
473 bool CEnvRegistry::IsBlacklistedElement(const SGUID& guid) const
475 return m_blacklist.find(guid) != m_blacklist.end();
478 //////////////////////////////////////////////////////////////////////////
479 bool CEnvRegistry::RegisterSettings(const char* name, const IEnvSettingsPtr& pSettings)
481 return m_settings.insert(TSettingsMap::value_type(name, pSettings)).second;
484 //////////////////////////////////////////////////////////////////////////
485 IEnvSettingsPtr CEnvRegistry::GetSettings(const char* name) const
487 TSettingsMap::const_iterator iSettings = m_settings.find(name);
488 return iSettings != m_settings.end() ? iSettings->second : IEnvSettingsPtr();
491 //////////////////////////////////////////////////////////////////////////
492 void CEnvRegistry::VisitSettings(const EnvSettingsVisitor& visitor) const
494 CRY_ASSERT(visitor);
495 if(visitor)
497 for(TSettingsMap::const_iterator iSettings = m_settings.begin(), iEndSettings = m_settings.end(); iSettings != iEndSettings; ++ iSettings)
499 if(visitor(iSettings->first, iSettings->second) != EVisitStatus::Continue)
501 break;
507 //////////////////////////////////////////////////////////////////////////
508 void CEnvRegistry::LoadAllSettings()
510 MEMSTAT_CONTEXT(EMemStatContextType::Other, "Schematyc: Load All Settings");
511 CRY_PROFILE_FUNCTION(PROFILE_LOADING_ONLY);
512 const char* szSettingsFolder = gEnv->pSchematyc2->GetSettingsFolder();
513 const char* szSettingsExtension = gEnv->pSchematyc2->GetSettingsExtension();
514 for(TSettingsMap::const_iterator iSettings = m_settings.begin(), iEndSettings = m_settings.end(); iSettings != iEndSettings; ++ iSettings)
516 const char* szName = iSettings->first.c_str();
517 stack_string fileName = szSettingsFolder;
518 fileName.append("/");
519 fileName.append(szName);
520 fileName.append(".");
521 fileName.append(szSettingsExtension);
522 fileName.MakeLower();
523 Serialization::LoadXmlFile(*iSettings->second, fileName);
527 //////////////////////////////////////////////////////////////////////////
528 void CEnvRegistry::SaveAllSettings()
530 MEMSTAT_CONTEXT(EMemStatContextType::Other, "Schematyc: Save All Settings");
531 const char* szSttingsFolder = gEnv->pSchematyc2->GetSettingsFolder();
532 const char* szSettingsExtension = gEnv->pSchematyc2->GetSettingsExtension();
533 for(TSettingsMap::const_iterator iSettings = m_settings.begin(), iEndSettings = m_settings.end(); iSettings != iEndSettings; ++ iSettings)
535 const char* szName = iSettings->first.c_str();
536 stack_string fileName = szSttingsFolder;
537 fileName.append("/");
538 fileName.append(szName);
539 fileName.append(".");
540 fileName.append(szSettingsExtension);
541 fileName.MakeLower();
542 Serialization::SaveXmlFile(fileName, *iSettings->second, szName);
546 //////////////////////////////////////////////////////////////////////////
547 void CEnvRegistry::Validate() const
549 ValidateComponentDependencies();
552 //////////////////////////////////////////////////////////////////////////
553 void CEnvRegistry::Clear()
555 m_typeDescsById.clear();
556 m_typeDescsByGUID.clear();
557 m_signals.clear();
558 m_globalFunctions.clear();
559 m_foundations.clear();
560 m_abstractInterfaces.clear();
561 m_componentFactories.clear();
562 m_componentMemberFunctions.clear();
563 m_actionFactories.clear();
564 m_actionMemberFunctions.clear();
567 //////////////////////////////////////////////////////////////////////////
568 void CEnvRegistry::ValidateComponentDependencies() const
570 for(TComponentFactoryMap::const_iterator iComponentFactory = m_componentFactories.begin(), iEndComponentFactory = m_componentFactories.end(); iComponentFactory != iEndComponentFactory; ++ iComponentFactory)
572 const SGUID componentGUID = iComponentFactory->first;
573 const IComponentFactory& componentFactory = *iComponentFactory->second;
574 for(size_t iComponentDependency = 0, componentDependencyCount = componentFactory.GetDependencyCount(); iComponentDependency < componentDependencyCount; ++ iComponentDependency)
576 const SGUID dependencyComponentGUID = componentFactory.GetDependencyGUID(iComponentDependency);
577 IComponentFactoryConstPtr pDependencyComponentFactory = GetComponentFactory(dependencyComponentGUID);
578 if(pDependencyComponentFactory)
580 if((pDependencyComponentFactory->GetFlags() & EComponentFlags::Singleton) == 0)
582 CryFatalError("%s : Non-singleton %s component detected as dependency of %s!", __FUNCTION__, pDependencyComponentFactory->GetName(), componentFactory.GetName());
584 if(EnvComponentUtils::IsDependency(*pDependencyComponentFactory, componentGUID))
586 CryFatalError("%s : Circular dependency detected between %s and %s components!", __FUNCTION__, componentFactory.GetName(), pDependencyComponentFactory->GetName());
589 else
591 char stringBuffer[StringUtils::s_guidStringBufferSize];
592 CryFatalError("%s : Unable to resolve dependency %s for %s component!", __FUNCTION__, StringUtils::SysGUIDToString(dependencyComponentGUID.sysGUID, stringBuffer), componentFactory.GetName());