Use wink-signals

boost.signals sadly requires way too many dependencies, libsigc++ requires too much configuration and all the other libs I tested mostly required constexpr as callbacks (due to passing callbacks via template list), which doesn't server this purpose, so wink-signals seems just about right for what we do.
This commit is contained in:
momo5502 2016-02-11 11:26:41 +01:00
parent dea6d5714a
commit 8cef16017e
18 changed files with 56 additions and 70 deletions

3
.gitmodules vendored
View File

@ -20,3 +20,6 @@
[submodule "deps/protobuf"]
path = deps/protobuf
url = https://github.com/google/protobuf.git
[submodule "deps/Wink-Signals"]
path = deps/Wink-Signals
url = https://github.com/miguelmartin75/Wink-Signals.git

1
deps/Wink-Signals vendored Submodule

@ -0,0 +1 @@
Subproject commit 7fde542d24a7fa0cdac23fbd093f9bd60b9c2c52

2
deps/protobuf vendored

@ -1 +1 @@
Subproject commit bc215089ca7d9da3a2b313b250d1dee665eda438
Subproject commit 028d59fccda7a94c3fd39095261caceb77456af3

View File

@ -109,6 +109,7 @@ workspace "iw4x"
"./deps/libtomcrypt/src/headers",
"./deps/libtommath",
"./deps/protobuf/src",
"./deps/Wink-Signals",
}
-- Virtual paths

View File

@ -5,7 +5,7 @@ namespace Components
bool AssetHandler::BypassState = false;
std::map<Game::XAssetType, AssetHandler::IAsset*> AssetHandler::AssetInterfaces;
std::map<Game::XAssetType, AssetHandler::Callback> AssetHandler::TypeCallbacks;
std::vector<AssetHandler::RestrictCallback> AssetHandler::RestrictCallbacks;
wink::signal<wink::slot<AssetHandler::RestrictCallback>> AssetHandler::RestrictSignal;
std::map<void*, void*> AssetHandler::Relocations;
@ -110,15 +110,11 @@ namespace Components
const char* name = Game::DB_GetXAssetNameHandlers[type](asset);
if (!name) return false;
for (auto callback : AssetHandler::RestrictCallbacks)
{
if (!callback(type, *asset, name))
{
return false;
}
}
bool restrict = false;
AssetHandler::RestrictSignal(type, *asset, name, &restrict);
return true;
// If no slot restricts the loading, we can load the asset
return (!restrict);
}
void __declspec(naked) AssetHandler::AddAssetStub()
@ -149,9 +145,9 @@ namespace Components
AssetHandler::TypeCallbacks[type] = callback;
}
void AssetHandler::OnLoad(RestrictCallback callback)
void AssetHandler::OnLoad(RestrictCallback* callback)
{
AssetHandler::RestrictCallbacks.push_back(callback);
AssetHandler::RestrictSignal.connect(callback);
}
void AssetHandler::Relocate(void* start, void* to, DWORD size)
@ -279,6 +275,7 @@ namespace Components
}
AssetHandler::AssetInterfaces.clear();
//AssetHandler::RestrictSignal.clear();
AssetHandler::TypeCallbacks.clear();
}
}

View File

@ -14,14 +14,14 @@ namespace Components
};
typedef Game::XAssetHeader(*Callback)(Game::XAssetType type, std::string name);
typedef bool(*RestrictCallback)(Game::XAssetType type, Game::XAssetHeader asset, std::string name);
typedef void(RestrictCallback)(Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict);
AssetHandler();
~AssetHandler();
const char* GetName() { return "AssetHandler"; };
static void OnFind(Game::XAssetType type, Callback callback);
static void OnLoad(RestrictCallback callback);
static void OnLoad(RestrictCallback* callback);
static void Relocate(void* start, void* to, DWORD size = 4);
@ -50,7 +50,7 @@ namespace Components
static std::map<Game::XAssetType, IAsset*> AssetInterfaces;
static std::map<Game::XAssetType, Callback> TypeCallbacks;
static std::vector<RestrictCallback> RestrictCallbacks;
static wink::signal<wink::slot<RestrictCallback>> RestrictSignal;
static std::map<void*, void*> Relocations;
};

View File

@ -2,8 +2,7 @@
namespace Components
{
Dvar::Var Dedicated::Dedi;
std::vector<Dedicated::Callback> Dedicated::FrameCallbacks;
wink::signal<wink::slot<Dedicated::Callback>> Dedicated::FrameSignal;
bool Dedicated::IsDedicated()
{
@ -144,18 +143,14 @@ namespace Components
Network::SendCommand(master, "heartbeat", "IW4");
}
void Dedicated::OnFrame(Dedicated::Callback callback)
void Dedicated::OnFrame(Dedicated::Callback* callback)
{
Dedicated::FrameCallbacks.push_back(callback);
Dedicated::FrameSignal.connect(callback);
}
void Dedicated::FrameStub()
{
for (auto callback : Dedicated::FrameCallbacks)
{
callback();
}
Dedicated::FrameSignal();
Utils::Hook::Call<void()>(0x5A8E80)();
}
@ -250,6 +245,6 @@ namespace Components
Dedicated::~Dedicated()
{
Dedicated::FrameCallbacks.clear();
//Dedicated::FrameSignal.clear();
}
}

View File

@ -3,7 +3,7 @@ namespace Components
class Dedicated : public Component
{
public:
typedef void(*Callback)();
typedef void(Callback)();
Dedicated();
~Dedicated();
@ -13,11 +13,10 @@ namespace Components
static void Heartbeat();
static void OnFrame(Callback callback);
static void OnFrame(Callback* callback);
private:
static Dvar::Var Dedi;
static std::vector<Callback> FrameCallbacks;
static wink::signal<wink::slot<Callback>> FrameSignal;
static void MapRotate();
static void FrameStub();

View File

@ -2,7 +2,7 @@
namespace Components
{
std::vector<Dvar::Callback> Dvar::RegistrationCallbacks;
wink::signal<wink::slot<Dvar::Callback>> Dvar::RegistrationSignal;
Dvar::Var::Var(std::string dvarName) : Var()
{
@ -131,18 +131,15 @@ namespace Components
return Game::Dvar_RegisterInt(name, value, min, max, flag.val, description);
}
void Dvar::OnInit(Dvar::Callback callback)
void Dvar::OnInit(Dvar::Callback* callback)
{
Dvar::RegistrationCallbacks.push_back(callback);
Dvar::RegistrationSignal.connect(callback);
}
Game::dvar_t* Dvar::RegisterName(const char* name, const char* default, Game::dvar_flag flag, const char* description)
{
// Run callbacks
for (auto callback : Dvar::RegistrationCallbacks)
{
callback();
}
Dvar::RegistrationSignal();
// Name watcher
Renderer::OnFrame([] ()
@ -205,6 +202,6 @@ namespace Components
Dvar::~Dvar()
{
Dvar::RegistrationCallbacks.clear();
//Dvar::RegistrationSignal.clear();
}
}

View File

@ -3,7 +3,7 @@ namespace Components
class Dvar : public Component
{
public:
typedef void(*Callback)();
typedef void(Callback)();
struct Flag
{
@ -42,14 +42,14 @@ namespace Components
~Dvar();
const char* GetName() { return "Dvar"; };
static void OnInit(Callback callback);
static void OnInit(Callback* callback);
// Only strings and bools use this type of declaration
template<typename T> static Var Register(const char* name, T value, Flag flag, const char* description);
template<typename T> static Var Register(const char* name, T value, T min, T max, Flag flag, const char* description);
private:
static std::vector<Callback> RegistrationCallbacks;
static wink::signal<wink::slot<Callback>> RegistrationSignal;
static Game::dvar_t* RegisterName(const char* name, const char* default, Game::dvar_flag flag, const char* description);
};

View File

@ -50,13 +50,14 @@ namespace Components
return FastFiles::LoadLocalizeZones(data.data(), data.size(), sync);
}
bool Maps::LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, std::string name)
void Maps::LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict)
{
if (std::find(Maps::CurrentDependencies.begin(), Maps::CurrentDependencies.end(), FastFiles::Current()) != Maps::CurrentDependencies.end())
{
if (type == Game::XAssetType::ASSET_TYPE_GAME_MAP_MP || type == Game::XAssetType::ASSET_TYPE_COL_MAP_MP || type == Game::XAssetType::ASSET_TYPE_GFX_MAP || type == Game::XAssetType::ASSET_TYPE_MAP_ENTS || type == Game::XAssetType::ASSET_TYPE_COM_MAP || type == Game::XAssetType::ASSET_TYPE_FX_MAP)
{
return false;
*restrict = true;
return;
}
}
@ -70,8 +71,6 @@ namespace Components
asset.mapEnts->entitystring = mapEntities.data();
}
}
return true;
}
void Maps::GetBSPName(char* buffer, size_t size, const char* format, const char* mapname)

View File

@ -19,7 +19,7 @@ namespace Components
static std::vector<std::string> CurrentDependencies;
static void GetBSPName(char* buffer, size_t size, const char* format, const char* mapname);
static bool LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, std::string name);
static void LoadAssetRestrict(Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict);
static void LoadMapZones(Game::XZoneInfo *zoneInfo, unsigned int zoneCount, int sync);
void ReallocateEntryPool();

View File

@ -2,7 +2,7 @@
namespace Components
{
std::vector<QuickPatch::Callback> QuickPatch::ShutdownCallbacks;
wink::signal<wink::slot<QuickPatch::Callback>> QuickPatch::ShutdownSignal;
int64_t* QuickPatch::GetStatsID()
{
@ -12,17 +12,13 @@ namespace Components
void QuickPatch::OnShutdown(QuickPatch::Callback callback)
{
QuickPatch::ShutdownCallbacks.push_back(callback);
QuickPatch::ShutdownSignal.connect(callback);
}
void QuickPatch::ShutdownStub(int channel, const char* message)
{
Game::Com_Printf(0, message);
for (auto callback : QuickPatch::ShutdownCallbacks)
{
if (callback) callback();
}
QuickPatch::ShutdownSignal();
}
void QuickPatch::UnlockStats()
@ -207,6 +203,6 @@ namespace Components
QuickPatch::~QuickPatch()
{
QuickPatch::ShutdownCallbacks.clear();
//QuickPatch::ShutdownSignal.clear();
}
}

View File

@ -3,17 +3,17 @@ namespace Components
class QuickPatch : public Component
{
public:
typedef void(*Callback)();
typedef void(Callback)();
QuickPatch();
~QuickPatch();
const char* GetName() { return "QuickPatch"; };
static void UnlockStats();
static void OnShutdown(Callback callback);
static void OnShutdown(Callback* callback);
private:
static std::vector<Callback> ShutdownCallbacks;
static wink::signal<wink::slot<Callback>> ShutdownSignal;
static int64_t* GetStatsID();
static void ShutdownStub(int channel, const char* message);

View File

@ -3,7 +3,7 @@
namespace Components
{
Utils::Hook Renderer::DrawFrameHook;
std::vector<Renderer::Callback> Renderer::FrameCallbacks;
wink::signal<wink::slot<Renderer::Callback>> Renderer::FrameSignal;
void __declspec(naked) Renderer::FrameHook()
{
@ -16,15 +16,12 @@ namespace Components
void Renderer::FrameHandler()
{
for (auto callback : Renderer::FrameCallbacks)
{
callback();
}
Renderer::FrameSignal();
}
void Renderer::OnFrame(Renderer::Callback callback)
void Renderer::OnFrame(Renderer::Callback* callback)
{
Renderer::FrameCallbacks.push_back(callback);
Renderer::FrameSignal.connect(callback);
}
int Renderer::Width()
@ -46,6 +43,8 @@ namespace Components
Renderer::~Renderer()
{
Renderer::DrawFrameHook.Uninstall();
Renderer::FrameCallbacks.clear();
// As I don't want to include my fork as submodule, we have to wait till my pull request gets accepted in order to do this.
//Renderer::FrameSignal.clear();
}
}

View File

@ -3,7 +3,7 @@ namespace Components
class Renderer : public Component
{
public:
typedef void(*Callback)();
typedef void(Callback)();
Renderer();
~Renderer();
@ -12,13 +12,13 @@ namespace Components
static int Width();
static int Height();
static void OnFrame(Callback callback);
static void OnFrame(Callback* callback);
private:
static void FrameHook();
static void FrameHandler();
static std::vector<Callback> FrameCallbacks;
static wink::signal<wink::slot<Callback>> FrameSignal;
static Utils::Hook DrawFrameHook;
};
}

View File

@ -447,7 +447,7 @@ namespace Components
static_assert(sizeof(Game::XFile) == 40, "Invalid XFile structure!");
static_assert(Game::MAX_XFILE_COUNT == 8, "XFile block enum is invalid!");
AssetHandler::OnLoad([] (Game::XAssetType type, Game::XAssetHeader asset, std::string name)
AssetHandler::OnLoad([] (Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict)
{
// static void* blocTable = 0;
//
@ -471,8 +471,6 @@ namespace Components
// }
// }
// }
return true;
});
if (ZoneBuilder::IsEnabled())

View File

@ -59,6 +59,7 @@
//#include <asio.hpp>
#include <json11.hpp>
#include <tomcrypt.h>
#include <wink/signal.hpp>
// Protobuf
#include <proto/node.pb.h>