CS2-Kit
C++23 library for CS2 Metamod:Source plugin development
Loading...
Searching...
No Matches
Schema.cpp
Go to the documentation of this file.
1#include "Sdk/Schema.hpp"
2
3#include <CS2Kit/Sdk/GameInterfaces.hpp>
4#include <CS2Kit/Utils/Log.hpp>
5#include <schemasystem/schemasystem.h>
6
7namespace CS2Kit::Sdk
8{
9
10using namespace CS2Kit::Utils;
11
13{
14 if (!GameInterfaces::Instance().SchemaSystem)
15 {
16 Log::Warn("ISchemaSystem not available.");
17 return false;
18 }
19
20 Log::Info("Schema system initialized.");
21 return true;
22}
23
24int SchemaService::GetOffset(const char* className, const char* fieldName)
25{
26 auto* schemaSystem = GameInterfaces::Instance().SchemaSystem;
27 if (!schemaSystem)
28 return -1;
29
30 auto classIt = _offsetCache.find(className);
31 if (classIt != _offsetCache.end())
32 {
33 auto fieldIt = classIt->second.find(fieldName);
34 if (fieldIt != classIt->second.end())
35 return fieldIt->second;
36 }
37
38#ifdef _WIN32
39 const char* moduleName = "server.dll";
40#else
41 const char* moduleName = "libserver.so";
42#endif
43
44 CSchemaSystemTypeScope* pTypeScope = schemaSystem->FindTypeScopeForModule(moduleName);
45 if (!pTypeScope)
46 {
47 Log::Error("Schema: Failed to find type scope for {}.", moduleName);
48 return -1;
49 }
50
51 SchemaMetaInfoHandle_t<CSchemaClassInfo> hClassInfo = pTypeScope->FindDeclaredClass(className);
52 CSchemaClassInfo* pClassInfo = hClassInfo.Get();
53 if (!pClassInfo)
54 {
55 Log::Error("Schema: Class '{}' not found.", className);
56 return -1;
57 }
58
59 for (int i = 0; i < pClassInfo->m_nFieldCount; ++i)
60 {
61 SchemaClassFieldData_t& field = pClassInfo->m_pFields[i];
62 if (strcmp(field.m_pszName, fieldName) == 0)
63 {
64 int offset = field.m_nSingleInheritanceOffset;
65 _offsetCache[className][fieldName] = offset;
66 // Log::Info("Schema: {}::{} = 0x{:X} ({})", className, fieldName, offset, offset);
67 return offset;
68 }
69 }
70
71 Log::Warn("Schema: Field '{}' not found in '{}'.", fieldName, className);
72 return -1;
73}
74
75} // namespace CS2Kit::Sdk
int GetOffset(const char *className, const char *fieldName)
Definition Schema.cpp:24