[Loader] Make use of typeinfo for singleton and name resolving

This commit is contained in:
momo5502 2017-05-31 12:02:14 +02:00
parent 1628afb4c3
commit 4bb1157234
64 changed files with 23 additions and 248 deletions

View File

@ -114,7 +114,7 @@ namespace Components
#ifdef DEBUG
if(!Loader::PerformingUnitTests())
{
Logger::Print("Unregistering component: %s\n", component->getName());
Logger::Print("Unregistering component: %s\n", component->getName().data());
}
#endif
delete component;
@ -167,7 +167,7 @@ namespace Components
for (auto component : Loader::Components)
{
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
Logger::Print("Testing '%s'...\n", component->getName());
Logger::Print("Testing '%s'...\n", component->getName().data());
#endif
auto startTime = std::chrono::high_resolution_clock::now();
bool testRes = component->unitTest();
@ -195,7 +195,7 @@ namespace Components
#ifdef DEBUG
if(!Loader::PerformingUnitTests())
{
Logger::Print("Component registered: %s\n", component->getName());
Logger::Print("Component registered: %s\n", component->getName().data());
}
#endif
Loader::Components.push_back(component);

View File

@ -9,7 +9,12 @@ namespace Components
virtual ~Component() {};
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
virtual const char* getName() { return "Unknown"; };
virtual std::string getName()
{
std::string name = typeid(*this).name();
Utils::String::Replace(name, "class Components::", "");
return name;
};
#endif
// It's illegal to spawn threads in DLLMain, and apparently it causes problems if they are destroyed there as well.
@ -34,6 +39,20 @@ namespace Components
static bool IsPostgame();
static bool IsComInitialized();
template <typename T>
static T* GetInstance()
{
for (auto& component : Loader::Components)
{
if (typeid(*component) == typeid(T))
{
return reinterpret_cast<T*>(component);
}
}
return nullptr;
}
static Utils::Memory::Allocator* GetAlloctor();
private:

View File

@ -16,10 +16,6 @@ namespace Components
AntiCheat();
~AntiCheat();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "AntiCheat"; };
#endif
static void CrashClient();
static void InitLoadLibHook();

View File

@ -7,10 +7,6 @@ namespace Components
public:
ArenaLength();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ArenaLength"; };
#endif
static Game::newMapArena_t NewArenas[128];
private:

View File

@ -22,10 +22,6 @@ namespace Components
AssetHandler();
~AssetHandler();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "AssetHandler"; };
#endif
static void OnFind(Game::XAssetType type, Utils::Slot<Callback> callback);
static void OnLoad(Utils::Slot<RestrictCallback> callback);

View File

@ -8,10 +8,6 @@ namespace Components
Auth();
~Auth();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Auth"; };
#endif
void preDestroy() override;
bool unitTest() override;

View File

@ -10,10 +10,6 @@ namespace Components
Bans();
~Bans();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Bans"; };
#endif
static void BanClientNum(int num, std::string reason);
static bool IsBanned(Entry entry);

View File

@ -8,10 +8,6 @@ namespace Components
Bots();
~Bots();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Bots"; };
#endif
private:
static std::vector<std::string> BotNames;

View File

@ -8,10 +8,6 @@ namespace Components
Changelog();
~Changelog();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Changelog"; };
#endif
static void LoadChangelog();
private:

View File

@ -8,10 +8,6 @@ namespace Components
Colors();
~Colors();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Colors"; };
#endif
static void Strip(const char* in, char* out, int max);
static std::string Strip(std::string in);

View File

@ -50,10 +50,6 @@ namespace Components
Command();
~Command();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Command"; };
#endif
static Game::cmd_function_t* Allocate();
static void Add(const char* name, Utils::Slot<Callback> callback);

View File

@ -7,10 +7,6 @@ namespace Components
public:
ConnectProtocol();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ConnectProtocol"; };
#endif
static bool IsEvaluated();
static bool Used();

View File

@ -11,10 +11,6 @@ namespace Components
Console();
~Console();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Console"; };
#endif
static void SetSkipShutdown();
static void FreeNativeConsole();

View File

@ -7,10 +7,6 @@ namespace Components
public:
D3D9Ex();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "D3D9Ex"; };
#endif
private:
class D3D9Device : public IDirect3DDevice9

View File

@ -10,10 +10,6 @@ namespace Components
Dedicated();
~Dedicated();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Dedicated"; };
#endif
static SteamID PlayerGuids[18][2];
static bool IsEnabled();

View File

@ -8,10 +8,6 @@ namespace Components
Discovery();
~Discovery();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Discovery"; };
#endif
void preDestroy() override;
static void Perform();

View File

@ -9,10 +9,6 @@ namespace Components
Download();
~Download();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Download"; };
#endif
void preDestroy() override;
static void InitiateClientDownload(std::string mod, bool map = false);

View File

@ -44,10 +44,6 @@ namespace Components
Dvar();
~Dvar();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Dvar"; };
#endif
static void OnInit(Utils::Slot<Callback> callback);
// Only strings and bools use this type of declaration

View File

@ -8,9 +8,6 @@ namespace Components
Exception();
~Exception();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Exception"; };
#endif
static LPTOP_LEVEL_EXCEPTION_FILTER Hook();
static void SetMiniDumpType(bool codeseg, bool dataseg);

View File

@ -8,10 +8,6 @@ namespace Components
FastFiles();
~FastFiles();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "FastFiles"; };
#endif
static void AddZonePath(std::string path);
static std::string Current();
static bool Ready();

View File

@ -88,10 +88,6 @@ namespace Components
FileSystem();
~FileSystem();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "FileSystem"; };
#endif
static std::vector<std::string> GetFileList(std::string path, std::string extension);
static std::vector<std::string> GetSysFileList(std::string path, std::string extension, bool folders = false);
static void DeleteFile(std::string folder, std::string file);

View File

@ -8,10 +8,6 @@ namespace Components
Flags();
~Flags();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Flags"; };
#endif
static bool HasFlag(std::string flag);
private:

View File

@ -7,10 +7,6 @@ namespace Components
public:
FrameTime();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "FrameTime"; };
#endif
private:
static void SVFrameWaitStub();
static void SVFrameWaitFunc();

View File

@ -8,10 +8,6 @@ namespace Components
Friends();
~Friends();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Friends"; };
#endif
static void UpdateFriends();
static void UpdateRank();
static void UpdateServer(Network::Address server, std::string hostname, std::string mapname);

View File

@ -7,10 +7,6 @@ namespace Components
public:
Gametypes();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Gametypes"; };
#endif
private:
static unsigned int GetGametypeCount();
static const char* GetGametypeText(unsigned int index, int column);

View File

@ -65,10 +65,6 @@ namespace Components
public:
IPCPipe();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "IPCPipe"; };
#endif
void preDestroy() override;
static bool Write(std::string command, std::string data);

View File

@ -10,10 +10,6 @@ namespace Components
public:
Lean();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Lean"; };
#endif
private:
static Game::kbutton_t in_leanleft;
static Game::kbutton_t in_leanright;

View File

@ -8,10 +8,6 @@ namespace Components
Localization();
~Localization();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Localization"; };
#endif
static void Set(std::string key, std::string value);
static const char* Get(const char* key);

View File

@ -8,10 +8,6 @@ namespace Components
Logger();
~Logger();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Logger"; };
#endif
static void MessagePrint(int channel, std::string message);
static void Print(int channel, const char* message, ...);
static void Print(const char* message, ...);

View File

@ -49,10 +49,6 @@ namespace Components
Maps();
~Maps();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Maps"; };
#endif
static void HandleAsSPMap();
static void AddDependency(std::string expression, std::string zone);

View File

@ -8,10 +8,6 @@ namespace Components
Materials();
~Materials();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Materials"; };
#endif
static int FormatImagePath(char* buffer, size_t size, int, int, const char* image);
private:

View File

@ -11,10 +11,6 @@ namespace Components
Menus();
~Menus();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Menus"; };
#endif
static void FreeEverything();
static void Add(std::string menu);

View File

@ -8,10 +8,6 @@ namespace Components
ModList();
~ModList();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ModList"; };
#endif
static void RunMod(std::string mod);
private:

View File

@ -8,10 +8,6 @@ namespace Components
ModelSurfs();
~ModelSurfs();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ModelSurfs"; };
#endif
private:
static std::unordered_map<void*, IUnknown*> BufferMap;
static std::unordered_map<std::string, Game::CModelAllocData*> AllocMap;

View File

@ -7,10 +7,6 @@ namespace Components
public:
Monitor();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Monitor"; };
#endif
static bool IsEnabled();
private:

View File

@ -8,10 +8,6 @@ namespace Components
MusicalTalent();
~MusicalTalent();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "MusicalTalent"; };
#endif
static void Replace(std::string sound, const char* file);
private:

View File

@ -58,10 +58,6 @@ namespace Components
Network();
~Network();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Network"; };
#endif
static void Handle(std::string packet, Utils::Slot<Callback> callback);
static void OnStart(Utils::Slot<CallbackRaw> callback);

View File

@ -8,10 +8,6 @@ namespace Components
News();
~News();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "News"; };
#endif
void preDestroy() override;
bool unitTest() override;

View File

@ -21,10 +21,6 @@ namespace Components
Node();
~Node();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Node"; };
#endif
bool unitTest() override;
static void SyncNodeList();

View File

@ -8,10 +8,6 @@ namespace Components
Party();
~Party();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Party"; };
#endif
static Network::Address Target();
static void Connect(Network::Address target);
static const char* GetLobbyInfo(SteamID lobby, std::string key);

View File

@ -8,10 +8,6 @@ namespace Components
PlayerName();
~PlayerName();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "PlayerName"; };
#endif
private:
static std::string PlayerNames[18];

View File

@ -10,10 +10,6 @@ namespace Components
Playlist();
~Playlist();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Playlist"; };
#endif
static void LoadPlaylist();
static std::string ReceivedPlaylistBuffer;

View File

@ -10,10 +10,6 @@ namespace Components
QuickPatch();
~QuickPatch();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "QuickPatch"; };
#endif
bool unitTest() override;
static void UnlockStats();

View File

@ -8,10 +8,6 @@ namespace Components
RCon();
~RCon();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "RCon"; };
#endif
private:
class Container
{

View File

@ -7,10 +7,6 @@ namespace Components
public:
RawFiles();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "RawFiles"; };
#endif
static void* RawFiles::LoadModdableRawfileFunc(const char* filename);
};
}

View File

@ -11,10 +11,6 @@ namespace Components
Renderer();
~Renderer();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Renderer"; };
#endif
static int Width();
static int Height();

View File

@ -26,10 +26,6 @@ namespace Components
Script();
~Script();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Script"; };
#endif
static int LoadScriptAndLabel(std::string script, std::string label);
static void AddFunction(std::string name, Game::scr_function_t function, bool isDev = false);

View File

@ -8,10 +8,6 @@ namespace Components
ServerInfo();
~ServerInfo();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ServerInfo"; };
#endif
static Utils::InfoString GetInfo();
private:

View File

@ -32,10 +32,6 @@ namespace Components
ServerList();
~ServerList();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ServerList"; };
#endif
static void Refresh(UIScript::Token);
static void RefreshVisibleList(UIScript::Token);
static void UpdateVisibleList(UIScript::Token);

View File

@ -7,10 +7,6 @@ namespace Components
public:
Singleton();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Singleton"; };
#endif
static bool IsFirstInstance();
private:

View File

@ -10,10 +10,6 @@ namespace Components
public:
SlowMotion();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "SlowMotion"; };
#endif
private:
static int Delay;

View File

@ -7,10 +7,6 @@ namespace Components
public:
StartupMessages();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "StartupMessages"; };
#endif
static void AddMessage(std::string message);
private:

View File

@ -8,10 +8,6 @@ namespace Components
Stats();
~Stats();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Stats"; };
#endif
static bool IsMaxLevel();
private:

View File

@ -8,10 +8,6 @@ namespace Components
StringTable();
~StringTable();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "StringTable"; };
#endif
private:
static std::unordered_map<std::string, Game::StringTable*> StringTableMap;

View File

@ -26,10 +26,6 @@ namespace Components
StructuredData();
~StructuredData();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "StructuredData"; };
#endif
private:
static bool UpdateVersionOffsets(Game::StructuredDataDefSet *set, Game::StructuredDataBuffer *buffer, Game::StructuredDataDef *oldDef);

View File

@ -7,10 +7,6 @@ namespace Components
public:
Theatre();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Theatre"; };
#endif
static void StopRecording();
private:

View File

@ -7,10 +7,6 @@ namespace Components
public:
Threading();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Threading"; };
#endif
private:
static void FrameEpilogueStub();
static void PacketEventStub();

View File

@ -11,10 +11,6 @@ namespace Components
typedef WinToastLib::WinToastTemplate Template;
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Toast"; };
#endif
static void Show(std::string image, std::string title, std::string description, int length);
static bool ShowNative(const WinToastLib::WinToastTemplate& toast);

View File

@ -19,10 +19,6 @@ namespace Components
UIFeeder();
~UIFeeder();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "UIFeeder"; };
#endif
static void Add(float feeder, GetItemCount_t itemCountCb, GetItemText_t itemTextCb, Select_t selectCb);
private:

View File

@ -8,10 +8,6 @@ namespace Components
UIScript();
~UIScript();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "UIScript"; };
#endif
class Token
{
public:

View File

@ -12,10 +12,6 @@ namespace Components
public:
Weapon();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Weapon"; };
#endif
private:
static Game::XAssetHeader WeaponFileLoad(Game::XAssetType type, std::string filename);
static void PatchLimit();

View File

@ -7,10 +7,6 @@ namespace Components
public:
Window();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Window"; };
#endif
static int Width();
static int Height();
static int Width(HWND window);

View File

@ -95,7 +95,6 @@ namespace Components
~ZoneBuilder();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "ZoneBuilder"; };
bool unitTest() override;
#endif

View File

@ -12,10 +12,6 @@ namespace Components
Zones();
~Zones();
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
const char* getName() override { return "Zones"; };
#endif
static void SetVersion(int version);
static int Version() { return Zones::ZoneVersion; };