Skip to content

Commit bb6257f

Browse files
feat: add map ents loader sp (#7)
1 parent 8553817 commit bb6257f

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/game/sp_main.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace sp
5050

5151
DB_EnumXAssets_FastFile_t DB_EnumXAssets_FastFile = reinterpret_cast<DB_EnumXAssets_FastFile_t>(0x822AEF88);
5252

53+
Load_MapEntsPtr_t Load_MapEntsPtr = reinterpret_cast<Load_MapEntsPtr_t>(0x822B9788);
54+
5355
Scr_AddSourceBuffer_t Scr_AddSourceBuffer = reinterpret_cast<Scr_AddSourceBuffer_t>(0x821C5A18);
5456
Scr_ReadFile_FastFile_t Scr_ReadFile_FastFile = reinterpret_cast<Scr_ReadFile_FastFile_t>(0x821C5978);
5557

@@ -87,6 +89,70 @@ namespace sp
8789
}
8890
}
8991

92+
Detour Load_MapEntsPtr_Detour;
93+
94+
void Load_MapEntsPtr_Hook()
95+
{
96+
// TODO: don't write null byte to file
97+
// and add null byte to entityString when reading from file
98+
99+
xbox::DbgPrint("Load_MapEntsPtr_Hook\n");
100+
101+
// TODO: write comment what this is ***
102+
// Get pointer to pointer stored at 0x82475914
103+
MapEnts **varMapEntsPtr = *(MapEnts ***)0x825875D8;
104+
105+
Load_MapEntsPtr_Detour.GetOriginal<decltype(&Load_MapEntsPtr_Hook)>()();
106+
107+
// Validate pointer before dereferencing
108+
if (varMapEntsPtr && *varMapEntsPtr)
109+
{
110+
MapEnts *mapEnts = *varMapEntsPtr;
111+
112+
// Write stock map ents to disk
113+
std::string file_path = "game:\\dump\\";
114+
file_path += mapEnts->name;
115+
file_path += ".ents"; // iw4x naming convention
116+
std::replace(file_path.begin(), file_path.end(), '/', '\\'); // Replace forward slashes with backslashes
117+
filesystem::write_file_to_disk(file_path.c_str(), mapEnts->entityString, mapEnts->numEntityChars);
118+
119+
// Load map ents from file
120+
// Path to check for existing entity file
121+
std::string raw_file_path = "game:\\raw\\";
122+
raw_file_path += mapEnts->name;
123+
raw_file_path += ".ents"; // IW4x naming convention
124+
std::replace(raw_file_path.begin(), raw_file_path.end(), '/', '\\'); // Replace forward slashes with backslashes
125+
126+
// If the file exists, replace entityString
127+
if (filesystem::file_exists(raw_file_path))
128+
{
129+
xbox::DbgPrint("Found entity file: %s\n", raw_file_path.c_str());
130+
std::string new_entity_string = filesystem::read_file_to_string(raw_file_path);
131+
if (!new_entity_string.empty())
132+
{
133+
// Allocate new memory and copy the data
134+
size_t new_size = new_entity_string.size() + 1; // Include null terminator
135+
char *new_memory = static_cast<char *>(malloc(new_size));
136+
137+
if (new_memory)
138+
{
139+
memcpy(new_memory, new_entity_string.c_str(), new_size); // Copy with null terminator
140+
mapEnts->entityString = new_memory;
141+
xbox::DbgPrint("Replaced entityString from file: %s\n", raw_file_path.c_str());
142+
}
143+
else
144+
{
145+
xbox::DbgPrint("Failed to allocate memory for entityString replacement.\n");
146+
}
147+
}
148+
}
149+
}
150+
else
151+
{
152+
xbox::DbgPrint("Hooked Load_MapEntsPtr: varMapEntsPtr is NULL or invalid.\n");
153+
}
154+
}
155+
90156
Detour Scr_ReadFile_FastFile_Detour;
91157

92158
char *Scr_ReadFile_FastFile_Hook(const char *filename, const char *extFilename, const char *codePos, bool archive)
@@ -184,6 +250,9 @@ namespace sp
184250
CL_GamepadButtonEvent_Detour = Detour(CL_GamepadButtonEvent, CL_GamepadButtonEvent_Hook);
185251
CL_GamepadButtonEvent_Detour.Install();
186252

253+
Load_MapEntsPtr_Detour = Detour(Load_MapEntsPtr, Load_MapEntsPtr_Hook);
254+
Load_MapEntsPtr_Detour.Install();
255+
187256
Scr_ReadFile_FastFile_Detour = Detour(Scr_ReadFile_FastFile, Scr_ReadFile_FastFile_Hook);
188257
Scr_ReadFile_FastFile_Detour.Install();
189258

src/game/sp_structs.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
namespace sp
44
{
5+
struct cmd_function_s
6+
{
7+
cmd_function_s *next;
8+
const char *name;
9+
const char *autoCompleteDir;
10+
const char *autoCompleteExt;
11+
void(__fastcall *function)();
12+
};
13+
514
enum keyNum_t : __int32
615
{
716
K_NONE = 0x0,
@@ -140,13 +149,11 @@ namespace sp
140149
K_LAST_KEY = 0xDF,
141150
};
142151

143-
struct cmd_function_s
152+
struct MapEnts
144153
{
145-
cmd_function_s *next;
146154
const char *name;
147-
const char *autoCompleteDir;
148-
const char *autoCompleteExt;
149-
void(__fastcall *function)();
155+
char *entityString;
156+
int numEntityChars;
150157
};
151158

152159
enum XAssetType : __int32
@@ -275,6 +282,8 @@ namespace sp
275282

276283
typedef void (*DB_EnumXAssets_FastFile_t)(XAssetType type, void (*func)(void *asset, void *inData), void *inData, bool includeOverride);
277284

285+
typedef void (*Load_MapEntsPtr_t)();
286+
278287
typedef char *(*Scr_AddSourceBuffer_t)(const char *filename, const char *extFilename, const char *codePos, bool archive);
279288
typedef char *(*Scr_ReadFile_FastFile_t)(const char *filename, const char *extFilename, const char *codePos, bool archive);
280289
}

0 commit comments

Comments
 (0)