iw4x-client/src/Components/Loader.cpp

222 lines
5.0 KiB
C++
Raw Normal View History

2022-02-27 07:53:44 -05:00
#include <STDInclude.hpp>
2015-12-23 08:45:53 -05:00
namespace Components
{
2016-09-03 09:52:40 -04:00
bool Loader::Pregame = true;
bool Loader::Postgame = false;
bool Loader::Uninitializing = false;
2015-12-23 08:45:53 -05:00
std::vector<Component*> Loader::Components;
2016-09-03 09:52:40 -04:00
bool Loader::IsPregame()
{
2022-12-25 12:23:53 -05:00
return Pregame;
2016-09-03 09:52:40 -04:00
}
2017-02-24 07:30:31 -05:00
bool Loader::IsPostgame()
{
2022-12-25 12:23:53 -05:00
return Postgame;
2017-02-24 07:30:31 -05:00
}
bool Loader::IsUninitializing()
{
2022-12-25 12:23:53 -05:00
return Uninitializing;
}
2015-12-23 08:45:53 -05:00
void Loader::Initialize()
{
2022-12-25 12:23:53 -05:00
Pregame = true;
Postgame = false;
Uninitializing = false;
2017-06-02 09:36:20 -04:00
Utils::Memory::GetAllocator()->clear();
2016-09-03 09:52:40 -04:00
2022-12-25 12:23:53 -05:00
Register(new Auth());
Register(new Command());
Register(new Dvar());
Register(new Exception()); // Install our exception handler as early as posssible to get better debug dumps from startup crashes
Register(new Flags());
Register(new Network());
Register(new Logger());
Register(new Singleton());
Register(new UIScript());
Register(new ZoneBuilder());
Register(new ArenaLength());
Register(new AssetHandler());
Register(new Bans());
Register(new Bots());
Register(new Branding());
Register(new Bullet());
Register(new CardTitles());
Register(new Ceg());
Register(new Changelog());
Register(new Chat());
Register(new ClanTags());
Register(new ClientCommand());
Register(new ConnectProtocol());
Register(new Console());
Register(new D3D9Ex());
Register(new Debug());
Register(new Dedicated());
Register(new Discovery());
Register(new Download());
Register(new Elevators());
Register(new Events());
Register(new FastFiles());
Register(new FileSystem());
Register(new Friends());
Register(new Gamepad());
Register(new Gametypes());
Register(new IPCPipe());
Register(new Lean());
Register(new Localization());
Register(new MapDump());
Register(new MapRotation());
Register(new Maps());
Register(new Materials());
Register(new Menus());
Register(new ModList());
Register(new ModelSurfs());
Register(new NetworkDebug());
Register(new News());
Register(new Node());
Register(new Party());
Register(new PlayerMovement());
Register(new PlayerName());
Register(new Playlist());
Register(new QuickPatch());
Register(new RCon());
Register(new RawFiles());
Register(new RawMouse());
Register(new Renderer());
Register(new Scheduler());
Register(new Security());
Register(new ServerCommands());
Register(new ServerInfo());
Register(new ServerList());
Register(new Session());
Register(new SlowMotion());
Register(new SoundMutexFix());
Register(new StartupMessages());
Register(new Stats());
Register(new StringTable());
Register(new StructuredData());
Register(new TextRenderer());
Register(new Theatre());
Register(new Threading());
Register(new Toast());
Register(new UIFeeder());
Register(new UserInfo());
Register(new VisionFile());
Register(new Voice());
Register(new Vote());
Register(new Weapon());
Register(new Window());
Register(new Zones());
Register(new GSC());
Pregame = false;
2022-06-16 10:15:26 -04:00
// Make sure preDestroy is called when the game shuts down
2022-12-25 12:23:53 -05:00
Scheduler::OnGameShutdown(PreDestroy);
2015-12-23 08:45:53 -05:00
}
void Loader::Uninitialize()
{
2022-12-25 12:23:53 -05:00
Uninitializing = true;
PreDestroyNoPostGame();
2022-12-25 12:23:53 -05:00
std::reverse(Components.begin(), Components.end());
for (auto& component : Components)
2015-12-23 08:45:53 -05:00
{
2016-08-14 10:18:44 -04:00
#ifdef DEBUG
2022-12-25 12:23:53 -05:00
if (!IsPerformingUnitTests())
2016-09-16 05:04:28 -04:00
{
2022-06-12 17:07:53 -04:00
Logger::Print("Unregister component: {}\n", component->getName());
2016-09-16 05:04:28 -04:00
}
2016-08-14 10:18:44 -04:00
#endif
2015-12-23 08:45:53 -05:00
delete component;
}
2022-12-25 12:23:53 -05:00
Components.clear();
2017-06-02 09:36:20 -04:00
Utils::Memory::GetAllocator()->clear();
2022-12-25 12:23:53 -05:00
Uninitializing = false;
2015-12-23 08:45:53 -05:00
}
void Loader::PreDestroy()
{
2022-12-25 12:23:53 -05:00
if (!Postgame)
{
2022-12-25 12:23:53 -05:00
Postgame = true;
2022-12-25 12:23:53 -05:00
auto components = Components;
2017-02-24 07:30:31 -05:00
std::reverse(components.begin(), components.end());
2022-12-25 12:23:53 -05:00
for (auto& component : components)
{
component->preDestroy();
}
}
}
2017-02-24 07:30:31 -05:00
void Loader::PreDestroyNoPostGame()
{
2022-12-25 12:23:53 -05:00
if (!Postgame)
2017-02-24 07:30:31 -05:00
{
2022-12-25 12:23:53 -05:00
auto components = Components;
2017-02-24 07:30:31 -05:00
std::reverse(components.begin(), components.end());
2022-12-25 12:23:53 -05:00
for (auto& component : components)
2017-02-24 07:30:31 -05:00
{
component->preDestroy();
}
2022-12-25 12:23:53 -05:00
Postgame = true;
2017-02-24 07:30:31 -05:00
}
}
bool Loader::PerformUnitTests()
{
bool result = true;
Logger::Print("Performing unit tests for components:\n");
2022-12-25 12:23:53 -05:00
for (const auto& component : Components)
{
#if defined(FORCE_UNIT_TESTS)
Logger::Debug("Testing '{}'...\n", component->getName());
2016-08-15 11:07:32 -04:00
#endif
auto startTime = std::chrono::high_resolution_clock::now();
auto testRes = component->unitTest();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startTime).count();
2022-06-12 17:07:53 -04:00
Logger::Print("Test done ({}ms): {}\n\n", duration, (testRes ? "Success" : "Error"));
result &= testRes;
}
return result;
}
2017-07-12 05:37:23 -04:00
bool Loader::IsPerformingUnitTests()
{
2016-02-19 17:57:06 -05:00
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
return Flags::HasFlag("tests");
#else
return false;
#endif
}
2015-12-23 08:45:53 -05:00
void Loader::Register(Component* component)
{
if (component)
{
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
2022-12-25 12:23:53 -05:00
if (!IsPerformingUnitTests())
2016-09-16 05:04:28 -04:00
{
2022-06-12 17:07:53 -04:00
Logger::Print("Component registered: {}\n", component->getName());
2016-09-16 05:04:28 -04:00
}
2016-08-14 10:18:44 -04:00
#endif
2022-12-25 12:23:53 -05:00
Components.push_back(component);
2015-12-23 08:45:53 -05:00
}
}
}