-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmetadata_stringifier.cpp
More file actions
165 lines (143 loc) · 4.84 KB
/
metadata_stringifier.cpp
File metadata and controls
165 lines (143 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* =============================================================================
* DumpSource2
* Copyright (C) 2024 ValveResourceFormat Contributors
*
* source2gen
* Copyright 2024 neverlosecc
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "globalvariables.h"
#include "interfaces.h"
#include <filesystem>
#include <map>
#include "metadatalist.h"
#include <optional>
#include <cstring>
#include <fmt/format.h>
#include "metadata_stringifier.h"
#include <modules.h>
#include <unordered_set>
class SimpleCUtlString {
public:
const char* Get() {
return m_pString;
}
private:
const char* m_pString = nullptr;
};
namespace Dumpers::Schemas
{
std::unordered_set<std::string> g_classWithBrokenDefaults =
{
"CastSphereSATParams_t",
"Dop26_t",
"FourCovMatrices3",
"VMixVocoderDesc_t",
};
// Any function called after this will have uninitialized variables set to zero
#ifdef WIN32
__declspec(noinline)
#else
__attribute__((noinline))
#endif
void CleanStack() {
// stack size might need to be increased for larger classes (perhaps use alloca with class size + extra)
volatile char stack[0x10000];
for(size_t i = 0; i < sizeof(stack); ++i) {
stack[i] = 0;
}
}
// Determine how and if to output metadata entry value based on it's type.
std::optional<std::string> GetMetadataValue(const SchemaMetadataEntryData_t& entry, const char* metadataTargetName)
{
if (g_mapMetadataNameToValue.find(entry.m_pszName) != g_mapMetadataNameToValue.end())
{
auto valueType = g_mapMetadataNameToValue.at(entry.m_pszName);
switch (valueType)
{
case MetadataValueType::STRING:
{
auto value = *static_cast<const char**>(entry.m_pData);
Globals::stringsIgnoreStream << value << "\n";
return fmt::format("\"{}\"", value);
}
case MetadataValueType::INTEGER:
return std::to_string(*static_cast<int*>(entry.m_pData));
case MetadataValueType::FLOAT:
return std::to_string(*static_cast<float*>(entry.m_pData));
case MetadataValueType::INLINE_STRING:
{
// max 8 characters. Also check for null term.
char* result = static_cast<char*>(entry.m_pData);
for (uint8_t i = 0; i < 8; ++i) {
if (result[i] == '\0') {
return fmt::format("\"{}\"", std::string(result, i));
}
}
return fmt::format("\"{}\"", std::string(result, 8));
}
case MetadataValueType::VARNAME:
{
auto value = static_cast<CSchemaVarName*>(entry.m_pData);
const auto check_ptr = [](const char* ptr) -> bool {
// Authored: source2gen
// @note: hotfix for the deadlock 14/09/24 update,
// where they filled some ptrs with -1 instead of nullptr
return ptr != nullptr && ptr != reinterpret_cast<const char*>(-1);
};
std::stringstream stringStream;
auto hasType = check_ptr(value->m_pszType);
auto hasName = check_ptr(value->m_pszName);
stringStream << "\"";
if (hasType)
stringStream << value->m_pszType;
if (hasName)
{
if (hasType)
stringStream << " ";
stringStream << value->m_pszName;
}
stringStream << "\"";
return stringStream.str();
}
case MetadataValueType::KV3DEFAULTS:
typedef void* (*GetKV3DefaultsFn)();
typedef int (*SaveKV3AsJsonFn)(void* kv3, SimpleCUtlString& err, SimpleCUtlString& str);
if (!entry.m_pData || !(*(void**)entry.m_pData) || g_classWithBrokenDefaults.contains(metadataTargetName)) return "Could not parse KV3 Defaults";
CleanStack(); // Prepare stack for uninitialized variables in class constructor inside GetKV3Defaults
auto value = reinterpret_cast<GetKV3DefaultsFn>(*(void**)entry.m_pData)();
if (!value) return "Could not parse KV3 Defaults";
#ifdef WIN32
static auto SaveKV3AsJson = Modules::tier0->GetSymbol<SaveKV3AsJsonFn>("?SaveKV3AsJSON@@YA_NPEBVKeyValues3@@PEAVCUtlString@@1@Z");
#else
static auto SaveKV3AsJson = Modules::tier0->GetSymbol<SaveKV3AsJsonFn>("_Z13SaveKV3AsJSONPK10KeyValues3P10CUtlStringS3_");
#endif
if (!SaveKV3AsJson) {
spdlog::critical("SaveKV3AsJson not found");
return {};
}
SimpleCUtlString err;
SimpleCUtlString buf;
int res = SaveKV3AsJson(*(void**)value, err, buf);
if (res) {
return buf.Get();
}
return "Could not parse KV3 Defaults";
}
}
return {};
}
} // namespace Dumpers::Schemas