CS2-Kit
C++23 library for CS2 Metamod:Source plugin development
Loading...
Searching...
No Matches
CS2Kit.cpp
Go to the documentation of this file.
2#include "Sdk/Schema.hpp"
3
4#include <CS2Kit/CS2Kit.hpp>
5#include <CS2Kit/Core/Scheduler.hpp>
6#include <CS2Kit/Core/ILogger.hpp>
7#include <CS2Kit/Core/Paths.hpp>
8#include <CS2Kit/Menu/MenuManager.hpp>
9#include <CS2Kit/Sdk/ChatInputCapture.hpp>
10#include <CS2Kit/Sdk/ConVarService.hpp>
11#include <CS2Kit/Sdk/Entity.hpp>
12#include <CS2Kit/Sdk/GameData.hpp>
13#include <CS2Kit/Sdk/GameEventService.hpp>
14#include <CS2Kit/Sdk/GameInterfaces.hpp>
15#include <CS2Kit/Sdk/UserMessage.hpp>
16#include <CS2Kit/Utils/Log.hpp>
17#include <ISmmAPI.h>
18#include <eiface.h>
19#include <engine/igameeventsystem.h>
20#include <icvar.h>
21#include <interfaces/interfaces.h>
22#include <networksystem/inetworkmessages.h>
23#include <schemasystem/schemasystem.h>
24
25namespace CS2Kit
26{
27
28static constexpr const char* DefaultGameDataPath = "addons/cs2-kit/gamedata/signatures.jsonc";
30
31bool Initialize(ISmmAPI* ismm, char* error, size_t maxlen, const InitParams& params)
32{
33 // 1. Set up logging
34 if (params.Logger)
35 {
36 Core::SetGlobalLogger(params.Logger);
37 }
38 else
39 {
40 g_consoleLogger.SetPrefix(params.LogPrefix);
42 }
43
44 // 2. Set base directory for path resolution
45 Core::SetBaseDir(ismm->GetBaseDir());
46
47 Utils::Log::Info("Initializing CS2Kit...");
48
49 // 3. Resolve SDK interfaces via Metamod
50 auto resolveEngine = [&](const char* version) -> void* {
51 return ismm->VInterfaceMatch(ismm->GetEngineFactory(), version, 0);
52 };
53 auto resolveServer = [&](const char* version) -> void* {
54 return ismm->VInterfaceMatch(ismm->GetServerFactory(), version, 0);
55 };
56
57 auto& gi = Sdk::GameInterfaces::Instance();
58
59 gi.ServerGameDLL = static_cast<IServerGameDLL*>(resolveServer(INTERFACEVERSION_SERVERGAMEDLL));
60 if (!gi.ServerGameDLL)
61 {
62 ismm->Format(error, maxlen, "Could not find interface: %s", INTERFACEVERSION_SERVERGAMEDLL);
63 return false;
64 }
65
66 gi.ServerGameClients = static_cast<IServerGameClients*>(resolveServer(INTERFACEVERSION_SERVERGAMECLIENTS));
67 if (!gi.ServerGameClients)
68 {
69 ismm->Format(error, maxlen, "Could not find interface: %s", INTERFACEVERSION_SERVERGAMECLIENTS);
70 return false;
71 }
72
73 gi.Engine = static_cast<IVEngineServer2*>(resolveEngine(INTERFACEVERSION_VENGINESERVER));
74 if (!gi.Engine)
75 {
76 ismm->Format(error, maxlen, "Could not find interface: %s", INTERFACEVERSION_VENGINESERVER);
77 return false;
78 }
79
80 gi.GameEventSystem = static_cast<IGameEventSystem*>(resolveEngine(GAMEEVENTSYSTEM_INTERFACE_VERSION));
81 if (!gi.GameEventSystem)
82 {
83 ismm->Format(error, maxlen, "Could not find interface: %s", GAMEEVENTSYSTEM_INTERFACE_VERSION);
84 return false;
85 }
86
87 gi.NetworkMessages = static_cast<INetworkMessages*>(resolveEngine(NETWORKMESSAGES_INTERFACE_VERSION));
88 if (!gi.NetworkMessages)
89 {
90 ismm->Format(error, maxlen, "Could not find interface: %s", NETWORKMESSAGES_INTERFACE_VERSION);
91 return false;
92 }
93
94 gi.SchemaSystem = static_cast<ISchemaSystem*>(resolveEngine(SCHEMASYSTEM_INTERFACE_VERSION));
95 if (!gi.SchemaSystem)
96 {
97 ismm->Format(error, maxlen, "Could not find interface: %s", SCHEMASYSTEM_INTERFACE_VERSION);
98 return false;
99 }
100
101 gi.CVar = static_cast<ICvar*>(resolveEngine(CVAR_INTERFACE_VERSION));
102 if (!gi.CVar)
103 {
104 ismm->Format(error, maxlen, "Could not find interface: %s", CVAR_INTERFACE_VERSION);
105 return false;
106 }
107
108 gi.GameResourceService =
109 static_cast<IGameResourceService*>(resolveEngine(GAMERESOURCESERVICESERVER_INTERFACE_VERSION));
110 if (!gi.GameResourceService)
111 {
112 ismm->Format(error, maxlen, "Could not find interface: %s", GAMERESOURCESERVICESERVER_INTERFACE_VERSION);
113 return false;
114 }
115
116 // 4. Set g_pCVar (required by HL2SDK's tier1/convar.cpp)
117 g_pCVar = gi.CVar;
118
119 // 5. Load game data (signatures and offsets)
120 const char* gameDataPath = params.GameDataPath ? params.GameDataPath : DefaultGameDataPath;
121 Utils::Log::Info("Loading game data from {}...", gameDataPath);
122 Sdk::GameData::Instance().Load(gameDataPath);
123
124 // 6. Initialize SDK subsystems
125 Utils::Log::Info("Initializing SDK message system...");
126 if (!Sdk::MessageSystem::Instance().Initialize())
127 {
128 Utils::Log::Error("Failed to initialize message system.");
129 return false;
130 }
131
132 Utils::Log::Info("Initializing schema system...");
133 if (!Sdk::SchemaService::Instance().Initialize())
134 Utils::Log::Warn("Schema system init failed (button detection may not work).");
135
136 Utils::Log::Info("Initializing entity system...");
137 if (!Sdk::EntitySystem::Instance().Initialize())
138 Utils::Log::Warn("Entity system init failed (menus may not work).");
139
140 Utils::Log::Info("Resolving game event manager...");
141 if (!Sdk::MessageSystem::Instance().InitGameEventManager())
142 Utils::Log::Warn("Game event manager not resolved (center HTML display will not work).");
143
144 Utils::Log::Info("Initializing ConVar service...");
145 if (!Sdk::ConVarService::Instance().Initialize())
146 Utils::Log::Warn("ConVar service init failed.");
147
148 Utils::Log::Info("Initializing game event service...");
149 if (!Sdk::GameEventService::Instance().Initialize())
150 Utils::Log::Warn("Game event service init failed.");
151
152 Utils::Log::Info("CS2Kit initialized.");
153 return true;
154}
155
157{
158 Core::Scheduler::Instance().CancelAll();
159}
160
162{
163 Core::Scheduler::Instance().OnGameFrame();
164 Menu::MenuManager::Instance().OnGameFrame();
165}
166
167void OnPlayerDisconnect(int slot)
168{
169 Menu::MenuManager::Instance().OnPlayerDisconnect(slot);
170 Sdk::ChatInputCapture::Instance().OnPlayerDisconnect(slot);
171}
172
173} // namespace CS2Kit
Default console logger implementation using HL2SDK's ConColorMsg. Created automatically by CS2Kit::In...
void SetPrefix(const char *prefix)
void SetGlobalLogger(ILogger *logger)
Definition Logger.cpp:12
void SetBaseDir(const std::filesystem::path &baseDir)
Definition Paths.cpp:8
void OnPlayerDisconnect(int slot)
Definition CS2Kit.cpp:167
static constexpr const char * DefaultGameDataPath
Definition CS2Kit.cpp:28
bool Initialize(ISmmAPI *ismm, char *error, size_t maxlen, const InitParams &params)
Definition CS2Kit.cpp:31
void OnGameFrame()
Definition CS2Kit.cpp:161
static Core::ConsoleLogger g_consoleLogger
Definition CS2Kit.cpp:29
void Shutdown()
Definition CS2Kit.cpp:156