8#include <engine2/isource2server.h>
9#include <engine2/isource2serverconfig.h>
14#include <khook/memory.hpp>
16#include <system_error>
18#include <tier0/icommandline.h>
32static InitReturnVal_t (*
g_init)(
void* server) =
nullptr;
38 void** slot = *
static_cast<void***
>(object) + index;
39 KHook::Memory::SetAccess(slot,
sizeof(
void*),
40 KHook::Memory::Flags::READ | KHook::Memory::Flags::WRITE | KHook::Memory::Flags::EXECUTE);
41 const Fn original =
reinterpret_cast<Fn>(*slot);
42 *slot =
reinterpret_cast<void*
>(replacement);
49 std::filesystem::path dir =
OwnPath().parent_path();
50 for (
int level = 0; level < 4; ++level)
52 dir = dir.parent_path();
57static std::string
ReadFile(
const std::filesystem::path& path)
59 std::ifstream file(path, std::ios::binary);
60 return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
67 if (!CommandLine()->HasParm(
"-dedicated"))
72 const std::filesystem::path gameDir =
GameDir();
73 const std::filesystem::path gameinfo = gameDir /
"gameinfo.gi";
74 const std::string text =
ReadFile(gameinfo);
77 Warning(
"[VoltMod] Could not read %s, so VoltMod is not loaded.\n", gameinfo.string().c_str());
83 const std::filesystem::path server = gameDir.parent_path() / searchPath /
"bin" /
PlatformDir /
ServerFile;
84 std::error_code missing;
85 if (!std::filesystem::is_regular_file(server, missing))
91 auto* factory =
module ? reinterpret_cast<InterfaceFactory>(FindExport(module, "CreateInterface")) : nullptr;
92 if (factory ==
nullptr)
94 Warning(
"[VoltMod] Could not load %s: %s\n", server.string().c_str(),
LastError().c_str());
98 Msg(
"[VoltMod] Loader started; forwarding the game server to %s\n", server.string().c_str());
104 Warning(
"[VoltMod] No Game path in %s holds a server library, so VoltMod is not loaded.\n",
105 gameinfo.string().c_str());
122 if (
g_start.EngineFactory ==
nullptr ||
g_start.ServerFactory ==
nullptr)
124 Warning(
"[VoltMod] The engine and game factories were not found, so VoltMod is not loaded.\n");
132 Warning(
"[VoltMod] Could not load %s: %s\n", path.string().c_str(),
LastError().c_str());
138 if (start ==
nullptr || stop ==
nullptr)
140 Warning(
"[VoltMod] Could not load %s: %s\n", path.string().c_str(),
LastError().c_str());
145 char error[512] =
"";
146 if (!start(&
g_start, error,
sizeof(error)))
148 Warning(
"[VoltMod] The host did not start: %s\n", error);
156 g_start.EngineFactory = engineFactory;
160static InitReturnVal_t
OnInit(
void* server)
190static void Patch(
const char* name,
void*
object)
192 if (std::strcmp(name, INTERFACEVERSION_SERVERCONFIG) == 0 &&
g_connect ==
nullptr)
197 else if (std::strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0 &&
g_init ==
nullptr)
213 if (returnCode !=
nullptr)
215 *returnCode = IFACE_FAILED;
220 void*
object = next(name, returnCode);
221 if (
object !=
nullptr)
void * CreateInterface(const char *name, int *returnCode)
The loader's KHook, which every module's hooks go through.
static void Patch(const char *name, void *object)
static bool OnConnect(void *config, InterfaceFactory engineFactory)
constexpr std::string_view HostFile
static void OnDisconnect(void *config)
void *(*)(const char *name, int *returnCode) InterfaceFactory
static HostStopFn g_stopHost
static InitReturnVal_t(* g_init)(void *server)
static std::string LastError()
constexpr std::string_view PlatformDir
static std::string ReadFile(const std::filesystem::path &path)
constexpr const char * HostStopName
static InterfaceFactory LoadNextServer()
bool(*)(const LoaderHandoff *start, char *error, size_t errorSize) HostStartFn
static void OnShutdown(void *server)
std::filesystem::path OwnPath()
constexpr const char * HostStartName
static void(* g_disconnect)(void *config)
static void(* g_shutdown)(void *server)
static LoaderHandoff g_start
static std::filesystem::path GameDir()
std::vector< std::string > GameSearchPaths(std::string_view gameinfo)
The Game values under SearchPaths in a gameinfo.gi, in file order.
InterfaceFactory FactoryAt(const void *address)
void * OpenModule(const std::filesystem::path &path)
static KHookForwarder g_dispatcher
static bool(* g_connect)(void *config, InterfaceFactory engineFactory)
static std::string g_gameDir
static void * FindExport(void *handle, const char *name)
static InterfaceFactory FindNextFactory()
constexpr std::string_view ServerFile
static InitReturnVal_t OnInit(void *server)
static Fn SwapSlot(void *object, int index, Fn replacement)