forked from ValveResourceFormat/DumpSource2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappframework.cpp
More file actions
187 lines (162 loc) · 6.53 KB
/
appframework.cpp
File metadata and controls
187 lines (162 loc) · 6.53 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* =============================================================================
* DumpSource2
* Copyright (C) 2024 ValveResourceFormat Contributors
* =============================================================================
*
* 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 "appframework.h"
#include "utils/module.h"
#include "modules.h"
#include "interfaces.h"
#include "application.h"
#include "globalvariables.h"
#include <schemasystem/schemasystem.h>
#include <fmt/format.h>
#include <map>
#include <spdlog/spdlog.h>
static DumperApplication g_Application;
struct AppSystemInfo
{
bool gameBin;
const char* moduleName;
std::string interfaceVersion;
bool connect = true;
};
std::vector<AppSystemInfo> g_appSystems{
{ false, "filesystem_stdio", FILESYSTEM_INTERFACE_VERSION },
{ false, "resourcesystem", RESOURCESYSTEM_INTERFACE_VERSION },
{ true, "client", "Source2ClientConfig001" },
{ false, "engine2", SOURCE2ENGINETOSERVER_INTERFACE_VERSION },
{ true, "host", "GameSystem2HostHook" },
{ true, "modtools", "Source2ModTools001" },
#ifdef GAME_CS2
{ true, "matchmaking", MATCHFRAMEWORK_INTERFACE_VERSION },
#endif
{ true, "server", SOURCE2SERVERCONFIG_INTERFACE_VERSION },
{ false, "animationsystem", ANIMATIONSYSTEM_INTERFACE_VERSION },
{ false, "materialsystem2", TEXTLAYOUT_INTERFACE_VERSION },
{ false, "meshsystem", MESHSYSTEM_INTERFACE_VERSION, false },
{ false, "networksystem", NETWORKSYSTEM_INTERFACE_VERSION, false }, // can't connect on linux cuz of missing gameinfo in IApplication
{ false, "panorama", PANORAMAUIENGINE_INTERFACE_VERSION },
{ false, "particles", PARTICLESYSTEMMGR_INTERFACE_VERSION, false }, // needs renderdevice interface
{ false, "pulse_system", PULSESYSTEM_INTERFACE_VERSION },
#ifdef _WIN32
{ false, "rendersystemdx11", RENDER_UTILS_INTERFACE_VERSION },
#else
{ false, "rendersystemvulkan", RENDER_UTILS_INTERFACE_VERSION },
#endif
{ false, "scenefilecache", "SceneFileCache002" },
{ false, "scenesystem", SCENEUTILS_INTERFACE_VERSION },
{ false, "soundsystem", SOUNDOPSYSTEMEDIT_INTERFACE_VERSION },
{ false, "steamaudio", STEAMAUDIO_INTERFACE_VERSION, false },
{ false, "vphysics2", VPHYSICS2HANDLE_INTERFACE_VERSION },
{ false, "worldrenderer", WORLD_RENDERER_MGR_INTERFACE_VERSION },
{ false, "assetsystem", ASSETSYSTEM_INTERFACE_VERSION, false },
{ false, "assetpreview", ASSETPREVIEWSYSTEM_INTERFACE_VERSION, false },
{ false, "assetbrowser", ASSETBROWSERSYSTEM_INTERFACE_VERSION, false },
{ false, "resourcecompiler", RESOURCECOMPILERSYSTEM_INTERFACE_VERSION, false },
{ false, "tools/hammer", "ToolSystem2_001", false },
{ false, "tools/met", "ToolSystem2_001", false },
{ false, "tools/pet", "ToolSystem2_001", false },
{ false, "tools/cs2_item_editor", "ToolSystem2_001", false },
{ false, "tools/workshopmanager", "ToolSystem2_001", false },
{ false, "tools/modeldoc_editor", "ToolSystem2_ModelDoc", false },
};
std::map<std::string, IAppSystem*> g_factoryMap;
void* AppSystemFactory(const char* pName, int* pReturnCode)
{
if (!strcmp(pName, CVAR_INTERFACE_VERSION))
return Interfaces::cvar;
if (!strcmp(pName, SCHEMASYSTEM_INTERFACE_VERSION))
return Interfaces::schemaSystem;
if (!strcmp(pName, APPLICATION_INTERFACE_VERSION))
return &g_Application;
if (g_factoryMap.find(pName) != g_factoryMap.end())
{
spdlog::trace("Connected {} interface", pName);
return g_factoryMap.at(pName);
}
return nullptr;
}
// SceneUtils_001 tries to set convars on init
// which then calls convar change handlers that try to load main scenesystem appsystem
void SetConvarValueStub(ICvar* icvar, ConVarRef ref)
{
ConVarRefAbstract cvar(ref);
if (!cvar.IsConVarDataValid())
return;
// it also crashes if r_dopixelvisibility is true
if (!strcmp("r_dopixelvisibility", cvar.GetName()))
{
cvar.SetBool(false);
}
}
void InitializeCoreModules()
{
spdlog::info("Initializing core modules");
// Load modules (dlopen)
Modules::schemaSystem = std::make_unique<CModule>("", "schemasystem");
Modules::tier0 = std::make_unique<CModule>("", "tier0");
// Find interfaces
Interfaces::cvar = Modules::tier0->FindInterface<ICvar*>(CVAR_INTERFACE_VERSION);
g_pCVar = Interfaces::cvar;
Interfaces::schemaSystem = Modules::schemaSystem->FindInterface<CSchemaSystem*>(SCHEMASYSTEM_INTERFACE_VERSION);
// Manually connect interfaces
Interfaces::cvar->Connect(Modules::tier0->GetFactory());
Interfaces::cvar->Init();
Interfaces::schemaSystem->Connect(&AppSystemFactory);
Interfaces::schemaSystem->Init();
auto vtb = *((void***)Interfaces::cvar);
#ifdef _WIN32
DWORD _;
VirtualProtect(vtb + 14, 8, PAGE_EXECUTE_READWRITE, &_);
*(vtb + 14) = &SetConvarValueStub;
#else
void* patchBytes = (void*)&SetConvarValueStub;
Plat_WriteMemory(vtb + 14, (uint8_t*)&patchBytes, 8);
#endif
}
void InitializeAppSystems()
{
spdlog::info("Initializing app systems");
for (const auto& appSystem : g_appSystems)
{
std::string path = appSystem.gameBin ? fmt::format("../../{}/bin/{}", GAME_PATH, PLATFORM_FOLDER) : "";
auto targetPath = (std::filesystem::current_path() / path / (MODULE_PREFIX + std::string(appSystem.moduleName) + MODULE_EXT)).lexically_normal().generic_string();
if (!std::filesystem::exists(targetPath))
{
spdlog::info("Skipping module as it does not exist: {}", targetPath);
continue;
}
spdlog::trace("Creating module {}", path);
CModule module(path.c_str(), appSystem.moduleName);
spdlog::trace("Finding interface for module {}", path);
auto interface = module.FindInterface<IAppSystem*>(appSystem.interfaceVersion.c_str());
g_factoryMap[appSystem.interfaceVersion] = interface;
spdlog::trace("Connecting app system for module {}", path);
interface->Connect(&AppSystemFactory);
if (appSystem.connect)
{
interface->Init();
}
else
{
// We can't connect this interface, let's at least dump schemas
typedef void* (*InstallSchemaBindings)(const char* interfaceName, void* pSchemaSystem);
InstallSchemaBindings fn = (InstallSchemaBindings)dlsym(module.m_hModule, "InstallSchemaBindings");
fn(SCHEMASYSTEM_INTERFACE_VERSION, Interfaces::schemaSystem);
}
}
}