Files
Shiip-of-Hakinian-Espanol/soh/soh/ShipInit.hpp
nickpons666 365612432e
Some checks failed
generate-builds / generate-soh-otr (push) Has been cancelled
generate-builds / build-macos (push) Has been cancelled
generate-builds / build-linux (push) Has been cancelled
generate-builds / build-windows (push) Has been cancelled
Initial commit: Proyecto completo de Shipwright-develop
2026-03-28 00:22:57 -06:00

76 lines
1.9 KiB
C++

#ifndef SHIP_INIT_HPP
#define SHIP_INIT_HPP
#ifdef __cplusplus
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <functional>
struct ShipInit {
static std::unordered_map<std::string, std::vector<std::function<void()>>>& GetAll() {
static std::unordered_map<std::string, std::vector<std::function<void()>>> shipInitFuncs;
return shipInitFuncs;
}
static void InitAll() {
ShipInit::Init("*");
}
static void Init(const std::string& path) {
auto& shipInitFuncs = ShipInit::GetAll();
for (const auto& initFunc : shipInitFuncs[path]) {
initFunc();
}
}
};
/**
* @brief Register a function to execute on boot and (optionally) in other situations
*
* @param initFunc The function to execute
* @param updatePaths Strings to specify additional situations in which to execute the function
*
* ### Examples:
*
* #### Execute function `bar` on boot
*
* ```cpp
* static RegisterShipInitFunc foo(bar);
* ```
*
* #### Execute function `bar` on boot and when the CVar `baz` might have changed
*
* ```cpp
* static RegisterShipInitFunc foo(bar, { "baz" });
* ```
*
* #### Execute function `bar` on boot and when `IS_RANDO` might have changed
*
* ```cpp
* static RegisterShipInitFunc foo(bar, { "IS_RANDO" });
* ```
*
* ### Additional Information:
*
* To get a better sense of when your function will be executed
* you can look for `ShipInit::Init` calls throughout the codebase
*/
struct RegisterShipInitFunc {
RegisterShipInitFunc(std::function<void()> initFunc, const std::set<std::string>& updatePaths = {}) {
auto& shipInitFuncs = ShipInit::GetAll();
shipInitFuncs["*"].push_back(initFunc);
for (const auto& path : updatePaths) {
shipInitFuncs[path].push_back(initFunc);
}
}
};
#endif // __cplusplus
#endif // SHIP_INIT_HPP