Skip to content

Commit

Permalink
Add class registries for dynamic modules that will be auto-filled.
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamyCecil committed Jun 12, 2024
1 parent e816e6b commit 5076d9a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/Engine/Base/CTString.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ ENGINE_API CTString RemoveSpecialCodes( const CTString &str);

#include <Engine/Base/CTString.inl>

// [Cecil] Hashing function for compatibility with STL
namespace std {
template<class Key> struct hash;

template<> struct hash<CTString> {
inline size_t operator()(const CTString &str) const noexcept {
return str.GetHash();
};
};
};

#endif /* include-once check. */

22 changes: 22 additions & 0 deletions Sources/Engine/Base/DynamicModules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright (c) 2024 Dreamy Cecil
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

#include "StdH.h"

#include <Engine/Base/DynamicModules.h>

EntityClassRegistry_t *_pEntityClassRegistry = NULL;
ShaderRenderRegistry_t *_pShaderRenderRegistry = NULL;
ShaderDescRegistry_t *_pShaderDescRegistry = NULL;
79 changes: 79 additions & 0 deletions Sources/Engine/Base/DynamicModules.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright (c) 2024 Dreamy Cecil
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

#ifndef SE_INCL_DYNAMICMODULES_H
#define SE_INCL_DYNAMICMODULES_H

#ifdef PRAGMA_ONCE
#pragma once
#endif

#include <Engine/Entities/EntityProperties.h>
#include <Engine/Graphics/Shader.h>

#include <unordered_map>

// Entity class registry under class names specified in ECL files
typedef std::unordered_map<CTString, CDLLEntityClass *> EntityClassRegistry_t;
ENGINE_API extern EntityClassRegistry_t *_pEntityClassRegistry;

// Registries of shader methods under method names specified in SHA files
typedef std::unordered_map<CTString, FShaderRender> ShaderRenderRegistry_t;
ENGINE_API extern ShaderRenderRegistry_t *_pShaderRenderRegistry;

typedef std::unordered_map<CTString, FShaderDesc> ShaderDescRegistry_t;
ENGINE_API extern ShaderDescRegistry_t *_pShaderDescRegistry;

// Structure for automatically registering classes from dynamic modules upon their initialization
struct DynamicModuleClass
{
// Method for adding a library entity class upon module initialization
// Generated by ECC using ENTITY_CLASSDEFINITION() in global scope, for example:
// DynamicModuleClass CAmmoItem_AddToRegistry("CAmmoItem", &CAmmoItem_DLLClass);
DynamicModuleClass(const char *strClass, CDLLEntityClass *pToAdd)
{
// Create a new registry
if (_pEntityClassRegistry == NULL) _pEntityClassRegistry = new EntityClassRegistry_t;

// Add new entity class (or replace an existing one)
(*_pEntityClassRegistry)[strClass] = pToAdd;
};

// Method for adding shader render function upon module initialization
// Generated using SHADER_MAIN() macro, for example:
// DynamicModuleClass Shader_Detail_AddToRegistry("Shader_Detail", &Shader_Detail);
DynamicModuleClass(const char *strMethod, FShaderRender pToAdd)
{
// Create a new registry
if (_pShaderRenderRegistry == NULL) _pShaderRenderRegistry = new ShaderRenderRegistry_t;

// Add new entity class (or replace an existing one)
(*_pShaderRenderRegistry)[strMethod] = pToAdd;
};

// Method for adding shader description function upon module initialization
// Generated using SHADER_DESC() macro, for example:
// DynamicModuleClass Shader_Desc_Detail_AddToRegistry("Shader_Desc_Detail", &Shader_Desc_Detail);
DynamicModuleClass(const char *strMethod, FShaderDesc pToAdd)
{
// Create a new registry
if (_pShaderDescRegistry == NULL) _pShaderDescRegistry = new ShaderDescRegistry_t;

// Add new entity class (or replace an existing one)
(*_pShaderDescRegistry)[strMethod] = pToAdd;
};
};

#endif // include-once check
1 change: 1 addition & 0 deletions Sources/Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <Engine/Base/ProgressHook.h>
#include <Engine/Base/Registry.h>
#include <Engine/Base/IFeel.h>
#include <Engine/Base/DynamicModules.h> // [Cecil]

// [Cecil] File system interface
#include <Engine/OS/FileSystem.h>
Expand Down
2 changes: 2 additions & 0 deletions Sources/Engine/Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
<ClCompile Include="Base\CRCTable.cpp" />
<ClCompile Include="Base\CTString.cpp" />
<ClCompile Include="Base\Directory.cpp" />
<ClCompile Include="Base\DynamicModules.cpp" />
<ClCompile Include="Base\ErrorReporting.cpp" />
<ClCompile Include="Base\FileName.cpp" />
<ClCompile Include="Base\IFeel.cpp" />
Expand Down Expand Up @@ -633,6 +634,7 @@
<ClInclude Include="Base\CRC.h" />
<ClInclude Include="Base\CRCTable.h" />
<ClInclude Include="Base\CTString.h" />
<ClInclude Include="Base\DynamicModules.h" />
<ClInclude Include="Base\ErrorReporting.h" />
<ClInclude Include="Base\ErrorTable.h" />
<ClInclude Include="Base\FileName.h" />
Expand Down
6 changes: 6 additions & 0 deletions Sources/Engine/Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@
<ClCompile Include="Sound\SoundAPI_SDL.cpp">
<Filter>Source Files\Sound</Filter>
</ClCompile>
<ClCompile Include="Base\DynamicModules.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base\Anim.h">
Expand Down Expand Up @@ -1362,6 +1365,9 @@
<ClInclude Include="Sound\SoundAPI_SDL.h">
<Filter>Header Files\Sound Headers</Filter>
</ClInclude>
<ClInclude Include="Base\DynamicModules.h">
<Filter>Header Files\Base Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="Base\CTString.inl">
Expand Down

0 comments on commit 5076d9a

Please sign in to comment.