VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
SchemaCheck.cpp
Go to the documentation of this file.
2
7#include "Schema/Layout.hpp"
8
10#include <VoltMod/Core/Log.hpp>
12#include <entity2/entityclass.h>
13#include <entity2/entitysystem.h>
14#include <filesystem>
15#include <format>
16#include <fstream>
17#include <gameentitysystem.h>
18#include <interfaces/interfaces.h>
19#include <schemasystem/schemasystem.h>
20#include <string>
21#include <string_view>
22#include <vector>
23
24namespace VoltMod::Schema
25{
26
27/** Output consumed by `voltmod framework schemagen`. */
28static constexpr std::string_view DumpPath = "addons/voltmod/gamedata/dumps/schema.json";
29
30/** The build stamp near the top of the dump at @p path, or empty. */
31static std::string DumpedBuild(const std::filesystem::path& path)
32{
33 std::ifstream file(path, std::ios::binary);
34 if (!file.is_open())
35 {
36 return {};
37 }
38
39 std::string head(256, '\0');
40 file.read(head.data(), static_cast<std::streamsize>(head.size()));
41 head.resize(static_cast<size_t>(file.gcount()));
42
43 constexpr std::string_view key = "\"build\":";
44 const size_t at = head.find(key);
45 if (at == std::string::npos)
46 {
47 return {};
48 }
49
50 const size_t open = head.find('"', at + key.size());
51 const size_t close = open == std::string::npos ? std::string::npos : head.find('"', open + 1);
52 return close == std::string::npos ? std::string() : head.substr(open + 1, close - open - 1);
53}
54
55static CSchemaSystemTypeScope* ServerScope(ISchemaSystem* schema)
56{
57 if (!schema || !schema->SchemaSystemIsReady())
58 {
59 return nullptr;
60 }
61 const std::string moduleName = PlatformModuleName("server");
62 return schema->FindTypeScopeForModule(moduleName.c_str());
63}
64
65void WriteSchemaDump(ISchemaSystem* schema, CGameEntitySystem* entities)
66{
67 static bool attempted = false; // one try per process; the build stamp prevents repeats
68 if (attempted || !entities)
69 {
70 return;
71 }
72
73 CSchemaSystemTypeScope* server = ServerScope(schema);
74 const std::filesystem::path output = ResolvePath(DumpPath);
75 if (!server || DumpedBuild(output) == GameBuild())
76 {
77 return;
78 }
79
80 // One serializer database covers every networked class.
81 const CEntityClass* entityClass = entities->FindClassByName("CBaseEntity");
82 const CNetworkSerializerClassInfo* serializer = entityClass ? entityClass->m_NetworkSerializerInfo : nullptr;
83 if (!serializer || !serializer->m_pDatabase)
84 {
85 return;
86 }
87
88 attempted = true;
89 const Status written =
90 WriteDumpFile(schema->GlobalTypeScope(), server, *serializer->m_pDatabase, DumpPath, GameBuild());
91 if (!written)
92 {
93 Log::Warn("Schema: no dump written to {}: {}", output.string(), written.error().Detail);
94 return;
95 }
96 Log::Info("Schema: dumped game build {} to {}.", GameBuild(), output.string());
97}
98
99LiveField FindLiveField(ISchemaSystem* schema, std::string_view className, std::string_view field)
100{
101 CSchemaSystemTypeScope* server = ServerScope(schema);
102 if (!server)
103 {
104 return {};
105 }
106
107 const std::string name(className);
108 CSchemaClassInfo* live = server->FindDeclaredClass(name.c_str()).Get();
109 if (CSchemaSystemTypeScope* global = schema->GlobalTypeScope(); !live && global)
110 {
111 live = global->FindDeclaredClass(name.c_str()).Get();
112 }
113
114 const SchemaClassFieldData_t* found = live ? FindField(live, field) : nullptr;
115 if (!found)
116 {
117 return {};
118 }
119
120 int size = 0;
121 uint8_t alignment = 0;
122 const bool sized = found->m_pType && found->m_pType->GetSizeAndAlignment(size, alignment);
123 return {.Offset = found->m_nSingleInheritanceOffset, .Size = sized ? size : 0};
124}
125
126} // namespace VoltMod::Schema
127
128namespace VoltMod
129{
130
131SchemaCheck::SchemaCheck(PluginRegistry& registry, ISchemaSystem* schema) : _registry(registry), _schema(schema)
132{
133 _registry.SetSchemaFields([schema](std::string_view className, std::string_view field) {
134 return Schema::FindLiveField(schema, className, field);
135 });
136 Check();
137}
138
140{
141 // The first map is the next chance when the schema scope was not there at host load.
142 if (!_schemaLoaded)
143 {
144 Check();
145 }
146 Schema::WriteSchemaDump(_schema, ::GameEntitySystem());
147}
148
149void SchemaCheck::Check()
150{
151 _schemaLoaded = Schema::ServerScope(_schema) != nullptr;
152 if (!_schemaLoaded)
153 {
154 Log::Error("Schema: the server schema scope is not ready");
155 return;
156 }
157
158 const Status resolved =
159 Schema::ResolveLayout(Schema::GeneratedLayout(), [this](std::string_view className, std::string_view field) {
160 return Schema::FindLiveField(_schema, className, field);
161 });
162 if (!resolved)
163 {
164 Log::Error("Schema: {}", resolved.error().Detail);
165 return;
166 }
167 Log::Info("Schema: every generated field found on game build {}.", GameBuild());
168}
169
170} // namespace VoltMod
The loaded plugins and the engine events raised on them, one per process.
SchemaCheck(PluginRegistry &registry, ISchemaSystem *schema)
void Error(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:97
void Info(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:79
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
static CSchemaSystemTypeScope * ServerScope(ISchemaSystem *schema)
const SchemaClassFieldData_t * FindField(const CSchemaClassInfo *klass, std::string_view field)
static std::string DumpedBuild(const std::filesystem::path &path)
LiveField FindLiveField(ISchemaSystem *schema, std::string_view className, std::string_view field)
static constexpr std::string_view DumpPath
Status WriteDumpFile(CSchemaSystemTypeScope *global, CSchemaSystemTypeScope *server, const CNetworkSerializerCodeGenDatabase &network, std::string_view path, std::string_view gameBuild)
Status ResolveLayout(std::span< FieldLayout > fields, const LiveFieldLookup &lookup)
Definition Layout.cpp:10
std::span< FieldLayout > GeneratedLayout()
Definition Layout.cpp:137
void WriteSchemaDump(ISchemaSystem *schema, CGameEntitySystem *entities)
std::filesystem::path ResolvePath(std::string_view relativePath)
a relative path against the base directory. If the path is already absolute, returns it as-is.
Definition Paths.cpp:14
std::expected< void, Error > Status
Definition Result.hpp:67
std::string_view GameBuild()
Definition GameBuild.cpp:10
std::string PlatformModuleName(std::string_view moduleName)