Skip to content

Commit 86c5925

Browse files
committed
feat: dump module metadata
1 parent 70c5640 commit 86c5925

9 files changed

Lines changed: 150 additions & 17 deletions

File tree

src/main/appframework.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,7 @@ void InitializeAppSystems()
183183

184184
fn(SCHEMASYSTEM_INTERFACE_VERSION, Interfaces::schemaSystem);
185185
}
186+
187+
Modules::allModules.emplace_back(std::move(module));
186188
}
187189
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* =============================================================================
3+
* DumpSource2
4+
* Copyright (C) 2026 ValveResourceFormat Contributors
5+
* =============================================================================
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, version 3.0, as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU General Public License along with
17+
* this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "module_metadata.h"
21+
#include <spdlog/spdlog.h>
22+
#include "modules.h"
23+
#include "utils/module.h"
24+
#include "utils/common.h"
25+
#include "globalvariables.h"
26+
#include "keyvalues3.h"
27+
#include <unordered_set>
28+
29+
namespace Dumpers::ModuleMetadata
30+
{
31+
32+
void GetModuleMetadata(const CModule& module, SimpleCUtlString& err, SimpleCUtlString& buf)
33+
{
34+
spdlog::info("Dumping metadata for {}", module.m_pszModule);
35+
36+
typedef void* (*ExtractModuleMetadata)(SimpleCUtlString& str);
37+
auto extractModuleMetadataFn = module.GetSymbol<ExtractModuleMetadata>("ExtractModuleMetadata");
38+
39+
SimpleCUtlString additional_info;
40+
auto kv3 = extractModuleMetadataFn(additional_info);
41+
42+
typedef int (*SaveKV3Text_ToString)(KV3ID_t const&, void* kv3, SimpleCUtlString& err, SimpleCUtlString& str);
43+
#ifdef WIN32
44+
static auto saveKV3Text_ToStringFn = Modules::tier0->GetSymbol<SaveKV3Text_ToString>("?SaveKV3Text_ToString@@YA_NAEBUKV3ID_t@@PEBVKeyValues3@@PEAVCUtlString@@2I@Z");
45+
#else
46+
static auto saveKV3Text_ToStringFn = Modules::tier0->GetSymbol<SaveKV3Text_ToString>("_Z20SaveKV3Text_ToStringRK7KV3ID_tPK10KeyValues3P10CUtlStringS6_j");
47+
#endif
48+
49+
saveKV3Text_ToStringFn(g_KV3Encoding_Text, kv3, err, buf);
50+
51+
if (additional_info.Get())
52+
spdlog::warn("{} has additional_info {}", module.m_pszModule, additional_info.Get());
53+
}
54+
55+
void Dump()
56+
{
57+
spdlog::info("Dumping module metadata");
58+
std::unordered_set<std::string> foundModules;
59+
const auto outputPath = Globals::outputPath / "module_metadata";
60+
61+
for (const auto& module : Modules::allModules)
62+
{
63+
SimpleCUtlString err, buf;
64+
GetModuleMetadata(module, err, buf);
65+
66+
if (buf.Get())
67+
{
68+
auto sanitizedModuleName = std::string(module.m_pszModule);
69+
std::replace(sanitizedModuleName.begin(), sanitizedModuleName.end(), '/', '_');
70+
foundModules.insert(sanitizedModuleName);
71+
72+
73+
if (!std::filesystem::is_directory(outputPath) && !std::filesystem::create_directory(outputPath))
74+
{
75+
spdlog::error("Failed to create module_metadata directory");
76+
return;
77+
}
78+
79+
std::ofstream output((outputPath / sanitizedModuleName).replace_extension(".kv3"));
80+
output << buf.Get() << std::endl;
81+
}
82+
}
83+
84+
for (const auto& typeScopePath : std::filesystem::directory_iterator(outputPath))
85+
{
86+
if (foundModules.find(typeScopePath.path().stem().string()) == foundModules.end())
87+
{
88+
spdlog::info("Removing orphan metadata file {}", typeScopePath.path().generic_string());
89+
std::filesystem::remove(typeScopePath.path());
90+
}
91+
}
92+
}
93+
94+
} // namespace Dumpers::ModuleMetadata
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* =============================================================================
3+
* DumpSource2
4+
* Copyright (C) 2026 ValveResourceFormat Contributors
5+
* =============================================================================
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License, version 3.0, as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU General Public License along with
17+
* this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#pragma once
20+
21+
namespace Dumpers::ModuleMetadata
22+
{
23+
24+
void Dump();
25+
26+
} // namespace Dumpers::ModuleMetadata

src/main/dumpers/schemas/metadata_stringifier.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,7 @@
3232
#include <modules.h>
3333
#include <vector>
3434
#include <regex>
35-
36-
class SimpleCUtlString
37-
{
38-
public:
39-
const char* Get()
40-
{
41-
return m_pString;
42-
}
43-
44-
private:
45-
const char* m_pString = nullptr;
46-
};
35+
#include "utils/common.h"
4736

4837
namespace Dumpers::Schemas
4938
{

src/main/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
#include "main.h"
21-
#include "utils/common.h"
2221
#include "interfaces.h"
2322
#include "globalvariables.h"
2423
#include "appframework.h"
@@ -27,7 +26,8 @@
2726

2827
#include "dumpers/concommands/concommands.h"
2928
#include "dumpers/schemas/schemas.h"
30-
#include <modules.h>
29+
#include "dumpers/module_metadata/module_metadata.h"
30+
3131
#include <fmt/format.h>
3232

3333
void Usage()
@@ -84,6 +84,7 @@ int main(int argc, char** argv)
8484

8585
Dumpers::ConCommands::Dump();
8686
Dumpers::Schemas::Dump();
87+
Dumpers::ModuleMetadata::Dump();
8788

8889
std::ofstream file(Globals::outputPath / ".stringsignore");
8990
file << Globals::stringsIgnoreStream.str();

src/main/modules.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
#include <memory>
2222
#include "utils/module.h"
23+
#include <vector>
2324

2425
namespace Modules
2526
{
27+
inline std::vector<CModule> allModules;
2628

2729
inline std::unique_ptr<CModule> schemaSystem = nullptr;
2830
inline std::unique_ptr<CModule> tier0 = nullptr;

src/main/utils/common.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "common.h"
12
#include <cstdarg>
23
#include <stdlib.h>
34
#include <stdio.h>
@@ -7,6 +8,10 @@
78
#include "utlbuffer.h"
89
#include <spdlog/spdlog.h>
910

11+
const char* SimpleCUtlString::Get() {
12+
return m_pString;
13+
}
14+
1015
void ExitError(const char* pMsg, ...)
1116
{
1217
static char szBuffer[2048];

src/main/utils/common.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
#pragma once
12
#ifdef _WIN32
23
#define PLATFORM_FOLDER "win64"
34
#else
45
#define PLATFORM_FOLDER "linuxsteamrt64"
56
#endif
67

7-
void ExitError(const char* pMsg, ...);
8+
void ExitError(const char* pMsg, ...);
9+
10+
class SimpleCUtlString
11+
{
12+
public:
13+
const char* Get();
14+
15+
private:
16+
const char* m_pString = nullptr;
17+
};

src/main/utils/module.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ class CModule
186186
}
187187

188188
template <typename T>
189-
T GetSymbol(const char* name)
189+
T GetSymbol(const char* name) const
190190
{
191-
return (T)dlsym(m_hModule, name);
191+
auto ptr = (T)dlsym(m_hModule, name);
192+
if (!ptr)
193+
ExitError("Could not find %s in %s", name, m_pszModule);
194+
195+
return ptr;
192196
}
193197
#ifdef _WIN32
194198
void InitializeSections();

0 commit comments

Comments
 (0)